Skip to content

Commit

Permalink
Merge pull request #48 from brave-intl/state-pt2
Browse files Browse the repository at this point in the history
load ledger state before publisher state
  • Loading branch information
bridiver authored Aug 14, 2018
2 parents 291aacb + 61b07b9 commit 9739d0b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
4 changes: 4 additions & 0 deletions include/bat/ledger/ledger_callback_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ namespace ledger {
LEDGER_EXPORT enum Result {
OK = 0,
ERROR = 1,
NO_PUBLISHER_STATE = 2,
NO_LEDGER_STATE = 3,
INVALID_PUBLISHER_STATE = 4,
INVALID_LEDGER_STATE = 5,
// some more useful result codes should go here
};

Expand Down
7 changes: 5 additions & 2 deletions src/bat_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ std::string BatClient::buildURL(const std::string& path, const std::string& pref
return url + prefix + path;
}

void BatClient::loadState(const std::string& data) {
bool BatClient::loadState(const std::string& data) {
braveledger_bat_helper::CLIENT_STATE_ST state;
braveledger_bat_helper::loadFromJson(state, data.c_str());
if (!braveledger_bat_helper::loadFromJson(state, data.c_str()))
return false;


LOG(ERROR) << "!!!bat address == " << state.walletInfo_.addressBAT_;
LOG(ERROR) << "!!!card address == " << state.walletInfo_.addressCARD_ID_;

state_.reset(new braveledger_bat_helper::CLIENT_STATE_ST(state));
return true;
}

void BatClient::registerPersona() {
Expand Down
2 changes: 1 addition & 1 deletion src/bat_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BatClient {
explicit BatClient(bat_ledger::LedgerImpl* ledger);
~BatClient();

void loadState(const std::string& data);
bool loadState(const std::string& data);
void registerPersona();
void requestCredentialsCallback(bool result, const std::string& response);
void registerPersonaCallback(bool result, const std::string& response);
Expand Down
7 changes: 5 additions & 2 deletions src/bat_publishers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,14 @@ void BatPublishers::saveState() {
ledger_->SavePublisherState(data, this);
}

void BatPublishers::loadState(const std::string& data) {
bool BatPublishers::loadState(const std::string& data) {
braveledger_bat_helper::PUBLISHER_STATE_ST state;
braveledger_bat_helper::loadFromJson(state, data.c_str());
if (!braveledger_bat_helper::loadFromJson(state, data.c_str()))
return false;

state_.reset(new braveledger_bat_helper::PUBLISHER_STATE_ST(state));
calcScoreConsts();
return true;
}

void BatPublishers::OnPublisherStateSaved(ledger::Result result) {
Expand Down
2 changes: 1 addition & 1 deletion src/bat_publishers.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler {

~BatPublishers() override;

void loadState(const std::string& data);
bool loadState(const std::string& data);

void saveVisit(const ledger::VisitData& visit_data, const uint64_t& duration);

Expand Down
29 changes: 17 additions & 12 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LedgerImpl::~LedgerImpl() {
}

void LedgerImpl::Initialize() {
LoadPublisherState(this);
LoadLedgerState(this);
}

void LedgerImpl::CreateWallet() {
Expand Down Expand Up @@ -105,15 +105,15 @@ void LedgerImpl::OnBackground(uint32_t tab_id, const uint64_t& current_time) {
}

void LedgerImpl::OnMediaStart(uint32_t tab_id, const uint64_t& current_time) {
// TODO
// TODO
}

void LedgerImpl::OnMediaStop(uint32_t tab_id, const uint64_t& current_time) {
// TODO
// TODO
}

void LedgerImpl::OnXHRLoad(uint32_t tab_id, const std::string& url) {
// TODO
// TODO
//LOG(ERROR) << "!!!LedgerImpl::OnXHRLoad " << url;
}

Expand All @@ -123,10 +123,15 @@ void LedgerImpl::LoadLedgerState(ledger::LedgerCallbackHandler* handler) {

void LedgerImpl::OnLedgerStateLoaded(ledger::Result result,
const std::string& data) {
if (result == ledger::Result::OK)
bat_client_->loadState(data);
if (result == ledger::Result::OK) {
if (!bat_client_->loadState(data)) {
OnWalletInitialized(ledger::Result::INVALID_LEDGER_STATE);
}
} else {
OnWalletInitialized(result);
}

OnWalletInitialized(result);
LoadPublisherState(this);
}

void LedgerImpl::LoadPublisherState(ledger::LedgerCallbackHandler* handler) {
Expand All @@ -135,13 +140,13 @@ void LedgerImpl::LoadPublisherState(ledger::LedgerCallbackHandler* handler) {

void LedgerImpl::OnPublisherStateLoaded(ledger::Result result,
const std::string& data) {
if (result != ledger::Result::OK) {
OnWalletInitialized(result);
return;
if (result == ledger::Result::OK) {
if (!bat_publishers_->loadState(data)) {
result = ledger::Result::INVALID_PUBLISHER_STATE;
}
}

bat_publishers_->loadState(data);
LoadLedgerState(this);
OnWalletInitialized(result);
}

void LedgerImpl::SaveLedgerState(const std::string& data) {
Expand Down

0 comments on commit 9739d0b

Please sign in to comment.