Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds random delay between transactions (uplift to 0.69.x) #3543

Merged
merged 2 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 136 additions & 11 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3307,22 +3307,27 @@ void RewardsServiceImpl::FetchBalance(FetchBalanceCallback callback) {

void RewardsServiceImpl::SaveExternalWallet(const std::string& wallet_type,
ledger::ExternalWalletPtr wallet) {
auto* perfs =
auto* wallets =
profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets);

base::Value new_perfs(perfs->Clone());
base::Value new_wallets(wallets->Clone());

base::Value dict(base::Value::Type::DICTIONARY);
dict.SetStringKey("token", wallet->token);
dict.SetStringKey("address", wallet->address);
dict.SetIntKey("status", static_cast<int>(wallet->status));
dict.SetStringKey("one_time_string", wallet->one_time_string);
dict.SetStringKey("user_name", wallet->user_name);
dict.SetBoolKey("transferred", wallet->transferred);
auto* old_wallet = new_wallets.FindDictKey(wallet_type);
base::Value new_wallet(base::Value::Type::DICTIONARY);
if (old_wallet) {
new_wallet = old_wallet->Clone();
}

new_wallet.SetStringKey("token", wallet->token);
new_wallet.SetStringKey("address", wallet->address);
new_wallet.SetIntKey("status", static_cast<int>(wallet->status));
new_wallet.SetStringKey("one_time_string", wallet->one_time_string);
new_wallet.SetStringKey("user_name", wallet->user_name);
new_wallet.SetBoolKey("transferred", wallet->transferred);

new_perfs.SetKey(wallet_type, std::move(dict));
new_wallets.SetKey(wallet_type, std::move(new_wallet));

profile_->GetPrefs()->Set(prefs::kRewardsExternalWallets, new_perfs);
profile_->GetPrefs()->Set(prefs::kRewardsExternalWallets, new_wallets);
}

void RewardsServiceImpl::GetExternalWallets(
Expand Down Expand Up @@ -3582,6 +3587,126 @@ void RewardsServiceImpl::OnGetServerPublisherInfo(
callback(std::move(info));
}

void RewardsServiceImpl::SetTransferFee(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee) {
if (!transfer_fee) {
return;
}

base::Value fee(base::Value::Type::DICTIONARY);
fee.SetStringKey("id", transfer_fee->id);
fee.SetDoubleKey("amount", transfer_fee->amount);
fee.SetIntKey("execution_timestamp", transfer_fee->execution_timestamp);
fee.SetIntKey("execution_id", transfer_fee->execution_id);

auto* external_wallets =
profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets);

base::Value new_external_wallets(external_wallets->Clone());

auto* wallet = new_external_wallets.FindDictKey(wallet_type);
base::Value new_wallet(base::Value::Type::DICTIONARY);
if (wallet) {
new_wallet = wallet->Clone();
}

auto* fees = wallet->FindListKey("transfer_fees");
base::Value new_fees(base::Value::Type::DICTIONARY);
if (fees) {
new_fees = fees->Clone();
}
new_fees.SetKey(transfer_fee->id, std::move(fee));

new_wallet.SetKey("transfer_fees", std::move(new_fees));
new_external_wallets.SetKey(wallet_type, std::move(new_wallet));

profile_->GetPrefs()->Set(
prefs::kRewardsExternalWallets,
new_external_wallets);
}

ledger::TransferFeeList RewardsServiceImpl::GetTransferFees(
const std::string& wallet_type) {
ledger::TransferFeeList fees;

auto* external_wallets =
profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets);

auto* wallet_key = external_wallets->FindKey(wallet_type);

const base::DictionaryValue* wallet;
if (!wallet_key || !wallet_key->GetAsDictionary(&wallet)) {
return fees;
}

auto* fees_key = wallet->FindKey("transfer_fees");
const base::DictionaryValue* fee_dict;
if (!fees_key || !fees_key->GetAsDictionary(&fee_dict)) {
return fees;
}

