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

Fixes contribution with unblinded tokens (1.3.x) #4342

Merged
merged 1 commit into from
Jan 13, 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 @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to hard code it in version 1.3.x as we can't do db migration. This was done through db in version 1.4.x

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 @@ -50,10 +50,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 @@ -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));
Expand All @@ -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));
Expand All @@ -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());

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