Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add verified tips calculation into notification

Add test for GetAmountFromVerifiedAuto

Fix lint

lint fixes

Adding tests

Combined tests to create WillTriggerNotification test

spacing

Updated methods to static
  • Loading branch information
Jason Sadler committed Apr 13, 2019
1 parent 7886077 commit 9145189
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 5 deletions.
14 changes: 13 additions & 1 deletion components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,21 @@ void BatLedgerImpl::GetRewardsMainEnabled(
std::move(callback).Run(ledger_->GetRewardsMainEnabled());
}

void BatLedgerImpl::OnHasSufficientBalanceToReconcile(
CallbackHolder<HasSufficientBalanceToReconcileCallback>* holder,
bool sufficient) {
if (holder->is_valid()) {
std::move(holder->get()).Run(sufficient);
}
delete holder;
}

void BatLedgerImpl::HasSufficientBalanceToReconcile(
HasSufficientBalanceToReconcileCallback callback) {
std::move(callback).Run(ledger_->HasSufficientBalanceToReconcile());
auto* holder = new CallbackHolder<HasSufficientBalanceToReconcileCallback>(
AsWeakPtr(), std::move(callback));
ledger_->HasSufficientBalanceToReconcile(
std::bind(BatLedgerImpl::OnHasSufficientBalanceToReconcile, holder, _1));
}

// static
Expand Down
4 changes: 4 additions & 0 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ class BatLedgerImpl : public mojom::BatLedger,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info);

static void OnHasSufficientBalanceToReconcile(
CallbackHolder<HasSufficientBalanceToReconcileCallback>* holder,
bool sufficient);

