Skip to content

Commit

Permalink
Adds random delay between transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Sep 19, 2019
1 parent 3c9db54 commit eccefc8
Show file tree
Hide file tree
Showing 21 changed files with 424 additions and 28 deletions.
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 @@ -3314,22 +3314,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 @@ -3589,6 +3594,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 @@ -633,6 +633,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 @@ -899,4 +899,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 @@ -183,6 +183,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 @@ -928,4 +928,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 @@ -188,6 +188,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 @@ -272,4 +272,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 @@ -279,6 +279,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 @@ -237,3 +237,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 @@ -395,6 +395,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
17 changes: 17 additions & 0 deletions vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1546,4 +1546,21 @@ void LedgerImpl::ClearState(const std::string& name) {
ledger_client_->ClearState(name);
}

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

ledger::TransferFeeList LedgerImpl::GetTransferFees(
const std::string& wallet_type) const {
return ledger_client_->GetTransferFees(wallet_type);
}

void LedgerImpl::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 @@ -542,6 +542,15 @@ class LedgerImpl : public ledger::Ledger,

void ClearState(const std::string& name);

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

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

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

private:
void OnLoad(ledger::VisitDataPtr visit_data,
Expand Down
Loading

0 comments on commit eccefc8

Please sign in to comment.