Skip to content

Commit

Permalink
Mark tokens as spendable if contribution failes
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jun 30, 2020
1 parent 73c8438 commit 6eb5e73
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
1 change: 1 addition & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "contributionStepCreds", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_CREDS }, // NOLINT
{ "contributionStepRewardsOff", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_REWARDS_OFF }, // NOLINT
{ "contributionStepAutoContributeOff", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_AUTO_CONTRIBUTE_OFF }, // NOLINT
{ "contributionStepRetryCount", IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_RETRY_COUNT }, // NOLINT
{ "rewardsNotEnabled", IDS_BRAVE_REWARDS_INTERNALS_REWARDS_NOT_ENABLED }, // NOLINT
{ "rewardsTypeAuto", IDS_BRAVE_REWARDS_INTERNALS_REWARDS_TYPE_AUTO }, // NOLINT
{ "rewardsTypeOneTimeTip", IDS_BRAVE_REWARDS_INTERNALS_REWARDS_TYPE_ONE_TIME_TIP }, // NOLINT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const getProcessorString = (processor: number) => {

const getContributionStepString = (step: number) => {
switch (step) {
case -7:
return getLocale('contributionStepRetryCount')
case -6:
return getLocale('contributionStepAutoContributeOff')
case -5:
Expand Down
1 change: 1 addition & 0 deletions components/resources/brave_components_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@
<message name="IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_CREDS" desc="">Creds</message>
<message name="IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_REWARDS_OFF" desc="">Rewards was turned off</message>
<message name="IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_AUTO_CONTRIBUTE_OFF" desc="">Auto-contribute was turned off</message>
<message name="IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_STEP_RETRY_COUNT" desc="">Stopped retrying</message>
<message name="IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTION_TYPE" desc="Contribution type">Type:</message>
<message name="IDS_BRAVE_REWARDS_INTERNALS_CONTRIBUTIONS" desc="">Contributions</message>
<message name="IDS_BRAVE_REWARDS_INTERNALS_DOWNLOAD_BUTTON" desc="">Download full log</message>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module ledger.mojom;

enum ContributionStep {
STEP_RETRY_COUNT = -7,
STEP_AC_OFF = -6,
STEP_REWARDS_OFF = -5,
STEP_AC_TABLE_EMPTY = -4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ ledger::ContributionStep ConvertResultIntoContributionStep(
case ledger::Result::AC_OFF: {
return ledger::ContributionStep::STEP_AC_OFF;
}
case ledger::Result::TOO_MANY_RESULTS: {
return ledger::ContributionStep::STEP_RETRY_COUNT;
}
default: {
return ledger::ContributionStep::STEP_FAILED;
}
Expand Down Expand Up @@ -284,7 +287,8 @@ void Contribution::ContributionCompleted(

auto save_callback = std::bind(&Contribution::ContributionCompletedSaved,
this,
_1);
_1,
contribution->contribution_id);

ledger_->UpdateContributionInfoStepAndCount(
contribution->contribution_id,
Expand All @@ -293,11 +297,20 @@ void Contribution::ContributionCompleted(
save_callback);
}

void Contribution::ContributionCompletedSaved(const ledger::Result result) {
void Contribution::ContributionCompletedSaved(
const ledger::Result result,
const std::string& contribution_id) {
if (result != ledger::Result::LEDGER_OK) {
BLOG(0, "Contribution step and count failed");
return;
}

auto callback = std::bind(&Contribution::OnMarkUnblindedTokensAsSpendable,
this,
_1,
contribution_id);
ledger_->MarkUnblindedTokensAsSpendable(
contribution_id,
callback);
}

void Contribution::ContributeUnverifiedPublishers() {
Expand Down Expand Up @@ -697,13 +710,9 @@ void Contribution::SetRetryCounter(ledger::ContributionInfoPtr contribution) {

if (contribution->retry_count == 3) {
BLOG(0, "Contribution failed after 3 retries");
auto callback = std::bind(&Contribution::OnMarkUnblindedTokensAsSpendable,
this,
_1,
braveledger_bind_util::FromContributionToString(contribution->Clone()));
ledger_->MarkUnblindedTokensAsSpendable(
contribution->contribution_id,
callback);
ledger_->ContributionCompleted(
ledger::Result::TOO_MANY_RESULTS,
std::move(contribution));
return;
}

Expand All @@ -721,24 +730,12 @@ void Contribution::SetRetryCounter(ledger::ContributionInfoPtr contribution) {

void Contribution::OnMarkUnblindedTokensAsSpendable(
const ledger::Result result,
const std::string& contribution_string) {
auto contribution = braveledger_bind_util::FromStringToContribution(
contribution_string);
if (!contribution) {
BLOG(0, "Contribution was not converted successfully");
return;
}

if (result != ledger::Result::LEDGER_OK) {
BLOG(0, "Failed to mark unblinded tokens as unreserved for contribution "
<< contribution->contribution_id);
}

// Even if we can't mark the tokens as unreserved, mark the
// contribution as completed
ledger_->ContributionCompleted(
ledger::Result::LEDGER_ERROR,
std::move(contribution));
const std::string& contribution_id) {
BLOG_IF(
1,
result != ledger::Result::LEDGER_OK,
"Failed to mark unblinded tokens as unreserved for contribution "
<< contribution_id);
}

void Contribution::Retry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ class Contribution {
const ledger::Result result,
const uint64_t reconcile_stamp);

void ContributionCompletedSaved(const ledger::Result result);
void ContributionCompletedSaved(
const ledger::Result result,
const std::string& contribution_id);

void OnProcessContributionQueue(ledger::ContributionQueuePtr info);

Expand Down Expand Up @@ -173,7 +175,7 @@ class Contribution {

void OnMarkUnblindedTokensAsSpendable(
const ledger::Result result,
const std::string& contribution_string);
const std::string& contribution_id);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
std::unique_ptr<Unverified> unverified_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ void ContributionSKU::OnOrder(
callback);
return;
}
case ledger::ContributionStep::STEP_RETRY_COUNT:
case ledger::ContributionStep::STEP_REWARDS_OFF:
case ledger::ContributionStep::STEP_AC_OFF:
case ledger::ContributionStep::STEP_AC_TABLE_EMPTY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ void Unblinded::Retry(
get_callback);
return;
}
case ledger::ContributionStep::STEP_RETRY_COUNT:
case ledger::ContributionStep::STEP_REWARDS_OFF:
case ledger::ContributionStep::STEP_AC_OFF:
case ledger::ContributionStep::STEP_AC_TABLE_EMPTY:
Expand Down

0 comments on commit 6eb5e73

Please sign in to comment.