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

fix: fix the cancel unbond #12967

Merged
merged 21 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: address the pr comments
  • Loading branch information
gsk967 committed Aug 20, 2022
commit eef238baf9bc2fe625ee91d4fbdfb8081e906497
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/staking) [#12409](https://github.com/cosmos/cosmos-sdk/pull/12409) Migrate `x/staking` to self-managed parameters and deprecate it's usage of `x/params`.
* (x/bank) [#11859](https://github.com/cosmos/cosmos-sdk/pull/11859) Move the SendEnabled information out of the Params and into the state store directly.
* (x/gov) [#12771](https://github.com/cosmos/cosmos-sdk/pull/12771) Initial deposit requirement for proposals at submission time.
* (x/staking) [#12967](https://github.com/cosmos/cosmos-sdk/pull/12967) `unbond` now creates only one unbonding delegation entry when multiple ubd msgs broadcast at a single height.
* (x/staking) [#12967](https://github.com/cosmos/cosmos-sdk/pull/12967) `unbond` now creates only one unbonding delegation entry when multiple unbondings exist at a single height (e.g. through multiple messages in a transaction).

### API Breaking Changes

Expand Down
29 changes: 19 additions & 10 deletions x/staking/migrations/v5/store.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
package v5

import (
"sort"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

// MigrateStore performs in-place store migrations from v3 to v4.
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
/*
this migrateStore will remove the ubdEntries with same creation_height
and create a new ubdEntry with updated balance and initial_balance
*/
// this migrateStore will remove the ubdEntries with same creation_height
// and create a new ubdEntry with updated balance and initial_balance
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UnbondingDelegationKey)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
ubd := types.MustUnmarshalUBD(cdc, iterator.Value())

entriesAtSameCreationHeight := make(map[int64][]types.UnbondingDelegationEntry)

var creationHeights []int64
gsk967 marked this conversation as resolved.
Show resolved Hide resolved

for _, ubdEntry := range ubd.Entries {
entriesAtSameCreationHeight[ubdEntry.CreationHeight] = append(entriesAtSameCreationHeight[ubdEntry.CreationHeight], ubdEntry)
creationHeights = append(creationHeights, ubdEntry.CreationHeight)
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
}

// clear the old ubdEntries
ubd.Entries = nil
gsk967 marked this conversation as resolved.
Show resolved Hide resolved

for _, entries := range entriesAtSameCreationHeight {
sort.Slice(creationHeights, func(i, j int) bool { return creationHeights[i] < creationHeights[j] })

for index := 0; index < len(creationHeights); index++ {
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
var ubdEntry types.UnbondingDelegationEntry
for _, entry := range entries {
for _, entry := range entriesAtSameCreationHeight[creationHeights[index]] {
ubdEntry.Balance = ubdEntry.Balance.Add(entry.Balance)
ubdEntry.InitialBalance = ubdEntry.InitialBalance.Add(entry.InitialBalance)
ubdEntry.CreationHeight = entry.CreationHeight
Expand All @@ -38,22 +46,23 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Binar
}

// set the new ubd to the store
setUBDToStore(ctx, storeKey, cdc, ubd)
setUBDToStore(ctx, store, cdc, ubd)
}

return nil
}

func setUBDToStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, ubd types.UnbondingDelegation) {
func setUBDToStore(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, ubd types.UnbondingDelegation) {
delegatorAddress := sdk.MustAccAddressFromBech32(ubd.DelegatorAddress)

store := ctx.KVStore(storeKey)
bz := types.MustMarshalUBD(cdc, ubd)

addr, err := sdk.ValAddressFromBech32(ubd.ValidatorAddress)
if err != nil {
panic(err)
}

key := types.GetUBDKey(delegatorAddress, addr)

store.Set(key, bz)
store.Set(types.GetUBDByValIndexKey(delegatorAddress, addr), []byte{}) // index, store empty bytes
}