Skip to content

Commit 3f5426b

Browse files
committed
Fixes contribution with unblinded tokens
Resolves brave/brave-browser#7608
1 parent 3fe4f39 commit 3f5426b

File tree

11 files changed

+214
-7
lines changed

11 files changed

+214
-7
lines changed

components/brave_rewards/browser/database/database_promotion.cc

+23
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ bool DatabasePromotion::Migrate(sql::Database* db, const int target) {
139139
case 13: {
140140
return MigrateToV13(db);
141141
}
142+
case 14: {
143+
return MigrateToV14(db);
144+
}
142145
default: {
143146
NOTREACHED();
144147
return false;
@@ -181,6 +184,26 @@ bool DatabasePromotion::MigrateToV13(sql::Database* db) {
181184
return true;
182185
}
183186

187+
bool DatabasePromotion::MigrateToV14(sql::Database* db) {
188+
DCHECK(db);
189+
if (!db) {
190+
return false;
191+
}
192+
193+
const std::string query = base::StringPrintf(
194+
"UPDATE %s SET approximate_value = "
195+
"(SELECT (suggestions * 0.25) FROM %s as ps "
196+
"WHERE ps.promotion_id = %s.promotion_id)",
197+
table_name_,
198+
table_name_,
199+
table_name_);
200+
201+
sql::Statement statement(
202+
db->GetCachedStatement(SQL_FROM_HERE, query.c_str()));
203+
204+
return statement.Run();
205+
}
206+
184207
bool DatabasePromotion::InsertOrUpdate(
185208
sql::Database* db,
186209
ledger::PromotionPtr info) {

components/brave_rewards/browser/database/database_promotion.h

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class DatabasePromotion: public DatabaseTable {
4949

5050
bool MigrateToV13(sql::Database* db);
5151

52+
bool MigrateToV14(sql::Database* db);
53+
5254
std::unique_ptr<DatabasePromotionCreds> creds_;
5355
};
5456

components/brave_rewards/browser/database/database_unblinded_token.cc

+70-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
* You can obtain one at http://mozilla.org/MPL/2.0/. */
55

6-
#include "brave/components/brave_rewards/browser/database/database_unblinded_token.h"
7-
86
#include <string>
97
#include <utility>
108

119
#include "base/bind.h"
1210
#include "base/strings/stringprintf.h"
1311
#include "base/strings/string_util.h"
12+
#include "brave/components/brave_rewards/browser/database/database_unblinded_token.h"
13+
#include "brave/components/brave_rewards/browser/database/database_util.h"
1414
#include "sql/statement.h"
1515
#include "sql/transaction.h"
1616

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

24+
int64_t GetExpirationDate(const int32_t type, const int64_t stamp) {
25+
const auto promotion_type = static_cast<ledger::PromotionType>(type);
26+
if (promotion_type == ledger::PromotionType::ADS) {
27+
return 0;
28+
}
29+
30+
return stamp;
31+
}
32+
2433
} // namespace
2534

2635
DatabaseUnblindedToken::DatabaseUnblindedToken(
@@ -55,6 +64,14 @@ bool DatabaseUnblindedToken::Init(sql::Database* db) {
5564
}
5665

5766
bool DatabaseUnblindedToken::CreateTable(sql::Database* db) {
67+
return CreateTableV10(db);
68+
}
69+
70+
bool DatabaseUnblindedToken::CreateIndex(sql::Database* db) {
71+
return CreateIndexV10(db);
72+
}
73+
74+
bool DatabaseUnblindedToken::CreateTableV10(sql::Database* db) {
5875
if (db->DoesTableExist(table_name_)) {
5976
return true;
6077
}
@@ -77,10 +94,57 @@ bool DatabaseUnblindedToken::CreateTable(sql::Database* db) {
7794
return db->Execute(query.c_str());
7895
}
7996

80-
bool DatabaseUnblindedToken::CreateIndex(sql::Database* db) {
97+
bool DatabaseUnblindedToken::CreateIndexV10(sql::Database* db) {
8198
return this->InsertIndex(db, table_name_, "token_id");
8299
}
83100

101+
bool DatabaseUnblindedToken::Migrate(sql::Database* db, const int target) {
102+
switch (target) {
103+
case 10: {
104+
return MigrateToV10(db);
105+
}
106+
case 14: {
107+
return MigrateToV14(db);
108+
}
109+
default: {
110+
NOTREACHED();
111+
return false;
112+
}
113+
}
114+
}
115+
116+
bool DatabaseUnblindedToken::MigrateToV10(sql::Database* db) {
117+
if (db->DoesTableExist(table_name_)) {
118+
DropTable(db, table_name_);
119+
}
120+
121+
if (!CreateTableV10(db)) {
122+
return false;
123+
}
124+
125+
if (!CreateIndexV10(db)) {
126+
return false;
127+
}
128+
129+
return true;
130+
}
131+
132+
bool DatabaseUnblindedToken::MigrateToV14(sql::Database* db) {
133+
DCHECK(db);
134+
if (!db) {
135+
return false;
136+
}
137+
138+
const std::string query = base::StringPrintf(
139+
"UPDATE %s SET value = 0.25",
140+
table_name_);
141+
142+
sql::Statement statement(
143+
db->GetCachedStatement(SQL_FROM_HERE, query.c_str()));
144+
145+
return statement.Run();
146+
}
147+
84148
bool DatabaseUnblindedToken::InsertOrUpdate(
85149
sql::Database* db,
86150
ledger::UnblindedTokenPtr info) {
@@ -126,7 +190,7 @@ ledger::UnblindedTokenList DatabaseUnblindedToken::GetAllRecords(
126190
ledger::UnblindedTokenList list;
127191
const std::string query = base::StringPrintf(
128192
"SELECT u.token_id, u.token_value, u.public_key, u.value, "
129-
"u.promotion_id, p.expires_at FROM %s as u "
193+
"u.promotion_id, p.expires_at, p.type FROM %s as u "
130194
"LEFT JOIN promotion as p ON p.promotion_id = u.promotion_id",
131195
table_name_);
132196

@@ -139,7 +203,8 @@ ledger::UnblindedTokenList DatabaseUnblindedToken::GetAllRecords(
139203
info->public_key = statement.ColumnString(2);
140204
info->value = statement.ColumnDouble(3);
141205
info->promotion_id = statement.ColumnString(4);
142-
info->expires_at = statement.ColumnInt64(5);
206+
info->expires_at =
207+
GetExpirationDate(statement.ColumnInt(6), statement.ColumnInt64(5));
143208

144209
list.push_back(std::move(info));
145210
}

components/brave_rewards/browser/database/database_unblinded_token.h

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class DatabaseUnblindedToken: public DatabaseTable {
2727

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

30+
bool Migrate(sql::Database* db, const int target);
31+
3032
bool InsertOrUpdate(sql::Database* db, ledger::UnblindedTokenPtr info);
3133

3234
ledger::UnblindedTokenList GetAllRecords(sql::Database* db);
@@ -38,6 +40,15 @@ class DatabaseUnblindedToken: public DatabaseTable {
3840
static bool DeleteRecordsForPromotion(
3941
sql::Database* db,
4042
const std::string& promotion_id);
43+
44+
private:
45+
bool CreateTableV10(sql::Database* db);
46+
47+
bool CreateIndexV10(sql::Database* db);
48+
49+
bool MigrateToV10(sql::Database* db);
50+
51+
bool MigrateToV14(sql::Database* db);
4152
};
4253

4354
} // namespace brave_rewards

components/brave_rewards/browser/database/publisher_info_database.cc

+21-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace brave_rewards {
2424

2525
namespace {
2626

27-
const int kCurrentVersionNumber = 13;
27+
const int kCurrentVersionNumber = 14;
2828
const int kCompatibleVersionNumber = 1;
2929

3030

@@ -843,6 +843,23 @@ bool PublisherInfoDatabase::MigrateV12toV13() {
843843
return transaction.Commit();
844844
}
845845

846+
bool PublisherInfoDatabase::MigrateV13toV14() {
847+
sql::Transaction transaction(&GetDB());
848+
if (!transaction.Begin()) {
849+
return false;
850+
}
851+
852+
if (!promotion_->Migrate(&GetDB(), 14)) {
853+
return false;
854+
}
855+
856+
if (!unblinded_token_->Migrate(&GetDB(), 14)) {
857+
return false;
858+
}
859+
860+
return transaction.Commit();
861+
}
862+
846863
bool PublisherInfoDatabase::Migrate(int version) {
847864
switch (version) {
848865
case 2: {
@@ -881,6 +898,9 @@ bool PublisherInfoDatabase::Migrate(int version) {
881898
case 13: {
882899
return MigrateV12toV13();
883900
}
901+
case 14: {
902+
return MigrateV13toV14();
903+
}
884904
default:
885905
NOTREACHED();
886906
return false;

components/brave_rewards/browser/database/publisher_info_database.h

+2
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ class PublisherInfoDatabase {
193193

194194
bool MigrateV12toV13();
195195

196+
bool MigrateV13toV14();
197+
196198
bool Migrate(int version);
197199

198200
sql::InitStatus EnsureCurrentVersion();

components/brave_rewards/browser/database/publisher_info_database_unittest.cc

+32
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,38 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv12tov13_Promotion) {
13131313
EXPECT_EQ(CountTableRows("promotion"), 1);
13141314
}
13151315

1316+
TEST_F(PublisherInfoDatabaseTest, Migrationv13tov14) {
1317+
base::ScopedTempDir temp_dir;
1318+
base::FilePath db_file;
1319+
CreateMigrationDatabase(&temp_dir, &db_file, 13, 14);
1320+
EXPECT_TRUE(publisher_info_database_->Init());
1321+
1322+
ASSERT_EQ(publisher_info_database_->GetTableVersionNumber(), 14);
1323+
1324+
const std::string schema = publisher_info_database_->GetSchema();
1325+
EXPECT_EQ(schema, GetSchemaString(14));
1326+
}
1327+
1328+
TEST_F(PublisherInfoDatabaseTest, Migrationv13tov14_UnblindedToken) {
1329+
base::ScopedTempDir temp_dir;
1330+
base::FilePath db_file;
1331+
CreateMigrationDatabase(&temp_dir, &db_file, 13, 14);
1332+
EXPECT_TRUE(publisher_info_database_->Init());
1333+
EXPECT_EQ(CountTableRows("unblinded_tokens"), 5);
1334+
1335+
ledger::UnblindedTokenList list =
1336+
publisher_info_database_->GetAllUnblindedTokens();
1337+
EXPECT_EQ(list.at(0)->value, 0.25);
1338+
EXPECT_EQ(list.at(1)->value, 0.25);
1339+
EXPECT_EQ(list.at(2)->value, 0.25);
1340+
EXPECT_EQ(list.at(3)->value, 0.25);
1341+
EXPECT_EQ(list.at(4)->value, 0.25);
1342+
1343+
ledger::PromotionPtr promotion = publisher_info_database_->GetPromotion(
1344+
"36baa4c3-f92d-4121-b6d9-db44cb273a02");
1345+
EXPECT_EQ(promotion->approximate_value, 1.25);
1346+
}
1347+
13161348
TEST_F(PublisherInfoDatabaseTest, DeleteActivityInfo) {
13171349
base::ScopedTempDir temp_dir;
13181350
base::FilePath db_file;
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
index|activity_info_publisher_id_index|activity_info|CREATE INDEX activity_info_publisher_id_index ON activity_info (publisher_id)
2+
index|contribution_info_publishers_contribution_id_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_contribution_id_index ON contribution_info_publishers (contribution_id)
3+
index|contribution_info_publishers_publisher_key_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_publisher_key_index ON contribution_info_publishers (publisher_key)
4+
index|pending_contribution_publisher_id_index|pending_contribution|CREATE INDEX pending_contribution_publisher_id_index ON pending_contribution (publisher_id)
5+
index|promotion_creds_promotion_id_index|promotion_creds|CREATE INDEX promotion_creds_promotion_id_index ON promotion_creds (promotion_id)
6+
index|promotion_promotion_id_index|promotion|CREATE INDEX promotion_promotion_id_index ON promotion (promotion_id)
7+
index|recurring_donation_publisher_id_index|recurring_donation|CREATE INDEX recurring_donation_publisher_id_index ON recurring_donation (publisher_id)
8+
index|server_publisher_amounts_publisher_key_index|server_publisher_amounts|CREATE INDEX server_publisher_amounts_publisher_key_index ON server_publisher_amounts (publisher_key)
9+
index|server_publisher_banner_publisher_key_index|server_publisher_banner|CREATE INDEX server_publisher_banner_publisher_key_index ON server_publisher_banner (publisher_key)
10+
index|server_publisher_info_publisher_key_index|server_publisher_info|CREATE INDEX server_publisher_info_publisher_key_index ON server_publisher_info (publisher_key)
11+
index|server_publisher_links_publisher_key_index|server_publisher_links|CREATE INDEX server_publisher_links_publisher_key_index ON server_publisher_links (publisher_key)
12+
index|sqlite_autoindex_activity_info_1|activity_info|
13+
index|sqlite_autoindex_contribution_info_1|contribution_info|
14+
index|sqlite_autoindex_media_publisher_info_1|media_publisher_info|
15+
index|sqlite_autoindex_meta_1|meta|
16+
index|sqlite_autoindex_promotion_1|promotion|
17+
index|sqlite_autoindex_promotion_creds_1|promotion_creds|
18+
index|sqlite_autoindex_publisher_info_1|publisher_info|
19+
index|sqlite_autoindex_recurring_donation_1|recurring_donation|
20+
index|sqlite_autoindex_server_publisher_amounts_1|server_publisher_amounts|
21+
index|sqlite_autoindex_server_publisher_banner_1|server_publisher_banner|
22+
index|sqlite_autoindex_server_publisher_info_1|server_publisher_info|
23+
index|sqlite_autoindex_server_publisher_links_1|server_publisher_links|
24+
index|unblinded_tokens_token_id_index|unblinded_tokens|CREATE INDEX unblinded_tokens_token_id_index ON unblinded_tokens (token_id)
25+
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)
26+
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))
27+
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))
28+
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)
29+
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)
30+
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)
31+
table|meta|meta|CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)
32+
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)
33+
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))
34+
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)
35+
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)
36+
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)
37+
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)
38+
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)
39+
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)
40+
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)
41+
table|sqlite_sequence|sqlite_sequence|CREATE TABLE sqlite_sequence(name,seq)
42+
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)

vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void Unblinded::OnUnblindedTokens(
157157
continue;
158158
}
159159

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

vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ void HandleExpiredPromotions(
5454
static_cast<uint64_t>(base::Time::Now().ToDoubleT());
5555

5656
for (auto& item : *promotions) {
57+
// we shouldn't expire ad grant
58+
if (item.second->type == ledger::PromotionType::ADS) {
59+
continue;
60+
}
61+
5762
if (item.second->expires_at > 0 &&
5863
item.second->expires_at <= current_time) {
5964
item.second->status = ledger::PromotionStatus::OVER;
@@ -170,6 +175,11 @@ void Promotion::OnGetAllPromotions(
170175

171176
if (list.size() > 0) {
172177
for (auto & item : list) {
178+
// if the server return expiration for ads we need to set it to 0
179+
if (item->type == ledger::PromotionType::ADS) {
180+
item->expires_at = 0;
181+
}
182+
173183
if (item->legacy_claimed) {
174184
item->status = ledger::PromotionStatus::CLAIMED;
175185
ClaimTokens(item->Clone(), [](const ledger::Result _){});

0 commit comments

Comments
 (0)