-
Notifications
You must be signed in to change notification settings - Fork 887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes contribution with unblinded tokens #4317
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
|
@@ -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( | ||
|
@@ -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; | ||
} | ||
|
@@ -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) { | ||
|
@@ -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_); | ||
|
||
|
@@ -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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fixes ads grants as some may expired, but we don't want to exclude them |
||
GetExpirationDate(statement.ColumnInt(6), statement.ColumnInt64(5)); | ||
|
||
list.push_back(std::move(info)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
42 changes: 42 additions & 0 deletions
42
test/data/rewards-data/migration/publisher_info_schema_v14.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,7 +157,7 @@ void Unblinded::OnUnblindedTokens( | |
continue; | ||
} | ||
|
||
if (item->value + current_amount > reconcile.fee) { | ||
if (current_amount >= reconcile.fee) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the fix as we decided to go over if needed |
||
break; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this fixes users in bad state