Skip to content

Commit

Permalink
Merge pull request #5940 from brave/fix-empty-balance-1.10
Browse files Browse the repository at this point in the history
Fixes balance drop on Android (uplift to 1.10.x)
  • Loading branch information
NejcZdovc authored Jun 25, 2020
2 parents ec99b2e + a30fa13 commit 777c2e0
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 12 deletions.
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterUint64Pref(prefs::kStatePromotionLastFetchStamp, 0ull);
registry->RegisterBooleanPref(prefs::kStatePromotionCorruptedMigrated, false);
registry->RegisterBooleanPref(prefs::kStateAnonTransferChecked, false);
registry->RegisterBooleanPref(prefs::kStateEmptyBalanceChecked, false);
}

} // namespace brave_rewards
2 changes: 2 additions & 0 deletions components/brave_rewards/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ const char kStatePromotionLastFetchStamp[] =
const char kStatePromotionCorruptedMigrated[] =
"brave.rewards.promotion_corrupted_migrated2";
const char kStateAnonTransferChecked[] = "brave.rewards.anon_transfer_checked";
const char kStateEmptyBalanceChecked[] =
"brave.rewards.empty_balance_checked";
} // namespace prefs
} // namespace brave_rewards
1 change: 1 addition & 0 deletions components/brave_rewards/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern const char kStateUpholdAnonAddress[];
extern const char kStatePromotionLastFetchStamp[];
extern const char kStatePromotionCorruptedMigrated[];
extern const char kStateAnonTransferChecked[];
extern const char kStateEmptyBalanceChecked[];

extern const char kUseRewardsStagingServer[];
} // namespace prefs
Expand Down
4 changes: 4 additions & 0 deletions vendor/bat-native-ledger/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ source_set("ledger") {
"src/bat/ledger/internal/publisher/publisher.h",
"src/bat/ledger/internal/publisher/publisher_server_list.cc",
"src/bat/ledger/internal/publisher/publisher_server_list.h",
"src/bat/ledger/internal/recovery/recovery.cc",
"src/bat/ledger/internal/recovery/recovery.h",
"src/bat/ledger/internal/recovery/recovery_empty_balance.cc",
"src/bat/ledger/internal/recovery/recovery_empty_balance.h",
"src/bat/ledger/internal/report/report.cc",
"src/bat/ledger/internal/report/report.h",
"src/bat/ledger/internal/request/request_attestation.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ using GetCreateScriptCallback =
std::function<void(const std::string&, const int)>;

using GetCredsBatchCallback = std::function<void(CredsBatchPtr)>;
using GetAllCredsBatchCallback = std::function<void(CredsBatchList)>;
using GetCredsBatchListCallback = std::function<void(CredsBatchList)>;
using GetPromotionListCallback = std::function<void(PromotionList)>;

using SKUOrderCallback =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void Database::SaveSignedCreds(
creds_batch_->SaveSignedCreds(std::move(info), callback);
}

void Database::GetAllCredsBatches(ledger::GetAllCredsBatchCallback callback) {
void Database::GetAllCredsBatches(ledger::GetCredsBatchListCallback callback) {
creds_batch_->GetAllRecords(callback);
}

Expand All @@ -226,6 +226,12 @@ void Database::UpdateCredsBatchesStatus(
callback);
}

void Database::GetCredsBatchesByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback) {
creds_batch_->GetRecordsByTriggers(trigger_ids, callback);
}

