Skip to content

Commit

Permalink
refactor(x/staking): migrate ValidatorByConsAddr key to collections (
Browse files Browse the repository at this point in the history
  • Loading branch information
atheeshp authored Aug 3, 2023
1 parent a7977b8 commit 9443fc1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (x/staking) [#17260](https://github.com/cosmos/cosmos-sdk/pull/17260) Use collections for `ValidatorByConsAddr`:
* remove from `types`: `GetValidatorByConsAddrKey`
* (x/staking) [#17248](https://github.com/cosmos/cosmos-sdk/pull/17248) Use collections for `UnbondingType`.
* remove from `types`: `GetUnbondingTypeKey`.
* (client) [#17259](https://github.com/cosmos/cosmos-sdk/pull/17259) Remove deprecated `clientCtx.PrintObjectLegacy`. Use `clientCtx.PrintProto` or `clientCtx.PrintRaw` instead.
* (x/distribution) [#17115](https://github.com/cosmos/cosmos-sdk/pull/17115) Use collections for `PreviousProposer` and `ValidatorSlashEvents`:
* remove from `Keeper`: `GetPreviousProposerConsAddr`, `SetPreviousProposerConsAddr`, `GetValidatorHistoricalReferenceCount`, `GetValidatorSlashEvent`, `SetValidatorSlashEvent`.
* (x/slashing) [17063](https://github.com/cosmos/cosmos-sdk/pull/17063) Use collections for `HistoricalInfo`:
* (x/feegrant) [16535](https://github.com/cosmos/cosmos-sdk/pull/16535) Use collections for `FeeAllowance`, `FeeAllowanceQueue`.
* (x/staking) [17063](https://github.com/cosmos/cosmos-sdk/pull/17063) Use collections for `HistoricalInfo`:
* (x/feegrant) [#16535](https://github.com/cosmos/cosmos-sdk/pull/16535) Use collections for `FeeAllowance`, `FeeAllowanceQueue`.
* (x/staking) [#17063](https://github.com/cosmos/cosmos-sdk/pull/17063) Use collections for `HistoricalInfo`:
* remove `Keeper`: `GetHistoricalInfo`, `SetHistoricalInfo`,
* (x/staking) [17062](https://github.com/cosmos/cosmos-sdk/pull/17062) Use collections for `ValidatorUpdates`:
* (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`:
* (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`:
* remove `Keeper`: `SetLastTotalPower`, `GetLastTotalPower`
Expand Down
20 changes: 14 additions & 6 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"cosmossdk.io/collections"
collcodec "cosmossdk.io/collections/codec"
addresscodec "cosmossdk.io/core/address"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
Expand Down Expand Up @@ -32,12 +33,13 @@ type Keeper struct {
validatorAddressCodec addresscodec.Codec
consensusAddressCodec addresscodec.Codec

Schema collections.Schema
HistoricalInfo collections.Map[uint64, types.HistoricalInfo]
LastTotalPower collections.Item[math.Int]
ValidatorUpdates collections.Item[types.ValidatorUpdates]
DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte]
UnbondingType collections.Map[uint64, uint64]
Schema collections.Schema
HistoricalInfo collections.Map[uint64, types.HistoricalInfo]
LastTotalPower collections.Item[math.Int]
ValidatorUpdates collections.Item[types.ValidatorUpdates]
DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte]
ValidatorByConsensusAddress collections.Map[sdk.ConsAddress, sdk.ValAddress]
UnbondingType collections.Map[uint64, uint64]
}

// NewKeeper creates a new staking Keeper instance
Expand Down Expand Up @@ -87,6 +89,12 @@ func NewKeeper(
collections.PairKeyCodec(sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), sdk.AccAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
collections.BytesValue,
),
ValidatorByConsensusAddress: collections.NewMap(
sb, types.ValidatorsByConsAddrKey,
"validator_by_cons_addr",
sdk.LengthPrefixedAddressKey(sdk.ConsAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
collcodec.KeyToValueCodec(sdk.ValAddressKey),
),
UnbondingType: collections.NewMap(sb, types.UnbondingTypeKey, "unbonding_type", collections.Uint64Key, collections.Uint64Value),
}

Expand Down
12 changes: 6 additions & 6 deletions x/staking/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

gogotypes "github.com/cosmos/gogoproto/types"

"cosmossdk.io/collections"
corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
Expand Down Expand Up @@ -44,9 +45,8 @@ func (k Keeper) mustGetValidator(ctx context.Context, addr sdk.ValAddress) types

// GetValidatorByConsAddr gets a single validator by consensus address
func (k Keeper) GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) (validator types.Validator, err error) {
store := k.storeService.OpenKVStore(ctx)
opAddr, err := store.Get(types.GetValidatorByConsAddrKey(consAddr))
if err != nil {
opAddr, err := k.ValidatorByConsensusAddress.Get(ctx, consAddr)
if err != nil && !errors.Is(err, collections.ErrNotFound) {
return validator, err
}

Expand Down Expand Up @@ -79,8 +79,8 @@ func (k Keeper) SetValidatorByConsAddr(ctx context.Context, validator types.Vali
if err != nil {
return err
}
store := k.storeService.OpenKVStore(ctx)
return store.Set(types.GetValidatorByConsAddrKey(consPk), validator.GetOperator())

return k.ValidatorByConsensusAddress.Set(ctx, consPk, validator.GetOperator())
}

// SetValidatorByPowerIndex sets a validator by power index
Expand Down Expand Up @@ -219,7 +219,7 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err
return err
}

if err = store.Delete(types.GetValidatorByConsAddrKey(valConsAddr)); err != nil {
if err = k.ValidatorByConsensusAddress.Remove(ctx, valConsAddr); err != nil {
return err
}

Expand Down
18 changes: 16 additions & 2 deletions x/staking/migrations/v2/keys.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package v2

import "strconv"
import (
"strconv"

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

const (
// ModuleName is the name of the module
ModuleName = "staking"
)

var HistoricalInfoKey = []byte{0x50} // prefix for the historical info
var (
ValidatorsByConsAddrKey = []byte{0x22} // prefix for validators by consensus address
HistoricalInfoKey = []byte{0x50} // prefix for the historical info
)

// GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects.
func GetHistoricalInfoKey(height int64) []byte {
return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...)
}

// GetValidatorByConsAddrKey creates the key for the validator with pubkey
// VALUE: validator operator address ([]byte)
func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte {
return append(ValidatorsByConsAddrKey, address.MustLengthPrefix(addr)...)
}
2 changes: 1 addition & 1 deletion x/staking/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestStoreMigration(t *testing.T) {
{
"ValidatorsByConsAddrKey",
v1.GetValidatorByConsAddrKey(consAddr),
types.GetValidatorByConsAddrKey(consAddr),
v2.GetValidatorByConsAddrKey(consAddr),
},
{
"ValidatorsByPowerIndexKey",
Expand Down
12 changes: 3 additions & 9 deletions x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ var (
LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators
LastTotalPowerKey = collections.NewPrefix(18) // prefix for the total power

ValidatorsKey = []byte{0x21} // prefix for each key to a validator
ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey
ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power
ValidatorsKey = []byte{0x21} // prefix for each key to a validator
ValidatorsByConsAddrKey = collections.NewPrefix(34) // prefix for each key to a validator index, by pubkey
ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power

DelegationKey = []byte{0x31} // key for a delegation
UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation
Expand Down Expand Up @@ -84,12 +84,6 @@ func GetValidatorKey(operatorAddr sdk.ValAddress) []byte {
return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...)
}

// GetValidatorByConsAddrKey creates the key for the validator with pubkey
// VALUE: validator operator address ([]byte)
func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte {
return append(ValidatorsByConsAddrKey, address.MustLengthPrefix(addr)...)
}

// AddressFromValidatorsKey creates the validator operator address from ValidatorsKey
func AddressFromValidatorsKey(key []byte) []byte {
kv.AssertKeyAtLeastLength(key, 3)
Expand Down

0 comments on commit 9443fc1

Please sign in to comment.