Skip to content

Commit

Permalink
Fixes balance drop on android
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jun 29, 2020
1 parent 29fe441 commit 3f644e9
Show file tree
Hide file tree
Showing 19 changed files with 456 additions and 13 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 @@ -93,6 +93,7 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kStateParametersTipChoices, "");
registry->RegisterStringPref(prefs::kStateParametersMonthlyTipChoices, "");
registry->RegisterBooleanPref(prefs::kStateFetchOldBalance, true);
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 @@ -64,5 +64,7 @@ const char kStateParametersMonthlyTipChoices[] =
"brave.rewards.parameters.tip.monthly_choices";
const char kStateFetchOldBalance[] =
"brave.rewards.fetch_old_balance";
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 @@ -51,6 +51,7 @@ extern const char kStateParametersAutoContributeChoices[];
extern const char kStateParametersTipChoices[];
extern const char kStateParametersMonthlyTipChoices[];
extern const char kStateFetchOldBalance[];
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 @@ -252,6 +252,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_api.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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 @@ -245,7 +245,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 @@ -269,6 +269,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 @@ -167,7 +167,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 @@ -181,6 +181,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 @@ -286,7 +286,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 @@ -313,17 +313,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) {
BLOG(0, "Response is wrong");
Expand Down Expand Up @@ -422,4 +422,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
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ void DatabaseUnblindedToken::InsertOrUpdateList(
auto transaction = ledger::DBTransaction::New();

const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s "
"INSERT OR IGNORE INTO %s "
"(token_id, token_value, public_key, value, creds_id, expires_at) "
"VALUES (?, ?, ?, ?, ?, ?)",
kTableName);
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 @@ -23,6 +23,7 @@
#include "bat/ledger/internal/bat_helper.h"
#include "bat/ledger/internal/legacy/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 @@ -112,6 +113,7 @@ void LedgerImpl::OnWalletInitializedInternal(
bat_contribution_->Initialize();
bat_promotion_->Initialize();
bat_api_->Initialize();
braveledger_recovery::Check(this);

SetConfirmationsWalletInfo();
} else {
Expand Down Expand Up @@ -1495,7 +1497,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 @@ -1652,4 +1655,10 @@ void LedgerImpl::FetchParameters() {
bat_api_->FetchParameters();
}

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 @@ -615,7 +615,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 @@ -708,6 +708,10 @@ class LedgerImpl : public ledger::Ledger {

void FetchParameters();

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

private:
void OnStateInitialized(
const ledger::Result result,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* 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/state_keys.h"

namespace braveledger_recovery {

void Check(bat_ledger::LedgerImpl* ledger) {
if (!ledger->GetBooleanState(ledger::kStateEmptyBalanceChecked)) {
BLOG(1, "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 3f644e9

Please sign in to comment.