Skip to content

Commit

Permalink
Merge pull request #5728 from brave/multiple-publishers-ac
Browse files Browse the repository at this point in the history
Fixes ac for multiple publishers
  • Loading branch information
NejcZdovc authored Jun 3, 2020
2 parents da87729 + 44f7fa9 commit 19f11df
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 19 deletions.
75 changes: 75 additions & 0 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,81 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, AutoContribution) {
"-20.0BAT");
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
AutoContributionMultiplePublishers) {
// Enable Rewards
EnableRewards();

ClaimPromotionViaCode();

// Visit verified publisher
const bool verified = true;
VisitPublisher("duckduckgo.com", verified);
VisitPublisher("laurenwags.github.io", verified);

// Trigger contribution process
rewards_service()->StartMonthlyContributionForTest();

// Wait for reconciliation to complete successfully
WaitForACReconcileCompleted();
ASSERT_EQ(ac_reconcile_status_, ledger::Result::LEDGER_OK);

// Make sure that balance is updated correctly
IsBalanceCorrect();

// Check that summary table shows the appropriate contribution
rewards_service_browsertest_utils::WaitForElementToContain(
contents(),
"[color=contribute]",
"-20.0BAT");
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
AutoContributionMultiplePublishersUphold) {
SetUpUpholdWallet(50.0);
// Enable Rewards
EnableRewards();

ledger::SKUOrderItemList items;
auto item = ledger::SKUOrderItem::New();
item->order_item_id = "ed193339-e58c-483c-8d61-7decd3c24827";
item->order_id = "a38b211b-bf78-42c8-9479-b11e92e3a76c";
item->quantity = 80;
item->price = 0.25;
item->description = "description";
item->type = ledger::SKUOrderItemType::SINGLE_USE;
items.push_back(std::move(item));

auto order = ledger::SKUOrder::New();
order->order_id = "a38b211b-bf78-42c8-9479-b11e92e3a76c";
order->total_amount = 20;
order->merchant_id = "";
order->location = "brave.com";
order->items = std::move(items);
sku_order_ = std::move(order);

// Visit verified publisher
const bool verified = true;
VisitPublisher("duckduckgo.com", verified);
VisitPublisher("laurenwags.github.io", verified);

// Trigger contribution process
rewards_service()->StartMonthlyContributionForTest();

// Wait for reconciliation to complete successfully
WaitForACReconcileCompleted();
ASSERT_EQ(ac_reconcile_status_, ledger::Result::LEDGER_OK);

// Make sure that balance is updated correctly
IsBalanceCorrect();

// Check that summary table shows the appropriate contribution
rewards_service_browsertest_utils::WaitForElementToContain(
contents(),
"[color=contribute]",
"-20.0BAT");
}

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, AutoContributeWhenACOff) {
EnableRewards();

Expand Down
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2442,6 +2442,7 @@ void RewardsServiceImpl::SetLedgerEnvForTesting() {
bat_ledger_service_->SetTesting();

SetPublisherMinVisitTime(1);
SetShortRetries(true);

// this is needed because we are using braveledger_request_util::buildURL
// directly in BraveRewardsBrowserTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@ void Contribution::OnResult(
}

if (result == ledger::Result::RETRY_LONG) {
if (ledger::short_retries) {
SetRetryTimer(contribution->contribution_id, 1);
return;
}

if (contribution->processor ==
ledger::ContributionProcessor::BRAVE_TOKENS) {
SetRetryTimer(contribution->contribution_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ void Unblinded::OnUnblindedTokens(
ledger::UnblindedTokenList list,
const std::string& contribution_id,
GetContributionInfoAndUnblindedTokensCallback callback) {
if (list.empty()) {
BLOG(0, "Token list is empty");
callback(nullptr, {});
return;
}
BLOG_IF(1, list.empty(), "Token list is empty");

std::vector<ledger::UnblindedToken> converted_list;
for (auto& item : list) {
Expand Down Expand Up @@ -363,17 +359,22 @@ void Unblinded::OnProcessTokens(
return;
}

bool single_publisher = contribution->publishers.size() == 1;

for (auto& publisher : contribution->publishers) {
if (publisher->total_amount == publisher->contributed_amount) {
bool final_publisher = false;
for (auto publisher = contribution->publishers.begin();
publisher != contribution->publishers.end();
publisher++) {
if ((*publisher)->total_amount == (*publisher)->contributed_amount) {
continue;
}

if (std::next(publisher) == contribution->publishers.end()) {
final_publisher = true;
}

std::vector<ledger::UnblindedToken> token_list;
double current_amount = 0.0;
for (auto& item : list) {
if (current_amount >= publisher->total_amount) {
if (current_amount >= (*publisher)->total_amount) {
break;
}

Expand All @@ -385,12 +386,12 @@ void Unblinded::OnProcessTokens(
this,
_1,
contribution->contribution_id,
publisher->publisher_key,
single_publisher,
(*publisher)->publisher_key,
final_publisher,
callback);

braveledger_credentials::CredentialsRedeem redeem;
redeem.publisher_key = publisher->publisher_key;
redeem.publisher_key = (*publisher)->publisher_key;
redeem.type = contribution->type;
redeem.processor = contribution->processor;
redeem.token_list = token_list;
Expand All @@ -414,7 +415,7 @@ void Unblinded::TokenProcessed(
const ledger::Result result,
const std::string& contribution_id,
const std::string& publisher_key,
const bool single_publisher,
const bool final_publisher,
ledger::ResultCallback callback) {
if (result != ledger::Result::LEDGER_OK) {
BLOG(0, "Tokens were not processed correctly");
Expand All @@ -426,7 +427,7 @@ void Unblinded::TokenProcessed(
this,
_1,
contribution_id,
single_publisher,
final_publisher,
callback);

ledger_->UpdateContributionInfoContributedAmount(
Expand All @@ -438,9 +439,9 @@ void Unblinded::TokenProcessed(
void Unblinded::ContributionAmountSaved(
const ledger::Result result,
const std::string& contribution_id,
const bool single_publisher,
const bool final_publisher,
ledger::ResultCallback callback) {
if (single_publisher) {
if (final_publisher) {
callback(result);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ class Unblinded {
const ledger::Result result,
const std::string& contribution_id,
const std::string& publisher_key,
const bool single_publisher,
const bool final_publisher,
ledger::ResultCallback callback);

void ContributionAmountSaved(
const ledger::Result result,
const std::string& contribution_id,
const bool single_publisher,
const bool final_publisher,
ledger::ResultCallback callback);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
Expand Down

0 comments on commit 19f11df

Please sign in to comment.