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

Bug: unstake did not zero capacity #1954 #1955

Merged
merged 7 commits into from
Apr 29, 2024

Conversation

shannonwells
Copy link
Collaborator

@shannonwells shannonwells commented Apr 24, 2024

Goal

The goal of this PR is to ensure that when an account unstakes all token for Capacity, the StakingTargetDetails is reaped. This eliminates an issue with rounding errors.

Closes #1954

Discussion

A bug was found where StakingTargetDetails.amount was zero (correctly) but StakingTargetDetails.capacity was non-zero (incorrectly), when unstaking everything for a target. This is due to rounding errors. Additionally, we should simply reap the StakingTargetDetails when everything is unstaked.

The change corrects for rounding errors and also reaps the StakingTargetDetails record if the resulting amount is zero (without checking the capacity value, but this is reasonable)

Note the CapacityDetails for Provider is not affected by this bug.

Checklist

  • Chain spec updated
  • Tests added

capacity_details.total_capacity_issued,
);

let capacity_to_withdraw = if staking_target_details.amount.eq(&amount) {
Copy link
Collaborator Author

@shannonwells shannonwells Apr 24, 2024

Choose a reason for hiding this comment

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

This if/else actually addresses the rounding issue. One could argue that it's redundant given the above, and one could also argue that it's very slightly more performant in such case. I'm kind of on the fence about it.

@@ -528,7 +528,11 @@ impl<T: Config> Pallet<T> {
target: MessageSourceId,
target_details: StakingTargetDetails<BalanceOf<T>>,
) {
StakingTargetLedger::<T>::insert(staker, target, target_details);
if target_details.amount.is_zero() {
Copy link
Collaborator Author

@shannonwells shannonwells Apr 24, 2024

Choose a reason for hiding this comment

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

this was an oversight - if they've unstaked everything this record should be deleted.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Vaguely remember Wil or someone issued a similar fix in frequency, maybe they are unrelated

Copy link
Collaborator

Choose a reason for hiding this comment

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

@shannonwells shannonwells marked this pull request as ready for review April 24, 2024 21:02
@shannonwells shannonwells requested a review from wilwade as a code owner April 24, 2024 21:02
Copy link

codecov bot commented Apr 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.13%. Comparing base (9f4a9fb) to head (89a7976).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1955      +/-   ##
==========================================
+ Coverage   83.11%   83.13%   +0.01%     
==========================================
  Files          56       56              
  Lines        4525     4530       +5     
==========================================
+ Hits         3761     3766       +5     
  Misses        764      764              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Apr 24, 2024
@github-actions github-actions bot added metadata-changed Metadata has changed since the latest full release and removed metadata-changed Metadata has changed since the latest full release labels Apr 24, 2024
@aramikm
Copy link
Collaborator

aramikm commented Apr 25, 2024

@shannonwells If chain spec is updated you might need to bump the spec version in runtime?

NVM ,it's already done.

Copy link
Collaborator

@aramikm aramikm left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Collaborator

@saraswatpuneet saraswatpuneet left a comment

Choose a reason for hiding this comment

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

Lgtm, maybe it will worth checking all the places where similar bugs might persist

@github-actions github-actions bot added metadata-changed Metadata has changed since the latest full release and removed metadata-changed Metadata has changed since the latest full release labels Apr 25, 2024
@shannonwells shannonwells enabled auto-merge (squash) April 26, 2024 23:09
Copy link
Collaborator

@wilwade wilwade left a comment

Choose a reason for hiding this comment

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

Code looks good. One question remaining.

  • Reviewed bug
  • Reviewed code logic
  • Reviewed test logic

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add a migration to resolve any lingering issues from the bug?

Copy link
Collaborator Author

@shannonwells shannonwells Apr 29, 2024

Choose a reason for hiding this comment

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

I think I could run some kind of script to check if there are any mainnet records with incorrect values, but I feel like that part is slightly less urgent than actually fixing the bug. It would be easy to devise a migration script that reaps StakingTargetDetails with a zero amount though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Collaborator

@mattheworris mattheworris left a comment

Choose a reason for hiding this comment

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

  • Reviewed changes, no comments

Looks good, 🚢 it!

Copy link
Collaborator

@wilwade wilwade left a comment

Choose a reason for hiding this comment

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

Question resolved. Approved

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good.

@shannonwells shannonwells merged commit 10c144c into main Apr 29, 2024
31 checks passed
@shannonwells shannonwells deleted the bugs/unstake-didnot-zero-capacity-#1954 branch April 29, 2024 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metadata-changed Metadata has changed since the latest full release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potential data / bug in Unstaking Capacity
5 participants