Skip to content

Commit

Permalink
Fixes contribution with unblinded tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc authored and gdregalo committed Jan 10, 2020
1 parent 40dd198 commit d564a3d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* 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 "brave/components/brave_rewards/browser/database/database_unblinded_token.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_util.h"
#include "brave/components/brave_rewards/browser/database/database_unblinded_token.h"
#include "brave/components/brave_rewards/browser/database/database_util.h"
#include "sql/statement.h"
#include "sql/transaction.h"

Expand All @@ -21,6 +21,15 @@ namespace {
const char* table_name_ = "unblinded_tokens";
const int minimum_version_ = 10;

int64_t GetExpirationDate(const int32_t type, const int64_t stamp) {
const auto promotion_type = static_cast<ledger::PromotionType>(type);
if (promotion_type == ledger::PromotionType::ADS) {
return 0;
}

return stamp;
}

} // namespace

DatabaseUnblindedToken::DatabaseUnblindedToken(
Expand Down Expand Up @@ -126,7 +135,7 @@ ledger::UnblindedTokenList DatabaseUnblindedToken::GetAllRecords(
ledger::UnblindedTokenList list;
const std::string query = base::StringPrintf(
"SELECT u.token_id, u.token_value, u.public_key, u.value, "
"u.promotion_id, p.expires_at FROM %s as u "
"u.promotion_id, p.expires_at, p.type FROM %s as u "
"LEFT JOIN promotion as p ON p.promotion_id = u.promotion_id",
table_name_);

Expand All @@ -139,7 +148,8 @@ ledger::UnblindedTokenList DatabaseUnblindedToken::GetAllRecords(
info->public_key = statement.ColumnString(2);
info->value = statement.ColumnDouble(3);
info->promotion_id = statement.ColumnString(4);
info->expires_at = statement.ColumnInt64(5);
info->expires_at =
GetExpirationDate(statement.ColumnInt(6), statement.ColumnInt64(5));

list.push_back(std::move(info));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ void Unblinded::OnUnblindedTokens(
continue;
}

if (item->value + current_amount > reconcile.fee) {
if (current_amount >= reconcile.fee) {
break;
}

current_amount += item->value;
current_amount += 0.25;
token_list.push_back(std::move(item));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UnblindedTest : public ::testing::Test {
.WillByDefault(
Invoke([](const std::string& viewing_id) {
ledger::CurrentReconcileProperties reconcile;
reconcile.fee = 5.0;
reconcile.fee = 1.0;
return reconcile;
}));
}
Expand All @@ -53,10 +53,6 @@ TEST_F(UnblindedTest, NotEnoughFunds) {
EXPECT_CALL(*mock_ledger_impl_,
ReconcileComplete(ledger::Result::NOT_ENOUGH_FUNDS, _, _, _, _));

std::vector<std::string> delete_list;
delete_list.push_back("1");
EXPECT_CALL(*mock_ledger_impl_, DeleteUnblindedTokens(delete_list, _));

ON_CALL(*mock_ledger_impl_, GetAllUnblindedTokens(_))
.WillByDefault(
Invoke([](ledger::GetAllUnblindedTokensCallback callback) {
Expand All @@ -65,8 +61,8 @@ TEST_F(UnblindedTest, NotEnoughFunds) {
auto info = ledger::UnblindedToken::New();
info->id = 1;
info->token_value = "asdfasdfasdfsad=";
info->value = 2;
info->expires_at = 1574133178;
info->value = 0.25;
info->expires_at = 32574133178;
list.push_back(info->Clone());

callback(std::move(list));
Expand All @@ -93,12 +89,24 @@ TEST_F(UnblindedTest, PromotionExpiredDeleteToken) {
auto info = ledger::UnblindedToken::New();
info->id = 1;
info->token_value = "asdfasdfasdfsad=";
info->value = 5;
info->value = 0.25;
info->expires_at = 1574133178;
list.push_back(info->Clone());

info->id = 2;
info->expires_at = 22574133178;
info->expires_at = 32574133178;
list.push_back(info->Clone());

info->id = 3;
list.push_back(info->Clone());

info->id = 4;
list.push_back(info->Clone());

info->id = 5;
list.push_back(info->Clone());

info->id = 6;
list.push_back(info->Clone());

callback(std::move(list));
Expand All @@ -125,7 +133,7 @@ TEST_F(UnblindedTest, PromotionExpiredDeleteTokensNotEnoughFunds) {
auto info = ledger::UnblindedToken::New();
info->id = 1;
info->token_value = "asdfasdfasdfsad=";
info->value = 3;
info->value = 0.25;
info->expires_at = 1574133178;
list.push_back(info->Clone());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ void HandleExpiredPromotions(
static_cast<uint64_t>(base::Time::Now().ToDoubleT());

for (auto& item : *promotions) {
// we shouldn't expire ad grant
if (item.second->type == ledger::PromotionType::ADS) {
continue;
}

if (item.second->expires_at > 0 &&
item.second->expires_at <= current_time) {
item.second->status = ledger::PromotionStatus::OVER;
Expand Down Expand Up @@ -170,6 +175,11 @@ void Promotion::OnGetAllPromotions(

if (list.size() > 0) {
for (auto & item : list) {
// if the server return expiration for ads we need to set it to 0
if (item->type == ledger::PromotionType::ADS) {
item->expires_at = 0;
}

if (item->legacy_claimed) {
item->status = ledger::PromotionStatus::CLAIMED;
ClaimTokens(item->Clone(), [](const ledger::Result _){});
Expand Down

0 comments on commit d564a3d

Please sign in to comment.