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

Prevent crash on failed pgf payment #1991

Merged
merged 2 commits into from
Oct 17, 2023
Merged

Conversation

grarco
Copy link
Contributor

@grarco grarco commented Oct 16, 2023

Describe your changes

Fixes a bug for which the ledger crashed in case of a failed PGF payment.

Indicate on which release or other PRs this topic is based on

Commit 3f979bf from base branch

Checklist before merging to draft

  • I have added a changelog
  • Git history is in acceptable state

@Fraccaman
Copy link
Member

should we also do the same for cpgf? or does it already have the same behavior?

@grarco
Copy link
Contributor Author

grarco commented Oct 16, 2023

I think that the cPGF payment done in finalize_block is fine as it is. We are only propagating the error when crediting the tokens for the PGF account itself, which I think is fine since we can't really recover from that. For cpgf and stewards crediting we are instead already managing any possible errors

credit_tokens(
&mut self.wl_storage,
&staking_token,
&pgf_address,
pgf_inflation_amount,
)?;
tracing::info!(
"Minting {} tokens for PGF rewards distribution into the PGF \
account.",
pgf_inflation_amount.to_string_native()
);
let mut pgf_fundings = pgf::get_payments(&self.wl_storage)?;
// we want to pay first the oldest fundings
pgf_fundings.sort_by(|a, b| a.id.cmp(&b.id));
for funding in pgf_fundings {
if credit_tokens(
&mut self.wl_storage,
&staking_token,
&funding.detail.target,
funding.detail.amount,
)
.is_ok()
{
tracing::info!(
"Minted {} tokens for {} project.",
funding.detail.amount.to_string_native(),
&funding.detail.target,
);
} else {
tracing::warn!(
"Failed Minting {} tokens for {} project.",
funding.detail.amount.to_string_native(),
&funding.detail.target,
);
}
}
// Pgf steward inflation
let stewards = pgf::get_stewards(&self.wl_storage)?;
let pgf_steward_reward = match stewards.len() {
0 => Dec::zero(),
_ => pgf_steward_inflation
.trunc_div(&Dec::from(stewards.len()))
.unwrap_or_default(),
};
for steward in stewards {
for (address, percentage) in steward.reward_distribution {
let pgf_steward_reward = pgf_steward_reward
.checked_mul(&percentage)
.unwrap_or_default();
let reward_amount = token::Amount::from(pgf_steward_reward);
if credit_tokens(
&mut self.wl_storage,
&staking_token,
&address,
reward_amount,
)
.is_ok()
{
tracing::info!(
"Minting {} tokens for steward {}.",
reward_amount.to_string_native(),
address,
);
} else {
tracing::warn!(
"Failed minting {} tokens for steward {}.",
reward_amount.to_string_native(),
address,
);
}
}
}

@grarco grarco requested a review from Fraccaman October 16, 2023 16:53
@grarco grarco marked this pull request as ready for review October 16, 2023 16:53
@Fraccaman Fraccaman mentioned this pull request Oct 16, 2023
@brentstone brentstone merged commit e4cfa4e into main Oct 17, 2023
14 checks passed
@brentstone brentstone deleted the grarco/fix-pgf-payment-crash branch October 17, 2023 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants