Skip to content

Commit

Permalink
Merge pull request #2071 from /issues/3659
Browse files Browse the repository at this point in the history
Change payment token redemption timing
  • Loading branch information
yrliou authored Mar 26, 2019
2 parents 243949c + a4da645 commit dcc0def
Show file tree
Hide file tree
Showing 21 changed files with 294 additions and 61 deletions.
65 changes: 57 additions & 8 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -115,8 +113,9 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest {
InProcessBrowserTest::SetUpOnMainThread();
brave::RegisterPathProvider();
ReadTestData();
rewards_service_ = static_cast<RewardsServiceImpl*>(
RewardsServiceFactory::GetForProfile(browser()->profile()));
rewards_service_ = static_cast<brave_rewards::RewardsServiceImpl*>(
brave_rewards::RewardsServiceFactory::GetForProfile(
browser()->profile()));
rewards_service_->SetLedgerEnvForTesting();
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<brave_net::BraveURLFetcher> factory;
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
27 changes: 26 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ void RewardsServiceImpl::StartLedger() {
#endif
SetProduction(isProduction);

SetDebug(false);

const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();

Expand Down Expand Up @@ -2453,6 +2455,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);
Expand Down Expand Up @@ -2543,6 +2559,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);
Expand All @@ -2557,6 +2577,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);
}
Expand Down Expand Up @@ -2713,7 +2737,8 @@ void RewardsServiceImpl::GetAddressesForPaymentId(
callback));
}

int GetExcludedPublishersNumberOnFileTaskRunner(PublisherInfoDatabase* backend) {
int GetExcludedPublishersNumberOnFileTaskRunner(
PublisherInfoDatabase* backend) {
if (!backend) {
return 0;
}
Expand Down
8 changes: 6 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class PublisherInfoDatabase;
class RewardsNotificationServiceImpl;

using GetProductionCallback = base::Callback<void(bool)>;
using GetDebugCallback = base::Callback<void(bool)>;
using GetReconcileTimeCallback = base::Callback<void(int32_t)>;
using GetShortRetriesCallback = base::Callback<void(bool)>;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -407,8 +410,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;
Expand Down
9 changes: 9 additions & 0 deletions components/services/bat_ledger/bat_ledger_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
55 changes: 29 additions & 26 deletions components/services/bat_ledger/bat_ledger_service_impl.h
Original file line number Diff line number Diff line change
@@ -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/. */

Expand All @@ -13,31 +14,33 @@
namespace bat_ledger {

class BatLedgerServiceImpl : public mojom::BatLedgerService {
public:
explicit BatLedgerServiceImpl(
std::unique_ptr<service_manager::ServiceContextRef> 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_manager::ServiceContextRef> service_ref_;
bool initialized_;

DISALLOW_COPY_AND_ASSIGN(BatLedgerServiceImpl);
public:
explicit BatLedgerServiceImpl(
std::unique_ptr<service_manager::ServiceContextRef> 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_manager::ServiceContextRef> 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_
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_serve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}

Expand Down
2 changes: 2 additions & 0 deletions vendor/bat-native-confirmations/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions vendor/bat-native-confirmations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace confirmations {

bool _is_production = false;
bool _is_debug = false;

const char _confirmations_name[] = "confirmations.json";

Expand Down
Loading

0 comments on commit dcc0def

Please sign in to comment.