Skip to content

Commit

Permalink
Merge pull request #1800 from brave/async_proof_batch
Browse files Browse the repository at this point in the history
Make ProofBatch async to avoid blocking the ledger utility process
  • Loading branch information
yrliou authored Feb 28, 2019
2 parents b32bba3 + ec256f8 commit 3bf58fc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
44 changes: 30 additions & 14 deletions vendor/bat-native-ledger/src/bat_contribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <utility>

#include "anon/anon.h"
#include "base/task/post_task.h"
#include "base/task_runner_util.h"
#include "bat_contribution.h"
#include "ledger_impl.h"
#include "rapidjson_bat_helper.h"
Expand Down Expand Up @@ -1130,7 +1132,7 @@ void BatContribution::PrepareBatchCallback(
}

void BatContribution::Proof() {
braveledger_bat_helper::BathProofs batch_proof;
braveledger_bat_helper::BatchProofs batch_proofs;

braveledger_bat_helper::Transactions transactions =
ledger_->GetTransactions();
Expand All @@ -1148,29 +1150,37 @@ void BatContribution::Proof() {
braveledger_bat_helper::BATCH_PROOF batch_proof_el;
batch_proof_el.transaction_ = transactions[k];
batch_proof_el.ballot_ = ballots[i];
batch_proof.push_back(batch_proof_el);
batch_proofs.push_back(batch_proof_el);
}
}
}
}

ProofBatch(batch_proof);
base::PostTaskAndReplyWithResult(
ledger_->GetTaskRunner().get(),
FROM_HERE,
base::BindOnce(&BatContribution::ProofBatch,
base::Unretained(this),
batch_proofs),
base::BindOnce(&BatContribution::ProofBatchCallback,
base::Unretained(this),
batch_proofs));
}

