From 9507a2c2cc6b6e337779acaeee439022ba157d86 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 7 Jan 2020 07:13:09 -0800 Subject: [PATCH] Fixes contribution with unblinded tokens Resolves https://github.com/brave/brave-browser/issues/7608 --- .../database/database_unblinded_token.cc | 18 +++++++++--- .../contribution/contribution_unblinded.cc | 4 +-- .../contribution_unblinded_unittest.cc | 28 ++++++++++++------- .../ledger/internal/promotion/promotion.cc | 10 +++++++ 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/components/brave_rewards/browser/database/database_unblinded_token.cc b/components/brave_rewards/browser/database/database_unblinded_token.cc index 08cbdae57939..3c7af9a1b389 100644 --- a/components/brave_rewards/browser/database/database_unblinded_token.cc +++ b/components/brave_rewards/browser/database/database_unblinded_token.cc @@ -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 #include #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" @@ -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(type); + if (promotion_type == ledger::PromotionType::ADS) { + return 0; + } + + return stamp; +} + } // namespace DatabaseUnblindedToken::DatabaseUnblindedToken( @@ -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_); @@ -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)); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc index e3e399fbac27..7c6d00a10f71 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc @@ -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)); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded_unittest.cc index 8a3be3208f8d..b360f0c67af8 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded_unittest.cc @@ -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; })); } @@ -50,10 +50,6 @@ TEST_F(UnblindedTest, NotEnoughFunds) { EXPECT_CALL(*mock_ledger_impl_, ReconcileComplete(ledger::Result::NOT_ENOUGH_FUNDS, _, _, _, _)); - std::vector 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) { @@ -62,8 +58,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)); @@ -90,12 +86,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)); @@ -122,7 +130,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()); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc index 80bd645dc9bb..a531adf91329 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion.cc @@ -54,6 +54,11 @@ void HandleExpiredPromotions( static_cast(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; @@ -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 _){});