From 0c56ac469690ed831b1fc528ab32b0aca969f261 Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Wed, 27 Feb 2019 14:41:26 -0800 Subject: [PATCH 1/2] Make ProofBatch async to avoid blocking the ledger utility process --- .../bat-native-ledger/src/bat_contribution.cc | 20 +++++++++++++++++-- .../bat-native-ledger/src/bat_contribution.h | 18 ++++++++++------- vendor/bat-native-ledger/src/ledger_impl.cc | 8 ++++++++ vendor/bat-native-ledger/src/ledger_impl.h | 8 ++++++++ vendor/bat-native-ledger/src/logging.h | 9 ++------- 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat_contribution.cc b/vendor/bat-native-ledger/src/bat_contribution.cc index bf20b6c23de0..98d773933aeb 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.cc +++ b/vendor/bat-native-ledger/src/bat_contribution.cc @@ -11,6 +11,8 @@ #include #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" @@ -1154,10 +1156,18 @@ void BatContribution::Proof() { } } - ProofBatch(batch_proof); + base::PostTaskAndReplyWithResult( + ledger_->GetTaskRunner().get(), + FROM_HERE, + base::BindOnce(&BatContribution::ProofBatch, + base::Unretained(this), + batch_proof), + base::BindOnce(&BatContribution::ProofBatchCallback, + base::Unretained(this), + batch_proof)); } -void BatContribution::ProofBatch( +std::vector BatContribution::ProofBatch( const braveledger_bat_helper::BathProofs& batch_proof) { std::vector proofs; @@ -1210,6 +1220,12 @@ void BatContribution::ProofBatch( proofs.push_back(annon_proof); } + return proofs; +} + +void BatContribution::ProofBatchCallback( + const braveledger_bat_helper::BathProofs& batch_proof, + const std::vector& proofs) { braveledger_bat_helper::Ballots ballots = ledger_->GetBallots(); for (size_t i = 0; i < batch_proof.size(); i++) { diff --git a/vendor/bat-native-ledger/src/bat_contribution.h b/vendor/bat-native-ledger/src/bat_contribution.h index b6abc71c8f5e..bd0d086a243c 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.h +++ b/vendor/bat-native-ledger/src/bat_contribution.h @@ -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; @@ -243,8 +244,11 @@ class BatContribution { void Proof(); - void ProofBatch( + std::vector ProofBatch( const braveledger_bat_helper::BathProofs& batch_proof); + void ProofBatchCallback( + const braveledger_bat_helper::BathProofs& batch_proof, + const std::vector& proofs); void PrepareVoteBatch(); diff --git a/vendor/bat-native-ledger/src/ledger_impl.cc b/vendor/bat-native-ledger/src/ledger_impl.cc index 684f7d3288c0..2ec1d9586903 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/ledger_impl.cc @@ -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" @@ -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), @@ -1294,4 +1298,8 @@ void LedgerImpl::GetConfirmationsHistory( to_timestamp_seconds, callback); } +scoped_refptr LedgerImpl::GetTaskRunner() { + return task_runner_; +} + } // namespace bat_ledger diff --git a/vendor/bat-native-ledger/src/ledger_impl.h b/vendor/bat-native-ledger/src/ledger_impl.h index 06d81ed9993c..44c16d700e38 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.h +++ b/vendor/bat-native-ledger/src/ledger_impl.h @@ -12,6 +12,7 @@ #include #include +#include "base/memory/scoped_refptr.h" #include "bat/confirmations/confirmations_client.h" #include "bat/ledger/ledger.h" #include "bat/ledger/ledger_callback_handler.h" @@ -19,6 +20,10 @@ #include "bat_helper.h" #include "logging.h" +namespace base { +class SequencedTaskRunner; +} + namespace braveledger_bat_client { class BatClient; } @@ -430,6 +435,8 @@ class LedgerImpl : public ledger::Ledger, const int line, const ledger::LogLevel log_level) const; + scoped_refptr GetTaskRunner(); + private: void AddRecurringPayment(const std::string& publisher_id, const double& value) override; @@ -501,6 +508,7 @@ class LedgerImpl : public ledger::Ledger, std::unique_ptr bat_contribution_; std::unique_ptr bat_confirmations_; + scoped_refptr task_runner_; bool initialized_; bool initializing_; diff --git a/vendor/bat-native-ledger/src/logging.h b/vendor/bat-native-ledger/src/logging.h index e2d6f6f948c7..ad688e246545 100644 --- a/vendor/bat-native-ledger/src/logging.h +++ b/vendor/bat-native-ledger/src/logging.h @@ -12,11 +12,6 @@ #define BVLOG(client, severity) \ client->LogVerbose(__FILE__, __LINE__, severity)->stream() -#endif // BAT_REWARDS_LOGGING_H_ +#include "base/logging.h" -#if defined(NDEBUG) -#define DCHECK(x) (void)(x) -#else -#include "assert.h" -#define DCHECK(x) assert(x) -#endif +#endif // BAT_REWARDS_LOGGING_H_ From ec256f8a5190fec5870301860736a9a9e7cbc7ba Mon Sep 17 00:00:00 2001 From: Jocelyn Liu Date: Thu, 28 Feb 2019 09:34:33 -0800 Subject: [PATCH 2/2] s/batch_proof/batch_proofs and fix typo of BatchProofs type --- .../bat-native-ledger/src/bat_contribution.cc | 30 +++++++++---------- .../bat-native-ledger/src/bat_contribution.h | 4 +-- vendor/bat-native-ledger/src/bat_helper.h | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat_contribution.cc b/vendor/bat-native-ledger/src/bat_contribution.cc index 98d773933aeb..46cb5858b471 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.cc +++ b/vendor/bat-native-ledger/src/bat_contribution.cc @@ -1132,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(); @@ -1150,7 +1150,7 @@ 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); } } } @@ -1161,26 +1161,26 @@ void BatContribution::Proof() { FROM_HERE, base::BindOnce(&BatContribution::ProofBatch, base::Unretained(this), - batch_proof), + batch_proofs), base::BindOnce(&BatContribution::ProofBatchCallback, base::Unretained(this), - batch_proof)); + batch_proofs)); } std::vector BatContribution::ProofBatch( - const braveledger_bat_helper::BathProofs& batch_proof) { + const braveledger_bat_helper::BatchProofs& batch_proofs) { std::vector 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; } @@ -1200,13 +1200,13 @@ std::vector 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()); @@ -1224,13 +1224,13 @@ std::vector BatContribution::ProofBatch( } void BatContribution::ProofBatchCallback( - const braveledger_bat_helper::BathProofs& batch_proof, + const braveledger_bat_helper::BatchProofs& batch_proofs, const std::vector& 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]; } } @@ -1238,7 +1238,7 @@ void BatContribution::ProofBatchCallback( ledger_->SetBallots(ballots); - if (batch_proof.size() != proofs.size()) { + if (batch_proofs.size() != proofs.size()) { AddRetry(braveledger_bat_helper::ContributionRetry::STEP_PROOF, ""); return; } diff --git a/vendor/bat-native-ledger/src/bat_contribution.h b/vendor/bat-native-ledger/src/bat_contribution.h index bd0d086a243c..dc08bb8aa323 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.h +++ b/vendor/bat-native-ledger/src/bat_contribution.h @@ -245,9 +245,9 @@ class BatContribution { void Proof(); std::vector ProofBatch( - const braveledger_bat_helper::BathProofs& batch_proof); + const braveledger_bat_helper::BatchProofs& batch_proofs); void ProofBatchCallback( - const braveledger_bat_helper::BathProofs& batch_proof, + const braveledger_bat_helper::BatchProofs& batch_proofs, const std::vector& proofs); void PrepareVoteBatch(); diff --git a/vendor/bat-native-ledger/src/bat_helper.h b/vendor/bat-native-ledger/src/bat_helper.h index 8b85dbcfd594..ad7d72b43d3d 100644 --- a/vendor/bat-native-ledger/src/bat_helper.h +++ b/vendor/bat-native-ledger/src/bat_helper.h @@ -425,7 +425,7 @@ struct BATCH_PROOF { BALLOT_ST ballot_; }; -typedef std::vector BathProofs; +typedef std::vector BatchProofs; enum class SERVER_TYPES { LEDGER,