-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
fix!: x/staking - remove delegation with amount is zero #10254
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8db3268
x/staking: update Unbond
alexanderbez 2ee9447
cl++
alexanderbez 3144054
Merge branch 'master' into bez/10216-del-removal
alexanderbez aaab239
add check
alexanderbez f9d957b
update import genesis logic
alexanderbez 56599e4
Merge branch 'master' into bez/10216-del-removal
alexanderbez 5b61ca5
Merge branch 'master' into bez/10216-del-removal
alexanderbez a17ab96
update import genesis logic
alexanderbez a8cdfbd
x/staking: update consensus version
alexanderbez e85e055
x/staking: prep in-place migration
alexanderbez 18c5579
x/staking: prep in-place migration
alexanderbez 5c8ed20
x/staking: prep in-place migration
alexanderbez fd1152b
x/staking: prep in-place migration
alexanderbez 6835009
Merge branch 'master' into bez/10216-del-removal
alexanderbez 43834cf
updates
alexanderbez 7f4e1a2
updates
alexanderbez 395dd09
Merge branch 'master' into bez/10216-del-removal
alexanderbez 25cbe22
remove genesis changes
alexanderbez 0051730
Merge branch 'master' into bez/10216-del-removal
alexanderbez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package v045 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/address" | ||
v040staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v040" | ||
) | ||
|
||
func getValidatorKey(operatorAddr sdk.ValAddress) []byte { | ||
return append(v040staking.ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package v045 | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
v040staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v040" | ||
) | ||
|
||
// MigrateStore performs in-place store migrations from v0.43/v0.44 to v0.45. | ||
// The migration includes: | ||
// | ||
// - Removing delegations that have a zero share or token amount. | ||
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error { | ||
store := ctx.KVStore(storeKey) | ||
|
||
return purgeDelegations(store, cdc) | ||
} | ||
|
||
func purgeDelegations(store sdk.KVStore, cdc codec.BinaryCodec) error { | ||
prefixDelStore := prefix.NewStore(store, v040staking.DelegationKey) | ||
|
||
delStoreIter := prefixDelStore.Iterator(nil, nil) | ||
defer delStoreIter.Close() | ||
|
||
valCache := make(map[string]v040staking.Validator) | ||
|
||
for ; delStoreIter.Valid(); delStoreIter.Next() { | ||
var delegation v040staking.Delegation | ||
if err := cdc.Unmarshal(delStoreIter.Value(), &delegation); err != nil { | ||
return err | ||
} | ||
|
||
validator, ok := valCache[delegation.ValidatorAddress] | ||
if !ok { | ||
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cdc.Unmarshal(store.Get(getValidatorKey(valAddr)), &validator); err != nil { | ||
return err | ||
} | ||
|
||
valCache[delegation.ValidatorAddress] = validator | ||
} | ||
|
||
// TODO: On-chain, we call BeforeDelegationRemoved prior to removing the | ||
alexanderbez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// object from state. Do we need to do the same here? | ||
if validator.TokensFromShares(delegation.Shares).TruncateInt().IsZero() || delegation.Shares.IsZero() { | ||
prefixDelStore.Delete(delStoreIter.Key()) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexanderbez do you think this could break delegator shares invariant, causing #10750 ?
@aleem1314 did some debugging on this, here's some logs:
just reverting this line and simulations pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm could be! But what invariant is broken exactly? The delegation should be deleted here. Is there some invariant that expects this not to be deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
DelegatorSharesInvariant
one. Maybe it should be updated to takeTruncateInt
into account? The error message is:which soulds like some truncating error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's keep the discussion in the new issue created 👍