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

remove another potential abort in staking_pool #7571

Merged
merged 2 commits into from
Jan 23, 2023
Merged

Conversation

emmazzz
Copy link
Contributor

@emmazzz emmazzz commented Jan 22, 2023

This showed up when stress unit testing delegation code with random amounts, random operations, etc. The rewards pool balance is 1 less than the rewards we are trying to withdraw, possibly due to rounding. The exact reason why this happened needs to be investigated. This PR removes the possibility of aborting at balance::split when withdrawing rewards from rewards pool.

UPDATE: switching the order in which we process new delegations and delegation withdraws seems to fix the issue. It's still not super clear whether the original order was the only trigger of the abort though.

@vercel
Copy link

vercel bot commented Jan 22, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

4 Ignored Deployments
Name Status Preview Comments Updated
explorer ⬜️ Ignored (Inspect) Jan 23, 2023 at 9:33PM (UTC)
explorer-storybook ⬜️ Ignored (Inspect) Jan 23, 2023 at 9:33PM (UTC)
frenemies ⬜️ Ignored (Inspect) Jan 23, 2023 at 9:33PM (UTC)
wallet-adapter ⬜️ Ignored (Inspect) Jan 23, 2023 at 9:33PM (UTC)

@@ -352,6 +352,11 @@ module sui::staking_pool {
if (total_sui_withdraw_amount >= principal_withdraw_amount)
total_sui_withdraw_amount - principal_withdraw_amount
else 0;
// This may happen when we are withdrawing everything from the pool and
// the rewards pool balance may be 1 less than reward_withdraw_amount.
if (balance::value(&pool.rewards_pool) < reward_withdraw_amount) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The exact reason why this happened needs to be investigated.

I think I would feel better if we did the investigation rather than adding this code--any case where we might be creating money (even if it's just 1 MIST) has to be avoided because it will make our reconciliation of the total SUI supply impossible.

Copy link
Collaborator

@sblackshear sblackshear Jan 23, 2023

Choose a reason for hiding this comment

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

However: if we think running in testnet with this code added + emitting an event that will help us debug the issue, I'm ok with that. But I don't think we should plan to keep this code around once we've fixed the root cause.

Copy link
Collaborator

@sblackshear sblackshear left a comment

Choose a reason for hiding this comment

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

Accepting to unblock, but please add the event + TODO

@@ -352,6 +352,11 @@ module sui::staking_pool {
if (total_sui_withdraw_amount >= principal_withdraw_amount)
total_sui_withdraw_amount - principal_withdraw_amount
else 0;
// This may happen when we are withdrawing everything from the pool and
// the rewards pool balance may be 1 less than reward_withdraw_amount.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might make sense to assert that the leftover amount is 1 to check this assumption.

@lxfind
Copy link
Contributor

lxfind commented Jan 23, 2023

If this can be reproduced entirely locally, we probably can just investigate by adding a ton of prints (locally)? Not sure if events are really needed

// the rewards pool balance may be 1 less than reward_withdraw_amount.
// TODO: FIGURE OUT EXACTLY WHY THIS CAN HAPPEN.
if (balance::value(&pool.rewards_pool) < reward_withdraw_amount) {
reward_withdraw_amount = balance::value(&pool.rewards_pool);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can use math::min

@@ -352,6 +352,12 @@ module sui::staking_pool {
if (total_sui_withdraw_amount >= principal_withdraw_amount)
total_sui_withdraw_amount - principal_withdraw_amount
else 0;
// This may happen when we are withdrawing everything from the pool and
// the rewards pool balance may be 1 less than reward_withdraw_amount.
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't necessarily have to be 1 less though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right although I've only seen 1 in test output, we don't know if it can only be 1. changing the comments

let reward_withdraw_amount = staking_pool::process_pending_delegation_withdraws(
&mut self.delegation_staking_pool, ctx);
self.metadata.next_epoch_delegation = self.metadata.next_epoch_delegation - reward_withdraw_amount;
// assert!(delegate_amount(self) == self.metadata.next_epoch_delegation, 0);
staking_pool::process_pending_delegations(&mut self.delegation_staking_pool, ctx);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to add a TODO here so that one day we could add the assert back? Or is it never going to be guaranteed down the road?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add a TODO here. I think this assert does hold today but I don't want to risk having it here and failing advance_epoch for next_epoch_delegation which doesn't matter too much now.

@emmazzz emmazzz requested a review from lxfind January 23, 2023 21:29
@emmazzz emmazzz enabled auto-merge (squash) January 23, 2023 21:32
@emmazzz emmazzz merged commit e6d8e81 into main Jan 23, 2023
@emmazzz emmazzz deleted the reward_withdraw branch January 23, 2023 21:45
emmazzz added a commit that referenced this pull request Jan 23, 2023
This showed up when stress unit testing delegation code with random
amounts, random operations, etc. The rewards pool balance is 1 less than
the rewards we are trying to withdraw, possibly due to rounding. The
exact reason why this happened needs to be investigated. This PR removes
the possibility of aborting at `balance::split` when withdrawing rewards
from rewards pool.

UPDATE: switching the order in which we process new delegations and
delegation withdraws seems to fix the issue. It's still not super clear
whether the original order was the only trigger of the abort though.
emmazzz added a commit that referenced this pull request Jan 24, 2023
This showed up when stress unit testing delegation code with random
amounts, random operations, etc. The rewards pool balance is 1 less than
the rewards we are trying to withdraw, possibly due to rounding. The
exact reason why this happened needs to be investigated. This PR removes
the possibility of aborting at `balance::split` when withdrawing rewards
from rewards pool.

UPDATE: switching the order in which we process new delegations and
delegation withdraws seems to fix the issue. It's still not super clear
whether the original order was the only trigger of the abort though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants