Skip to content

Commit

Permalink
refactor(x/staking): migrate ValidatorUpdates to collections (#17062)
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 authored Jul 20, 2023
1 parent c6926b6 commit 535d711
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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`:
Expand Down
35 changes: 4 additions & 31 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion x/staking/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) (updates
}
}

valUpdates := types.ValidatorUpdates{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
}

Expand Down
4 changes: 2 additions & 2 deletions x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 535d711

Please sign in to comment.