-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for BAP reporting endpoint
- Loading branch information
1 parent
5228a6c
commit 5326c21
Showing
18 changed files
with
409 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
vendor/bat-native-ledger/src/bat/ledger/internal/promotion/bap_reporter.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* Copyright (c) 2021 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 "bat/ledger/internal/promotion/bap_reporter.h" | ||
|
||
#include "base/time/time.h" | ||
#include "bat/ledger/global_constants.h" | ||
#include "bat/ledger/internal/common/time_util.h" | ||
#include "bat/ledger/internal/ledger_impl.h" | ||
#include "bat/ledger/option_keys.h" | ||
|
||
using std::placeholders::_1; | ||
using std::placeholders::_2; | ||
|
||
namespace ledger { | ||
namespace promotion { | ||
|
||
namespace { | ||
|
||
constexpr int64_t kRetryDelay = 24 * base::Time::kSecondsPerHour; | ||
constexpr int64_t kRetryAfterFailureDelay = 10 * base::Time::kSecondsPerMinute; | ||
constexpr int64_t kMaxRetryAfterFailureDelay = 4 * base::Time::kSecondsPerHour; | ||
|
||
} // namespace | ||
|
||
BAPReporter::BAPReporter(LedgerImpl* ledger) | ||
: ledger_(ledger), endpoint_(ledger) { | ||
DCHECK(ledger); | ||
} | ||
|
||
BAPReporter::~BAPReporter() = default; | ||
|
||
void BAPReporter::ReportBAPAmount() { | ||
if (running_) | ||
return; | ||
|
||
timer_.Stop(); | ||
|
||
bool should_report = ledger_->ledger_client()->GetBooleanOption( | ||
option::kShouldReportBAPAmount); | ||
|
||
// Only run this reporter if the user is in a BAP region and we haven't | ||
// successfully reported yet. | ||
if (!should_report || ledger_->state()->GetBAPReported()) | ||
return; | ||
|
||
running_ = true; | ||
|
||
// First, get the user's unspent BAP tokens. | ||
ledger_->database()->GetSpendableUnblindedTokensByBatchTypes( | ||
{type::CredsBatchType::PROMOTION}, | ||
std::bind(&BAPReporter::OnUnblindedTokens, this, _1)); | ||
} | ||
|
||
void BAPReporter::OnUnblindedTokens( | ||
std::vector<type::UnblindedTokenPtr> tokens) { | ||
double amount = 0; | ||
for (const auto& token : tokens) | ||
amount += token->value; | ||
|
||
// If the user has no BAP, then schedule a retry and exit. | ||
if (amount <= 0) { | ||
ScheduleRetryAfterZeroBalance(); | ||
return; | ||
} | ||
|
||
// Send the amount to the server. | ||
endpoint_.Request(amount, | ||
std::bind(&BAPReporter::OnEndpointResponse, this, _1)); | ||
} | ||
|
||
void BAPReporter::OnEndpointResponse(bool success) { | ||
// If the server reported an error, assume a temporary problem and try again | ||
// later. | ||
if (!success) { | ||
ScheduleRetryAfterFailure(); | ||
return; | ||
} | ||
|
||
BLOG(1, "BAP successsfully reported"); | ||
|
||
// Set a flag to indicate that we don't need to report again. | ||
ledger_->state()->SetBAPReported(true); | ||
running_ = false; | ||
retry_count_ = 0; | ||
} | ||
|
||
void BAPReporter::ScheduleRetryAfterZeroBalance() { | ||
running_ = false; | ||
|
||
base::TimeDelta delay = base::TimeDelta::FromSeconds(kRetryDelay); | ||
|
||
BLOG(1, "User has zero balance - rescheduling BAP reporting in " << delay); | ||
timer_.Start( | ||
FROM_HERE, delay, | ||
base::BindOnce(&BAPReporter::ReportBAPAmount, base::Unretained(this))); | ||
} | ||
|
||
void BAPReporter::ScheduleRetryAfterFailure() { | ||
running_ = false; | ||
|
||
base::TimeDelta delay = util::GetRandomizedDelayWithBackoff( | ||
base::TimeDelta::FromSeconds(kRetryAfterFailureDelay), | ||
base::TimeDelta::FromSeconds(kMaxRetryAfterFailureDelay), retry_count_++); | ||
|
||
BLOG(1, "BAP reporting failed - rescheduling in " << delay); | ||
timer_.Start( | ||
FROM_HERE, delay, | ||
base::BindOnce(&BAPReporter::ReportBAPAmount, base::Unretained(this))); | ||
} | ||
|
||
} // namespace promotion | ||
} // namespace ledger |
49 changes: 49 additions & 0 deletions
49
vendor/bat-native-ledger/src/bat/ledger/internal/promotion/bap_reporter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Copyright (c) 2021 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/. */ | ||
|
||
#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_PROMOTION_BAP_REPORTER_H_ | ||
#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_PROMOTION_BAP_REPORTER_H_ | ||
|
||
#include <functional> | ||
#include <vector> | ||
|
||
#include "base/timer/timer.h" | ||
#include "bat/ledger/internal/promotion/bap_reporter_endpoint.h" | ||
#include "bat/ledger/ledger.h" | ||
|
||
namespace ledger { | ||
|
||
class LedgerImpl; | ||
|
||
namespace promotion { | ||
|
||
class BAPReporter { | ||
public: | ||
explicit BAPReporter(LedgerImpl* ledger); | ||
|
||
BAPReporter(const BAPReporter&) = delete; | ||
BAPReporter& operator=(const BAPReporter&) = delete; | ||
|
||
~BAPReporter(); | ||
|
||
void ReportBAPAmount(); | ||
|
||
private: | ||
void OnUnblindedTokens(std::vector<type::UnblindedTokenPtr> tokens); | ||
void OnEndpointResponse(bool success); | ||
void ScheduleRetryAfterZeroBalance(); | ||
void ScheduleRetryAfterFailure(); | ||
|
||
LedgerImpl* ledger_; | ||
bool running_ = false; | ||
int retry_count_ = 0; | ||
base::OneShotTimer timer_; | ||
BAPReporterEndpoint endpoint_; | ||
}; | ||
|
||
} // namespace promotion | ||
} // namespace ledger | ||
|
||
#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_PROMOTION_BAP_REPORTER_H_ |
92 changes: 92 additions & 0 deletions
92
vendor/bat-native-ledger/src/bat/ledger/internal/promotion/bap_reporter_endpoint.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* Copyright (c) 2021 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 "bat/ledger/internal/promotion/bap_reporter_endpoint.h" | ||
|
||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/json/json_writer.h" | ||
#include "bat/ledger/internal/common/request_util.h" | ||
#include "bat/ledger/internal/endpoint/promotion/promotions_util.h" | ||
#include "bat/ledger/internal/ledger_impl.h" | ||
#include "net/http/http_status_code.h" | ||
|
||
using std::placeholders::_1; | ||
|
||
namespace ledger { | ||
namespace promotion { | ||
|
||
namespace { | ||
|
||
const char kEndpointPath[] = "/v1/promotions/report-bap"; | ||
|
||
} // namespace | ||
|
||
BAPReporterEndpoint::BAPReporterEndpoint(LedgerImpl* ledger) : ledger_(ledger) { | ||
DCHECK(ledger_); | ||
} | ||
|
||
BAPReporterEndpoint::~BAPReporterEndpoint() = default; | ||
|
||
void BAPReporterEndpoint::Request(double amount, Callback callback) { | ||
const auto wallet = ledger_->wallet()->GetWallet(); | ||
if (!wallet) { | ||
BLOG(0, "Wallet is null"); | ||
callback(false); | ||
return; | ||
} | ||
|
||
base::Value body(base::Value::Type::DICTIONARY); | ||
body.SetDoubleKey("amount", amount); | ||
std::string payload; | ||
base::JSONWriter::Write(body, &payload); | ||
|
||
const std::string sign_url = std::string("post ") + kEndpointPath; | ||
auto headers = util::BuildSignHeaders(sign_url, payload, wallet->payment_id, | ||
wallet->recovery_seed); | ||
|
||
auto request = type::UrlRequest::New(); | ||
request->url = endpoint::promotion::GetServerUrl(kEndpointPath); | ||
request->headers = std::move(headers); | ||
request->content = std::move(payload); | ||
request->content_type = "application/json; charset=utf-8"; | ||
request->method = type::UrlMethod::POST; | ||
ledger_->LoadURL( | ||
std::move(request), | ||
std::bind(&BAPReporterEndpoint::OnFetchCompleted, this, callback, _1)); | ||
} | ||
|
||
void BAPReporterEndpoint::OnFetchCompleted(Callback callback, | ||
const type::UrlResponse& response) { | ||
ledger::LogUrlResponse(__func__, response); | ||
|
||
bool success = false; | ||
switch (response.status_code) { | ||
case net::HTTP_BAD_REQUEST: { | ||
BLOG(0, "Invalid request"); | ||
break; | ||
} | ||
case net::HTTP_INTERNAL_SERVER_ERROR: { | ||
BLOG(0, "Internal server error"); | ||
break; | ||
} | ||
case net::HTTP_OK: | ||
case net::HTTP_CONFLICT: { | ||
success = true; | ||
break; | ||
} | ||
default: { | ||
BLOG(0, "Unexpected reponse code " << response.status_code); | ||
break; | ||
} | ||
} | ||
|
||
callback(success); | ||
} | ||
|
||
} // namespace promotion | ||
} // namespace ledger |
Oops, something went wrong.