Skip to content

Commit

Permalink
fix(x/staking): DelegationsByValidator migrations (backport #17154) (
Browse files Browse the repository at this point in the history
…#17156)

Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
  • Loading branch information
mergify[bot] and atheeshp authored Jul 27, 2023
1 parent 46be66e commit abd0bd5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
14 changes: 13 additions & 1 deletion x/staking/migrations/v5/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)

const (
Expand All @@ -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
Expand Down Expand Up @@ -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...)
}
2 changes: 1 addition & 1 deletion x/staking/migrations/v5/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions x/staking/migrations/v5/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit abd0bd5

Please sign in to comment.