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

Wait for initial rewards API parameter fetch from server (uplift to 1.12.x) #6048

Merged
merged 1 commit into from
Jul 14, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ window.cr.define('brave_rewards', function () {

function rewardsParameters (properties: Rewards.RewardsParameters) {
getActions().onRewardsParameters(properties)
// Get the current AC amount after rewards parameters have been
// updated, as the default AC amount may have been changed.
getActions().getContributionAmount()
}

function promotions (properties: Rewards.PromotionResponse) {
Expand Down
4 changes: 2 additions & 2 deletions vendor/bat-native-ledger/src/bat/ledger/internal/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void API::OnTimer(const uint32_t timer_id) {
parameters_->OnTimer(timer_id);
}

void API::FetchParameters() {
parameters_->Fetch();
void API::FetchParameters(ledger::GetRewardsParametersCallback callback) {
parameters_->Fetch(callback);
}

} // namespace braveledger_api
2 changes: 1 addition & 1 deletion vendor/bat-native-ledger/src/bat/ledger/internal/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class API {

void OnTimer(const uint32_t timer_id);

void FetchParameters();
void FetchParameters(ledger::GetRewardsParametersCallback callback);

private:
bat_ledger::LedgerImpl* ledger_; // NOT OWNED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <string>
#include <utility>

#include "base/json/json_reader.h"
#include "base/time/time.h"
Expand Down Expand Up @@ -96,13 +97,24 @@ void APIParameters::Initialize() {

void APIParameters::OnTimer(const uint32_t timer_id) {
if (timer_id == refresh_timer_id_) {
refresh_timer_id_ = 0;
Fetch();
return;
}
}

void APIParameters::Fetch() {
Fetch([](ledger::RewardsParametersPtr) {});
}

void APIParameters::Fetch(ledger::GetRewardsParametersCallback callback) {
bool first_request = callbacks_.empty();
callbacks_.push_back(callback);
if (!first_request) {
BLOG(1, "API parameters fetch in progress");
return;
}

refresh_timer_id_ = 0;

auto url_callback = std::bind(&APIParameters::OnFetch,
this,
_1);
Expand All @@ -115,6 +127,7 @@ void APIParameters::OnFetch(const ledger::UrlResponse& response) {
BLOG(6, ledger::UrlResponseToString(__func__, response));

if (response.status_code != net::HTTP_OK) {
RunCallbacks();
SetRefreshTimer(brave_base::random::Geometric(90));
return;
}
Expand All @@ -123,15 +136,30 @@ void APIParameters::OnFetch(const ledger::UrlResponse& response) {
auto result = ParseFetchResponse(response.body, &parameters);
if (result != ledger::Result::LEDGER_OK) {
BLOG(1, "Couldn't parse response");
RunCallbacks();
SetRefreshTimer(
brave_base::random::Geometric(10 * base::Time::kSecondsPerMinute));
return;
}

braveledger_state::SetRewardsParameters(ledger_, parameters);
RunCallbacks();
SetRefreshTimer();
}

void APIParameters::RunCallbacks() {
// Execute callbacks with the current parameters stored in state.
// If the last fetch failed, callbacks will be run with the last
// successfully fetched parameters or a default set of parameters.
auto parameters = braveledger_state::GetRewardsParameters(ledger_);
DCHECK(parameters);

auto callbacks = std::move(callbacks_);
for (auto& callback : callbacks) {
callback(parameters->Clone());
}
}

void APIParameters::SetRefreshTimer(const int delay) {
if (refresh_timer_id_ != 0) {
BLOG(1, "Params timer in progress");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef BRAVELEDGER_API_API_PARAMETERS_H_
#define BRAVELEDGER_API_API_PARAMETERS_H_

#include <vector>

#include "bat/ledger/ledger.h"

namespace bat_ledger {
Expand All @@ -25,13 +27,18 @@ class APIParameters {

void Fetch();

void Fetch(ledger::GetRewardsParametersCallback callback);

private:
void OnFetch(const ledger::UrlResponse& response);

void RunCallbacks();

void SetRefreshTimer(const int delay = 0);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
uint32_t refresh_timer_id_;
std::vector<ledger::GetRewardsParametersCallback> callbacks_;
};

} // namespace braveledger_api
Expand Down
14 changes: 9 additions & 5 deletions vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,15 @@ void LedgerImpl::ContributionCompleted(

void LedgerImpl::GetRewardsParameters(
ledger::GetRewardsParametersCallback callback) {
callback(braveledger_state::GetRewardsParameters(this));
auto params = braveledger_state::GetRewardsParameters(this);
if (params->rate == 0.0) {
// A rate of zero indicates that the rewards parameters have
// not yet been successfully initialized from the server.
BLOG(1, "Rewards parameters not set - fetching from server");
bat_api_->FetchParameters(callback);
} else {
callback(std::move(params));
}
}

void LedgerImpl::ClaimPromotion(
Expand Down Expand Up @@ -1716,10 +1724,6 @@ void LedgerImpl::SaveProcessedPublisherList(
bat_database_->SaveProcessedPublisherList(list, callback);
}

void LedgerImpl::FetchParameters() {
bat_api_->FetchParameters();
}

void LedgerImpl::Shutdown(ledger::ResultCallback callback) {
shutting_down_ = true;
ledger_client_->ClearAllNotifications();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,6 @@ class LedgerImpl : public ledger::Ledger {
const std::vector<std::string>& list,
ledger::ResultCallback callback);

void FetchParameters();

void Shutdown(ledger::ResultCallback callback) override;

void GetCredsBatchesByTriggers(
Expand Down