std::unique_ptr<BatLedgerClientMojoProxy> bat_ledger_client_mojo_proxy_;
std::unique_ptr<ledger::Ledger> ledger_;

Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ test("brave_unit_tests") {

if (brave_rewards_enabled) {
sources += [
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/bat_get_media_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/bat_get_media_unittest.h",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper_unittest.cc",
Expand Down
4 changes: 3 additions & 1 deletion vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ using ConfirmationsHistoryCallback = std::function<void(
using GetExcludedPublishersNumberDBCallback = std::function<void(uint32_t)>;
using OnWalletPropertiesCallback = std::function<void(const ledger::Result,
std::unique_ptr<ledger::WalletInfo>)>;
using HasSufficientBalanceToReconcileCallback = std::function<void(bool)>;

class LEDGER_EXPORT Ledger {
public:
Expand Down Expand Up @@ -269,7 +270,8 @@ class LEDGER_EXPORT Ledger {

virtual uint64_t GetBootStamp() const = 0;

virtual bool HasSufficientBalanceToReconcile() = 0;
virtual void HasSufficientBalanceToReconcile(
HasSufficientBalanceToReconcileCallback callback) = 0;

virtual void GetAddressesForPaymentId(
ledger::WalletAddressesCallback callback) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,78 @@ std::string BatContribution::GetAnonizeProof(
return proof;
}

void BatContribution::HasSufficientBalance(
ledger::HasSufficientBalanceToReconcileCallback callback) {
ledger::ActivityInfoFilter filter = ledger_->CreateActivityFilter(
std::string(),
ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED,
true,
ledger_->GetReconcileStamp(),
ledger_->GetPublisherAllowNonVerified(),
ledger_->GetPublisherMinVisits());
ledger_->GetActivityInfoList(
0,
0,
filter,
std::bind(&BatContribution::GetVerifiedAutoAmount,
this,
_1,
_2,
callback));
}

void BatContribution::GetVerifiedAutoAmount(
const ledger::PublisherInfoList& publisher_list,
uint32_t record,
ledger::HasSufficientBalanceToReconcileCallback callback) {
double total_reconcile_amount(GetAmountFromVerifiedAuto(
publisher_list, ledger_->GetContributionAmount()));
ledger_->GetRecurringTips(
std::bind(&BatContribution::GetVerifiedRecurringAmount,
this,
_1,
_2,
total_reconcile_amount,
callback));
}

// static
double BatContribution::GetAmountFromVerifiedAuto(
const ledger::PublisherInfoList& publisher_list,
double ac_amount) {
double non_verified_bat = 0.0;
for (const auto& publisher : publisher_list) {
if (!publisher.verified) {
non_verified_bat += (publisher.percent / 100.0) * ac_amount;
}
}
return ac_amount - non_verified_bat;
}

void BatContribution::GetVerifiedRecurringAmount(
const ledger::PublisherInfoList& publisher_list,
uint32_t record,
double total_reconcile_amount,
ledger::HasSufficientBalanceToReconcileCallback callback) {
total_reconcile_amount += GetAmountFromVerifiedRecurring(publisher_list);
callback(ledger_->GetBalance() >= total_reconcile_amount);
}

// static
double BatContribution::GetAmountFromVerifiedRecurring(
const ledger::PublisherInfoList& publisher_list) {
double total_recurring_amount(0.0);
for (const auto& publisher : publisher_list) {
if (publisher.id.empty()) {
continue;
}
if (publisher.verified) {
total_recurring_amount += publisher.weight;
}
}
return total_recurring_amount;
}

ledger::PublisherInfoList BatContribution::GetVerifiedListAuto(
const std::string& viewing_id,
const ledger::PublisherInfoList& list,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#define BRAVELEDGER_BAT_CONTRIBUTION_H_

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/gtest_prod_util.h"
#include "bat/ledger/ledger.h"
#include "bat/ledger/internal/bat_helper.h"

Expand Down Expand Up @@ -145,6 +147,8 @@ class BatContribution {
ledger::ACTIVITY_MONTH month,
int year,
uint32_t date);
void HasSufficientBalance(
ledger::HasSufficientBalanceToReconcileCallback callback);

private:
std::string GetAnonizeProof(const std::string& registrar_VK,
Expand Down Expand Up @@ -297,11 +301,34 @@ class BatContribution {

void DoRetry(const std::string& viewing_id);

void GetVerifiedAutoAmount(
const ledger::PublisherInfoList& publisher_list,
uint32_t record,
ledger::HasSufficientBalanceToReconcileCallback callback);

void GetVerifiedRecurringAmount(
const ledger::PublisherInfoList& publisher_list,
uint32_t record,
double budget,
ledger::HasSufficientBalanceToReconcileCallback callback);

static double GetAmountFromVerifiedAuto(
const ledger::PublisherInfoList& publisher_list,
double ac_amount);

static double GetAmountFromVerifiedRecurring(
const ledger::PublisherInfoList& publisher_list);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
uint32_t last_reconcile_timer_id_;
uint32_t last_prepare_vote_batch_timer_id_;
uint32_t last_vote_batch_timer_id_;
std::map<std::string, uint32_t> retry_timers_;

// For testing purposes
friend class BatContributionTest;
FRIEND_TEST_ALL_PREFIXES(BatContributionTest, GetAmountFromVerifiedAuto);
FRIEND_TEST_ALL_PREFIXES(BatContributionTest, GetAmountFromVerifiedRecurring);
};

} // namespace braveledger_bat_contribution
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* 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 <memory>
#include <vector>

#include "bat/ledger/internal/logging.h"
#include "bat/ledger/internal/bat_contribution.h"
#include "bat/ledger/ledger.h"
#include "testing/gtest/include/gtest/gtest.h"

// npm run test -- brave_unit_tests --filter=BatContributionTest.*

namespace braveledger_bat_contribution {

class BatContributionTest : public testing::Test {
protected:
void GetPublishersForAuto(
ledger::PublisherInfoList* publisher_info_list,
uint32_t iterations, /* total count of publishers */
uint32_t variation /* total count of verifieds */) {
// Can't have more verified publishers than total publishers
DCHECK(variation <= iterations);
for (uint32_t ix = 0; ix < iterations; ix++) {
std::unique_ptr<ledger::PublisherInfo> publisher_info(
std::make_unique<ledger::PublisherInfo>(
"example" + std::to_string(ix) + ".com"));
publisher_info->verified = ix < variation;
publisher_info->percent = (1.0 / iterations) * 100.0;
publisher_info_list->push_back(*publisher_info);
}
}

void GetPublishersForRecurring(
ledger::PublisherInfoList* publisher_info_list,
uint32_t iterations,
std::vector<uint32_t> amounts,
uint32_t variation) {
for (uint32_t ix = 0; ix < iterations; ix++) {
std::unique_ptr<ledger::PublisherInfo> publisher_info(
std::make_unique<ledger::PublisherInfo>(
"recurringexample" + std::to_string(ix) + ".com"));
publisher_info->weight = amounts[ix % amounts.size()];
publisher_info->verified = ix < variation;
publisher_info_list->push_back(*publisher_info);
}
}

bool WillTriggerNotification(
uint32_t auto_iterations,
uint32_t auto_variations,
double auto_amount_selected,
uint32_t recurring_iterations,
std::vector<uint32_t> recurring_amounts_selected,
uint32_t recurring_variation,
double wallet_balance) {
ledger::PublisherInfoList publisher_info_list_auto;
ledger::PublisherInfoList publisher_info_list_recurring;
GetPublishersForAuto(
&publisher_info_list_auto, auto_iterations, auto_variations);
double total_reconcile_amount =
BatContribution::GetAmountFromVerifiedAuto(
publisher_info_list_auto, auto_amount_selected);
GetPublishersForRecurring(
&publisher_info_list_recurring,
recurring_iterations,
recurring_amounts_selected,
recurring_variation);
total_reconcile_amount +=
BatContribution::GetAmountFromVerifiedRecurring(
publisher_info_list_recurring);
return wallet_balance < total_reconcile_amount;
}
};

TEST_F(BatContributionTest, GetAmountFromVerifiedAuto) {
ledger::PublisherInfoList publisher_info_list;

// 10 publishers total with 5 verified and budget of 30 BAT
GetPublishersForAuto(&publisher_info_list, 10, 5);
double amount =
BatContribution::GetAmountFromVerifiedAuto(publisher_info_list, 30);
EXPECT_EQ(amount, 15);

// 20 publishers total with 10 verified and budget of 30 BAT
publisher_info_list.clear();
GetPublishersForAuto(&publisher_info_list, 20, 10);
amount =
BatContribution::GetAmountFromVerifiedAuto(publisher_info_list, 30);
EXPECT_EQ(amount, 15);

// 50 publishers total with 5 verified and budget of 100 BAT
publisher_info_list.clear();
GetPublishersForAuto(&publisher_info_list, 50, 5);
amount =
BatContribution::GetAmountFromVerifiedAuto(publisher_info_list, 100);
EXPECT_EQ(amount, 10);

// 100 publishers total with 80 verified and budget of 1478 BAT
publisher_info_list.clear();
GetPublishersForAuto(&publisher_info_list, 100, 80);
amount =
BatContribution::GetAmountFromVerifiedAuto(publisher_info_list, 1478);
EXPECT_EQ(amount, 1182.40);

// 100 publishers total with 4 verified and budget of 100 BAT
publisher_info_list.clear();
GetPublishersForAuto(&publisher_info_list, 100, 4);
amount =
BatContribution::GetAmountFromVerifiedAuto(publisher_info_list, 100);
EXPECT_EQ(amount, 4);
}

TEST_F(BatContributionTest, GetAmountFromVerifiedRecurring) {
ledger::PublisherInfoList publisher_info_list;
GetPublishersForRecurring(&publisher_info_list, 5, {1, 5, 10}, 2);
double amount =
BatContribution::GetAmountFromVerifiedRecurring(publisher_info_list);
EXPECT_EQ(amount, 6);

publisher_info_list.clear();
GetPublishersForRecurring(&publisher_info_list, 7, {1, 5, 10}, 5);
amount =
BatContribution::GetAmountFromVerifiedRecurring(publisher_info_list);
EXPECT_EQ(amount, 22);

publisher_info_list.clear();
GetPublishersForRecurring(&publisher_info_list, 10, {5, 10, 20}, 7);
amount =
BatContribution::GetAmountFromVerifiedRecurring(publisher_info_list);
EXPECT_EQ(amount, 75);

publisher_info_list.clear();
GetPublishersForRecurring(&publisher_info_list, 10, {10, 20, 50}, 9);
amount =
BatContribution::GetAmountFromVerifiedRecurring(publisher_info_list);
EXPECT_EQ(amount, 240);

publisher_info_list.clear();
GetPublishersForRecurring(&publisher_info_list, 5, {1, 5, 10, 20, 50}, 5);
amount =
BatContribution::GetAmountFromVerifiedRecurring(publisher_info_list);
EXPECT_EQ(amount, 86);
}

TEST_F(BatContributionTest, WillTriggerNotification) {
EXPECT_TRUE(WillTriggerNotification(10, 5, 30, 5, {1, 5, 10}, 2, 20.9));
EXPECT_FALSE(WillTriggerNotification(10, 5, 30, 5, {1, 5, 10}, 2, 21));
EXPECT_TRUE(WillTriggerNotification(20, 10, 30, 7, {1, 5, 10}, 5, 36.9));
EXPECT_FALSE(WillTriggerNotification(20, 10, 30, 7, {1, 5, 10}, 5, 37));
EXPECT_TRUE(WillTriggerNotification(50, 5, 100, 10, {5, 10, 20}, 7, 84.9));
EXPECT_FALSE(WillTriggerNotification(50, 5, 100, 10, {5, 10, 20}, 7, 85));
EXPECT_TRUE(WillTriggerNotification(
100, 80, 1478, 10, {10, 20, 50}, 9, 1422.39));
EXPECT_FALSE(WillTriggerNotification(
100, 80, 1478, 10, {10, 20, 50}, 9, 1422.40));
EXPECT_TRUE(WillTriggerNotification(
100, 4, 100, 5, {1, 5, 10, 20, 50}, 5, 89.9));
EXPECT_FALSE(WillTriggerNotification(
100, 4, 100, 5, {1, 5, 10, 20, 50}, 5, 90));
}

} // namespace braveledger_bat_contribution
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,9 @@ double LedgerImpl::GetDefaultContributionAmount() {
return bat_state_->GetDefaultContributionAmount();
}

bool LedgerImpl::HasSufficientBalanceToReconcile() {
return GetBalance() >= GetContributionAmount();
void LedgerImpl::HasSufficientBalanceToReconcile(
ledger::HasSufficientBalanceToReconcileCallback callback) {
bat_contribution_->HasSufficientBalance(callback);
}

void LedgerImpl::SaveNormalizedPublisherList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class LedgerImpl : public ledger::Ledger,

double GetDefaultContributionAmount() override;

bool HasSufficientBalanceToReconcile() override;
void HasSufficientBalanceToReconcile(
ledger::HasSufficientBalanceToReconcileCallback callback) override;

void SaveNormalizedPublisherList(
const ledger::PublisherInfoList& normalized_list);
Expand Down

0 comments on commit 9145189

Please sign in to comment.