Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(x/staking): migrate ValidatorByConsAddr key to collections #17260

Merged
merged 12 commits into from
Aug 3, 2023
18 changes: 13 additions & 5 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,11 +33,12 @@ 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]
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]
}

// NewKeeper creates a new staking Keeper instance
Expand Down Expand Up @@ -86,6 +88,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),
),
}

schema, err := sb.Build()
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 @@ -91,12 +91,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need an API breaking Changes changelog entry for this.

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