Skip to content

Commit

Permalink
Merge pull request #41 from brave-intl/rewards
Browse files Browse the repository at this point in the history
first pass at updating state loading
  • Loading branch information
NejcZdovc authored Aug 10, 2018
2 parents 0230a43 + d2f2abe commit 2d9b7f6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 80 deletions.
7 changes: 4 additions & 3 deletions include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class LEDGER_EXPORT LedgerClient {
virtual std::string GenerateGUID() const = 0;
virtual void OnWalletCreated(Result result) = 0;
virtual void GetWalletProperties() = 0;
virtual void OnWalletProperties(ledger::WalletInfo) = 0;
virtual void OnWalletProperties(Result result,
std::unique_ptr<ledger::WalletInfo>) = 0;
virtual void OnReconcileComplete(Result result,
const std::string& viewing_id) = 0;

Expand All @@ -61,8 +62,8 @@ class LEDGER_EXPORT LedgerClient {
virtual void GetPromotion(const std::string& lang, const std::string& paymentId) = 0;
virtual void OnPromotion(ledger::Promo) = 0;
virtual void GetPromotionCaptcha() = 0;
virtual void OnPromotionCaptcha(std::string image) = 0;
virtual void OnRecoverWallet(const bool& error, const double& balance) = 0;
virtual void OnPromotionCaptcha(const std::string& image) = 0;
virtual void OnRecoverWallet(bool error, double balance) = 0;

virtual std::unique_ptr<ledger::LedgerURLLoader> LoadURL(
const std::string& url,
Expand Down
11 changes: 6 additions & 5 deletions src/bat_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ std::string BatClient::buildURL(const std::string& path, const std::string& pref
return url + prefix + path;
}

void BatClient::loadStateOrRegisterPersonaCallback(bool success, const std::string& data) {
void BatClient::loadStateCallback(bool success, const std::string& data) {
if (!success) {
LOG(ERROR) << "!!!here1";
registerPersona();
Expand Down Expand Up @@ -224,18 +224,19 @@ void BatClient::getWalletProperties() {
}

void BatClient::walletPropertiesCallback(bool success,
const std::string& response) {
const std::string& response) {

LOG(ERROR) << "!!!walletPropertiesCallback == " << response;

braveledger_bat_helper::WALLET_PROPERTIES_ST properties;
if (!success) {
ledger_->OnWalletProperties(ledger::Result::ERROR, properties);
return;
}

braveledger_bat_helper::WALLET_PROPERTIES_ST properties;
braveledger_bat_helper::loadFromJson(properties, response);
state_->walletProperties_ = properties;
ledger_->OnWalletProperties(properties);
ledger_->OnWalletProperties(ledger::Result::OK, properties);
}

bool BatClient::isReadyForReconcile() {
Expand Down Expand Up @@ -881,7 +882,7 @@ std::string BatClient::getWalletPassphrase() const {
void BatClient::saveState() {
std::string data;
braveledger_bat_helper::saveToJsonString(*state_, data);
ledger_->SaveLedgerState(data, this);
ledger_->SaveLedgerState(data);
}

void BatClient::recoverWallet(const std::string& passPhrase) {
Expand Down
9 changes: 4 additions & 5 deletions src/bat_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <vector>
#include <mutex>

#include "bat/ledger/ledger_callback_handler.h"
#include "bat/ledger/ledger_url_loader.h"
#include "bat_helper.h"
#include "url_request_handler.h"
Expand All @@ -20,12 +19,13 @@ class LedgerImpl;

namespace braveledger_bat_client {

class BatClient : public ledger::LedgerCallbackHandler {
class BatClient {
public:
explicit BatClient(bat_ledger::LedgerImpl* ledger);
~BatClient() override;
~BatClient();

void loadStateOrRegisterPersonaCallback(bool success, const std::string& data);
void loadStateCallback(bool success, const std::string& data);
void registerPersona();
void requestCredentialsCallback(bool result, const std::string& response);
void registerPersonaCallback(bool result, const std::string& response);
void setContributionAmount(const double& amount);
Expand Down Expand Up @@ -65,7 +65,6 @@ class BatClient : public ledger::LedgerCallbackHandler {
//void prepareBallotCallback(bool result, const std::string& response, const braveledger_bat_helper::FETCH_CALLBACK_EXTRA_DATA_ST& extraData);
//void commitBallotCallback(bool result, const std::string& response);
void vote(const std::string& publisher, const std::string& viewingId);
void registerPersona();
void reconcileCallback(bool result, const std::string& response);
void currentReconcile();
void currentReconcileCallback(bool result, const std::string& response);
Expand Down
15 changes: 0 additions & 15 deletions src/bat_publishers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ double BatPublishers::concaveScore(const uint64_t& duration) {
return (std::sqrt(b2_ + a4_ * duration) - b_) / (double)a2_;
}

void BatPublishers::initSynopsis() {
ledger_->LoadPublisherState(this);
}

const ledger::PublisherInfo::id_type getPublisherID(
const ledger::VisitData& visit_data) {
return visit_data.tld;
Expand Down Expand Up @@ -295,17 +291,6 @@ void BatPublishers::loadState(const std::string& data) {
calcScoreConsts();
}

void BatPublishers::OnPublisherStateLoaded(ledger::Result result,
const std::string& data) {
if (result != ledger::Result::OK) {
LOG(ERROR) << "Could not load publisher state";
return;
// TODO - error handling
}

loadState(data);
}

void BatPublishers::OnPublisherStateSaved(ledger::Result result) {
if (result != ledger::Result::OK) {
LOG(ERROR) << "Could not save publisher state";
Expand Down
7 changes: 1 addition & 6 deletions 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 initSynopsis();
void loadState(const std::string& data);

void saveVisit(const ledger::VisitData& visit_data);

Expand All @@ -54,8 +54,6 @@ class BatPublishers : public ledger::LedgerCallbackHandler {

private:
// LedgerCallbackHandler impl
void OnPublisherStateLoaded(ledger::Result result,
const std::string& data) override;
void OnPublisherStateSaved(ledger::Result result) override;

bool isEligableForContribution(const ledger::PublisherInfo& info);
Expand All @@ -66,9 +64,6 @@ class BatPublishers : public ledger::LedgerCallbackHandler {
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> publisher_info);


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

double concaveScore(const uint64_t& duration);

void saveState();
Expand Down
89 changes: 49 additions & 40 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ LedgerImpl::~LedgerImpl() {
}

void LedgerImpl::CreateWallet() {
initSynopsis(); // fix race condition here
LoadLedgerState(this);
LoadPublisherState(this);
}

void LedgerImpl::OnLoad(const ledger::VisitData& visit_data) {
Expand Down Expand Up @@ -74,13 +73,31 @@ void LedgerImpl::LoadLedgerState(ledger::LedgerCallbackHandler* handler) {
ledger_client_->LoadLedgerState(handler);
}

void LedgerImpl::OnLedgerStateLoaded(ledger::Result result,
const std::string& data) {
bat_client_->loadStateCallback(
result == ledger::Result::OK, data);
}

void LedgerImpl::LoadPublisherState(ledger::LedgerCallbackHandler* handler) {
ledger_client_->LoadPublisherState(handler);
}

void LedgerImpl::SaveLedgerState(const std::string& data,
ledger::LedgerCallbackHandler* handler) {
ledger_client_->SaveLedgerState(data, handler);
void LedgerImpl::OnPublisherStateLoaded(ledger::Result result,
const std::string& data) {
if (result != ledger::Result::OK) {
bat_client_->registerPersona();
LOG(ERROR) << "Could not load publisher state";
return;
// TODO - error handling
}

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

void LedgerImpl::SaveLedgerState(const std::string& data) {
ledger_client_->SaveLedgerState(data, this);
}

void LedgerImpl::SavePublisherState(const std::string& data,
Expand All @@ -92,17 +109,6 @@ std::string LedgerImpl::GenerateGUID() const {
return ledger_client_->GenerateGUID();
}

void LedgerImpl::OnLedgerStateLoaded(ledger::Result result,
const std::string& data) {
if (result != ledger::Result::OK) {
LOG(ERROR) << "Could not load ledger state";
//return;
// TODO - error handling
}

bat_client_->loadStateOrRegisterPersonaCallback(result == ledger::Result::OK, data);
}

void LedgerImpl::OnWalletCreated(ledger::Result result) {
ledger_client_->OnWalletCreated(result);
}
Expand All @@ -117,10 +123,6 @@ std::unique_ptr<ledger::LedgerURLLoader> LedgerImpl::LoadURL(const std::string&
url, headers, content, contentType, method, handler);
}

void LedgerImpl::initSynopsis() {
bat_publishers_->initSynopsis();
}

void LedgerImpl::RunIOTask(LedgerTaskRunnerImpl::Task io_task) {
std::unique_ptr<LedgerTaskRunnerImpl> task_runner(
new LedgerTaskRunnerImpl(io_task));
Expand Down Expand Up @@ -287,36 +289,41 @@ void LedgerImpl::OnMediaRequestCallback(uint64_t duration, const braveledger_bat
// SaveVisit(mediaPublisherInfo.publisher_, duration, true);
}

void LedgerImpl::OnWalletProperties(const braveledger_bat_helper::WALLET_PROPERTIES_ST& properties) {
ledger::WalletInfo info;
void LedgerImpl::OnWalletProperties(ledger::Result result,
const braveledger_bat_helper::WALLET_PROPERTIES_ST& properties) {
std::unique_ptr<ledger::WalletInfo> info;

info.altcurrency_ = properties.altcurrency_;
info.probi_ = properties.probi_;
info.balance_ = properties.balance_;
info.rates_ = properties.rates_;
info.parameters_choices_ = properties.parameters_choices_;
info.parameters_range_ = properties.parameters_range_;
info.parameters_days_ = properties.parameters_days_;
if (result == ledger::Result::OK) {
info.reset(new ledger::WalletInfo);
info->altcurrency_ = properties.altcurrency_;
info->probi_ = properties.probi_;
info->balance_ = properties.balance_;
info->rates_ = properties.rates_;
info->parameters_choices_ = properties.parameters_choices_;
info->parameters_range_ = properties.parameters_range_;
info->parameters_days_ = properties.parameters_days_;

for (size_t i = 0; i < properties.grants_.size(); i ++) {
ledger::GRANT grant;
for (size_t i = 0; i < properties.grants_.size(); i ++) {
ledger::GRANT grant;

grant.altcurrency = properties.grants_[i].altcurrency;
grant.probi = properties.grants_[i].probi;
grant.expiryTime = properties.grants_[i].expiryTime;
grant.altcurrency = properties.grants_[i].altcurrency;
grant.probi = properties.grants_[i].probi;
grant.expiryTime = properties.grants_[i].expiryTime;

info.grants_.push_back(grant);
info->grants_.push_back(grant);
}
}

ledger_client_->OnWalletProperties(info);
ledger_client_->OnWalletProperties(result, std::move(info));
}

void LedgerImpl::GetWalletProperties() const {
bat_client_->getWalletProperties();
}

void LedgerImpl::GetPromotion(const std::string& lang, const std::string& paymentId) const {
bat_client_->getPromotion(lang, paymentId);
void LedgerImpl::GetPromotion(const std::string& lang,
const std::string& payment_id) const {
bat_client_->getPromotion(lang, payment_id);
}

void LedgerImpl::OnPromotion(const braveledger_bat_helper::PROMOTION_ST& properties) {
Expand Down Expand Up @@ -344,8 +351,10 @@ void LedgerImpl::RecoverWallet(const std::string& passPhrase) const {
bat_client_->recoverWallet(passPhrase);
}

void LedgerImpl::OnRecoverWallet(const bool& error, const double& balance) {
ledger_client_->OnRecoverWallet(error, balance);
void LedgerImpl::OnRecoverWallet(bool error, double balance) {
ledger_client_->OnRecoverWallet(error ? ledger::Result::ERROR :
ledger::Result::OK,
balance);
}


Expand Down
15 changes: 9 additions & 6 deletions src/ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>

#include "bat/ledger/ledger.h"
#include "bat/ledger/ledger_callback_handler.h"
#include "bat/ledger/ledger_client.h"
#include "bat/ledger/ledger_url_loader.h"
#include "bat_helper.h"
Expand Down Expand Up @@ -65,16 +66,16 @@ class LedgerImpl : public ledger::Ledger,
bool GetPublisherAllowNonVerified() const override;
double GetContributionAmount() const override;

void SaveLedgerState(const std::string& data,
ledger::LedgerCallbackHandler* handler);
void SaveLedgerState(const std::string& data);
void SavePublisherState(const std::string& data,
ledger::LedgerCallbackHandler* handler);
void LoadLedgerState(ledger::LedgerCallbackHandler* handler);
void LoadPublisherState(ledger::LedgerCallbackHandler* handler);

void OnWalletCreated(ledger::Result);

void OnWalletProperties(const braveledger_bat_helper::WALLET_PROPERTIES_ST&);
void OnWalletProperties(ledger::Result result,
const braveledger_bat_helper::WALLET_PROPERTIES_ST&);
void GetWalletProperties() const override;

void GetPromotion(const std::string& lang, const std::string& paymentId) const override;
Expand All @@ -85,7 +86,7 @@ class LedgerImpl : public ledger::Ledger,

std::string GetWalletPassphrase() const override;
void RecoverWallet(const std::string& passPhrase) const override;
void OnRecoverWallet(const bool& error, const double& balance);
void OnRecoverWallet(bool error, double balance);

std::unique_ptr<ledger::LedgerURLLoader> LoadURL(const std::string& url,
const std::vector<std::string>& headers,
Expand All @@ -109,7 +110,6 @@ class LedgerImpl : public ledger::Ledger,
void OnMediaStop(uint32_t tab_id) override;
void OnXHRLoad(uint32_t tab_id, const std::string& url) override;

void initSynopsis();
void OnSetPublisherInfo(ledger::PublisherInfoCallback callback,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info);
Expand All @@ -126,10 +126,13 @@ class LedgerImpl : public ledger::Ledger,
void OnMediaRequestCallback(uint64_t duration,
const braveledger_bat_helper::MEDIA_PUBLISHER_INFO& mediaPublisherInfo);

// LedgerCallbackHandler impl
// ledger::LedgerCallbacHandler implementation
void OnPublisherStateLoaded(ledger::Result result,
const std::string& data) override;
void OnLedgerStateLoaded(ledger::Result result,
const std::string& data) override;


ledger::LedgerClient* ledger_client_;
std::unique_ptr<braveledger_bat_client::BatClient> bat_client_;
std::unique_ptr<braveledger_bat_publishers::BatPublishers> bat_publishers_;
Expand Down

0 comments on commit 2d9b7f6

Please sign in to comment.