Fix Brave Ads race condition when users reset their Brave Rewards wallet

This commit is contained in:
Terry Mancey
2022-06-06 05:58:38 -05:00
parent ce8eef633e
commit 9549168536
6 changed files with 43 additions and 37 deletions
+27 -26
View File
@@ -64,30 +64,27 @@ void Account::OnPrefChanged(const std::string& path) {
}
}
bool Account::SetWallet(const std::string& id, const std::string& seed) {
const WalletInfo& last_wallet = wallet_->Get();
void Account::SetWallet(const std::string& id, const std::string& seed) {
const WalletInfo last_wallet_copy = GetWallet();
if (!wallet_->Set(id, seed)) {
NotifyInvalidWallet();
return false;
return;
}
const WalletInfo& wallet = wallet_->Get();
if (last_wallet.IsValid() && last_wallet != wallet) {
NotifyWalletDidChange(wallet);
Reset();
}
const WalletInfo& wallet = GetWallet();
NotifyWalletDidUpdate(wallet);
TopUpUnblindedTokens();
if (wallet.HasChanged(last_wallet_copy)) {
WalletDidChange(wallet);
return;
}
return true;
TopUpUnblindedTokens();
}
WalletInfo Account::GetWallet() const {
const WalletInfo& Account::GetWallet() const {
return wallet_->Get();
}
@@ -101,7 +98,7 @@ void Account::MaybeGetIssuers() const {
void Account::Deposit(const std::string& creative_instance_id,
const AdType& ad_type,
const ConfirmationType& confirmation_type) {
const ConfirmationType& confirmation_type) const {
DCHECK(!creative_instance_id.empty());
DCHECK_NE(AdType::kUndefined, ad_type.value());
DCHECK_NE(ConfirmationType::kUndefined, confirmation_type.value());
@@ -129,7 +126,7 @@ void Account::GetStatement(StatementCallback callback) const {
});
}
void Account::ProcessClearingCycle() {
void Account::ProcessClearingCycle() const {
confirmations_->ProcessRetryQueue();
if (ShouldRewardUser()) {
@@ -139,7 +136,7 @@ void Account::ProcessClearingCycle() {
///////////////////////////////////////////////////////////////////////////////
void Account::OnEnabledPrefChanged() {
void Account::OnEnabledPrefChanged() const {
MaybeGetIssuers();
}
@@ -164,21 +161,14 @@ void Account::ProcessDeposit(const std::string& creative_instance_id,
});
}
void Account::ProcessUnclearedTransactions() {
void Account::ProcessUnclearedTransactions() const {
const WalletInfo& wallet = GetWallet();
redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet);
}
void Account::TopUpUnblindedTokens() {
if (!ShouldRewardUser()) {
return;
}
void Account::WalletDidChange(const WalletInfo& wallet) const {
NotifyWalletDidChange(wallet);
const WalletInfo& wallet = GetWallet();
refill_unblinded_tokens_->MaybeRefill(wallet);
}
void Account::Reset() {
ResetRewards([=](const bool success) {
if (!success) {
BLOG(0, "Failed to reset rewards state");
@@ -188,9 +178,20 @@ void Account::Reset() {
BLOG(3, "Successfully reset rewards state");
NotifyStatementOfAccountsDidChange();
TopUpUnblindedTokens();
});
}
void Account::TopUpUnblindedTokens() const {
if (!ShouldRewardUser()) {
return;
}
const WalletInfo& wallet = GetWallet();
refill_unblinded_tokens_->MaybeRefill(wallet);
}
void Account::NotifyWalletDidUpdate(const WalletInfo& wallet) const {
for (AccountObserver& observer : observers_) {
observer.OnWalletDidUpdate(wallet);
@@ -49,32 +49,32 @@ class Account final : public ConfirmationsDelegate,
void OnPrefChanged(const std::string& path);
bool SetWallet(const std::string& id, const std::string& seed);
WalletInfo GetWallet() const;
void SetWallet(const std::string& id, const std::string& seed);
const WalletInfo& GetWallet() const;
void MaybeGetIssuers() const;
void Deposit(const std::string& creative_instance_id,
const AdType& ad_type,
const ConfirmationType& confirmation_type);
const ConfirmationType& confirmation_type) const;
void GetStatement(StatementCallback callback) const;
void ProcessClearingCycle();
void ProcessClearingCycle() const;
private:
void OnEnabledPrefChanged();
void OnEnabledPrefChanged() const;
void ProcessDeposit(const std::string& creative_instance_id,
const AdType& ad_type,
const ConfirmationType& confirmation_type,
const double value) const;
void ProcessUnclearedTransactions();
void ProcessUnclearedTransactions() const;
void TopUpUnblindedTokens();
void WalletDidChange(const WalletInfo& wallet) const;
void Reset();
void TopUpUnblindedTokens() const;
void NotifyWalletDidUpdate(const WalletInfo& wallet) const;
void NotifyWalletDidChange(const WalletInfo& wallet) const;
@@ -38,7 +38,7 @@ bool Wallet::Set(const std::string& id, const std::string& seed) {
return true;
}
WalletInfo Wallet::Get() const {
const WalletInfo& Wallet::Get() const {
return wallet_;
}
@@ -20,8 +20,7 @@ class Wallet final {
Wallet& operator=(const Wallet&) = delete;
bool Set(const std::string& id, const std::string& seed);
WalletInfo Get() const;
const WalletInfo& Get() const;
private:
WalletInfo wallet_;
@@ -19,6 +19,10 @@ bool WalletInfo::IsValid() const {
return !id.empty() && !secret_key.empty();
}
bool WalletInfo::HasChanged(const WalletInfo& rhs) const {
return rhs.IsValid() && (*this != rhs);
}
bool WalletInfo::operator==(const WalletInfo& rhs) const {
return id == rhs.id && secret_key == rhs.secret_key;
}
@@ -18,6 +18,8 @@ struct WalletInfo final {
bool IsValid() const;
bool HasChanged(const WalletInfo& rhs) const;
bool operator==(const WalletInfo& rhs) const;
bool operator!=(const WalletInfo& rhs) const;