Skip to content

Commit

Permalink
Merge pull request #5968 from brave/timers-init
Browse files Browse the repository at this point in the history
Fixes initialization flow and how timers are triggered
  • Loading branch information
NejcZdovc authored Jun 29, 2020
2 parents 399c09d + f7184a0 commit 0327f32
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
56 changes: 34 additions & 22 deletions vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,34 @@ LedgerImpl::~LedgerImpl() {
}
}

void LedgerImpl::OnWalletInitializedInternal(
ledger::Result result,
void LedgerImpl::OnInitialized(
const ledger::Result result,
ledger::ResultCallback callback) {
initializing_ = false;
callback(result);
if (result == ledger::Result::LEDGER_OK ||
result == ledger::Result::WALLET_CREATED) {
if (result == ledger::Result::LEDGER_OK) {
initialized_ = true;
bat_publisher_->SetPublisherServerListTimer(GetRewardsMainEnabled());
bat_contribution_->SetReconcileTimer();
bat_promotion_->Refresh(false);
bat_contribution_->Initialize();
bat_promotion_->Initialize();
bat_api_->Initialize();
braveledger_recovery::Check(this);

SetConfirmationsWalletInfo();
StartServices();
} else {
BLOG(0, "Failed to initialize wallet " << result);
}
}

void LedgerImpl::StartServices() {
if (!IsWalletCreated()) {
return;
}

bat_publisher_->SetPublisherServerListTimer(GetRewardsMainEnabled());
bat_contribution_->SetReconcileTimer();
bat_promotion_->Refresh(false);
bat_contribution_->Initialize();
bat_promotion_->Initialize();
bat_api_->Initialize();
SetConfirmationsWalletInfo();
braveledger_recovery::Check(this);
}

void LedgerImpl::Initialize(
const bool execute_create_script,
ledger::ResultCallback callback) {
Expand All @@ -139,7 +145,7 @@ void LedgerImpl::InitializeDatabase(
const bool execute_create_script,
ledger::ResultCallback callback) {
ledger::ResultCallback finish_callback =
std::bind(&LedgerImpl::OnWalletInitializedInternal,
std::bind(&LedgerImpl::OnInitialized,
this,
_1,
std::move(callback));
Expand Down Expand Up @@ -262,16 +268,22 @@ bool LedgerImpl::IsConfirmationsRunning() {
}

void LedgerImpl::CreateWallet(ledger::ResultCallback callback) {
if (initializing_) {
return;
}

initializing_ = true;
auto on_wallet = std::bind(&LedgerImpl::OnWalletInitializedInternal,
auto create_callback = std::bind(&LedgerImpl::OnCreateWallet,
this,
_1,
std::move(callback));
bat_wallet_->CreateWalletIfNecessary(std::move(on_wallet));
callback);

bat_wallet_->CreateWalletIfNecessary(create_callback);
}

void LedgerImpl::OnCreateWallet(
const ledger::Result result,
ledger::ResultCallback callback) {
if (result == ledger::Result::WALLET_CREATED) {
StartServices();
}

callback(result);
}

void LedgerImpl::OnLoad(ledger::VisitDataPtr visit_data,
Expand Down
13 changes: 10 additions & 3 deletions vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ class LedgerImpl : public ledger::Ledger {

void LoadPublisherState(ledger::OnLoadCallback callback);

void OnWalletInitializedInternal(ledger::Result result,
ledger::ResultCallback callback);

void GetRewardsParameters(
ledger::GetRewardsParametersCallback callback) override;

Expand Down Expand Up @@ -722,6 +719,12 @@ class LedgerImpl : public ledger::Ledger {
ledger::GetCredsBatchListCallback callback);

private:
void OnInitialized(
const ledger::Result result,
ledger::ResultCallback callback);

void StartServices();

void OnStateInitialized(
const ledger::Result result,
ledger::ResultCallback callback);
Expand All @@ -747,6 +750,10 @@ class LedgerImpl : public ledger::Ledger {

bool IsConfirmationsRunning();

void OnCreateWallet(
const ledger::Result result,
ledger::ResultCallback callback);

void OnLoad(ledger::VisitDataPtr visit_data,
const uint64_t& current_time) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ double GetAutoContributionAmount(bat_ledger::LedgerImpl* ledger) {

uint64_t GetReconcileStamp(bat_ledger::LedgerImpl* ledger) {
DCHECK(ledger);
return ledger->GetUint64State(ledger::kStateNextReconcileStamp);
auto stamp = ledger->GetUint64State(ledger::kStateNextReconcileStamp);
if (stamp == 0) {
ledger->ResetReconcileStamp();
stamp = ledger->GetUint64State(ledger::kStateNextReconcileStamp);
}

return stamp;
}

void SetReconcileStamp(
Expand Down

0 comments on commit 0327f32

Please sign in to comment.