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

Accumulated ads and earnings disappearing after upgrade #1952

Merged
merged 1 commit into from
Mar 14, 2019
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 @@ -172,66 +172,50 @@ bool ConfirmationsImpl::FromJSON(const std::string& json) {
}

// Catalog issuers
auto* catalog_issuers_value = dictionary->FindKey("catalog_issuers");
if (!catalog_issuers_value) {
return false;
if (!GetCatalogIssuersFromJSON(dictionary.get())) {
BLOG(WARNING) << "Failed to get catalog issuers from JSON: " << json;
}

base::DictionaryValue* catalog_issuers_dictionary;
if (!catalog_issuers_value->GetAsDictionary(&catalog_issuers_dictionary)) {
return false;
// Transaction history
if (!GetTransactionHistoryFromJSON(dictionary.get())) {
BLOG(WARNING) << "Failed to get transaction history from JSON: " << json;
}

std::string public_key;
std::map<std::string, std::string> catalog_issuers;
if (!GetCatalogIssuersFromDictionary(catalog_issuers_dictionary, &public_key,
&catalog_issuers)) {
return false;
// Unblinded tokens
if (!GetUnblindedTokensFromJSON(dictionary.get())) {
BLOG(WARNING) << "Failed to get unblinded tokens from JSON: " << json;
}

// Transaction history
auto* transaction_history_value = dictionary->FindKey("transaction_history");
if (!transaction_history_value) {
return false;
// Unblinded payment tokens
if (!GetUnblindedPaymentTokensFromJSON(dictionary.get())) {
BLOG(WARNING) <<
"Failed to get unblinded payment tokens from JSON: " << json;
}

base::DictionaryValue* transaction_history_dictionary;
if (!transaction_history_value->GetAsDictionary(
&transaction_history_dictionary)) {
return false;
}
return true;
}

std::vector<TransactionInfo> transaction_history;
if (!GetTransactionHistoryFromDictionary(transaction_history_dictionary,
&transaction_history)) {
bool ConfirmationsImpl::GetCatalogIssuersFromJSON(
base::DictionaryValue* dictionary) {
auto* catalog_issuers_value = dictionary->FindKey("catalog_issuers");
if (!catalog_issuers_value) {
return false;
}

transaction_history_ = transaction_history;

// Unblinded tokens
auto* unblinded_tokens_value = dictionary->FindKey("unblinded_tokens");
if (!unblinded_tokens_value) {
base::DictionaryValue* catalog_issuers_dictionary;
if (!catalog_issuers_value->GetAsDictionary(&catalog_issuers_dictionary)) {
return false;
}

base::ListValue unblinded_token_values(unblinded_tokens_value->GetList());

// Unblinded payment tokens
auto* unblinded_payment_tokens_value =
dictionary->FindKey("unblinded_payment_tokens");
if (!unblinded_payment_tokens_value) {
std::string public_key;
std::map<std::string, std::string> catalog_issuers;
if (!GetCatalogIssuersFromDictionary(dictionary, &public_key,
&catalog_issuers)) {
return false;
}

base::ListValue unblinded_payment_token_values(
unblinded_payment_tokens_value->GetList());

// Update state
public_key_ = public_key;
catalog_issuers_ = catalog_issuers;
unblinded_tokens_->SetTokensFromList(unblinded_token_values);
unblinded_payment_tokens_->SetTokensFromList(unblinded_payment_token_values);

return true;
}
Expand Down Expand Up @@ -285,6 +269,30 @@ bool ConfirmationsImpl::GetCatalogIssuersFromDictionary(
return true;
}

bool ConfirmationsImpl::GetTransactionHistoryFromJSON(
base::DictionaryValue* dictionary) {
auto* transaction_history_value = dictionary->FindKey("transaction_history");
if (!transaction_history_value) {
return false;
}

base::DictionaryValue* transaction_history_dictionary;
if (!transaction_history_value->GetAsDictionary(
&transaction_history_dictionary)) {
return false;
}

std::vector<TransactionInfo> transaction_history;
if (!GetTransactionHistoryFromDictionary(transaction_history_dictionary,
&transaction_history)) {
return false;
}

transaction_history_ = transaction_history;

return true;
}

bool ConfirmationsImpl::GetTransactionHistoryFromDictionary(
base::DictionaryValue* dictionary,
std::vector<TransactionInfo>* transaction_history) {
Expand All @@ -294,6 +302,7 @@ bool ConfirmationsImpl::GetTransactionHistoryFromDictionary(
// Transaction
auto* transactions_value = dictionary->FindKey("transactions");
if (!transactions_value) {
DCHECK(false) << "Transactions history dictionary missing transactions";
return false;
}

Expand All @@ -302,43 +311,83 @@ bool ConfirmationsImpl::GetTransactionHistoryFromDictionary(
for (auto& transaction_value : transactions_list_value) {
base::DictionaryValue* transaction_dictionary;
if (!transaction_value.GetAsDictionary(&transaction_dictionary)) {
return false;
DCHECK(false) << "Transaction should be a dictionary";
continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

It's OK to continue here when the data isn't as expected?

Copy link
Collaborator Author

@tmancey tmancey Mar 14, 2019

Choose a reason for hiding this comment

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

it is as if transaction gets into a bad state, we should still keep good transactions

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, got it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also this way bad transactions are still persisted in the state to help diagnose issues

}

TransactionInfo info;

// Timestamp
auto* timestamp_in_seconds_value =
transaction_dictionary->FindKey("timestamp_in_seconds");
if (!timestamp_in_seconds_value) {
return false;
if (timestamp_in_seconds_value) {
info.timestamp_in_seconds =
std::stoull(timestamp_in_seconds_value->GetString());
} else {
// timestamp missing, fallback to default
auto now = base::Time::Now();
info.timestamp_in_seconds =
static_cast<uint64_t>((now - base::Time()).InSeconds());
}
info.timestamp_in_seconds =
std::stoull(timestamp_in_seconds_value->GetString());

// Estimated redemption value
auto* estimated_redemption_value_value =
transaction_dictionary->FindKey("estimated_redemption_value");
if (!estimated_redemption_value_value) {
return false;
if (estimated_redemption_value_value) {
info.estimated_redemption_value =
estimated_redemption_value_value->GetDouble();
} else {
// estimated redemption value missing, fallback to default
info.estimated_redemption_value = 0.0;
}
info.estimated_redemption_value =
estimated_redemption_value_value->GetDouble();

// Confirmation type
// Confirmation type (>= 0.63.8)
auto* confirmation_type_value =
transaction_dictionary->FindKey("confirmation_type");
if (!confirmation_type_value) {
return false;
if (confirmation_type_value) {
info.confirmation_type = confirmation_type_value->GetString();
} else {
// confirmation type missing, fallback to default
info.confirmation_type = kConfirmationTypeView;
}
info.confirmation_type = confirmation_type_value->GetString();

transaction_history->push_back(info);
}

return true;
}

bool ConfirmationsImpl::GetUnblindedTokensFromJSON(
base::DictionaryValue* dictionary) {
auto* unblinded_tokens_value = dictionary->FindKey("unblinded_tokens");
if (!unblinded_tokens_value) {
return false;
}

base::ListValue unblinded_token_values(unblinded_tokens_value->GetList());

unblinded_tokens_->SetTokensFromList(unblinded_token_values);

return true;
}

bool ConfirmationsImpl::GetUnblindedPaymentTokensFromJSON(
base::DictionaryValue* dictionary) {
auto* unblinded_payment_tokens_value =
dictionary->FindKey("unblinded_payment_tokens");
if (!unblinded_payment_tokens_value) {
return false;
}

base::ListValue unblinded_payment_token_values(
unblinded_payment_tokens_value->GetList());

unblinded_payment_tokens_->SetTokensFromList(
unblinded_payment_token_values);

return true;
}

void ConfirmationsImpl::SaveState() {
BLOG(INFO) << "Saving confirmations state";

Expand Down Expand Up @@ -490,7 +539,7 @@ double ConfirmationsImpl::GetEstimatedRedemptionValue(
auto name = it->second;
if (!re2::RE2::Replace(&name, "BAT", "")) {
BLOG(ERROR) << "Could not estimate redemption value due to catalog"
<< " issuer name missing BAT";
<< " issuer name missing BAT";
}

estimated_redemption_value = stod(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,25 @@ class ConfirmationsImpl : public Confirmations {

bool FromJSON(const std::string& json);

bool GetCatalogIssuersFromJSON(
base::DictionaryValue* dictionary);
bool GetCatalogIssuersFromDictionary(
base::DictionaryValue* dictionary,
std::string* public_key,
std::map<std::string, std::string>* issuers) const;

bool GetTransactionHistoryFromJSON(
base::DictionaryValue* dictionary);
bool GetTransactionHistoryFromDictionary(
base::DictionaryValue* dictionary,
std::vector<TransactionInfo>* transaction_history);

bool GetUnblindedTokensFromJSON(
base::DictionaryValue* dictionary);

bool GetUnblindedPaymentTokensFromJSON(
base::DictionaryValue* dictionary);

// Confirmations::Client
ConfirmationsClient* confirmations_client_; // NOT OWNED

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,21 @@ class ConfirmationsUnblindedTokensTest : public ::testing::Test {
return unblinded_tokens;
}

base::ListValue GetUnblindedTokensAsList(const int count) {
base::Value GetUnblindedTokensAsList(const int count) {
base::Value list(base::Value::Type::LIST);

// auto tokens = GetUnblindedTokens(count);
// for (const auto& token : tokens) {
// auto token_base64 = token.encode_base64();
// auto token_value = base::Value(token_base64);
// list.GetList().push_back(std::move(token_value));
// }
auto tokens = GetUnblindedTokens(count);

base::ListValue list_values(list.GetList());
return list_values;
for (const auto& token : tokens) {
base::Value dictionary(base::Value::Type::DICTIONARY);
dictionary.SetKey("unblinded_token", base::Value(
token.unblinded_token.encode_base64()));
dictionary.SetKey("public_key", base::Value(token.public_key));

list.GetList().push_back(std::move(dictionary));
}

return list;
}
};

Expand Down Expand Up @@ -208,6 +211,8 @@ TEST_F(ConfirmationsUnblindedTokensTest, GetAllTokens_Exist) {

std::string expected_public_key = "RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="; // NOLINT

EXPECT_EQ(tokens.size(), expected_unblinded_tokens_base64.size());

unsigned int index = 0;
for (const auto& token_info : tokens) {
auto expected_unblinded_token_base64 =
Expand Down Expand Up @@ -250,6 +255,7 @@ TEST_F(ConfirmationsUnblindedTokensTest, GetTokensAsList_Exist) {

// Assert
base::ListValue list_values(list.GetList());
EXPECT_EQ(list_values.GetSize(), unblinded_tokens.size());
for (auto& value : list_values) {
base::DictionaryValue* dictionary;
if (!value.GetAsDictionary(&dictionary)) {
Expand Down Expand Up @@ -323,6 +329,7 @@ TEST_F(ConfirmationsUnblindedTokensTest, SetTokens_Exist) {
// Assert
unsigned int index = 0;
auto tokens = unblinded_tokens_->GetAllTokens();
EXPECT_EQ(tokens.size(), unblinded_tokens.size());
for (const auto& token_info : tokens) {
auto expected_token_info = unblinded_tokens.at(index);
if (token_info.unblinded_token != expected_token_info.unblinded_token) {
Expand Down Expand Up @@ -386,6 +393,8 @@ TEST_F(ConfirmationsUnblindedTokensTest, SetTokensFromList) {

auto tokens = unblinded_tokens_->GetAllTokens();

EXPECT_EQ(tokens.size(), expected_unblinded_tokens_base64.size());

unsigned int index = 0;
for (const auto& token_info : tokens) {
auto expected_unblinded_token_base64 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void UnblindedTokens::SetTokens(
confirmations_->SaveState();
}

bool UnblindedTokens::SetTokensFromList(const base::Value& list) {
void UnblindedTokens::SetTokensFromList(const base::Value& list) {
base::ListValue list_values(list.GetList());

std::vector<TokenInfo> tokens;
Expand All @@ -64,20 +64,23 @@ bool UnblindedTokens::SetTokensFromList(const base::Value& list) {
} else {
base::DictionaryValue* dictionary;
if (!value.GetAsDictionary(&dictionary)) {
return false;
DCHECK(false) << "Unblinded token should be a dictionary";
continue;
}

// Unblinded token
auto* unblinded_token_value = dictionary->FindKey("unblinded_token");
if (!unblinded_token_value) {
return false;
DCHECK(false) << "Unblinded token dictionary missing unblinded_token";
continue;
}
unblinded_token = unblinded_token_value->GetString();

// Public key
auto* public_key_value = dictionary->FindKey("public_key");
if (!public_key_value) {
return false;
DCHECK(false) << "Unblinded token dictionary missing public_key";
continue;
}
public_key = public_key_value->GetString();
}
Expand All @@ -90,8 +93,6 @@ bool UnblindedTokens::SetTokensFromList(const base::Value& list) {
}

SetTokens(tokens);

return true;
}

void UnblindedTokens::AddTokens(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UnblindedTokens {
base::Value GetTokensAsList();

void SetTokens(const std::vector<TokenInfo>& tokens);
bool SetTokensFromList(const base::Value& list);
void SetTokensFromList(const base::Value& list);

void AddTokens(const std::vector<TokenInfo>& tokens);

Expand Down