From abd0bd5b108cf49ce4c85f465061c0801a0f8dcb Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 27 Jul 2023 11:39:37 +0200 Subject: [PATCH] fix(x/staking): `DelegationsByValidator` migrations (backport #17154) (#17156) Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com> --- x/staking/migrations/v5/keys.go | 14 +++++++++++++- x/staking/migrations/v5/migrations_test.go | 2 +- x/staking/migrations/v5/store.go | 3 +-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/x/staking/migrations/v5/keys.go b/x/staking/migrations/v5/keys.go index d62b0a2dc0..5c11c8735c 100644 --- a/x/staking/migrations/v5/keys.go +++ b/x/staking/migrations/v5/keys.go @@ -6,6 +6,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" ) const ( @@ -15,8 +16,8 @@ const ( var ( DelegationKey = []byte{0x31} // key for a delegation - DelegationByValIndexKey = []byte{0x37} // key for delegations by a validator HistoricalInfoKey = []byte{0x50} // prefix for the historical info + DelegationByValIndexKey = []byte{0x71} // key for delegations by a validator ) // ParseDelegationKey parses given key and returns delagator, validator address bytes @@ -59,3 +60,14 @@ func GetHistoricalInfoKey(height int64) []byte { binary.BigEndian.PutUint64(heightBytes, uint64(height)) return append(HistoricalInfoKey, heightBytes...) } + +// GetDelegationsByValPrefixKey builds a prefix key bytes with the given validator address bytes. +func GetDelegationsByValPrefixKey(valAddr sdk.ValAddress) []byte { + return append(DelegationByValIndexKey, address.MustLengthPrefix(valAddr)...) +} + +// GetDelegationsByValKey creates the key for delegations by validator address +// VALUE: staking/Delegation +func GetDelegationsByValKey(valAddr sdk.ValAddress, delAddr sdk.AccAddress) []byte { + return append(GetDelegationsByValPrefixKey(valAddr), delAddr...) +} diff --git a/x/staking/migrations/v5/migrations_test.go b/x/staking/migrations/v5/migrations_test.go index 3cb91168e0..ef9ca6e8d7 100644 --- a/x/staking/migrations/v5/migrations_test.go +++ b/x/staking/migrations/v5/migrations_test.go @@ -112,7 +112,7 @@ func getValDelegations(ctx sdk.Context, cdc codec.Codec, storeKey storetypes.Sto var delegations []stakingtypes.Delegation store := ctx.KVStore(storeKey) - iterator := storetypes.KVStorePrefixIterator(store, stakingtypes.GetDelegationsByValPrefixKey(valAddr)) + iterator := storetypes.KVStorePrefixIterator(store, v5.GetDelegationsByValPrefixKey(valAddr)) for ; iterator.Valid(); iterator.Next() { var delegation stakingtypes.Delegation valAddr, delAddr, err := stakingtypes.ParseDelegationsByValKey(iterator.Key()) diff --git a/x/staking/migrations/v5/store.go b/x/staking/migrations/v5/store.go index 3b066d472a..569cc80c64 100644 --- a/x/staking/migrations/v5/store.go +++ b/x/staking/migrations/v5/store.go @@ -10,7 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking/types" ) func migrateDelegationsByValidatorIndex(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec) error { @@ -23,7 +22,7 @@ func migrateDelegationsByValidatorIndex(ctx sdk.Context, store storetypes.KVStor return err } - store.Set(types.GetDelegationsByValKey(val, del), []byte{}) + store.Set(GetDelegationsByValKey(val, del), []byte{}) } return nil