diff --git a/CHANGELOG.md b/CHANGELOG.md index a7a8b1c88296..734ee100dbfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,9 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/staking) [#17270](https://github.com/cosmos/cosmos-sdk/pull/17270) Use collections for `UnbondingDelegation`: + * remove from `types`: `GetUBDsKey` + * remove from `Keeper`: `IterateUnbondingDelegations`, `IterateDelegatorUnbondingDelegations` * (client/keys) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) `clientkeys.NewKeyOutput`, `MkConsKeyOutput`, `MkValKeyOutput`, `MkAccKeyOutput`, `MkAccKeysOutput` now take their corresponding address codec instead of using the global SDK config. * (x/staking) [#17336](https://github.com/cosmos/cosmos-sdk/pull/17336) Use collections for `RedelegationByValDstIndexKey`: * remove from `types`: `GetREDByValDstIndexKey`, `GetREDsToValDstIndexKey` diff --git a/simapp/export.go b/simapp/export.go index 9e40b65d985b..6676c644f699 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -7,6 +7,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + "cosmossdk.io/collections" storetypes "cosmossdk.io/store/types" servertypes "github.com/cosmos/cosmos-sdk/server/types" @@ -191,16 +192,20 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] } // iterate through unbonding delegations, reset creation height - err = app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) { - for i := range ubd.Entries { - ubd.Entries[i].CreationHeight = 0 - } - err = app.StakingKeeper.SetUnbondingDelegation(ctx, ubd) - if err != nil { - panic(err) - } - return false - }) + err = app.StakingKeeper.UnbondingDelegations.Walk( + ctx, + nil, + func(key collections.Pair[[]byte, []byte], ubd stakingtypes.UnbondingDelegation) (stop bool, err error) { + for i := range ubd.Entries { + ubd.Entries[i].CreationHeight = 0 + } + err = app.StakingKeeper.SetUnbondingDelegation(ctx, ubd) + if err != nil { + return true, err + } + return false, err + }, + ) if err != nil { panic(err) } diff --git a/x/staking/keeper/delegation.go b/x/staking/keeper/delegation.go index 7b67870b8e14..d5824cedcf89 100644 --- a/x/staking/keeper/delegation.go +++ b/x/staking/keeper/delegation.go @@ -130,23 +130,23 @@ func (k Keeper) RemoveDelegation(ctx context.Context, delegation types.Delegatio func (k Keeper) GetUnbondingDelegations(ctx context.Context, delegator sdk.AccAddress, maxRetrieve uint16) (unbondingDelegations []types.UnbondingDelegation, err error) { unbondingDelegations = make([]types.UnbondingDelegation, maxRetrieve) - store := k.storeService.OpenKVStore(ctx) - delegatorPrefixKey := types.GetUBDsKey(delegator) + i := 0 + rng := collections.NewPrefixedPairRange[[]byte, []byte](delegator) + err = k.UnbondingDelegations.Walk( + ctx, + rng, + func(key collections.Pair[[]byte, []byte], value types.UnbondingDelegation) (stop bool, err error) { + unbondingDelegations = append(unbondingDelegations, value) + i++ - iterator, err := store.Iterator(delegatorPrefixKey, storetypes.PrefixEndBytes(delegatorPrefixKey)) + if i >= int(maxRetrieve) { + return true, nil + } + return false, nil + }, + ) if err != nil { - return unbondingDelegations, err - } - defer iterator.Close() - - i := 0 - for ; iterator.Valid() && i < int(maxRetrieve); iterator.Next() { - unbondingDelegation, err := types.UnmarshalUBD(k.cdc, iterator.Value()) - if err != nil { - return unbondingDelegations, err - } - unbondingDelegations[i] = unbondingDelegation - i++ + return nil, err } return unbondingDelegations[:i], nil // trim if the array length < maxRetrieve @@ -154,18 +154,14 @@ func (k Keeper) GetUnbondingDelegations(ctx context.Context, delegator sdk.AccAd // GetUnbondingDelegation returns a unbonding delegation. func (k Keeper) GetUnbondingDelegation(ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (ubd types.UnbondingDelegation, err error) { - store := k.storeService.OpenKVStore(ctx) - key := types.GetUBDKey(delAddr, valAddr) - value, err := store.Get(key) + ubd, err = k.UnbondingDelegations.Get(ctx, collections.Join(delAddr.Bytes(), valAddr.Bytes())) if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return ubd, types.ErrNoUnbondingDelegation + } return ubd, err } - - if value == nil { - return ubd, types.ErrNoUnbondingDelegation - } - - return types.UnmarshalUBD(k.cdc, value) + return ubd, nil } // GetUnbondingDelegationsFromValidator returns all unbonding delegations from a @@ -195,63 +191,24 @@ func (k Keeper) GetUnbondingDelegationsFromValidator(ctx context.Context, valAdd return ubds, nil } -// IterateUnbondingDelegations iterates through all of the unbonding delegations. -func (k Keeper) IterateUnbondingDelegations(ctx context.Context, fn func(index int64, ubd types.UnbondingDelegation) (stop bool)) error { - store := k.storeService.OpenKVStore(ctx) - prefix := types.UnbondingDelegationKey - iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) - if err != nil { - return err - } - defer iterator.Close() - - for i := int64(0); iterator.Valid(); iterator.Next() { - ubd, err := types.UnmarshalUBD(k.cdc, iterator.Value()) - if err != nil { - return err - } - if stop := fn(i, ubd); stop { - break - } - i++ - } - - return nil -} - // GetDelegatorUnbonding returns the total amount a delegator has unbonding. func (k Keeper) GetDelegatorUnbonding(ctx context.Context, delegator sdk.AccAddress) (math.Int, error) { unbonding := math.ZeroInt() - err := k.IterateDelegatorUnbondingDelegations(ctx, delegator, func(ubd types.UnbondingDelegation) bool { - for _, entry := range ubd.Entries { - unbonding = unbonding.Add(entry.Balance) - } - return false - }) - return unbonding, err -} - -// IterateDelegatorUnbondingDelegations iterates through a delegator's unbonding delegations. -func (k Keeper) IterateDelegatorUnbondingDelegations(ctx context.Context, delegator sdk.AccAddress, cb func(ubd types.UnbondingDelegation) (stop bool)) error { - store := k.storeService.OpenKVStore(ctx) - prefix := types.GetUBDsKey(delegator) - iterator, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix)) + rng := collections.NewPrefixedPairRange[[]byte, []byte](delegator) + err := k.UnbondingDelegations.Walk( + ctx, + rng, + func(key collections.Pair[[]byte, []byte], ubd types.UnbondingDelegation) (stop bool, err error) { + for _, entry := range ubd.Entries { + unbonding = unbonding.Add(entry.Balance) + } + return false, nil + }, + ) if err != nil { - return err + return unbonding, err } - defer iterator.Close() - - for ; iterator.Valid(); iterator.Next() { - ubd, err := types.UnmarshalUBD(k.cdc, iterator.Value()) - if err != nil { - return err - } - if cb(ubd) { - break - } - } - - return nil + return unbonding, err } // GetDelegatorBonded returs the total amount a delegator has bonded. @@ -307,19 +264,17 @@ func (k Keeper) HasMaxUnbondingDelegationEntries(ctx context.Context, delegatorA // SetUnbondingDelegation sets the unbonding delegation and associated index. func (k Keeper) SetUnbondingDelegation(ctx context.Context, ubd types.UnbondingDelegation) error { + store := k.storeService.OpenKVStore(ctx) + delAddr, err := k.authKeeper.AddressCodec().StringToBytes(ubd.DelegatorAddress) if err != nil { return err } - - store := k.storeService.OpenKVStore(ctx) - bz := types.MustMarshalUBD(k.cdc, ubd) valAddr, err := k.validatorAddressCodec.StringToBytes(ubd.ValidatorAddress) if err != nil { return err } - key := types.GetUBDKey(delAddr, valAddr) - err = store.Set(key, bz) + err = k.UnbondingDelegations.Set(ctx, collections.Join(delAddr, valAddr), ubd) if err != nil { return err } @@ -329,23 +284,21 @@ func (k Keeper) SetUnbondingDelegation(ctx context.Context, ubd types.UnbondingD // RemoveUnbondingDelegation removes the unbonding delegation object and associated index. func (k Keeper) RemoveUnbondingDelegation(ctx context.Context, ubd types.UnbondingDelegation) error { - delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(ubd.DelegatorAddress) + store := k.storeService.OpenKVStore(ctx) + delAddr, err := k.authKeeper.AddressCodec().StringToBytes(ubd.DelegatorAddress) if err != nil { return err } - - store := k.storeService.OpenKVStore(ctx) - addr, err := k.validatorAddressCodec.StringToBytes(ubd.ValidatorAddress) + valAddr, err := k.validatorAddressCodec.StringToBytes(ubd.ValidatorAddress) if err != nil { return err } - key := types.GetUBDKey(delegatorAddress, addr) - err = store.Delete(key) + err = k.UnbondingDelegations.Remove(ctx, collections.Join(delAddr, valAddr)) if err != nil { return err } - return store.Delete(types.GetUBDByValIndexKey(delegatorAddress, addr)) + return store.Delete(types.GetUBDByValIndexKey(delAddr, valAddr)) } // SetUnbondingDelegationEntry adds an entry to the unbonding delegation at diff --git a/x/staking/keeper/genesis.go b/x/staking/keeper/genesis.go index cb0282760cef..4cd505ba6d1d 100644 --- a/x/staking/keeper/genesis.go +++ b/x/staking/keeper/genesis.go @@ -6,6 +6,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" + "cosmossdk.io/collections" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -214,10 +215,14 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { var unbondingDelegations []types.UnbondingDelegation - err := k.IterateUnbondingDelegations(ctx, func(_ int64, ubd types.UnbondingDelegation) (stop bool) { - unbondingDelegations = append(unbondingDelegations, ubd) - return false - }) + err := k.UnbondingDelegations.Walk( + ctx, + nil, + func(key collections.Pair[[]byte, []byte], value types.UnbondingDelegation) (stop bool, err error) { + unbondingDelegations = append(unbondingDelegations, value) + return false, nil + }, + ) if err != nil { panic(err) } diff --git a/x/staking/keeper/grpc_query.go b/x/staking/keeper/grpc_query.go index 961221998d6b..ca87fcacc743 100644 --- a/x/staking/keeper/grpc_query.go +++ b/x/staking/keeper/grpc_query.go @@ -358,16 +358,16 @@ func (k Querier) DelegatorUnbondingDelegations(ctx context.Context, req *types.Q return nil, err } - store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) - unbStore := prefix.NewStore(store, types.GetUBDsKey(delAddr)) - pageRes, err := query.Paginate(unbStore, req.Pagination, func(key, value []byte) error { - unbond, err := types.UnmarshalUBD(k.cdc, value) - if err != nil { - return err - } - unbondingDelegations = append(unbondingDelegations, unbond) - return nil - }) + _, pageRes, err := query.CollectionPaginate( + ctx, + k.UnbondingDelegations, + req.Pagination, + func(key collections.Pair[[]byte, []byte], value types.UnbondingDelegation) (types.UnbondingDelegation, error) { + unbondingDelegations = append(unbondingDelegations, value) + return value, nil + }, + query.WithCollectionPaginationPairPrefix[[]byte, []byte](delAddr), + ) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } diff --git a/x/staking/keeper/invariants.go b/x/staking/keeper/invariants.go index b23922643ed1..855f32ed8942 100644 --- a/x/staking/keeper/invariants.go +++ b/x/staking/keeper/invariants.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + "cosmossdk.io/collections" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -72,12 +73,16 @@ func ModuleAccountInvariants(k *Keeper) sdk.Invariant { panic(err) } - err = k.IterateUnbondingDelegations(ctx, func(_ int64, ubd types.UnbondingDelegation) bool { - for _, entry := range ubd.Entries { - notBonded = notBonded.Add(entry.Balance) - } - return false - }) + err = k.UnbondingDelegations.Walk( + ctx, + nil, + func(key collections.Pair[[]byte, []byte], ubd types.UnbondingDelegation) (stop bool, err error) { + for _, entry := range ubd.Entries { + notBonded = notBonded.Add(entry.Balance) + } + return false, nil + }, + ) if err != nil { panic(err) } diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 8d7683dfd211..6ff107627187 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -44,6 +44,7 @@ type Keeper struct { Redelegations collections.Map[collections.Triple[[]byte, []byte, []byte], types.Redelegation] Delegations collections.Map[collections.Pair[sdk.AccAddress, sdk.ValAddress], types.Delegation] UnbondingIndex collections.Map[uint64, []byte] + UnbondingDelegations collections.Map[collections.Pair[[]byte, []byte], types.UnbondingDelegation] RedelegationsByValDst collections.Map[collections.Triple[[]byte, []byte, []byte], []byte] RedelegationsByValSrc collections.Map[collections.Triple[[]byte, []byte, []byte], []byte] } @@ -145,6 +146,14 @@ func NewKeeper( ), collections.BytesValue, ), + UnbondingDelegations: collections.NewMap( + sb, types.UnbondingDelegationKey, + "unbonding_delegation", + collections.PairKeyCodec( + collections.BytesKey, + sdk.LengthPrefixedBytesKey, // sdk.LengthPrefixedBytesKey is needed to retain state compatibility + ), + codec.CollValue[types.UnbondingDelegation](cdc)), } schema, err := sb.Build() diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index 86b58bf9cbd1..8edb42ef5a6b 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "testing" + "time" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttime "github.com/cometbft/cometbft/types/time" @@ -13,6 +14,7 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" @@ -42,16 +44,19 @@ type KeeperTestSuite struct { queryClient stakingtypes.QueryClient msgServer stakingtypes.MsgServer key *storetypes.KVStoreKey + cdc codec.Codec } func (s *KeeperTestSuite) SetupTest() { require := s.Require() key := storetypes.NewKVStoreKey(stakingtypes.StoreKey) + s.key = key storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) s.key = key ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()}) encCfg := moduletestutil.MakeTestEncodingConfig() + s.cdc = encCfg.Codec ctrl := gomock.NewController(s.T()) accountKeeper := stakingtestutil.NewMockAccountKeeper(ctrl) @@ -228,3 +233,65 @@ func (s *KeeperTestSuite) TestDstRedelegationsMigrationToColls() { func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } + +// getUBDKey creates the key for an unbonding delegation by delegator and validator addr +// VALUE: staking/UnbondingDelegation +func getUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { + unbondingDelegationKey := []byte{0x32} + return append(append(unbondingDelegationKey, addresstypes.MustLengthPrefix(delAddr)...), addresstypes.MustLengthPrefix(valAddr)...) +} + +func (s *KeeperTestSuite) TestUnbondingDelegationsMigrationToColls() { + s.SetupTest() + + delAddrs, valAddrs := createValAddrs(100) + err := testutil.DiffCollectionsMigration( + s.ctx, + s.key, + 100, + func(i int64) { + ubd := stakingtypes.UnbondingDelegation{ + DelegatorAddress: delAddrs[i].String(), + ValidatorAddress: valAddrs[i].String(), + Entries: []stakingtypes.UnbondingDelegationEntry{ + { + CreationHeight: i, + CompletionTime: time.Unix(i, 0).UTC(), + Balance: math.NewInt(i), + UnbondingId: uint64(i), + }, + }, + } + bz := stakingtypes.MustMarshalUBD(s.cdc, ubd) + s.ctx.KVStore(s.key).Set(getUBDKey(delAddrs[i], valAddrs[i]), bz) + s.ctx.KVStore(s.key).Set(stakingtypes.GetUBDByValIndexKey(delAddrs[i], valAddrs[i]), []byte{}) + }, + "d03ca412f3f6849b5148a2ca49ac2555f65f90b7fab6a289575ed337f15c0f4b", + ) + s.Require().NoError(err) + + err = testutil.DiffCollectionsMigration( + s.ctx, + s.key, + 100, + func(i int64) { + ubd := stakingtypes.UnbondingDelegation{ + DelegatorAddress: delAddrs[i].String(), + ValidatorAddress: valAddrs[i].String(), + Entries: []stakingtypes.UnbondingDelegationEntry{ + { + CreationHeight: i, + CompletionTime: time.Unix(i, 0).UTC(), + Balance: math.NewInt(i), + UnbondingId: uint64(i), + }, + }, + } + err := s.stakingKeeper.SetUnbondingDelegation(s.ctx, ubd) + s.Require().NoError(err) + }, + "d03ca412f3f6849b5148a2ca49ac2555f65f90b7fab6a289575ed337f15c0f4b", + ) + + s.Require().NoError(err) +} diff --git a/x/staking/keeper/query_utils.go b/x/staking/keeper/query_utils.go index 65efcbe86709..ff0499d28783 100644 --- a/x/staking/keeper/query_utils.go +++ b/x/staking/keeper/query_utils.go @@ -4,7 +4,6 @@ import ( "context" "cosmossdk.io/collections" - storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -85,24 +84,18 @@ func (k Keeper) GetAllDelegatorDelegations(ctx context.Context, delegator sdk.Ac func (k Keeper) GetAllUnbondingDelegations(ctx context.Context, delegator sdk.AccAddress) ([]types.UnbondingDelegation, error) { unbondingDelegations := make([]types.UnbondingDelegation, 0) - store := k.storeService.OpenKVStore(ctx) - delegatorPrefixKey := types.GetUBDsKey(delegator) - - iterator, err := store.Iterator(delegatorPrefixKey, storetypes.PrefixEndBytes(delegatorPrefixKey)) // smallest to largest + rng := collections.NewPrefixUntilPairRange[[]byte, []byte](delegator) + err := k.UnbondingDelegations.Walk( + ctx, + rng, + func(key collections.Pair[[]byte, []byte], value types.UnbondingDelegation) (stop bool, err error) { + unbondingDelegations = append(unbondingDelegations, value) + return false, nil + }, + ) if err != nil { return nil, err } - defer iterator.Close() - - for i := 0; iterator.Valid(); iterator.Next() { - unbondingDelegation, err := types.UnmarshalUBD(k.cdc, iterator.Value()) - if err != nil { - return nil, err - } - unbondingDelegations = append(unbondingDelegations, unbondingDelegation) - i++ - } - return unbondingDelegations, nil } diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index bd874392b86e..b3507526b207 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -45,9 +45,7 @@ func (k Keeper) SetUnbondingType(ctx context.Context, id uint64, unbondingType t // GetUnbondingDelegationByUnbondingID returns a unbonding delegation that has an unbonding delegation entry with a certain ID func (k Keeper) GetUnbondingDelegationByUnbondingID(ctx context.Context, id uint64) (ubd types.UnbondingDelegation, err error) { - store := k.storeService.OpenKVStore(ctx) - - ubdKey, err := k.UnbondingIndex.Get(ctx, id) + ubdKey, err := k.UnbondingIndex.Get(ctx, id) // ubdKey => [UnbondingDelegationKey(Prefix)+len(delAddr)+delAddr+len(valAddr)+valAddr] if err != nil { if errors.Is(err, collections.ErrNotFound) { return types.UnbondingDelegation{}, types.ErrNoUnbondingDelegation @@ -59,18 +57,16 @@ func (k Keeper) GetUnbondingDelegationByUnbondingID(ctx context.Context, id uint return types.UnbondingDelegation{}, types.ErrNoUnbondingDelegation } - value, err := store.Get(ubdKey) - if err != nil { - return types.UnbondingDelegation{}, err - } + // remove prefix bytes and length bytes (since ubdKey obtained is prefixed by UnbondingDelegationKey prefix and length of the address) + delAddr := ubdKey[2 : (len(ubdKey)/2)+1] + // remove prefix length bytes + valAddr := ubdKey[2+len(ubdKey)/2:] - if value == nil { - return types.UnbondingDelegation{}, types.ErrNoUnbondingDelegation - } - - ubd, err = types.UnmarshalUBD(k.cdc, value) - // An error here means that what we got wasn't the right type + ubd, err = k.UnbondingDelegations.Get(ctx, collections.Join(delAddr, valAddr)) if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return types.UnbondingDelegation{}, types.ErrNoUnbondingDelegation + } return types.UnbondingDelegation{}, err } diff --git a/x/staking/migrations/v2/store_test.go b/x/staking/migrations/v2/store_test.go index 783a55d04e8b..61073e449e42 100644 --- a/x/staking/migrations/v2/store_test.go +++ b/x/staking/migrations/v2/store_test.go @@ -13,6 +13,7 @@ import ( sdktestuil "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" + sdkaddress "github.com/cosmos/cosmos-sdk/types/address" v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2" "github.com/cosmos/cosmos-sdk/x/staking/testutil" @@ -75,7 +76,7 @@ func TestStoreMigration(t *testing.T) { { "UnbondingDelegationKey", v1.GetUBDKey(addr4, valAddr1), - types.GetUBDKey(addr4, valAddr1), + unbondingKey(addr4, valAddr1), }, { "UnbondingDelegationByValIndexKey", @@ -139,3 +140,7 @@ func TestStoreMigration(t *testing.T) { }) } } + +func unbondingKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { + return append(append(types.UnbondingDelegationKey, sdkaddress.MustLengthPrefix(delAddr)...), sdkaddress.MustLengthPrefix(valAddr)...) +} diff --git a/x/staking/simulation/decoder_test.go b/x/staking/simulation/decoder_test.go index 70b84e365513..771a47a7318d 100644 --- a/x/staking/simulation/decoder_test.go +++ b/x/staking/simulation/decoder_test.go @@ -3,13 +3,11 @@ package simulation_test import ( "fmt" "testing" - "time" "github.com/stretchr/testify/require" "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" @@ -20,18 +18,15 @@ import ( var ( delPk1 = ed25519.GenPrivKey().PubKey() - delAddr1 = sdk.AccAddress(delPk1.Address()) valAddr1 = sdk.ValAddress(delPk1.Address()) ) func TestDecodeStore(t *testing.T) { cdc := testutil.MakeTestEncodingConfig().Codec dec := simulation.NewDecodeStore(cdc) - bondTime := time.Now().UTC() val, err := types.NewValidator(valAddr1.String(), delPk1, types.NewDescription("test", "test", "test", "test", "test")) require.NoError(t, err) - ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, math.OneInt(), 1, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos")) oneIntBz, err := math.OneInt().Marshal() require.NoError(t, err) @@ -40,7 +35,6 @@ func TestDecodeStore(t *testing.T) { {Key: types.LastTotalPowerKey, Value: oneIntBz}, {Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshal(&val)}, {Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()}, - {Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&ubd)}, {Key: []byte{0x99}, Value: []byte{0x99}}, }, } @@ -52,7 +46,6 @@ func TestDecodeStore(t *testing.T) { {"LastTotalPower", fmt.Sprintf("%v\n%v", math.OneInt(), math.OneInt())}, {"Validator", fmt.Sprintf("%v\n%v", val, val)}, {"LastValidatorPower/ValidatorsByConsAddr/ValidatorsByPowerIndex", fmt.Sprintf("%v\n%v", valAddr1, valAddr1)}, - {"UnbondingDelegation", fmt.Sprintf("%v\n%v", ubd, ubd)}, {"other", ""}, } for i, tt := range tests { diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 1e45f0dbde72..f5cad4d7fafe 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -40,7 +40,7 @@ var ( ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power DelegationKey = collections.NewPrefix(49) // key for a delegation - UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation + UnbondingDelegationKey = collections.NewPrefix(50) // key for an unbonding-delegation UnbondingDelegationByValIndexKey = []byte{0x33} // prefix for each key for an unbonding-delegation, by validator operator RedelegationKey = collections.NewPrefix(52) // key for a redelegation RedelegationByValSrcIndexKey = collections.NewPrefix(53) // prefix for each key for an redelegation, by source validator operator @@ -193,7 +193,7 @@ func ParseValidatorQueueKey(bz []byte) (time.Time, int64, error) { // GetUBDKey creates the key for an unbonding delegation by delegator and validator addr // VALUE: staking/UnbondingDelegation func GetUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { - return append(GetUBDsKey(delAddr.Bytes()), address.MustLengthPrefix(valAddr)...) + return append(append(UnbondingDelegationKey, address.MustLengthPrefix(delAddr)...), address.MustLengthPrefix(valAddr)...) } // GetUBDByValIndexKey creates the index-key for an unbonding delegation, stored by validator-index @@ -213,12 +213,7 @@ func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { kv.AssertKeyAtLeastLength(addrs, 3+int(valAddrLen)) delAddr := addrs[valAddrLen+2:] - return GetUBDKey(delAddr, valAddr) -} - -// GetUBDsKey creates the prefix for all unbonding delegations from a delegator -func GetUBDsKey(delAddr sdk.AccAddress) []byte { - return append(UnbondingDelegationKey, address.MustLengthPrefix(delAddr)...) + return append(append(UnbondingDelegationKey, address.MustLengthPrefix(delAddr)...), address.MustLengthPrefix(valAddr)...) } // GetUBDsByValIndexKey creates the prefix keyspace for the indexes of unbonding delegations for a validator