for (auto& item : *fee_dict) {
const base::DictionaryValue* fee_dict;
if (!item.second || !item.second->GetAsDictionary(&fee_dict)) {
continue;
}

auto fee = ledger::TransferFee::New();

auto* id = fee_dict->FindKey("id");
if (!id || !id->is_string()) {
continue;
}
fee->id = id->GetString();

auto* amount = fee_dict->FindKey("amount");
if (!amount || !amount->is_double()) {
continue;
}
fee->amount = amount->GetDouble();

auto* timestamp = fee_dict->FindKey("execution_timestamp");
if (!timestamp || !timestamp->is_int()) {
continue;
}
fee->execution_timestamp = timestamp->GetInt();

auto* execution_id = fee_dict->FindKey("execution_id");
if (!execution_id || !execution_id->is_int()) {
continue;
}
fee->execution_id = execution_id->GetInt();

fees.insert(std::make_pair(fee->id, std::move(fee)));
}

return fees;
}

void RewardsServiceImpl::RemoveTransferFee(
const std::string& wallet_type,
const std::string& id) {
auto* external_wallets =
profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets);

base::Value new_external_wallets(external_wallets->Clone());

const auto path = base::StringPrintf(
"%s.transfer_fees.%s",
wallet_type.c_str(),
id.c_str());

bool success = new_external_wallets.RemovePath(path);
if (!success) {
return;
}

profile_->GetPrefs()->Set(
prefs::kRewardsExternalWallets,
new_external_wallets);
}

bool RewardsServiceImpl::OnlyAnonWallet() {
const int32_t current_country =
country_codes::GetCountryIDFromPrefs(profile_->GetPrefs());
Expand Down
11 changes: 11 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ class RewardsServiceImpl : public RewardsService,
const std::string& publisher_key,
ledger::GetServerPublisherInfoCallback callback) override;

void SetTransferFee(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee) override;

ledger::TransferFeeList GetTransferFees(
const std::string& wallet_type) override;

void RemoveTransferFee(
const std::string& wallet_type,
const std::string& id) override;

// end ledger::LedgerClient

// Mojo Proxy methods
Expand Down
19 changes: 19 additions & 0 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,23 @@ void BatLedgerClientMojoProxy::GetServerPublisherInfo(
base::BindOnce(&OnGetServerPublisherInfo, std::move(callback)));
}

void BatLedgerClientMojoProxy::SetTransferFee(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee) {
bat_ledger_client_->SetTransferFee(wallet_type, std::move(transfer_fee));
}

ledger::TransferFeeList BatLedgerClientMojoProxy::GetTransferFees(
const std::string& wallet_type) {
base::flat_map<std::string, ledger::TransferFeePtr> list;
bat_ledger_client_->GetTransferFees(wallet_type, &list);
return mojo::FlatMapToMap(std::move(list));
}

void BatLedgerClientMojoProxy::RemoveTransferFee(
const std::string& wallet_type,
const std::string& id) {
bat_ledger_client_->RemoveTransferFee(wallet_type, id);
}

} // namespace bat_ledger
12 changes: 12 additions & 0 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient,
const std::string& publisher_key,
ledger::GetServerPublisherInfoCallback callback) override;


ledger::TransferFeeList GetTransferFees(
const std::string& wallet_type) override;

void SetTransferFee(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee) override;

void RemoveTransferFee(
const std::string& wallet_type,
const std::string& id) override;

private:
bool Connected() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,4 +945,23 @@ void LedgerClientMojoProxy::GetServerPublisherInfo(
_1));
}

void LedgerClientMojoProxy::GetTransferFees(
const std::string& wallet_type,
GetTransferFeesCallback callback) {
auto list = ledger_client_->GetTransferFees(wallet_type);
std::move(callback).Run(mojo::MapToFlatMap(std::move(list)));
}

void LedgerClientMojoProxy::SetTransferFee(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee) {
ledger_client_->SetTransferFee(wallet_type, std::move(transfer_fee));
}

void LedgerClientMojoProxy::RemoveTransferFee(
const std::string& wallet_type,
const std::string& id) {
ledger_client_->RemoveTransferFee(wallet_type, id);
}

} // namespace bat_ledger
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
const std::string& publisher_key,
GetServerPublisherInfoCallback callback) override;

void SetTransferFee(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee) override;

void GetTransferFees(
const std::string& wallet_type,
GetTransferFeesCallback callback) override;

void RemoveTransferFee(
const std::string& wallet_type,
const std::string& id) override;

