Skip to content

Commit

Permalink
Promotion expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
masparrow committed Nov 27, 2019
1 parent 7fe7fe2 commit 6a35358
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

namespace brave_rewards {

namespace {

const char* table_name_ = "unblinded_tokens";
const int minimum_version_ = 10;

} // namespace

DatabaseUnblindedToken::DatabaseUnblindedToken(
int current_db_version) :
DatabaseTable(current_db_version) {
Expand Down Expand Up @@ -154,4 +161,22 @@ bool DatabaseUnblindedToken::DeleteRecord(
return statement.Run();
}

// static
bool DatabaseUnblindedToken::DeleteRecordsForPromotion(
sql::Database* db,
const std::string& promotion_id) {
if (promotion_id.empty()) {
return false;
}

const std::string query = base::StringPrintf(
"DELETE FROM %s WHERE promotion_id = '%s'",
table_name_,
promotion_id.c_str());

sql::Statement statement(db->GetUniqueStatement(query.c_str()));

return statement.Run();
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class DatabaseUnblindedToken: public DatabaseTable {

bool DeleteRecord(sql::Database* db, const std::vector<std::string>& id_list);

private:
const char* table_name_ = "unblinded_tokens";
const int minimum_version_ = 10;
static bool DeleteRecordsForPromotion(
sql::Database* db,
const std::string& promotion_id);
};

} // namespace brave_rewards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,22 @@ bool PublisherInfoDatabase::DeleteUnblindedToken(
return unblinded_token_->DeleteRecord(&GetDB(), id_list);
}

bool PublisherInfoDatabase::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

bool initialized = Init();
DCHECK(initialized);

if (!initialized) {
return false;
}

return DatabaseUnblindedToken::DeleteRecordsForPromotion(
&GetDB(),
promotion_id);
}

// Other -------------------------------------------------------------------

void PublisherInfoDatabase::RecordP3AStats(bool auto_contributions_on) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class PublisherInfoDatabase {

bool DeleteUnblindedToken(const std::vector<std::string>& id_list);

bool DeleteUnblindedTokensForPromotion(
const std::string& promotion_id);

void RecordP3AStats(bool auto_contributions_on);

// Returns the current version of the publisher info database
Expand Down
26 changes: 26 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4336,6 +4336,32 @@ void RewardsServiceImpl::DeleteUnblindedToken(
callback));
}

ledger::Result DeleteUnblindedTokensForPromotionOnFileTaskRunner(
PublisherInfoDatabase* backend,
const std::string& promotion_id) {
if (!backend) {
return ledger::Result::LEDGER_ERROR;
}

const bool result = backend->DeleteUnblindedTokensForPromotion(promotion_id);

return result ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR;
}

void RewardsServiceImpl::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) {
base::PostTaskAndReplyWithResult(
file_task_runner_.get(),
FROM_HERE,
base::BindOnce(&DeleteUnblindedTokensForPromotionOnFileTaskRunner,
publisher_info_backend_.get(),
promotion_id),
base::BindOnce(&RewardsServiceImpl::OnResult,
AsWeakPtr(),
callback));
}

ledger::ClientInfoPtr GetDesktopClientInfo() {
auto info = ledger::ClientInfo::New();
info->platform = ledger::Platform::DESKTOP;
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@ class RewardsServiceImpl : public RewardsService,
const std::vector<std::string>& id_list,
ledger::ResultCallback callback) override;

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) override;

ledger::ClientInfoPtr GetClientInfo() override;

void UnblindedTokensReady() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,14 @@ void BatLedgerClientMojoProxy::DeleteUnblindedToken(
base::BindOnce(&OnResultCallback, std::move(callback)));
}

void BatLedgerClientMojoProxy::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) {
bat_ledger_client_->DeleteUnblindedTokensForPromotion(
promotion_id,
base::BindOnce(&OnResultCallback, std::move(callback)));
}

ledger::ClientInfoPtr BatLedgerClientMojoProxy::GetClientInfo() {
auto info = ledger::ClientInfo::New();
bat_ledger_client_->GetClientInfo(&info);
Expand Down
4 changes: 4 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 @@ -229,6 +229,10 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient,
const std::vector<std::string>& id_list,
ledger::ResultCallback callback) override;

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) override;

ledger::ClientInfoPtr GetClientInfo() override;

void UnblindedTokensReady() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,30 @@ void LedgerClientMojoProxy::DeleteUnblindedToken(
_1));
}

// static
void LedgerClientMojoProxy::OnDeleteUnblindedTokensForPromotion(
CallbackHolder<DeleteUnblindedTokensForPromotionCallback>* holder,
const ledger::Result result) {
DCHECK(holder);
if (holder->is_valid()) {
std::move(holder->get()).Run(result);
}
delete holder;
}

void LedgerClientMojoProxy::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
DeleteUnblindedTokensForPromotionCallback callback) {
auto* holder = new CallbackHolder<DeleteUnblindedTokensForPromotionCallback>(
AsWeakPtr(),
std::move(callback));
ledger_client_->DeleteUnblindedTokensForPromotion(
promotion_id,
std::bind(LedgerClientMojoProxy::OnDeleteUnblindedTokensForPromotion,
holder,
_1));
}