void BatContribution::ProofBatch(
const braveledger_bat_helper::BathProofs& batch_proof) {
std::vector<std::string> BatContribution::ProofBatch(
const braveledger_bat_helper::BatchProofs& batch_proofs) {
std::vector<std::string> proofs;

for (size_t i = 0; i < batch_proof.size(); i++) {
for (size_t i = 0; i < batch_proofs.size(); i++) {
braveledger_bat_helper::SURVEYOR_ST surveyor;
bool success = braveledger_bat_helper::loadFromJson(
surveyor,
batch_proof[i].ballot_.prepareBallot_);
batch_proofs[i].ballot_.prepareBallot_);

if (!success) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
"Failed to load surveyor state: " <<
batch_proof[i].ballot_.prepareBallot_;
batch_proofs[i].ballot_.prepareBallot_;
continue;
}

Expand All @@ -1190,13 +1200,13 @@ void BatContribution::ProofBatch(
}

std::string msg_key[1] = {"publisher"};
std::string msg_value[1] = {batch_proof[i].ballot_.publisher_};
std::string msg_value[1] = {batch_proofs[i].ballot_.publisher_};
std::string msg = braveledger_bat_helper::stringify(msg_key, msg_value, 1);

const char* proof = submitMessage(
msg.c_str(),
batch_proof[i].transaction_.masterUserToken_.c_str(),
batch_proof[i].transaction_.registrarVK_.c_str(),
batch_proofs[i].transaction_.masterUserToken_.c_str(),
batch_proofs[i].transaction_.registrarVK_.c_str(),
signature_to_send.c_str(),
surveyor.surveyorId_.c_str(),
surveyor.surveyVK_.c_str());
Expand All @@ -1210,19 +1220,25 @@ void BatContribution::ProofBatch(
proofs.push_back(annon_proof);
}

return proofs;
}

void BatContribution::ProofBatchCallback(
const braveledger_bat_helper::BatchProofs& batch_proofs,
const std::vector<std::string>& proofs) {
braveledger_bat_helper::Ballots ballots = ledger_->GetBallots();

for (size_t i = 0; i < batch_proof.size(); i++) {
for (size_t i = 0; i < batch_proofs.size(); i++) {
for (size_t j = 0; j < ballots.size(); j++) {
if (ballots[j].surveyorId_ == batch_proof[i].ballot_.surveyorId_) {
if (ballots[j].surveyorId_ == batch_proofs[i].ballot_.surveyorId_) {
ballots[j].proofBallot_ = proofs[i];
}
}
}

ledger_->SetBallots(ballots);

if (batch_proof.size() != proofs.size()) {
if (batch_proofs.size() != proofs.size()) {
AddRetry(braveledger_bat_helper::ContributionRetry::STEP_PROOF, "");
return;
}
Expand Down
20 changes: 12 additions & 8 deletions vendor/bat-native-ledger/src/bat_contribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@
// 5. PrepareBatch
// 6. PrepareBatchCallback
// 7. ProofBatch
// 8. SetTimer
// 9. PrepareVoteBatch
// 10. SetTimer
// 11. VoteBatch
// 12. VoteBatchCallback
// 13. SetTimer - we set timer until the whole batch is processed
// 8. ProofBatchCallback
// 9. SetTimer
// 10. PrepareVoteBatch
// 12. SetTimer
// 12. VoteBatch
// 13. VoteBatchCallback
// 14. SetTimer - we set timer until the whole batch is processed

namespace bat_ledger {
class LedgerImpl;
Expand Down Expand Up @@ -243,8 +244,11 @@ class BatContribution {

void Proof();

void ProofBatch(
const braveledger_bat_helper::BathProofs& batch_proof);
std::vector<std::string> ProofBatch(
const braveledger_bat_helper::BatchProofs& batch_proofs);
void ProofBatchCallback(
const braveledger_bat_helper::BatchProofs& batch_proofs,
const std::vector<std::string>& proofs);

void PrepareVoteBatch();

Expand Down
2 changes: 1 addition & 1 deletion vendor/bat-native-ledger/src/bat_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ struct BATCH_PROOF {
BALLOT_ST ballot_;
};

typedef std::vector<braveledger_bat_helper::BATCH_PROOF> BathProofs;
typedef std::vector<braveledger_bat_helper::BATCH_PROOF> BatchProofs;

enum class SERVER_TYPES {
LEDGER,
Expand Down
8 changes: 8 additions & 0 deletions vendor/bat-native-ledger/src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "ledger_impl.h"

#include "base/task/post_task.h"
#include "bat/ads/issuers_info.h"
#include "bat/ads/notification_info.h"
#include "bat/confirmations/confirmations.h"
Expand Down Expand Up @@ -45,6 +46,9 @@ LedgerImpl::LedgerImpl(ledger::LedgerClient* client) :
bat_get_media_(new BatGetMedia(this)),
bat_state_(new BatState(this)),
bat_contribution_(new BatContribution(this)),
task_runner_(base::CreateSequencedTaskRunnerWithTraits({
base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})),
initialized_(false),
initializing_(false),
last_tab_active_time_(0),
Expand Down Expand Up @@ -1288,4 +1292,8 @@ void LedgerImpl::GetConfirmationsHistory(
to_timestamp_seconds, callback);
}

scoped_refptr<base::SequencedTaskRunner> LedgerImpl::GetTaskRunner() {
return task_runner_;
}

} // namespace bat_ledger
8 changes: 8 additions & 0 deletions vendor/bat-native-ledger/src/ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
#include <fstream>
#include <vector>

#include "base/memory/scoped_refptr.h"
#include "bat/confirmations/confirmations_client.h"
#include "bat/ledger/ledger.h"
#include "bat/ledger/ledger_callback_handler.h"
#include "bat/ledger/ledger_client.h"
#include "bat_helper.h"
#include "logging.h"

namespace base {
class SequencedTaskRunner;
}

namespace braveledger_bat_client {
class BatClient;
}
Expand Down Expand Up @@ -426,6 +431,8 @@ class LedgerImpl : public ledger::Ledger,
const int line,
const ledger::LogLevel log_level) const;

scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();

private:
void AddRecurringPayment(const std::string& publisher_id,
const double& value) override;
Expand Down Expand Up @@ -497,6 +504,7 @@ class LedgerImpl : public ledger::Ledger,
std::unique_ptr<braveledger_bat_contribution::BatContribution>
bat_contribution_;
std::unique_ptr<confirmations::Confirmations> bat_confirmations_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;

bool initialized_;
bool initializing_;
Expand Down

0 comments on commit 3bf58fc

Please sign in to comment.