diff --git a/components/brave_rewards/common/pref_names.cc b/components/brave_rewards/common/pref_names.cc index 24241ce5dc80..17389c21cbcd 100644 --- a/components/brave_rewards/common/pref_names.cc +++ b/components/brave_rewards/common/pref_names.cc @@ -33,7 +33,7 @@ const char kUseRewardsStagingServer[] = "brave.rewards.use_staging_server"; const char kStatePromotionLastFetchStamp[] = "brave.rewards.promotion_last_fetch_stamp"; const char kStatePromotionCorruptedMigrated[] = - "brave.rewards.promotion_corrupted_migrated"; + "brave.rewards.promotion_corrupted_migrated2"; const char kStateAnonTransferChecked[] = "brave.rewards.anon_transfer_checked"; } // namespace prefs } // namespace brave_rewards diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom index 6ba49343f6e8..35ed9fe5a0a8 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom @@ -360,7 +360,8 @@ enum PromotionStatus { ACTIVE = 0, ATTESTED = 1, FINISHED = 4, - OVER = 5 + OVER = 5, + CORRUPTED = 6 }; struct Promotion { @@ -440,7 +441,8 @@ enum CredsBatchStatus { BLINDED = 1, CLAIMED = 2, SIGNED = 3, - FINISHED = 4 + FINISHED = 4, + CORRUPTED = 5 }; struct CredsBatch { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/credentials/credentials_promotion.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/credentials/credentials_promotion.cc index 3fbf9dbf36c5..127b1b5a3db6 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/credentials/credentials_promotion.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/credentials/credentials_promotion.cc @@ -102,6 +102,10 @@ void CredentialsPromotion::OnStart( callback(ledger::Result::LEDGER_OK); break; } + case ledger::CredsBatchStatus::CORRUPTED: { + callback(ledger::Result::LEDGER_ERROR); + break; + } } } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc index b2c295bd8eaf..1b967bc77c6c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.cc @@ -201,6 +201,18 @@ void Database::UpdateCredsBatchStatus( creds_batch_->UpdateStatus(trigger_id, trigger_type, status, callback); } +void Database::UpdateCredsBatchesStatus( + const std::vector& trigger_ids, + const ledger::CredsBatchType trigger_type, + const ledger::CredsBatchStatus status, + ledger::ResultCallback callback) { + creds_batch_->UpdateRecordsStatus( + trigger_ids, + trigger_type, + status, + callback); +} + /** * MEDIA PUBLISHER INFO */ @@ -296,6 +308,13 @@ void Database::UpdatePromotionStatus( promotion_->UpdateStatus(promotion_id, status, callback); } +void Database::UpdatePromotionsStatus( + const std::vector& promotion_ids, + const ledger::PromotionStatus status, + ledger::ResultCallback callback) { + promotion_->UpdateRecordsStatus(promotion_ids, status, callback); +} + void Database::PromotionCredentialCompleted( const std::string& promotion_id, ledger::ResultCallback callback) { @@ -314,6 +333,12 @@ void Database::GetPromotionListByType( promotion_->GetRecordsByType(types, callback); } +void Database::UpdatePromotionsBlankPublicKey( + const std::vector& ids, + ledger::ResultCallback callback) { + promotion_->UpdateRecordsBlankPublicKey(ids, callback); +} + /** * PUBLISHER INFO */ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h index 19e47d09d3b7..4cd32435540d 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database.h @@ -140,6 +140,12 @@ class Database { const ledger::CredsBatchStatus status, ledger::ResultCallback callback); + void UpdateCredsBatchesStatus( + const std::vector& trigger_ids, + const ledger::CredsBatchType trigger_type, + const ledger::CredsBatchStatus status, + ledger::ResultCallback callback); + /** * MEDIA PUBLISHER INFO */ @@ -207,6 +213,11 @@ class Database { const ledger::PromotionStatus status, ledger::ResultCallback callback); + void UpdatePromotionsStatus( + const std::vector& promotion_ids, + const ledger::PromotionStatus status, + ledger::ResultCallback callback); + void PromotionCredentialCompleted( const std::string& promotion_id, ledger::ResultCallback callback); @@ -219,6 +230,10 @@ class Database { const std::vector& types, ledger::GetPromotionListCallback callback); + void UpdatePromotionsBlankPublicKey( + const std::vector& ids, + ledger::ResultCallback callback); + /** * PUBLISHER INFO */ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.cc index 77b4d4394044..835741b91ed9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.cc @@ -378,4 +378,37 @@ void DatabaseCredsBatch::UpdateStatus( ledger_->RunDBTransaction(std::move(transaction), transaction_callback); } +void DatabaseCredsBatch::UpdateRecordsStatus( + const std::vector& trigger_ids, + const ledger::CredsBatchType trigger_type, + const ledger::CredsBatchStatus status, + ledger::ResultCallback callback) { + if (trigger_ids.empty()) { + callback(ledger::Result::LEDGER_ERROR); + return; + } + + auto transaction = ledger::DBTransaction::New(); + + const std::string query = base::StringPrintf( + "UPDATE %s SET status = ? WHERE trigger_id IN (%s) AND trigger_type = ?", + kTableName, + GenerateStringInCase(trigger_ids).c_str()); + + auto command = ledger::DBCommand::New(); + command->type = ledger::DBCommand::Type::RUN; + command->command = query; + + BindInt(command.get(), 0, static_cast(status)); + BindInt(command.get(), 1, static_cast(trigger_type)); + + transaction->commands.push_back(std::move(command)); + + auto transaction_callback = std::bind(&OnResultCallback, + _1, + callback); + + ledger_->RunDBTransaction(std::move(transaction), transaction_callback); +} + } // namespace braveledger_database diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.h b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.h index 3fd5dc6be9dd..baa15fb9004e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_creds_batch.h @@ -41,6 +41,12 @@ class DatabaseCredsBatch: public DatabaseTable { const ledger::CredsBatchStatus status, ledger::ResultCallback callback); + void UpdateRecordsStatus( + const std::vector& trigger_ids, + const ledger::CredsBatchType trigger_type, + const ledger::CredsBatchStatus status, + ledger::ResultCallback callback); + private: bool CreateTableV18(ledger::DBTransaction* transaction); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.cc index 998e441c6cb5..df8f22c6f36c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.cc @@ -469,6 +469,36 @@ void DatabasePromotion::UpdateStatus( ledger_->RunDBTransaction(std::move(transaction), transaction_callback); } +void DatabasePromotion::UpdateRecordsStatus( + const std::vector& ids, + const ledger::PromotionStatus status, + ledger::ResultCallback callback) { + if (ids.empty()) { + callback(ledger::Result::LEDGER_ERROR); + return; + } + + const std::string query = base::StringPrintf( + "UPDATE %s SET status = ? WHERE promotion_id IN (%s)", + kTableName, + GenerateStringInCase(ids).c_str()); + + auto transaction = ledger::DBTransaction::New(); + auto command = ledger::DBCommand::New(); + command->type = ledger::DBCommand::Type::RUN; + command->command = query; + + BindInt(command.get(), 0, static_cast(status)); + + transaction->commands.push_back(std::move(command)); + + auto transaction_callback = std::bind(&OnResultCallback, + _1, + callback); + + ledger_->RunDBTransaction(std::move(transaction), transaction_callback); +} + void DatabasePromotion::CredentialCompleted( const std::string& promotion_id, ledger::ResultCallback callback) { @@ -623,4 +653,27 @@ void DatabasePromotion::GetRecordsByType( ledger_->RunDBTransaction(std::move(transaction), transaction_callback); } +void DatabasePromotion::UpdateRecordsBlankPublicKey( + const std::vector& ids, + ledger::ResultCallback callback) { + const std::string query = base::StringPrintf( + "UPDATE %s as p SET public_keys = " + "(SELECT PRINTF('[\"%%s\"]', public_key) FROM creds_batch as cb " + "WHERE cb.trigger_id = p.promotion_id) WHERE p.promotion_id IN (%s)", + kTableName, + GenerateStringInCase(ids).c_str()); + + auto transaction = ledger::DBTransaction::New(); + auto command = ledger::DBCommand::New(); + command->type = ledger::DBCommand::Type::EXECUTE; + command->command = query; + transaction->commands.push_back(std::move(command)); + + auto transaction_callback = std::bind(&OnResultCallback, + _1, + callback); + + ledger_->RunDBTransaction(std::move(transaction), transaction_callback); +} + } // namespace braveledger_database diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.h b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.h index 04f446e7f97f..5a579efc08ad 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_promotion.h @@ -51,6 +51,11 @@ class DatabasePromotion: public DatabaseTable { const ledger::PromotionStatus status, ledger::ResultCallback callback); + void UpdateRecordsStatus( + const std::vector& ids, + const ledger::PromotionStatus status, + ledger::ResultCallback callback); + void CredentialCompleted( const std::string& promotion_id, ledger::ResultCallback callback); @@ -59,6 +64,10 @@ class DatabasePromotion: public DatabaseTable { const std::vector& types, ledger::GetPromotionListCallback callback); + void UpdateRecordsBlankPublicKey( + const std::vector& ids, + ledger::ResultCallback callback); + private: bool CreateTableV10(ledger::DBTransaction* transaction); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 8de536776451..5a1afd78698c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -1718,6 +1718,13 @@ void LedgerImpl::UpdatePromotionStatus( bat_database_->UpdatePromotionStatus(promotion_id, status, callback); } +void LedgerImpl::UpdatePromotionsStatus( + const std::vector& promotion_ids, + const ledger::PromotionStatus status, + ledger::ResultCallback callback) { + bat_database_->UpdatePromotionsStatus(promotion_ids, status, callback); +} + void LedgerImpl::PromotionCredentialCompleted( const std::string& promotion_id, ledger::ResultCallback callback) { @@ -1757,4 +1764,22 @@ void LedgerImpl::UpdateCredsBatchStatus( callback); } +void LedgerImpl::UpdateCredsBatchesStatus( + const std::vector& trigger_ids, + const ledger::CredsBatchType trigger_type, + const ledger::CredsBatchStatus status, + ledger::ResultCallback callback) { + bat_database_->UpdateCredsBatchesStatus( + trigger_ids, + trigger_type, + status, + callback); +} + +void LedgerImpl::UpdatePromotionsBlankPublicKey( + const std::vector& ids, + ledger::ResultCallback callback) { + bat_database_->UpdatePromotionsBlankPublicKey(ids, callback); +} + } // namespace bat_ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index 41780a38afbb..84e2061876e1 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -710,6 +710,11 @@ class LedgerImpl : public ledger::Ledger { const ledger::PromotionStatus status, ledger::ResultCallback callback); + void UpdatePromotionsStatus( + const std::vector& promotion_ids, + const ledger::PromotionStatus status, + ledger::ResultCallback callback); + void PromotionCredentialCompleted( const std::string& promotion_id, ledger::ResultCallback callback); @@ -732,6 +737,16 @@ class LedgerImpl : public ledger::Ledger { const ledger::CredsBatchStatus status, ledger::ResultCallback callback); + void UpdateCredsBatchesStatus( + const std::vector& trigger_ids, + const ledger::CredsBatchType trigger_type, + const ledger::CredsBatchStatus status, + ledger::ResultCallback callback); + + void UpdatePromotionsBlankPublicKey( + const std::vector& ids, + ledger::ResultCallback callback); + private: void InitializeConfirmations( const bool execute_create_script, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc index e9114ce76277..a90e4d382789 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc @@ -102,7 +102,7 @@ void Promotion::Initialize() { this, _1); - ledger_->GetAllCredsBatches(check_callback); + ledger_->GetAllPromotions(check_callback); } auto retry_callback = std::bind(&Promotion::Retry, @@ -463,6 +463,7 @@ void Promotion::Retry(ledger::PromotionMap promotions) { } case ledger::PromotionStatus::ACTIVE: case ledger::PromotionStatus::FINISHED: + case ledger::PromotionStatus::CORRUPTED: case ledger::PromotionStatus::OVER: { break; } @@ -504,8 +505,55 @@ void Promotion::Refresh(const bool retry_after_error) { ledger_->SetTimer(start_timer_in, &last_check_timer_id_); } -void Promotion::CheckForCorrupted(ledger::CredsBatchList list) { +void Promotion::CheckForCorrupted(const ledger::PromotionMap& promotions) { + if (promotions.empty()) { + return; + } + + std::vector corrupted_promotions; + + for (const auto& item : promotions) { + if (!item.second || + item.second->status != ledger::PromotionStatus::ATTESTED) { + continue; + } + + if (item.second->public_keys.empty() || + item.second->public_keys == "[]") { + corrupted_promotions.push_back(item.second->id); + } + } + + if (corrupted_promotions.empty()) { + BLOG(ledger_, ledger::LogLevel::LOG_INFO) << "No corrupted promotions"; + CorruptedPromotionFixed(ledger::Result::LEDGER_OK); + return; + } + + auto get_callback = std::bind(&Promotion::CorruptedPromotionFixed, + this, + _1); + + ledger_->UpdatePromotionsBlankPublicKey(corrupted_promotions, get_callback); +} + +void Promotion::CorruptedPromotionFixed(const ledger::Result result) { + if (result != ledger::Result::LEDGER_OK) { + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << + "Could not update public keys"; + return; + } + + auto check_callback = std::bind(&Promotion::CheckForCorruptedCreds, + this, + _1); + + ledger_->GetAllCredsBatches(check_callback); +} + +void Promotion::CheckForCorruptedCreds(ledger::CredsBatchList list) { if (list.empty()) { + ledger_->SetBooleanState(ledger::kStatePromotionCorruptedMigrated, true); return; } @@ -533,6 +581,7 @@ void Promotion::CheckForCorrupted(ledger::CredsBatchList list) { } if (corrupted_promotions.empty()) { + BLOG(ledger_, ledger::LogLevel::LOG_INFO) << "No corrupted creds"; ledger_->SetBooleanState(ledger::kStatePromotionCorruptedMigrated, true); return; } @@ -559,6 +608,8 @@ void Promotion::CorruptedPromotions( } if (corrupted_claims.GetList().empty()) { + BLOG(ledger_, ledger::LogLevel::LOG_INFO) << "No corrupted creds"; + ledger_->SetBooleanState(ledger::kStatePromotionCorruptedMigrated, true); return; } @@ -597,20 +648,50 @@ void Promotion::OnCheckForCorrupted( return; } - auto save_callback = std::bind(&Promotion::PromotionListDeleted, + ledger_->SetBooleanState(ledger::kStatePromotionCorruptedMigrated, true); + + auto update_callback = std::bind(&Promotion::ErrorStatusSaved, + this, + _1, + promotion_id_list); + + ledger_->UpdatePromotionsStatus( + promotion_id_list, + ledger::PromotionStatus::CORRUPTED, + update_callback); +} + +void Promotion::ErrorStatusSaved( + const ledger::Result result, + const std::vector& promotion_id_list) { + // even if promotions fail, let's try to update at least creds + if (result != ledger::Result::LEDGER_OK) { + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << + "Promotion status save failed"; + } + + auto update_callback = std::bind(&Promotion::ErrorCredsStatusSaved, this, _1); - ledger_->DeletePromotionList(promotion_id_list, save_callback); + ledger_->UpdateCredsBatchesStatus( + promotion_id_list, + ledger::CredsBatchType::PROMOTION, + ledger::CredsBatchStatus::CORRUPTED, + update_callback); } -void Promotion::PromotionListDeleted(const ledger::Result result) { +void Promotion::ErrorCredsStatusSaved(const ledger::Result result) { if (result != ledger::Result::LEDGER_OK) { - BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Error deleting promotions"; - return; + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Creds status save failed"; } - ledger_->SetBooleanState(ledger::kStatePromotionCorruptedMigrated, true); + // let's retry promotions that are valid now + auto retry_callback = std::bind(&Promotion::Retry, + this, + _1); + + ledger_->GetAllPromotions(retry_callback); } void Promotion::TransferTokens( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.h b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.h index b964018e3aae..e6bb4f431654 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.h @@ -106,7 +106,11 @@ class Promotion { void Retry(ledger::PromotionMap promotions); - void CheckForCorrupted(ledger::CredsBatchList list); + void CheckForCorrupted(const ledger::PromotionMap& promotions); + + void CorruptedPromotionFixed(const ledger::Result result); + + void CheckForCorruptedCreds(ledger::CredsBatchList list); void CorruptedPromotions( ledger::PromotionList promotions, @@ -118,7 +122,11 @@ class Promotion { const std::map& headers, const std::vector& promotion_id_list); - void PromotionListDeleted(const ledger::Result result); + void ErrorStatusSaved( + const ledger::Result result, + const std::vector& promotion_id_list); + + void ErrorCredsStatusSaved(const ledger::Result result); std::unique_ptr attestation_; std::unique_ptr transfer_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/request/request_promotion.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/request/request_promotion.cc index 9d34a25b0d73..5440a22a138a 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/request/request_promotion.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/request/request_promotion.cc @@ -51,7 +51,7 @@ std::string GetReedemSuggestionsUrl() { std::string ReportClobberedClaimsUrl() { return BuildUrl( "/promotions/reportclobberedclaims", - PREFIX_V1, + PREFIX_V2, ServerTypes::kPromotion); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state_keys.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state_keys.h index c833c60e4c24..efb7238b8bc0 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state_keys.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state_keys.h @@ -13,7 +13,7 @@ namespace ledger { const char kStateUpholdAnonAddress[] = "uphold_anon_address"; const char kStatePromotionLastFetchStamp[] = "promotion_last_fetch_stamp"; const char kStatePromotionCorruptedMigrated[] = - "promotion_corrupted_migrated"; + "promotion_corrupted_migrated2"; const char kStateAnonTransferChecked[] = "anon_transfer_checked"; } // namespace ledger