Skip to content

Commit

Permalink
Adds contribution queue
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Oct 19, 2019
1 parent bca533b commit 1bbc217
Show file tree
Hide file tree
Showing 43 changed files with 1,502 additions and 442 deletions.
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ source_set("browser") {

if (brave_rewards_enabled) {
sources += [
"database/database_contribution_queue.cc",
"database/database_contribution_queue.h",
"database/database_contribution_queue_publishers.cc",
"database/database_contribution_queue_publishers.h",
"database/database_server_publisher_amounts.cc",
"database/database_server_publisher_amounts.h",
"database/database_server_publisher_banner.cc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_rewards/browser/database/database_contribution_queue.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "bat/ledger/publisher_info.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace brave_rewards {

DatabaseContributionQueue::DatabaseContributionQueue(int current_db_version) :
DatabaseTable(current_db_version),
publishers_(std::make_unique<DatabaseContributionQueuePublishers>
(current_db_version)) {
}

DatabaseContributionQueue::~DatabaseContributionQueue() {
}

std::string DatabaseContributionQueue::GetIdColumnName() {
return base::StringPrintf("%s_id", table_name_);
}

bool DatabaseContributionQueue::Init(sql::Database* db) {
if (GetCurrentDBVersion() < minimum_version_) {
return true;
}

sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}

bool success = CreateTable(db);
if (!success) {
return false;
}

success = publishers_->Init(db);
if (!success) {
return false;
}

return transaction.Commit();
}

bool DatabaseContributionQueue::CreateTable(sql::Database* db) {
if (db->DoesTableExist(table_name_)) {
return true;
}

const std::string query = base::StringPrintf(
"CREATE TABLE %s ("
"%s INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"type INTEGER NOT NULL,"
"amount DOUBLE NOT NULL,"
"partial INTEGER NOT NULL DEFAULT 0,"
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL"
")",
table_name_,
GetIdColumnName().c_str());

return db->Execute(query.c_str());
}

bool DatabaseContributionQueue::CreateIndex(sql::Database* db) {
return true;
}

bool DatabaseContributionQueue::InsertOrUpdate(
sql::Database* db,
ledger::ContributionQueuePtr info) {
if (!info) {
return false;
}

sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s (%s, type, amount, partial) "
"VALUES (?, ?, ?, ?)",
table_name_,
GetIdColumnName().c_str());

sql::Statement statement(
db->GetCachedStatement(SQL_FROM_HERE, query.c_str()));

if (info->id != 0) {
statement.BindInt64(0, info->id);
} else {
statement.BindNull(0);
}

statement.BindInt(1, static_cast<int>(info->type));
statement.BindDouble(2, info->amount);
statement.BindBool(3, info->partial);

if (!statement.Run()) {
return false;
}

if (info->id == 0) {
info->id = db->GetLastInsertRowId();
}

if (!publishers_->InsertOrUpdate(db, info->Clone())) {
transaction.Rollback();
return false;
}

return transaction.Commit();
}

ledger::ContributionQueuePtr DatabaseContributionQueue::GetFirstRecord(
sql::Database* db) {
const std::string query = base::StringPrintf(
"SELECT %s, type, amount, partial FROM %s ORDER BY %s ASC LIMIT 1",
GetIdColumnName().c_str(),
table_name_,
GetIdColumnName().c_str());

sql::Statement statment(db->GetUniqueStatement(query.c_str()));

if (!statment.Step()) {
return nullptr;
}

auto info = ledger::ContributionQueue::New();
info->id = statment.ColumnInt64(0);
info->type = static_cast<ledger::RewardsType>(statment.ColumnInt(1));
info->amount = statment.ColumnDouble(2);
info->partial = static_cast<bool>(statment.ColumnInt(3));
info->publishers = publishers_->GetRecords(db, info->id);

return info;
}

bool DatabaseContributionQueue::DeleteRecord(
sql::Database* db,
const uint64_t id) {
if (!db->Execute("PRAGMA foreign_keys=1;")) {
LOG(ERROR) << "Error: deleting record for contribution queue with id" << id;
return false;
}

const std::string query = base::StringPrintf(
"DELETE FROM %s WHERE %s = ?",
table_name_,
GetIdColumnName().c_str());

sql::Statement statement(db->GetUniqueStatement(query.c_str()));
statement.BindInt64(0, id);

bool success = statement.Run();

if (!db->Execute("PRAGMA foreign_keys=0;")) {
return false;
}

return success;
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_CONTRIBUTION_QUEUE_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_CONTRIBUTION_QUEUE_H_

#include <memory>
#include <string>

#include "bat/ledger/contribution_queue.h"
#include "brave/components/brave_rewards/browser/database/database_contribution_queue_publishers.h"
#include "brave/components/brave_rewards/browser/database/database_table.h"

namespace brave_rewards {

class DatabaseContributionQueue: public DatabaseTable {
public:
explicit DatabaseContributionQueue(int current_db_version);
~DatabaseContributionQueue() override;

bool Init(sql::Database* db) override;

bool CreateTable(sql::Database* db) override;

bool CreateIndex(sql::Database* db) override;

bool InsertOrUpdate(sql::Database* db, ledger::ContributionQueuePtr info);

ledger::ContributionQueuePtr GetFirstRecord(sql::Database* db);

bool DeleteRecord(sql::Database* db, const uint64_t id);

private:
std::string GetIdColumnName();

const char* table_name_ = "contribution_queue";
const int minimum_version_ = 9;
std::unique_ptr<DatabaseContributionQueuePublishers> publishers_;
};

} // namespace brave_rewards

#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_CONTRIBUTION_QUEUE_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_rewards/browser/database/database_contribution_queue_publishers.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace brave_rewards {

DatabaseContributionQueuePublishers::DatabaseContributionQueuePublishers(
int current_db_version) : DatabaseTable(current_db_version) {
}

DatabaseContributionQueuePublishers::~DatabaseContributionQueuePublishers() {
}

bool DatabaseContributionQueuePublishers::Init(sql::Database* db) {
if (GetCurrentDBVersion() < minimum_version_) {
return true;
}

bool success = CreateTable(db);
return success;
}

bool DatabaseContributionQueuePublishers::CreateTable(sql::Database* db) {
if (db->DoesTableExist(table_name_)) {
return true;
}

const std::string query = base::StringPrintf(
"CREATE TABLE %s ("
"%s_id INTEGER NOT NULL,"
"publisher_key TEXT NOT NULL,"
"amount_percent DOUBLE NOT NULL,"
"CONSTRAINT fk_%s_publisher_key "
" FOREIGN KEY (publisher_key) "
" REFERENCES publisher_info (publisher_id),"
"CONSTRAINT fk_%s_id "
" FOREIGN KEY (%s_id) "
" REFERENCES %s (%s_id) "
" ON DELETE CASCADE"
")",
table_name_,
parent_table_name_,
table_name_,
table_name_,
parent_table_name_,
parent_table_name_,
parent_table_name_);

return db->Execute(query.c_str());
}

bool DatabaseContributionQueuePublishers::CreateIndex(sql::Database* db) {
return true;
}

bool DatabaseContributionQueuePublishers::InsertOrUpdate(
sql::Database* db,
ledger::ContributionQueuePtr info) {
if (!info) {
return false;
}

const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s (%s_id, publisher_key, amount_percent) "
"VALUES (?, ?, ?)",
table_name_,
parent_table_name_);

sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}

for (const auto& publisher : info->publishers) {
sql::Statement statement(
db->GetCachedStatement(SQL_FROM_HERE, query.c_str()));

statement.BindInt64(0, info->id);
statement.BindString(1, publisher->publisher_key);
statement.BindDouble(2, publisher->amount_percent);
statement.Run();
}

return transaction.Commit();
}

ledger::ContributionQueuePublisherList
DatabaseContributionQueuePublishers::GetRecords(
sql::Database* db,
const uint64_t queue_id) {
ledger::ContributionQueuePublisherList list;
const std::string query = base::StringPrintf(
"SELECT publisher_key, amount_percent "
"FROM %s WHERE %s_id = ?",
table_name_,
parent_table_name_);

sql::Statement statement(db->GetUniqueStatement(query.c_str()));
statement.BindInt64(0, queue_id);

while (statement.Step()) {
ledger::ContributionQueuePublisherPtr publisher =
ledger::ContributionQueuePublisher::New();

publisher->publisher_key = statement.ColumnString(0);
publisher->amount_percent = statement.ColumnDouble(1);
list.push_back(std::move(publisher));
}

return list;
}

} // namespace brave_rewards
Loading

0 comments on commit 1bbc217

Please sign in to comment.