From adda899de89996b6e3118d359364a4225556b838 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 19 Jul 2023 10:56:25 +0530 Subject: [PATCH 1/3] migrate ValidatorUpdates to collections --- x/staking/keeper/keeper.go | 35 ++++------------------------ x/staking/keeper/val_state_change.go | 5 +++- x/staking/types/keys.go | 4 ++-- 3 files changed, 10 insertions(+), 34 deletions(-) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 8d4d85a786fb..35b04a8deb90 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -4,8 +4,6 @@ import ( "context" "fmt" - abci "github.com/cometbft/cometbft/abci/types" - "cosmossdk.io/collections" addresscodec "cosmossdk.io/core/address" storetypes "cosmossdk.io/core/store" @@ -34,8 +32,9 @@ type Keeper struct { validatorAddressCodec addresscodec.Codec consensusAddressCodec addresscodec.Codec - Schema collections.Schema - LastTotalPower collections.Item[math.Int] + Schema collections.Schema + LastTotalPower collections.Item[math.Int] + ValidatorUpdates collections.Item[types.ValidatorUpdates] } // NewKeeper creates a new staking Keeper instance @@ -77,6 +76,7 @@ func NewKeeper( validatorAddressCodec: validatorAddressCodec, consensusAddressCodec: consensusAddressCodec, LastTotalPower: collections.NewItem(sb, types.LastTotalPowerKey, "last_total_power", sdk.IntValue), + ValidatorUpdates: collections.NewItem(sb, types.ValidatorUpdatesKey, "validator_updates", codec.CollValue[types.ValidatorUpdates](cdc)), } schema, err := sb.Build() @@ -127,30 +127,3 @@ func (k Keeper) ValidatorAddressCodec() addresscodec.Codec { func (k Keeper) ConsensusAddressCodec() addresscodec.Codec { return k.consensusAddressCodec } - -// SetValidatorUpdates sets the ABCI validator power updates for the current block. -func (k Keeper) SetValidatorUpdates(ctx context.Context, valUpdates []abci.ValidatorUpdate) error { - store := k.storeService.OpenKVStore(ctx) - bz, err := k.cdc.Marshal(&types.ValidatorUpdates{Updates: valUpdates}) - if err != nil { - return err - } - return store.Set(types.ValidatorUpdatesKey, bz) -} - -// GetValidatorUpdates returns the ABCI validator power updates within the current block. -func (k Keeper) GetValidatorUpdates(ctx context.Context) ([]abci.ValidatorUpdate, error) { - store := k.storeService.OpenKVStore(ctx) - bz, err := store.Get(types.ValidatorUpdatesKey) - if err != nil { - return nil, err - } - - var valUpdates types.ValidatorUpdates - err = k.cdc.Unmarshal(bz, &valUpdates) - if err != nil { - return nil, err - } - - return valUpdates.Updates, nil -} diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index c59328554b7c..d9477a3b4fac 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -256,8 +256,11 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) (updates } } + var valUpdates types.ValidatorUpdates + valUpdates.Updates = updates + // set the list of validator updates - if err = k.SetValidatorUpdates(ctx, updates); err != nil { + if err = k.ValidatorUpdates.Set(ctx, valUpdates); err != nil { return nil, err } diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 9b1c7e63ec06..57689058c9ec 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -53,8 +53,8 @@ var ( RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue ValidatorQueueKey = []byte{0x43} // prefix for the timestamps in validator queue - HistoricalInfoKey = []byte{0x50} // prefix for the historical info - ValidatorUpdatesKey = []byte{0x61} // prefix for the end block validator updates key + HistoricalInfoKey = []byte{0x50} // prefix for the historical info + ValidatorUpdatesKey = collections.NewPrefix(97) // prefix for the end block validator updates key ParamsKey = []byte{0x51} // prefix for parameters for module x/staking From 774a06adc37c0fa36630a71d525e26ecc069e182 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 19 Jul 2023 15:24:49 +0530 Subject: [PATCH 2/3] add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b2c31a4a966..0e893ca14d9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/staking) [17062](https://github.com/cosmos/cosmos-sdk/pull/17062) Use collections for `ValidatorUpdates`: + * remove `Keeper`: `SetValidatorUpdates`, `GetValidatorUpdates` * (x/slashing) [17023](https://github.com/cosmos/cosmos-sdk/pull/17023) Use collections for `ValidatorSigningInfo`: * remove `Keeper`: `SetValidatorSigningInfo`, `GetValidatorSigningInfo`, `IterateValidatorSigningInfos` * (x/staking) [#17026](https://github.com/cosmos/cosmos-sdk/pull/17026) Use collections for `LastTotalPower`: From 3434a221120df70961e852a81d5d9da1801eb148 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 20 Jul 2023 10:42:58 +0530 Subject: [PATCH 3/3] address review comment --- x/staking/keeper/val_state_change.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index d9477a3b4fac..43f1f463353d 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -256,9 +256,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) (updates } } - var valUpdates types.ValidatorUpdates - valUpdates.Updates = updates - + valUpdates := types.ValidatorUpdates{Updates: updates} // set the list of validator updates if err = k.ValidatorUpdates.Set(ctx, valUpdates); err != nil { return nil, err