Skip to content

Commit

Permalink
Merge pull request #4106 from /issues/6647
Browse files Browse the repository at this point in the history
Fixes browser doesn't retry to get signed tokens in all cases
  • Loading branch information
tmancey authored Nov 30, 2019
2 parents 40c82ce + ce677ad commit 14bd1e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ void RefillTokens::OnRequestSignedTokens(
base::Optional<base::Value> dictionary = base::JSONReader::Read(response);
if (!dictionary || !dictionary->is_dict()) {
BLOG(ERROR) << "Failed to parse response: " << response;
OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

// Get nonce
auto* nonce_value = dictionary->FindKey("nonce");
if (!nonce_value) {
BLOG(ERROR) << "Response missing nonce";
OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

Expand Down Expand Up @@ -188,29 +188,23 @@ void RefillTokens::OnGetSignedTokens(

if (response_status_code != net::HTTP_OK) {
BLOG(ERROR) << "Failed to get signed tokens";

if (response_status_code == net::HTTP_ACCEPTED) {
// Tokens are not ready yet
confirmations_->StartRetryingToGetRefillSignedTokens(
kRetryGettingRefillSignedTokensAfterSeconds);
}

OnRefill(FAILED);
return;
}

// Parse JSON response
base::Optional<base::Value> dictionary = base::JSONReader::Read(response);
if (!dictionary || !dictionary->is_dict()) {
BLOG(ERROR) << "Failed to parse response: " << response;
OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

// Get public key
auto* public_key_value = dictionary->FindKey("publicKey");
if (!public_key_value) {
BLOG(ERROR) << "Response missing publicKey";
OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

Expand All @@ -220,15 +214,15 @@ void RefillTokens::OnGetSignedTokens(
if (public_key_base64 != public_key_) {
BLOG(ERROR) << "Response public_key: " << public_key_value->GetString()
<< " does not match catalog issuers public key: " << public_key_;
OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

// Get batch proof
auto* batch_proof_value = dictionary->FindKey("batchProof");
if (!batch_proof_value) {
BLOG(ERROR) << "Response missing batchProof";
OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

Expand All @@ -239,7 +233,7 @@ void RefillTokens::OnGetSignedTokens(
auto* signed_tokens_value = dictionary->FindKey("signedTokens");
if (!signed_tokens_value) {
BLOG(ERROR) << "Response missing signedTokens";
OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

Expand Down Expand Up @@ -280,7 +274,7 @@ void RefillTokens::OnGetSignedTokens(

BLOG(ERROR) << " Public key: " << public_key_;

OnRefill(FAILED);
OnRefill(FAILED, false);
return;
}

Expand All @@ -300,20 +294,29 @@ void RefillTokens::OnGetSignedTokens(
<< " unblinded tokens, you now have " << unblinded_tokens_->Count()
<< " unblinded tokens";

OnRefill(SUCCESS);
OnRefill(SUCCESS, false);
}

void RefillTokens::OnRefill(const Result result) {
void RefillTokens::OnRefill(
const Result result,
const bool should_retry) {
blinded_tokens_.clear();
tokens_.clear();

confirmations_->SaveState();

if (result != SUCCESS) {
BLOG(ERROR) << "Failed to refill tokens";
} else {
confirmations_->SaveState();

BLOG(INFO) << "Successfully refilled tokens";
if (should_retry) {
confirmations_->StartRetryingToGetRefillSignedTokens(
kRetryGettingRefillSignedTokensAfterSeconds);
}

return;
}

blinded_tokens_.clear();
tokens_.clear();
BLOG(INFO) << "Successfully refilled tokens";
}

bool RefillTokens::ShouldRefillTokens() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class RefillTokens {

void Refill(const WalletInfo& wallet_info, const std::string& public_key);

void OnRefill(const Result result);

void RetryGettingSignedTokens();

private:
Expand All @@ -62,6 +60,10 @@ class RefillTokens {
const std::string& response,
const std::map<std::string, std::string>& headers);

void OnRefill(
const Result result,
const bool should_retry = true);

bool ShouldRefillTokens() const;
int CalculateAmountOfTokensToRefill() const;

Expand Down

0 comments on commit 14bd1e3

Please sign in to comment.