/**
* MEDIA PUBLISHER INFO
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Database {
ledger::CredsBatchPtr info,
ledger::ResultCallback callback);

void GetAllCredsBatches(ledger::GetAllCredsBatchCallback callback);
void GetAllCredsBatches(ledger::GetCredsBatchListCallback callback);

void UpdateCredsBatchStatus(
const std::string& trigger_id,
Expand All @@ -152,6 +152,10 @@ class Database {
const ledger::CredsBatchStatus status,
ledger::ResultCallback callback);

void GetCredsBatchesByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback);

/**
* MEDIA PUBLISHER INFO
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ void DatabaseContributionInfo::OnGetList(
GetIntColumn(record_pointer, 5));

contribution_ids.push_back(info->contribution_id);
list.push_back(std::move(info));
}

auto publisher_callback =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void DatabaseCredsBatch::SaveSignedCreds(
}

void DatabaseCredsBatch::GetAllRecords(
ledger::GetAllCredsBatchCallback callback) {
ledger::GetCredsBatchListCallback callback) {
auto transaction = ledger::DBTransaction::New();

const std::string query = base::StringPrintf(
Expand All @@ -305,17 +305,17 @@ void DatabaseCredsBatch::GetAllRecords(
transaction->commands.push_back(std::move(command));

auto transaction_callback =
std::bind(&DatabaseCredsBatch::OnGetAllRecords,
std::bind(&DatabaseCredsBatch::OnGetRecords,
this,
_1,
callback);

ledger_->RunDBTransaction(std::move(transaction), transaction_callback);
}

void DatabaseCredsBatch::OnGetAllRecords(
void DatabaseCredsBatch::OnGetRecords(
ledger::DBCommandResponsePtr response,
ledger::GetAllCredsBatchCallback callback) {
ledger::GetCredsBatchListCallback callback) {
if (!response ||
response->status != ledger::DBCommandResponse::Status::RESPONSE_OK) {
callback({});
Expand Down Expand Up @@ -411,4 +411,43 @@ void DatabaseCredsBatch::UpdateRecordsStatus(
ledger_->RunDBTransaction(std::move(transaction), transaction_callback);
}

void DatabaseCredsBatch::GetRecordsByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback) {
auto transaction = ledger::DBTransaction::New();

const std::string query = base::StringPrintf(
"SELECT creds_id, trigger_id, trigger_type, creds, blinded_creds, "
"signed_creds, public_key, batch_proof, status FROM %s "
"WHERE trigger_id IN (%s)",
kTableName,
GenerateStringInCase(trigger_ids).c_str());

auto command = ledger::DBCommand::New();
command->type = ledger::DBCommand::Type::READ;
command->command = query;

command->record_bindings = {
ledger::DBCommand::RecordBindingType::STRING_TYPE,
ledger::DBCommand::RecordBindingType::STRING_TYPE,
ledger::DBCommand::RecordBindingType::INT_TYPE,
ledger::DBCommand::RecordBindingType::STRING_TYPE,
ledger::DBCommand::RecordBindingType::STRING_TYPE,
ledger::DBCommand::RecordBindingType::STRING_TYPE,
ledger::DBCommand::RecordBindingType::STRING_TYPE,
ledger::DBCommand::RecordBindingType::STRING_TYPE,
ledger::DBCommand::RecordBindingType::INT_TYPE
};

transaction->commands.push_back(std::move(command));

auto transaction_callback =
std::bind(&DatabaseCredsBatch::OnGetRecords,
this,
_1,
callback);

ledger_->RunDBTransaction(std::move(transaction), transaction_callback);
}

} // namespace braveledger_database
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DatabaseCredsBatch: public DatabaseTable {
ledger::CredsBatchPtr creds,
ledger::ResultCallback callback);

void GetAllRecords(ledger::GetAllCredsBatchCallback callback);
void GetAllRecords(ledger::GetCredsBatchListCallback callback);

void UpdateStatus(
const std::string& trigger_id,
Expand All @@ -47,6 +47,10 @@ class DatabaseCredsBatch: public DatabaseTable {
const ledger::CredsBatchStatus status,
ledger::ResultCallback callback);

void GetRecordsByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback);

private:
bool CreateTableV18(ledger::DBTransaction* transaction);

Expand All @@ -58,9 +62,9 @@ class DatabaseCredsBatch: public DatabaseTable {
ledger::DBCommandResponsePtr response,
ledger::GetCredsBatchCallback callback);

void OnGetAllRecords(
void OnGetRecords(
ledger::DBCommandResponsePtr response,
ledger::GetAllCredsBatchCallback callback);
ledger::GetCredsBatchListCallback callback);
};

} // namespace braveledger_database
Expand Down
11 changes: 10 additions & 1 deletion vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "bat/ledger/internal/bat_helper.h"
#include "bat/ledger/internal/bat_state.h"
#include "bat/ledger/internal/promotion/promotion.h"
#include "bat/ledger/internal/recovery/recovery.h"
#include "bat/ledger/internal/report/report.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/media/helper.h"
Expand Down Expand Up @@ -113,6 +114,7 @@ void LedgerImpl::OnWalletInitializedInternal(
bat_promotion_->Refresh(false);
bat_contribution_->Initialize();
bat_promotion_->Initialize();
braveledger_recovery::Check(this);

// Set wallet info for Confirmations when launching the browser or creating
// a wallet for the first time
Expand Down Expand Up @@ -1723,7 +1725,8 @@ void LedgerImpl::PromotionCredentialCompleted(
bat_database_->PromotionCredentialCompleted(promotion_id, callback);
}

void LedgerImpl::GetAllCredsBatches(ledger::GetAllCredsBatchCallback callback) {
void LedgerImpl::GetAllCredsBatches(
ledger::GetCredsBatchListCallback callback) {
bat_database_->GetAllCredsBatches(callback);
}

Expand Down Expand Up @@ -1856,4 +1859,10 @@ void LedgerImpl::UpdatePromotionsBlankPublicKey(
bat_database_->UpdatePromotionsBlankPublicKey(ids, callback);
}

void LedgerImpl::GetCredsBatchesByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback) {
bat_database_->GetCredsBatchesByTriggers(trigger_ids, callback);
}

} // namespace bat_ledger
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ class LedgerImpl : public ledger::Ledger {
const std::string& promotion_id,
ledger::ResultCallback callback);

void GetAllCredsBatches(ledger::GetAllCredsBatchCallback callback);
void GetAllCredsBatches(ledger::GetCredsBatchListCallback callback);

void GetPromotionList(
const std::vector<std::string>& ids,
Expand Down Expand Up @@ -748,6 +748,10 @@ class LedgerImpl : public ledger::Ledger {
const std::vector<std::string>& ids,
ledger::ResultCallback callback);

void GetCredsBatchesByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback);

private:
void MaybeInitializeConfirmations(
const bool execute_create_script,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* 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 "bat/ledger/internal/recovery/recovery.h"
#include "bat/ledger/internal/recovery/recovery_empty_balance.h"
#include "bat/ledger/internal/state_keys.h"

namespace braveledger_recovery {

void Check(bat_ledger::LedgerImpl* ledger) {
if (!ledger->GetBooleanState(ledger::kStateEmptyBalanceChecked)) {
BLOG(ledger, ledger::LogLevel::LOG_INFO)
<< "Running empty balance check...";
EmptyBalance::Check(ledger);
}
}

} // namespace braveledger_recovery
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */

#ifndef BRAVELEDGER_RECOVERY_RECOVERY_H_
#define BRAVELEDGER_RECOVERY_RECOVERY_H_

#include "bat/ledger/internal/ledger_impl.h"

namespace braveledger_recovery {

void Check(bat_ledger::LedgerImpl* ledger);

} // namespace braveledger_recovery

#endif // BRAVELEDGER_RECOVERY_RECOVERY_H_
Loading

0 comments on commit 777c2e0

Please sign in to comment.