Skip to content

Commit

Permalink
Merge pull request #4317 from brave/tip-vg
Browse files Browse the repository at this point in the history
Fixes contribution with unblinded tokens
  • Loading branch information
NejcZdovc authored Jan 8, 2020
2 parents f0b5302 + 60cae44 commit d562836
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 7 deletions.
23 changes: 23 additions & 0 deletions components/brave_rewards/browser/database/database_promotion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ bool DatabasePromotion::Migrate(sql::Database* db, const int target) {
case 13: {
return MigrateToV13(db);
}
case 14: {
return MigrateToV14(db);
}
default: {
NOTREACHED();
return false;
Expand Down Expand Up @@ -181,6 +184,26 @@ bool DatabasePromotion::MigrateToV13(sql::Database* db) {
return true;
}

bool DatabasePromotion::MigrateToV14(sql::Database* db) {
DCHECK(db);
if (!db) {
return false;
}

const std::string query = base::StringPrintf(
"UPDATE %s SET approximate_value = "
"(SELECT (suggestions * 0.25) FROM %s as ps "
"WHERE ps.promotion_id = %s.promotion_id)",
table_name_,
table_name_,
table_name_);

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

return statement.Run();
}

bool DatabasePromotion::InsertOrUpdate(
sql::Database* db,
ledger::PromotionPtr info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class DatabasePromotion: public DatabaseTable {

bool MigrateToV13(sql::Database* db);

bool MigrateToV14(sql::Database* db);

std::unique_ptr<DatabasePromotionCreds> creds_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* 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_unblinded_token.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_util.h"
#include "brave/components/brave_rewards/browser/database/database_unblinded_token.h"
#include "brave/components/brave_rewards/browser/database/database_util.h"
#include "sql/statement.h"
#include "sql/transaction.h"

Expand All @@ -21,6 +21,15 @@ namespace {
const char* table_name_ = "unblinded_tokens";
const int minimum_version_ = 10;

int64_t GetExpirationDate(const int32_t type, const int64_t stamp) {
const auto promotion_type = static_cast<ledger::PromotionType>(type);
if (promotion_type == ledger::PromotionType::ADS) {
return 0;
}

return stamp;
}

} // namespace

DatabaseUnblindedToken::DatabaseUnblindedToken(
Expand Down Expand Up @@ -55,6 +64,14 @@ bool DatabaseUnblindedToken::Init(sql::Database* db) {
}

bool DatabaseUnblindedToken::CreateTable(sql::Database* db) {
return CreateTableV10(db);
}

bool DatabaseUnblindedToken::CreateIndex(sql::Database* db) {
return CreateIndexV10(db);
}

bool DatabaseUnblindedToken::CreateTableV10(sql::Database* db) {
if (db->DoesTableExist(table_name_)) {
return true;
}
Expand All @@ -77,10 +94,57 @@ bool DatabaseUnblindedToken::CreateTable(sql::Database* db) {
return db->Execute(query.c_str());
}

bool DatabaseUnblindedToken::CreateIndex(sql::Database* db) {
bool DatabaseUnblindedToken::CreateIndexV10(sql::Database* db) {
return this->InsertIndex(db, table_name_, "token_id");
}

bool DatabaseUnblindedToken::Migrate(sql::Database* db, const int target) {
switch (target) {
case 10: {
return MigrateToV10(db);
}
case 14: {
return MigrateToV14(db);
}
default: {
NOTREACHED();
return false;
}
}
}

bool DatabaseUnblindedToken::MigrateToV10(sql::Database* db) {
if (db->DoesTableExist(table_name_)) {
DropTable(db, table_name_);
}

if (!CreateTableV10(db)) {
return false;
}

if (!CreateIndexV10(db)) {
return false;
}

return true;
}

bool DatabaseUnblindedToken::MigrateToV14(sql::Database* db) {
DCHECK(db);
if (!db) {
return false;
}

const std::string query = base::StringPrintf(
"UPDATE %s SET value = 0.25",
table_name_);

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

return statement.Run();
}

bool DatabaseUnblindedToken::InsertOrUpdate(
sql::Database* db,
ledger::UnblindedTokenPtr info) {
Expand Down Expand Up @@ -126,7 +190,7 @@ ledger::UnblindedTokenList DatabaseUnblindedToken::GetAllRecords(
ledger::UnblindedTokenList list;
const std::string query = base::StringPrintf(
"SELECT u.token_id, u.token_value, u.public_key, u.value, "
"u.promotion_id, p.expires_at FROM %s as u "
"u.promotion_id, p.expires_at, p.type FROM %s as u "
"LEFT JOIN promotion as p ON p.promotion_id = u.promotion_id",
table_name_);

Expand All @@ -139,7 +203,8 @@ ledger::UnblindedTokenList DatabaseUnblindedToken::GetAllRecords(
info->public_key = statement.ColumnString(2);
info->value = statement.ColumnDouble(3);
info->promotion_id = statement.ColumnString(4);
info->expires_at = statement.ColumnInt64(5);
info->expires_at =
GetExpirationDate(statement.ColumnInt(6), statement.ColumnInt64(5));

list.push_back(std::move(info));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class DatabaseUnblindedToken: public DatabaseTable {

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

bool Migrate(sql::Database* db, const int target);

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

ledger::UnblindedTokenList GetAllRecords(sql::Database* db);
Expand All @@ -38,6 +40,15 @@ class DatabaseUnblindedToken: public DatabaseTable {
static bool DeleteRecordsForPromotion(
sql::Database* db,
const std::string& promotion_id);

private:
bool CreateTableV10(sql::Database* db);

bool CreateIndexV10(sql::Database* db);

bool MigrateToV10(sql::Database* db);

bool MigrateToV14(sql::Database* db);
};

} // namespace brave_rewards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace brave_rewards {

namespace {

const int kCurrentVersionNumber = 13;
const int kCurrentVersionNumber = 14;
const int kCompatibleVersionNumber = 1;


Expand Down Expand Up @@ -843,6 +843,23 @@ bool PublisherInfoDatabase::MigrateV12toV13() {
return transaction.Commit();
}

bool PublisherInfoDatabase::MigrateV13toV14() {
sql::Transaction transaction(&GetDB());
if (!transaction.Begin()) {
return false;
}

if (!promotion_->Migrate(&GetDB(), 14)) {
return false;
}

if (!unblinded_token_->Migrate(&GetDB(), 14)) {
return false;
}

return transaction.Commit();
}

bool PublisherInfoDatabase::Migrate(int version) {
switch (version) {
case 2: {
Expand Down Expand Up @@ -881,6 +898,9 @@ bool PublisherInfoDatabase::Migrate(int version) {
case 13: {
return MigrateV12toV13();
}
case 14: {
return MigrateV13toV14();
}
default:
NOTREACHED();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class PublisherInfoDatabase {

bool MigrateV12toV13();

bool MigrateV13toV14();

bool Migrate(int version);

sql::InitStatus EnsureCurrentVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,38 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv12tov13_Promotion) {
EXPECT_EQ(CountTableRows("promotion"), 1);
}

TEST_F(PublisherInfoDatabaseTest, Migrationv13tov14) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateMigrationDatabase(&temp_dir, &db_file, 13, 14);
EXPECT_TRUE(publisher_info_database_->Init());

ASSERT_EQ(publisher_info_database_->GetTableVersionNumber(), 14);

const std::string schema = publisher_info_database_->GetSchema();
EXPECT_EQ(schema, GetSchemaString(14));
}

TEST_F(PublisherInfoDatabaseTest, Migrationv13tov14_UnblindedToken) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateMigrationDatabase(&temp_dir, &db_file, 13, 14);
EXPECT_TRUE(publisher_info_database_->Init());
EXPECT_EQ(CountTableRows("unblinded_tokens"), 5);

ledger::UnblindedTokenList list =
publisher_info_database_->GetAllUnblindedTokens();
EXPECT_EQ(list.at(0)->value, 0.25);
EXPECT_EQ(list.at(1)->value, 0.25);
EXPECT_EQ(list.at(2)->value, 0.25);
EXPECT_EQ(list.at(3)->value, 0.25);
EXPECT_EQ(list.at(4)->value, 0.25);

ledger::PromotionPtr promotion = publisher_info_database_->GetPromotion(
"36baa4c3-f92d-4121-b6d9-db44cb273a02");
EXPECT_EQ(promotion->approximate_value, 1.25);
}

TEST_F(PublisherInfoDatabaseTest, DeleteActivityInfo) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
Expand Down
Binary file not shown.
42 changes: 42 additions & 0 deletions test/data/rewards-data/migration/publisher_info_schema_v14.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
index|activity_info_publisher_id_index|activity_info|CREATE INDEX activity_info_publisher_id_index ON activity_info (publisher_id)
index|contribution_info_publishers_contribution_id_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_contribution_id_index ON contribution_info_publishers (contribution_id)
index|contribution_info_publishers_publisher_key_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_publisher_key_index ON contribution_info_publishers (publisher_key)
index|pending_contribution_publisher_id_index|pending_contribution|CREATE INDEX pending_contribution_publisher_id_index ON pending_contribution (publisher_id)
index|promotion_creds_promotion_id_index|promotion_creds|CREATE INDEX promotion_creds_promotion_id_index ON promotion_creds (promotion_id)
index|promotion_promotion_id_index|promotion|CREATE INDEX promotion_promotion_id_index ON promotion (promotion_id)
index|recurring_donation_publisher_id_index|recurring_donation|CREATE INDEX recurring_donation_publisher_id_index ON recurring_donation (publisher_id)
index|server_publisher_amounts_publisher_key_index|server_publisher_amounts|CREATE INDEX server_publisher_amounts_publisher_key_index ON server_publisher_amounts (publisher_key)
index|server_publisher_banner_publisher_key_index|server_publisher_banner|CREATE INDEX server_publisher_banner_publisher_key_index ON server_publisher_banner (publisher_key)
index|server_publisher_info_publisher_key_index|server_publisher_info|CREATE INDEX server_publisher_info_publisher_key_index ON server_publisher_info (publisher_key)
index|server_publisher_links_publisher_key_index|server_publisher_links|CREATE INDEX server_publisher_links_publisher_key_index ON server_publisher_links (publisher_key)
index|sqlite_autoindex_activity_info_1|activity_info|
index|sqlite_autoindex_contribution_info_1|contribution_info|
index|sqlite_autoindex_media_publisher_info_1|media_publisher_info|
index|sqlite_autoindex_meta_1|meta|
index|sqlite_autoindex_promotion_1|promotion|
index|sqlite_autoindex_promotion_creds_1|promotion_creds|
index|sqlite_autoindex_publisher_info_1|publisher_info|
index|sqlite_autoindex_recurring_donation_1|recurring_donation|
index|sqlite_autoindex_server_publisher_amounts_1|server_publisher_amounts|
index|sqlite_autoindex_server_publisher_banner_1|server_publisher_banner|
index|sqlite_autoindex_server_publisher_info_1|server_publisher_info|
index|sqlite_autoindex_server_publisher_links_1|server_publisher_links|
index|unblinded_tokens_token_id_index|unblinded_tokens|CREATE INDEX unblinded_tokens_token_id_index ON unblinded_tokens (token_id)
table|activity_info|activity_info|CREATE TABLE activity_info(publisher_id LONGVARCHAR NOT NULL,duration INTEGER DEFAULT 0 NOT NULL,visits INTEGER DEFAULT 0 NOT NULL,score DOUBLE DEFAULT 0 NOT NULL,percent INTEGER DEFAULT 0 NOT NULL,weight DOUBLE DEFAULT 0 NOT NULL,reconcile_stamp INTEGER DEFAULT 0 NOT NULL,CONSTRAINT activity_unique UNIQUE (publisher_id, reconcile_stamp) CONSTRAINT fk_activity_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE)
table|contribution_info|contribution_info|CREATE TABLE contribution_info (contribution_id TEXT NOT NULL,amount DOUBLE NOT NULL,type INTEGER NOT NULL,step INTEGER NOT NULL DEFAULT -1,retry_count INTEGER NOT NULL DEFAULT -1,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (contribution_id))
table|contribution_info_publishers|contribution_info_publishers|CREATE TABLE contribution_info_publishers (contribution_id TEXT NOT NULL,publisher_key TEXT NOT NULL,total_amount DOUBLE NOT NULL,contributed_amount DOUBLE,CONSTRAINT fk_contribution_info_publishers_contribution_id FOREIGN KEY (contribution_id) REFERENCES "contribution_info_temp" (contribution_id) ON DELETE CASCADE,CONSTRAINT fk_contribution_info_publishers_publisher_id FOREIGN KEY (publisher_key) REFERENCES publisher_info (publisher_id))
table|contribution_queue|contribution_queue|CREATE TABLE contribution_queue (contribution_queue_id 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|contribution_queue_publishers|contribution_queue_publishers|CREATE TABLE contribution_queue_publishers (contribution_queue_id INTEGER NOT NULL,publisher_key TEXT NOT NULL,amount_percent DOUBLE NOT NULL,CONSTRAINT fk_contribution_queue_publishers_publisher_key FOREIGN KEY (publisher_key) REFERENCES publisher_info (publisher_id),CONSTRAINT fk_contribution_queue_publishers_id FOREIGN KEY (contribution_queue_id) REFERENCES contribution_queue (contribution_queue_id) ON DELETE CASCADE)
table|media_publisher_info|media_publisher_info|CREATE TABLE media_publisher_info(media_key TEXT NOT NULL PRIMARY KEY UNIQUE,publisher_id LONGVARCHAR NOT NULL,CONSTRAINT fk_media_publisher_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE)
table|meta|meta|CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)
table|pending_contribution|pending_contribution|CREATE TABLE pending_contribution (pending_contribution_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,publisher_id LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL,viewing_id LONGVARCHAR NOT NULL,type INTEGER NOT NULL,CONSTRAINT fk_pending_contribution_publisher_id FOREIGN KEY (publisher_id) REFERENCES publisher_info (publisher_id) ON DELETE CASCADE)
table|promotion|promotion|CREATE TABLE promotion (promotion_id TEXT NOT NULL,version INTEGER NOT NULL,type INTEGER NOT NULL,public_keys TEXT NOT NULL,suggestions INTEGER NOT NULL DEFAULT 0,approximate_value DOUBLE NOT NULL DEFAULT 0,status INTEGER NOT NULL DEFAULT 0,expires_at TIMESTAMP NOT NULL,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, claimed_at TIMESTAMP,PRIMARY KEY (promotion_id))
table|promotion_creds|promotion_creds|CREATE TABLE promotion_creds (promotion_id TEXT UNIQUE NOT NULL,tokens TEXT NOT NULL,blinded_creds TEXT NOT NULL,signed_creds TEXT,public_key TEXT,batch_proof TEXT,claim_id TEXT,CONSTRAINT fk_promotion_creds_promotion_id FOREIGN KEY (promotion_id) REFERENCES promotion (promotion_id) ON DELETE CASCADE)
table|publisher_info|publisher_info|CREATE TABLE publisher_info(publisher_id LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,excluded INTEGER DEFAULT 0 NOT NULL,name TEXT NOT NULL,favIcon TEXT NOT NULL,url TEXT NOT NULL,provider TEXT NOT NULL)
table|recurring_donation|recurring_donation|CREATE TABLE recurring_donation(publisher_id LONGVARCHAR NOT NULL PRIMARY KEY UNIQUE,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL,CONSTRAINT fk_recurring_donation_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE)
table|server_publisher_amounts|server_publisher_amounts|CREATE TABLE server_publisher_amounts (publisher_key LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,CONSTRAINT server_publisher_amounts_unique UNIQUE (publisher_key, amount) CONSTRAINT fk_server_publisher_amounts_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE)
table|server_publisher_banner|server_publisher_banner|CREATE TABLE server_publisher_banner (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,title TEXT,description TEXT,background TEXT,logo TEXT,CONSTRAINT fk_server_publisher_banner_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE)
table|server_publisher_info|server_publisher_info|CREATE TABLE server_publisher_info (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,status INTEGER DEFAULT 0 NOT NULL,excluded INTEGER DEFAULT 0 NOT NULL,address TEXT NOT NULL)
table|server_publisher_links|server_publisher_links|CREATE TABLE server_publisher_links (publisher_key LONGVARCHAR NOT NULL,provider TEXT,link TEXT,CONSTRAINT server_publisher_links_unique UNIQUE (publisher_key, provider) CONSTRAINT fk_server_publisher_links_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE)
table|sqlite_sequence|sqlite_sequence|CREATE TABLE sqlite_sequence(name,seq)
table|unblinded_tokens|unblinded_tokens|CREATE TABLE unblinded_tokens (token_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,token_value TEXT,public_key TEXT,value DOUBLE NOT NULL DEFAULT 0,promotion_id TEXT,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,CONSTRAINT fk_unblinded_tokens_promotion_id FOREIGN KEY (promotion_id) REFERENCES promotion (promotion_id) ON DELETE CASCADE)
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void Unblinded::OnUnblindedTokens(
continue;
}

if (item->value + current_amount > reconcile.fee) {
if (current_amount >= reconcile.fee) {
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ void HandleExpiredPromotions(
static_cast<uint64_t>(base::Time::Now().ToDoubleT());

for (auto& item : *promotions) {
// we shouldn't expire ad grant
if (item.second->type == ledger::PromotionType::ADS) {
continue;
}

if (item.second->expires_at > 0 &&
item.second->expires_at <= current_time) {
item.second->status = ledger::PromotionStatus::OVER;
Expand Down Expand Up @@ -170,6 +175,11 @@ void Promotion::OnGetAllPromotions(

if (list.size() > 0) {
for (auto & item : list) {
// if the server return expiration for ads we need to set it to 0
if (item->type == ledger::PromotionType::ADS) {
item->expires_at = 0;
}

if (item->legacy_claimed) {
item->status = ledger::PromotionStatus::CLAIMED;
ClaimTokens(item->Clone(), [](const ledger::Result _){});
Expand Down

0 comments on commit d562836

Please sign in to comment.