void LedgerClientMojoProxy::GetClientInfo(
GetClientInfoCallback callback) {
auto info = ledger_client_->GetClientInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
const std::vector<std::string>& id_list,
DeleteUnblindedTokenCallback callback) override;

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
DeleteUnblindedTokensForPromotionCallback callback) override;

void GetClientInfo(
GetClientInfoCallback callback) override;

Expand Down Expand Up @@ -455,8 +459,12 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
ledger::UnblindedTokenList list);

static void OnDeleteUnblindedToken(
CallbackHolder<DeleteUnblindedTokenCallback>* holder,
const ledger::Result result);
CallbackHolder<DeleteUnblindedTokenCallback>* holder,
const ledger::Result result);

static void OnDeleteUnblindedTokensForPromotion(
CallbackHolder<DeleteUnblindedTokensForPromotionCallback>* holder,
const ledger::Result result);

ledger::LedgerClient* ledger_client_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ interface BatLedgerClient {
InsertOrUpdateUnblindedToken(ledger.mojom.UnblindedToken info) => (ledger.mojom.Result result);
GetAllUnblindedTokens() => (array<ledger.mojom.UnblindedToken> info);
DeleteUnblindedToken(array<string> id_list) => (ledger.mojom.Result result);
DeleteUnblindedTokensForPromotion(string promotion_id) => (ledger.mojom.Result result);
[Sync]
GetClientInfo() => (ledger.mojom.ClientInfo info);
UnblindedTokensReady();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ class MockConfirmationsClient : public ConfirmationsClient {
const std::vector<std::string>& id_list,
ledger::ResultCallback callback));

MOCK_METHOD2(DeleteUnblindedTokensForPromotion, void(
const std::string& promotion_id,
ledger::ResultCallback callback));

MOCK_METHOD0(GetClientInfo, ledger::ClientInfoPtr());

MOCK_METHOD0(UnblindedTokensReady, void());
Expand Down
8 changes: 6 additions & 2 deletions vendor/bat-native-ledger/include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,12 @@ class LEDGER_EXPORT LedgerClient {
ledger::GetAllUnblindedTokensCallback callback) = 0;

virtual void DeleteUnblindedToken(
const std::vector<std::string>& id_list,
ledger::ResultCallback callback) = 0;
const std::vector<std::string>& id_list,
ledger::ResultCallback callback) = 0;

virtual void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) = 0;

virtual ledger::ClientInfoPtr GetClientInfo() = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,12 @@ void LedgerImpl::DeleteUnblindedToken(
ledger_client_->DeleteUnblindedToken(id_list, callback);
}

void LedgerImpl::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) {
ledger_client_->DeleteUnblindedTokensForPromotion(promotion_id, callback);
}

ledger::ClientInfoPtr LedgerImpl::GetClientInfo() {
return ledger_client_->GetClientInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ class LedgerImpl : public ledger::Ledger,
const std::vector<std::string>& id_list,
ledger::ResultCallback callback);

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback);

ledger::ClientInfoPtr GetClientInfo();

void UnblindedTokensReady();
Expand Down
29 changes: 29 additions & 0 deletions vendor/bat-native-ledger/src/bat/ledger/internal/promotion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ using challenge_bypass_ristretto::UnblindedToken;

namespace braveledger_promotion {

namespace {

void HandleExpiredPromotions(
bat_ledger::LedgerImpl* ledger,
ledger::PromotionMap* promotions) {
DCHECK(promotions);
if (!promotions) {
return;
}

const uint64_t current_time =
static_cast<uint64_t>(base::Time::Now().ToDoubleT());

for (auto& item : *promotions) {
if (item.second->expires_at <= current_time) {
item.second->status = ledger::PromotionStatus::OVER;

ledger->DeleteUnblindedTokensForPromotion(item.second->id,
[](const ledger::Result _){});
}
}
}

} // namespace

Promotion::Promotion(bat_ledger::LedgerImpl* ledger) :
attestation_(std::make_unique<braveledger_attestation::AttestationImpl>
(ledger)),
Expand Down Expand Up @@ -367,6 +392,8 @@ void Promotion::OnGetAllPromotions(
ledger::PromotionMap promotions,
const std::string& response,
ledger::FetchPromotionCallback callback) {
HandleExpiredPromotions(ledger_, &promotions);

ledger::PromotionList list;
bool success = ParseFetchResponse(response, &list);

Expand Down Expand Up @@ -523,6 +550,8 @@ void Promotion::OnTimer(const uint32_t timer_id) {
}

void Promotion::Retry(ledger::PromotionMap promotions) {
HandleExpiredPromotions(ledger_, &promotions);

for (auto & promotion : promotions) {
if (promotion.second->status == ledger::PromotionStatus::CLAIMED) {
FetchSignedTokens(
Expand Down
1 change: 1 addition & 0 deletions vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class NativeLedgerClient : public ledger::LedgerClient {
void InsertOrUpdateUnblindedToken(ledger::UnblindedTokenPtr info, ledger::ResultCallback callback) override;
void GetAllUnblindedTokens(ledger::GetAllUnblindedTokensCallback callback) override;
void DeleteUnblindedToken(const std::vector<std::string>& id_list, ledger::ResultCallback callback) override;
void DeleteUnblindedTokensForPromotion(const std::string& promotion_id, ledger::ResultCallback callback) override;
ledger::ClientInfoPtr GetClientInfo() override;
void UnblindedTokensReady() override;
};

0 comments on commit 6a35358

Please sign in to comment.