From 81fc89115650853e94aac7c770c34c1bdadd26bb Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Mon, 1 Apr 2019 11:22:26 +0100 Subject: [PATCH] Change payment token redemption timing fixes https://github.com/brave/brave-browser/issues/3659 --- .../browser/rewards_service_browsertest.cc | 65 ++++++++++++-- .../browser/rewards_service_impl.cc | 27 +++++- .../browser/rewards_service_impl.h | 8 +- .../bat_ledger/bat_ledger_service_impl.cc | 9 ++ .../bat_ledger/bat_ledger_service_impl.h | 55 ++++++------ .../public/interfaces/bat_ledger.mojom | 2 + .../src/bat/ads/internal/ads_serve.cc | 3 + vendor/bat-native-confirmations/BUILD.gn | 2 + vendor/bat-native-confirmations/README.md | 6 ++ .../include/bat/confirmations/confirmations.h | 3 + .../src/bat/confirmations/confirmations.cc | 1 + .../internal/confirmations_impl.cc | 89 ++++++++++++++++--- .../internal/confirmations_impl.h | 6 ++ .../confirmations/internal/payout_tokens.cc | 29 ++++-- .../confirmations/internal/payout_tokens.h | 3 +- .../confirmations/internal/security_helper.cc | 3 +- .../confirmations/internal/static_values.h | 7 +- .../src/bat/confirmations/internal/time.cc | 17 ++++ .../src/bat/confirmations/internal/time.h | 20 +++++ .../include/bat/ledger/ledger.h | 1 + .../src/bat/ledger/internal/ledger_impl.cc | 1 + .../src/bat/ledger/ledger.cc | 1 + 22 files changed, 296 insertions(+), 62 deletions(-) create mode 100644 vendor/bat-native-confirmations/src/bat/confirmations/internal/time.cc create mode 100644 vendor/bat-native-confirmations/src/bat/confirmations/internal/time.h diff --git a/components/brave_rewards/browser/rewards_service_browsertest.cc b/components/brave_rewards/browser/rewards_service_browsertest.cc index 77a796dffb01..42dbcc25817d 100644 --- a/components/brave_rewards/browser/rewards_service_browsertest.cc +++ b/components/brave_rewards/browser/rewards_service_browsertest.cc @@ -20,8 +20,6 @@ #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" -using namespace brave_rewards; - namespace brave_test_resp { std::string registrarVK_; std::string verification_; @@ -115,8 +113,9 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest { InProcessBrowserTest::SetUpOnMainThread(); brave::RegisterPathProvider(); ReadTestData(); - rewards_service_ = static_cast( - RewardsServiceFactory::GetForProfile(browser()->profile())); + rewards_service_ = static_cast( + brave_rewards::RewardsServiceFactory::GetForProfile( + browser()->profile())); rewards_service_->SetLedgerEnvForTesting(); } @@ -147,6 +146,12 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest { base::Unretained(this))); } + void GetDebug() { + rewards_service()->GetDebug( + base::Bind(&BraveRewardsBrowserTest::OnGetDebug, + base::Unretained(this))); + } + void ReadTestData() { base::FilePath path; ASSERT_TRUE(base::PathService::Get(brave::DIR_TEST_DATA, &path)); @@ -200,13 +205,16 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest { ASSERT_TRUE(jsResult.ExtractBool()); } - RewardsServiceImpl* rewards_service() { return rewards_service_; } + brave_rewards::RewardsServiceImpl* rewards_service() { + return rewards_service_; + } MOCK_METHOD1(OnGetProduction, void(bool)); + MOCK_METHOD1(OnGetDebug, void(bool)); MOCK_METHOD1(OnGetReconcileTime, void(int32_t)); MOCK_METHOD1(OnGetShortRetries, void(bool)); - RewardsServiceImpl* rewards_service_; + brave_rewards::RewardsServiceImpl* rewards_service_; MockURLFetcherFactory factory; }; @@ -387,6 +395,41 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) { GetProduction(); RunUntilIdle(); + // SetDebug(true) + EXPECT_CALL(*this, OnGetDebug(true)); + // Debug - true and 1 + EXPECT_CALL(*this, OnGetDebug(true)).Times(2); + // Debug - false and random + EXPECT_CALL(*this, OnGetDebug(false)).Times(2); + + rewards_service()->SetDebug(true); + GetDebug(); + RunUntilIdle(); + + // Debug - true + rewards_service()->SetDebug(false); + rewards_service()->HandleFlags("debug=true"); + GetDebug(); + RunUntilIdle(); + + // Debug - 1 + rewards_service()->SetDebug(false); + rewards_service()->HandleFlags("debug=1"); + GetDebug(); + RunUntilIdle(); + + // Debug - false + rewards_service()->SetDebug(true); + rewards_service()->HandleFlags("debug=false"); + GetDebug(); + RunUntilIdle(); + + // Debug - random + rewards_service()->SetDebug(true); + rewards_service()->HandleFlags("debug=werwe"); + GetDebug(); + RunUntilIdle(); + // positive number EXPECT_CALL(*this, OnGetReconcileTime(10)); // negative number and string @@ -428,36 +471,42 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) { IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsMultipleFlags) { EXPECT_CALL(*this, OnGetProduction(false)); + EXPECT_CALL(*this, OnGetDebug(true)); EXPECT_CALL(*this, OnGetReconcileTime(10)); EXPECT_CALL(*this, OnGetShortRetries(true)); rewards_service()->SetProduction(true); + rewards_service()->SetDebug(true); rewards_service()->SetReconcileTime(0); rewards_service()->SetShortRetries(false); rewards_service()->HandleFlags( - "staging=true,short-retries=true,reconcile-interval=10"); + "staging=true,debug=true,short-retries=true,reconcile-interval=10"); GetReconcileTime(); GetShortRetries(); GetProduction(); + GetDebug(); RunUntilIdle(); } IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsWrongInput) { EXPECT_CALL(*this, OnGetProduction(true)); + EXPECT_CALL(*this, OnGetDebug(false)); EXPECT_CALL(*this, OnGetReconcileTime(0)); EXPECT_CALL(*this, OnGetShortRetries(false)); rewards_service()->SetProduction(true); + rewards_service()->SetDebug(false); rewards_service()->SetReconcileTime(0); rewards_service()->SetShortRetries(false); rewards_service()->HandleFlags( - "staging=,shortretries=true,reconcile-interval"); + "staging=,debug=,shortretries=true,reconcile-interval"); GetReconcileTime(); GetShortRetries(); + GetDebug(); GetProduction(); RunUntilIdle(); } diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index e9a5d2adcae7..0273c878a6fb 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -401,6 +401,8 @@ void RewardsServiceImpl::StartLedger() { #endif SetProduction(isProduction); + SetDebug(false); + const base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); @@ -2444,6 +2446,20 @@ void RewardsServiceImpl::HandleFlags(const std::string& options) { continue; } + if (name == "debug") { + bool is_debug; + std::string lower = base::ToLowerASCII(value); + + if (lower == "true" || lower == "1") { + is_debug = true; + } else { + is_debug = false; + } + + SetDebug(is_debug); + continue; + } + if (name == "reconcile-interval") { int reconcile_int; bool success = base::StringToInt(value, &reconcile_int); @@ -2534,6 +2550,10 @@ void RewardsServiceImpl::GetProduction(const GetProductionCallback& callback) { bat_ledger_service_->GetProduction(callback); } +void RewardsServiceImpl::GetDebug(const GetDebugCallback& callback) { + bat_ledger_service_->GetDebug(callback); +} + void RewardsServiceImpl::GetReconcileTime( const GetReconcileTimeCallback& callback) { bat_ledger_service_->GetReconcileTime(callback); @@ -2548,6 +2568,10 @@ void RewardsServiceImpl::SetProduction(bool production) { bat_ledger_service_->SetProduction(production); } +void RewardsServiceImpl::SetDebug(bool debug) { + bat_ledger_service_->SetDebug(debug); +} + void RewardsServiceImpl::SetReconcileTime(int32_t time) { bat_ledger_service_->SetReconcileTime(time); } @@ -2704,7 +2728,8 @@ void RewardsServiceImpl::GetAddressesForPaymentId( callback)); } -int GetExcludedPublishersNumberOnFileTaskRunner(PublisherInfoDatabase* backend) { +int GetExcludedPublishersNumberOnFileTaskRunner( + PublisherInfoDatabase* backend) { if (!backend) { return 0; } diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 0ea39ec8fd96..f5171a15c8b6 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -66,6 +66,7 @@ class PublisherInfoDatabase; class RewardsNotificationServiceImpl; using GetProductionCallback = base::Callback; +using GetDebugCallback = base::Callback; using GetReconcileTimeCallback = base::Callback; using GetShortRetriesCallback = base::Callback; @@ -176,6 +177,8 @@ class RewardsServiceImpl : public RewardsService, void HandleFlags(const std::string& options); void SetProduction(bool production); void GetProduction(const GetProductionCallback& callback); + void SetDebug(bool debug); + void GetDebug(const GetDebugCallback& callback); void SetReconcileTime(int32_t time); void GetReconcileTime(const GetReconcileTimeCallback& callback); void SetShortRetries(bool short_retries); @@ -399,8 +402,9 @@ class RewardsServiceImpl : public RewardsService, void GetExcludedPublishersNumberDB( ledger::GetExcludedPublishersNumberDBCallback callback) override; - void OnGetExcludedPublishersNumberDB(ledger::GetExcludedPublishersNumberDBCallback callback, - int number); + void OnGetExcludedPublishersNumberDB( + ledger::GetExcludedPublishersNumberDBCallback callback, + int number); // URLFetcherDelegate impl void OnURLFetchComplete(const net::URLFetcher* source) override; diff --git a/components/services/bat_ledger/bat_ledger_service_impl.cc b/components/services/bat_ledger/bat_ledger_service_impl.cc index 96fed480ec0c..8222031b0829 100644 --- a/components/services/bat_ledger/bat_ledger_service_impl.cc +++ b/components/services/bat_ledger/bat_ledger_service_impl.cc @@ -44,6 +44,11 @@ void BatLedgerServiceImpl::SetProduction(bool is_production) { ledger::is_production = is_production; } +void BatLedgerServiceImpl::SetDebug(bool is_debug) { + DCHECK(!initialized_ || testing()); + ledger::is_debug = is_debug; +} + void BatLedgerServiceImpl::SetReconcileTime(int32_t time) { DCHECK(!initialized_ || testing()); ledger::reconcile_time = time; @@ -62,6 +67,10 @@ void BatLedgerServiceImpl::GetProduction(GetProductionCallback callback) { std::move(callback).Run(ledger::is_production); } +void BatLedgerServiceImpl::GetDebug(GetDebugCallback callback) { + std::move(callback).Run(ledger::is_debug); +} + void BatLedgerServiceImpl::GetReconcileTime(GetReconcileTimeCallback callback) { std::move(callback).Run(ledger::reconcile_time); } diff --git a/components/services/bat_ledger/bat_ledger_service_impl.h b/components/services/bat_ledger/bat_ledger_service_impl.h index 08e93fdb0e16..ef5a263d579f 100644 --- a/components/services/bat_ledger/bat_ledger_service_impl.h +++ b/components/services/bat_ledger/bat_ledger_service_impl.h @@ -1,4 +1,5 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -13,31 +14,33 @@ namespace bat_ledger { class BatLedgerServiceImpl : public mojom::BatLedgerService { - public: - explicit BatLedgerServiceImpl( - std::unique_ptr service_ref); - ~BatLedgerServiceImpl() override; - - // bat_ledger::mojom::BatLedgerService - void Create(mojom::BatLedgerClientAssociatedPtrInfo client_info, - mojom::BatLedgerAssociatedRequest bat_ledger) override; - - void SetProduction(bool isProduction) override; - void SetReconcileTime(int32_t time) override; - void SetShortRetries(bool short_retries) override; - void SetTesting() override; - - void GetProduction(GetProductionCallback callback) override; - void GetReconcileTime(GetReconcileTimeCallback callback) override; - void GetShortRetries(GetShortRetriesCallback callback) override; - - private: - const std::unique_ptr service_ref_; - bool initialized_; - - DISALLOW_COPY_AND_ASSIGN(BatLedgerServiceImpl); + public: + explicit BatLedgerServiceImpl( + std::unique_ptr service_ref); + ~BatLedgerServiceImpl() override; + + // bat_ledger::mojom::BatLedgerService + void Create(mojom::BatLedgerClientAssociatedPtrInfo client_info, + mojom::BatLedgerAssociatedRequest bat_ledger) override; + + void SetProduction(bool isProduction) override; + void SetDebug(bool isDebug) override; + void SetReconcileTime(int32_t time) override; + void SetShortRetries(bool short_retries) override; + void SetTesting() override; + + void GetProduction(GetProductionCallback callback) override; + void GetDebug(GetDebugCallback callback) override; + void GetReconcileTime(GetReconcileTimeCallback callback) override; + void GetShortRetries(GetShortRetriesCallback callback) override; + + private: + const std::unique_ptr service_ref_; + bool initialized_; + + DISALLOW_COPY_AND_ASSIGN(BatLedgerServiceImpl); }; -} // namespace bat_ledger +} // namespace bat_ledger -#endif // BRAVE_COMPONENTS_SERVICES_BAT_LEDGER_BAT_LEDGER_SERVICE_IMPL_H_ +#endif // BRAVE_COMPONENTS_SERVICES_BAT_LEDGER_BAT_LEDGER_SERVICE_IMPL_H_ diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index c3a33902adfe..2a2f90b31b52 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -9,11 +9,13 @@ interface BatLedgerService { Create(associated BatLedgerClient bat_ledger_client, associated BatLedger& bat_ledger); SetProduction(bool isProduction); + SetDebug(bool isDebug); SetReconcileTime(int32 time); SetShortRetries(bool short_retries); SetTesting(); GetProduction() => (bool production); + GetDebug() => (bool debug); GetReconcileTime() => (int32 time); GetShortRetries() => (bool short_retries); }; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_serve.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_serve.cc index d0b24517036a..c570baf7d4cd 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_serve.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_serve.cc @@ -199,6 +199,9 @@ void AdsServe::RetryDownloadingCatalog() { next_retry_start_timer_in_ *= 2; } + auto rand_delay = base::RandInt(0, next_retry_start_timer_in_ / 10); + next_retry_start_timer_in_ += rand_delay; + ads_->StartCollectingActivity(next_retry_start_timer_in_); } diff --git a/vendor/bat-native-confirmations/BUILD.gn b/vendor/bat-native-confirmations/BUILD.gn index e98230594957..4e6a570c044a 100644 --- a/vendor/bat-native-confirmations/BUILD.gn +++ b/vendor/bat-native-confirmations/BUILD.gn @@ -74,6 +74,8 @@ source_set("bat-native-confirmations") { "src/bat/confirmations/internal/security_helper.h", "src/bat/confirmations/internal/string_helper.cc", "src/bat/confirmations/internal/string_helper.h", + "src/bat/confirmations/internal/time.cc", + "src/bat/confirmations/internal/time.h", "src/bat/confirmations/internal/token_info.cc", "src/bat/confirmations/internal/token_info.h", "src/bat/confirmations/internal/unblinded_tokens.cc", diff --git a/vendor/bat-native-confirmations/README.md b/vendor/bat-native-confirmations/README.md index bd81cad4d7d1..297ca2608c07 100644 --- a/vendor/bat-native-confirmations/README.md +++ b/vendor/bat-native-confirmations/README.md @@ -131,6 +131,12 @@ Use staging Ads Serve as defined by `STAGING_SERVER` in `static_values.h`. Defau --rewards=staging=true ``` +Use shorter timers to help with testing token redemption as defined by `kDebugNextTokenRedemptionAfterSeconds` in `static_values.h`. + +``` +--rewards=debug=true +``` + Enable diagnostic logging, where `#` should set to a minimum log level. Valid values are from 0 to 3 where INFO = 0, WARNING = 1, ERROR = 2 and FATAL = 3. So if you want INFO, WARNING and ERROR you would choose 2 ``` diff --git a/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h b/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h index aa8ea66cc14f..b7c93ce991ce 100644 --- a/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h +++ b/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h @@ -23,6 +23,9 @@ namespace confirmations { // Determines whether to use the staging or production Ad Serve extern bool _is_production; +// Determines whether to enable or disable debugging +extern bool _is_debug; + extern const char _confirmations_name[]; using TransactionInfo = ::ledger::TransactionInfo; diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/confirmations.cc b/vendor/bat-native-confirmations/src/bat/confirmations/confirmations.cc index abef0178bbac..76476cb16495 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/confirmations.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/confirmations.cc @@ -10,6 +10,7 @@ namespace confirmations { bool _is_production = false; +bool _is_debug = false; const char _confirmations_name[] = "confirmations.json"; diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc index e1c67de4da00..7b8f0ad39562 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc @@ -14,7 +14,9 @@ #include "bat/confirmations/internal/redeem_token.h" #include "bat/confirmations/internal/payout_tokens.h" #include "bat/confirmations/internal/unblinded_tokens.h" +#include "bat/confirmations/internal/time.h" +#include "base/rand_util.h" #include "base/json/json_reader.h" #include "base/json/json_writer.h" #include "base/time/time.h" @@ -39,6 +41,7 @@ ConfirmationsImpl::ConfirmationsImpl( payout_redeemed_tokens_timer_id_(0), payout_tokens_(std::make_unique(this, confirmations_client, unblinded_payment_tokens_.get())), + next_token_redemption_date_in_seconds_(0), state_has_loaded_(false), confirmations_client_(confirmations_client) { } @@ -76,7 +79,9 @@ void ConfirmationsImpl::CheckReady() { is_initialized_ = true; BLOG(INFO) << "Successfully initialized"; - PayoutRedeemedTokens(); + auto start_timer_in = CalculateTokenRedemptionTimeInSeconds(); + StartPayingOutRedeemedTokens(start_timer_in); + RefillTokensIfNecessary(); } @@ -93,6 +98,10 @@ std::string ConfirmationsImpl::ToJSON() const { GetCatalogIssuersAsDictionary(public_key_, catalog_issuers_); dictionary.SetKey("catalog_issuers", base::Value(std::move(catalog_issuers))); + // Next token redemption date + dictionary.SetKey("next_token_redemption_date_in_seconds", base::Value( + std::to_string(next_token_redemption_date_in_seconds_))); + // Transaction history auto transaction_history = GetTransactionHistoryAsDictionary(transaction_history_); @@ -171,22 +180,24 @@ bool ConfirmationsImpl::FromJSON(const std::string& json) { return false; } - // Catalog issuers if (!GetCatalogIssuersFromJSON(dictionary.get())) { BLOG(WARNING) << "Failed to get catalog issuers from JSON: " << json; } - // Transaction history + if (!GetNextTokenRedemptionDateInSecondsFromJSON(dictionary.get())) { + BLOG(WARNING) + << "Failed to get next token redemption date in seconds from JSON: " + << json; + } + if (!GetTransactionHistoryFromJSON(dictionary.get())) { BLOG(WARNING) << "Failed to get transaction history from JSON: " << json; } - // Unblinded tokens if (!GetUnblindedTokensFromJSON(dictionary.get())) { BLOG(WARNING) << "Failed to get unblinded tokens from JSON: " << json; } - // Unblinded payment tokens if (!GetUnblindedPaymentTokensFromJSON(dictionary.get())) { BLOG(WARNING) << "Failed to get unblinded payment tokens from JSON: " << json; @@ -209,7 +220,7 @@ bool ConfirmationsImpl::GetCatalogIssuersFromJSON( std::string public_key; std::map catalog_issuers; - if (!GetCatalogIssuersFromDictionary(dictionary, &public_key, + if (!GetCatalogIssuersFromDictionary(catalog_issuers_dictionary, &public_key, &catalog_issuers)) { return false; } @@ -269,6 +280,20 @@ bool ConfirmationsImpl::GetCatalogIssuersFromDictionary( return true; } +bool ConfirmationsImpl::GetNextTokenRedemptionDateInSecondsFromJSON( + base::DictionaryValue* dictionary) { + auto* next_token_redemption_date_in_seconds_value = + dictionary->FindKey("next_token_redemption_date_in_seconds"); + if (!next_token_redemption_date_in_seconds_value) { + return false; + } + + next_token_redemption_date_in_seconds_ = + std::stoull(next_token_redemption_date_in_seconds_value->GetString()); + + return true; +} + bool ConfirmationsImpl::GetTransactionHistoryFromJSON( base::DictionaryValue* dictionary) { auto* transaction_history_value = dictionary->FindKey("transaction_history"); @@ -325,9 +350,7 @@ bool ConfirmationsImpl::GetTransactionHistoryFromDictionary( std::stoull(timestamp_in_seconds_value->GetString()); } else { // timestamp missing, fallback to default - auto now = base::Time::Now(); - info.timestamp_in_seconds = - static_cast((now - base::Time()).InSeconds()); + info.timestamp_in_seconds = Time::NowInSeconds(); } // Estimated redemption value @@ -551,11 +574,8 @@ double ConfirmationsImpl::GetEstimatedRedemptionValue( void ConfirmationsImpl::AppendTransactionToTransactionHistory( const double estimated_redemption_value, const ConfirmationType confirmation_type) { - auto now = base::Time::Now(); - auto now_in_seconds = (now - base::Time()).InSeconds(); - TransactionInfo info; - info.timestamp_in_seconds = now_in_seconds; + info.timestamp_in_seconds = Time::NowInSeconds(); info.estimated_redemption_value = estimated_redemption_value; switch (confirmation_type) { @@ -654,6 +674,49 @@ void ConfirmationsImpl::RefillTokensIfNecessary() const { refill_tokens_->Refill(wallet_info_, public_key_); } +uint64_t ConfirmationsImpl::CalculateTokenRedemptionTimeInSeconds() { + auto now_in_seconds = Time::NowInSeconds(); + + uint64_t start_timer_in; + + if (_is_debug) { + if (next_token_redemption_date_in_seconds_ - now_in_seconds >= + kDebugNextTokenRedemptionAfterSeconds) { + UpdateNextTokenRedemptionDate(); + SaveState(); + } + } + + if (next_token_redemption_date_in_seconds_ == 0) { + UpdateNextTokenRedemptionDate(); + SaveState(); + } + + if (now_in_seconds >= next_token_redemption_date_in_seconds_) { + // Browser was launched after the token redemption date + start_timer_in = base::RandInt(0, 5 * base::Time::kSecondsPerMinute); + } else { + start_timer_in = next_token_redemption_date_in_seconds_ - now_in_seconds; + } + + auto rand_delay = base::RandInt(0, start_timer_in / 10); + start_timer_in += rand_delay; + + return start_timer_in; +} + +void ConfirmationsImpl::UpdateNextTokenRedemptionDate() { + next_token_redemption_date_in_seconds_ = Time::NowInSeconds(); + + if (!_is_debug) { + next_token_redemption_date_in_seconds_ += + kNextTokenRedemptionAfterSeconds; + } else { + next_token_redemption_date_in_seconds_ += + kDebugNextTokenRedemptionAfterSeconds; + } +} + void ConfirmationsImpl::StartPayingOutRedeemedTokens( const uint64_t start_timer_in) { StopPayingOutRedeemedTokens(); diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h index 0f16c24de359..d045597f8dcf 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h @@ -62,6 +62,8 @@ class ConfirmationsImpl : public Confirmations { void ConfirmAd(std::unique_ptr info) override; // Payout redeemed tokens + void UpdateNextTokenRedemptionDate(); + uint64_t CalculateTokenRedemptionTimeInSeconds(); void StartPayingOutRedeemedTokens(const uint64_t start_timer_in); // State @@ -103,6 +105,7 @@ class ConfirmationsImpl : public Confirmations { void StopPayingOutRedeemedTokens(); bool IsPayingOutRedeemedTokens() const; std::unique_ptr payout_tokens_; + uint64_t next_token_redemption_date_in_seconds_; // State void OnStateSaved(const Result result); @@ -132,6 +135,9 @@ class ConfirmationsImpl : public Confirmations { std::string* public_key, std::map* issuers) const; + bool GetNextTokenRedemptionDateInSecondsFromJSON( + base::DictionaryValue* dictionary); + bool GetTransactionHistoryFromJSON( base::DictionaryValue* dictionary); bool GetTransactionHistoryFromDictionary( diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.cc index 6b527aceadbf..a06c900de9d6 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.cc @@ -22,6 +22,7 @@ PayoutTokens::PayoutTokens( ConfirmationsImpl* confirmations, ConfirmationsClient* confirmations_client, UnblindedTokens* unblinded_payment_tokens) : + next_retry_start_timer_in_(0), confirmations_(confirmations), confirmations_client_(confirmations_client), unblinded_payment_tokens_(unblinded_payment_tokens) { @@ -115,26 +116,38 @@ void PayoutTokens::OnRedeemPaymentTokens( void PayoutTokens::OnPayout(const Result result) { if (result != SUCCESS) { BLOG(ERROR) << "Failed to payout tokens"; + + RetryNextPayout(); } else { unblinded_payment_tokens_->RemoveAllTokens(); BLOG(INFO) << "Successfully paid out tokens"; - } - ScheduleNextPayout(); + ScheduleNextPayout(); + } } void PayoutTokens::ScheduleNextPayout() const { - auto start_timer_in = CalculateTimerForNextPayout(); + confirmations_->UpdateNextTokenRedemptionDate(); + confirmations_->SaveState(); + + auto start_timer_in = confirmations_->CalculateTokenRedemptionTimeInSeconds(); confirmations_->StartPayingOutRedeemedTokens(start_timer_in); } -uint64_t PayoutTokens::CalculateTimerForNextPayout() const { - auto start_timer_in = kPayoutAfterSeconds; - auto rand_delay = base::RandInt(0, start_timer_in / 10); - start_timer_in += rand_delay; +void PayoutTokens::RetryNextPayout() { + BLOG(INFO) << "Retry next payout"; + + if (next_retry_start_timer_in_ == 0) { + next_retry_start_timer_in_ = 2 * base::Time::kSecondsPerMinute; + } else { + next_retry_start_timer_in_ *= 2; + } + + auto rand_delay = base::RandInt(0, next_retry_start_timer_in_ / 10); + next_retry_start_timer_in_ += rand_delay; - return start_timer_in; + confirmations_->StartPayingOutRedeemedTokens(next_retry_start_timer_in_); } } // namespace confirmations diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.h index 92ae27625fae..f56e67842d06 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/payout_tokens.h @@ -42,7 +42,8 @@ class PayoutTokens { void OnPayout(const Result result); void ScheduleNextPayout() const; - uint64_t CalculateTimerForNextPayout() const; + uint64_t next_retry_start_timer_in_; + void RetryNextPayout(); ConfirmationsImpl* confirmations_; // NOT OWNED ConfirmationsClient* confirmations_client_; // NOT OWNED diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/security_helper.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/security_helper.cc index 78c98d48478a..8d99ddea63bd 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/security_helper.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/security_helper.cc @@ -45,7 +45,8 @@ std::string Security::Sign( std::vector signed_message(crypto_sign_BYTES + concatenated_message.length()); - unsigned long long signed_message_size = 0; + // Resolving the following linter error breaks the build on Windows + unsigned long long signed_message_size = 0; // NOLINT crypto_sign(&signed_message.front(), &signed_message_size, reinterpret_cast(concatenated_message.c_str()), concatenated_message.length(), &public_key.front()); diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/static_values.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/static_values.h index 7eaf379b0b87..3508f7e22d28 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/static_values.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/static_values.h @@ -20,8 +20,11 @@ static const int kMaximumUnblindedTokens = 50; static const uint64_t kRetryGettingRefillSignedTokensAfterSeconds = 15; -static const uint64_t kPayoutAfterSeconds = - base::Time::kSecondsPerHour * base::Time::kHoursPerDay; +static const uint64_t kNextTokenRedemptionAfterSeconds = + base::Time::kMicrosecondsPerWeek / base::Time::kMicrosecondsPerSecond; + +static const uint64_t kDebugNextTokenRedemptionAfterSeconds = + 25 * base::Time::kSecondsPerMinute; } // namespace confirmations diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/time.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/time.cc new file mode 100644 index 000000000000..29e5a96e28a3 --- /dev/null +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/time.cc @@ -0,0 +1,17 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "bat/confirmations/internal/time.h" + +#include "base/time/time.h" + +namespace confirmations { + +uint64_t Time::NowInSeconds() { + auto now = base::Time::Now(); + return static_cast((now - base::Time()).InSeconds()); +} + +} // namespace confirmations diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/time.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/time.h new file mode 100644 index 000000000000..b5e420a763df --- /dev/null +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/time.h @@ -0,0 +1,20 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BAT_CONFIRMATIONS_INTERNAL_TIME_H_ +#define BAT_CONFIRMATIONS_INTERNAL_TIME_H_ + +#include + +namespace confirmations { + +class Time { + public: + static uint64_t NowInSeconds(); +}; + +} // namespace confirmations + +#endif // BAT_CONFIRMATIONS_INTERNAL_TIME_H_ diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index e2a461ffa038..52231ef7d39b 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -22,6 +22,7 @@ namespace ledger { extern bool is_production; +extern bool is_debug; extern bool is_testing; extern int reconcile_time; // minutes extern bool short_retries; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 896fb43e3a04..30e31dbc79b9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -247,6 +247,7 @@ void LedgerImpl::SetConfirmationsWalletInfo( const braveledger_bat_helper::WALLET_INFO_ST& wallet_info) { if (!bat_confirmations_) { confirmations::_is_production = ledger::is_production; + confirmations::_is_debug = ledger::is_debug; bat_confirmations_.reset( confirmations::Confirmations::CreateInstance(ledger_client_)); diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index 85cb17cbdce7..eb117dc66e40 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -14,6 +14,7 @@ namespace ledger { bool is_production = true; +bool is_debug = false; bool is_testing = false; int reconcile_time = 0; // minutes bool short_retries = false;