private:
// workaround to pass base::OnceCallback into std::bind
// also serves as a wrapper for passing ledger::LedgerCallbackHandler*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,9 @@ interface BatLedgerClient {
ClearAndInsertServerPublisherList(array<ledger.mojom.ServerPublisherInfo> list) => (ledger.mojom.Result result);

GetServerPublisherInfo(string publisher_key) => (ledger.mojom.ServerPublisherInfo? info);

[Sync]
GetTransferFees(string wallet_type) => (map<string, ledger.mojom.TransferFee> list);
SetTransferFee(string wallet_type, ledger.mojom.TransferFee transfer_fee);
RemoveTransferFee(string wallet_type, string id);
};
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,17 @@ class MockConfirmationsClient : public ConfirmationsClient {
MOCK_METHOD2(GetServerPublisherInfo, void(
const std::string& publisher_key,
ledger::GetServerPublisherInfoCallback callback));

MOCK_METHOD2(SetTransferFee, void(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee));

MOCK_METHOD1(GetTransferFees, ledger::TransferFeeList(
const std::string& wallet_type));

MOCK_METHOD2(RemoveTransferFee, void(
const std::string& wallet_type,
const std::string& id));
};

} // namespace confirmations
Expand Down
11 changes: 11 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,17 @@ class LEDGER_EXPORT LedgerClient {
virtual void GetServerPublisherInfo(
const std::string& publisher_key,
ledger::GetServerPublisherInfoCallback callback) = 0;

virtual void SetTransferFee(
const std::string& wallet_type,
ledger::TransferFeePtr transfer_fee) = 0;

virtual ledger::TransferFeeList GetTransferFees(
const std::string& wallet_type) = 0;

virtual void RemoveTransferFee(
const std::string& wallet_type,
const std::string& id) = 0;
};

} // namespace ledger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,10 @@ struct ServerPublisherInfo {
string address;
PublisherBanner? banner;
};

struct TransferFee {
string id;
double amount;
uint64 execution_timestamp;
uint32 execution_id;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef BAT_LEDGER_WALLET_PROPERTIES_HANDLER_
#define BAT_LEDGER_WALLET_PROPERTIES_HANDLER_

#include <map>
#include <string>

#include "bat/ledger/public/interfaces/ledger.mojom.h"

Expand All @@ -17,6 +19,10 @@ using WalletPropertiesPtr = ledger::mojom::WalletPropertiesPtr;
using ExternalWallet = ledger::mojom::ExternalWallet;
using ExternalWalletPtr = ledger::mojom::ExternalWalletPtr;

using TransferFee = ledger::mojom::TransferFee;
using TransferFeePtr = ledger::mojom::TransferFeePtr;
using TransferFeeList = std::map<std::string, TransferFeePtr>;

const char kWalletAnonymous[] = "anonymous";
const char kWalletUphold[] = "uphold";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ Contribution::Contribution(bat_ledger::LedgerImpl* ledger) :
Contribution::~Contribution() {
}


void Contribution::OnStartUp() {
// Check if we have some more pending ballots to go out
phase_two_->PrepareBallots();
void Contribution::Initialize() {
phase_two_->Initialize();
uphold_->Initialize();

// Resume in progress contributions
braveledger_bat_helper::CurrentReconciles currentReconciles =
Expand Down Expand Up @@ -395,6 +394,7 @@ void Contribution::InitReconcile(
void Contribution::OnTimer(uint32_t timer_id) {
phase_two_->OnTimer(timer_id);
unverified_->OnTimer(timer_id);
uphold_->OnTimer(timer_id);

if (timer_id == last_reconcile_timer_id_) {
last_reconcile_timer_id_ = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Contribution {

~Contribution();

void OnStartUp();
void Initialize();

// Initial point for contribution
// In this step we get balance from the server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ PhaseTwo::PhaseTwo(bat_ledger::LedgerImpl* ledger,
PhaseTwo::~PhaseTwo() {
}

void PhaseTwo::Initialize() {
// Check if we have some more pending ballots to go out
PrepareBallots();
}

void PhaseTwo::Start(const std::string& viewing_id) {
unsigned int ballots_count = GetBallotsCount(viewing_id);
const auto reconcile = ledger_->GetReconcileById(viewing_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class PhaseTwo {

~PhaseTwo();

void Initialize();

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

void PrepareBallots();
Expand Down
Loading