From a4543e593c3ccfee34770dbff66fce85a8e052ba Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 29 May 2020 18:52:49 +0200 Subject: [PATCH] Reset reconcile stamp even if ac is off Resolves https://github.com/brave/brave-browser/issues/10006 --- .../browser/rewards_service_browsertest.cc | 78 +++++++++++++++++++ .../internal/contribution/contribution.cc | 14 +++- .../internal/contribution/contribution.h | 4 +- .../internal/contribution/contribution_ac.cc | 5 +- .../internal/contribution/contribution_ac.h | 2 +- 5 files changed, 94 insertions(+), 9 deletions(-) diff --git a/components/brave_rewards/browser/rewards_service_browsertest.cc b/components/brave_rewards/browser/rewards_service_browsertest.cc index 15d82c153c9a..0c64fecb805e 100644 --- a/components/brave_rewards/browser/rewards_service_browsertest.cc +++ b/components/brave_rewards/browser/rewards_service_browsertest.cc @@ -2809,3 +2809,81 @@ IN_PROC_BROWSER_TEST_F( "[data-test-id=notification-close]", false); } + +IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, CheckIfReconcileWasReset) { + EnableRewards(); + uint64_t current_stamp = 0; + + base::RunLoop run_loop_first; + rewards_service_->GetReconcileStamp( + base::BindLambdaForTesting([&](uint64_t stamp) { + current_stamp = stamp; + run_loop_first.Quit(); + })); + run_loop_first.Run(); + + ClaimPromotionViaCode(); + + VisitPublisher("duckduckgo.com", true); + + TipPublisher("duckduckgo.com", ContributionType::MonthlyTip, true); + + base::RunLoop run_loop_second; + rewards_service_->GetReconcileStamp( + base::BindLambdaForTesting([&](uint64_t stamp) { + ASSERT_NE(current_stamp, stamp); + run_loop_second.Quit(); + })); + run_loop_second.Run(); +} + +IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, CheckIfReconcileWasResetACOff) { + EnableRewards(); + uint64_t current_stamp = 0; + + rewards_service_->SetAutoContribute(false); + + base::RunLoop run_loop_first; + rewards_service_->GetReconcileStamp( + base::BindLambdaForTesting([&](uint64_t stamp) { + current_stamp = stamp; + run_loop_first.Quit(); + })); + run_loop_first.Run(); + + ClaimPromotionViaCode(); + TipPublisher("duckduckgo.com", ContributionType::MonthlyTip, true); + + base::RunLoop run_loop_second; + rewards_service_->GetReconcileStamp( + base::BindLambdaForTesting([&](uint64_t stamp) { + ASSERT_NE(current_stamp, stamp); + run_loop_second.Quit(); + })); + run_loop_second.Run(); +} + +IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, CheckIfReconcileWasResetEmpty) { + EnableRewards(); + uint64_t current_stamp = 0; + + base::RunLoop run_loop_first; + rewards_service_->GetReconcileStamp( + base::BindLambdaForTesting([&](uint64_t stamp) { + current_stamp = stamp; + run_loop_first.Quit(); + })); + run_loop_first.Run(); + + ClaimPromotionViaCode(); + + rewards_service_->StartMonthlyContributionForTest(); + + base::RunLoop run_loop_second; + rewards_service_->GetReconcileStamp( + base::BindLambdaForTesting([&](uint64_t stamp) { + ASSERT_NE(current_stamp, stamp); + run_loop_second.Quit(); + })); + run_loop_second.Run(); +} diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc index c60092b36361..ac5408681330 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc @@ -150,25 +150,31 @@ void Contribution::ResetReconcileStamp() { } void Contribution::StartMonthlyContribution() { + const auto reconcile_stamp = ledger_->GetReconcileStamp(); + ResetReconcileStamp(); + if (!ledger_->GetRewardsMainEnabled()) { - ResetReconcileStamp(); return; } + BLOG(1, "Staring monthly contribution"); auto callback = std::bind(&Contribution::StartAutoContribute, this, - _1); + _1, + reconcile_stamp); monthly_->Process(callback); } -void Contribution::StartAutoContribute(const ledger::Result result) { +void Contribution::StartAutoContribute( + const ledger::Result result, + const uint64_t reconcile_stamp) { if (result != ledger::Result::LEDGER_OK) { BLOG(0, "Monthly contribution failed"); } - ac_->Process(); + ac_->Process(reconcile_stamp); } void Contribution::OnBalance( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h index be15bdcd587d..235ca32b8369 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h @@ -105,7 +105,9 @@ class Contribution { ledger::ResultCallback callback); private: - void StartAutoContribute(const ledger::Result result); + void StartAutoContribute( + const ledger::Result result, + const uint64_t reconcile_stamp); void ContributionCompletedSaved(const ledger::Result result); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc index bdb74b861303..0fc5f5ee8e3e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc @@ -22,7 +22,7 @@ ContributionAC::ContributionAC(bat_ledger::LedgerImpl* ledger, ContributionAC::~ContributionAC() = default; -void ContributionAC::Process() { +void ContributionAC::Process(const uint64_t reconcile_stamp) { if (!ledger_->GetRewardsMainEnabled() || !ledger_->GetAutoContribute()) { BLOG(1, "Auto contribution is off"); return; @@ -34,7 +34,7 @@ void ContributionAC::Process() { "", ledger::ExcludeFilter::FILTER_ALL_EXCEPT_EXCLUDED, true, - ledger_->GetReconcileStamp(), + reconcile_stamp, false, ledger_->GetPublisherMinVisits()); @@ -43,7 +43,6 @@ void ContributionAC::Process() { _1); ledger_->GetActivityInfoList(0, 0, std::move(filter), get_callback); - contribution_->ResetReconcileStamp(); } void ContributionAC::PreparePublisherList(ledger::PublisherInfoList list) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.h b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.h index 0d919e7ad2ce..f25b964ca513 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.h @@ -23,7 +23,7 @@ class ContributionAC { ~ContributionAC(); - void Process(); + void Process(const uint64_t reconcile_stamp); private: void PreparePublisherList(ledger::PublisherInfoList list);