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
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 ++
gsk967 committed Aug 23, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 184bfd878b684ef9a0f9f81e44777dd08b5ebd16
6 changes: 0 additions & 6 deletions x/staking/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import (
v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2"
v3 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v3"
v4 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v4"
v5 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v5"
)

// Migrator is a struct for handling in-place store migrations.
@@ -37,8 +36,3 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v4.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc, m.legacySubspace)
}

// Migrate4to5 migrates x/staking state from consensus version 4 to 5.
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v5.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}
55 changes: 55 additions & 0 deletions x/staking/migrations/v4/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v4

import (
"sort"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -21,5 +23,58 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Binar
bz := cdc.MustMarshal(&legacyParams)
store.Set(types.ParamsKey, bz)

// this will remove the ubdEntries with same creation_height
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
// and create a new ubdEntry with updated balance and initial_balance

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)
for _, ubdEntry := range ubd.Entries {
entriesAtSameCreationHeight[ubdEntry.CreationHeight] = append(entriesAtSameCreationHeight[ubdEntry.CreationHeight], ubdEntry)
}

creationHeights := make([]int64, 0, len(entriesAtSameCreationHeight))
for k := range entriesAtSameCreationHeight {
creationHeights = append(creationHeights, k)
}

sort.Slice(creationHeights, func(i, j int) bool { return creationHeights[i] < creationHeights[j] })

ubd.Entries = make([]types.UnbondingDelegationEntry, 0, len(creationHeights))

for _, h := range creationHeights {
var ubdEntry types.UnbondingDelegationEntry
for _, entry := range entriesAtSameCreationHeight[h] {
ubdEntry.Balance = ubdEntry.Balance.Add(entry.Balance)
ubdEntry.InitialBalance = ubdEntry.InitialBalance.Add(entry.InitialBalance)
ubdEntry.CreationHeight = entry.CreationHeight
ubdEntry.CompletionTime = entry.CompletionTime
}
ubd.Entries = append(ubd.Entries, ubdEntry)
}

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

return nil
}

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

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)
}
68 changes: 0 additions & 68 deletions x/staking/migrations/v5/store.go

This file was deleted.

5 changes: 1 addition & 4 deletions x/staking/module.go
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ import (
)

const (
consensusVersion uint64 = 5
consensusVersion uint64 = 4
)

var (
@@ -152,9 +152,6 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err))
}
if err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4to5); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 4 to 5: %v", types.ModuleName, err))
}
}

// InitGenesis performs genesis initialization for the staking module.