refactor(x/staking)!: remove comet deps (#20728)
This commit is contained in:
@@ -96,6 +96,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
* [#17655](https://github.com/cosmos/cosmos-sdk/pull/17655) `QueryHistoricalInfo` was adjusted to return `HistoricalRecord` and marked `Hist` as deprecated.
|
||||
* [#20295](https://github.com/cosmos/cosmos-sdk/pull/20295) `GetValidatorByConsAddr` now returns the Cosmos SDK `cryptotypes.Pubkey` instead of `cometcrypto.Publickey`. The caller is responsible to translate the returned value to the expected type.
|
||||
* Remove `CmtConsPublicKey()` and `TmConsPublicKey()` from `Validator` interface and as methods on the `Validator` struct.
|
||||
* [#20728](https://github.com/cosmos/cosmos-sdk/pull/20728) Remove `NewHistoricalInfo` and related functions to Historical Info & `GetCmtConsPubKey`, `ToCmtValidator` & `ToCmtValidators` as comet validator type is no longer used in the staking module.
|
||||
|
||||
### State Breaking changes
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
abci "github.com/cometbft/cometbft/abci/types"
|
||||
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
|
||||
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
cmtcrypto "github.com/cometbft/cometbft/crypto"
|
||||
cmttypes "github.com/cometbft/cometbft/types"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"cosmossdk.io/x/staking/types"
|
||||
|
||||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
||||
)
|
||||
|
||||
// GetCmtConsPubKey gets the validator's public key as a cmtcrypto.PubKey.
|
||||
func GetCmtConsPubKey(v types.Validator) (cmtcrypto.PubKey, error) {
|
||||
pk, err := v.ConsPubKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cryptocodec.ToCmtPubKeyInterface(pk)
|
||||
}
|
||||
|
||||
// ToCmtValidator casts an SDK validator to a CometBFT type Validator.
|
||||
func ToCmtValidator(v types.Validator, r math.Int) (*cmttypes.Validator, error) {
|
||||
tmPk, err := GetCmtConsPubKey(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cmttypes.NewValidator(tmPk, v.ConsensusPower(r)), nil
|
||||
}
|
||||
|
||||
// ToCmtValidators casts all validators to the corresponding CometBFT type.
|
||||
func ToCmtValidators(v types.Validators, r math.Int) ([]*cmttypes.Validator, error) {
|
||||
validators := make([]*cmttypes.Validator, len(v.Validators))
|
||||
var err error
|
||||
for i, val := range v.Validators {
|
||||
validators[i], err = ToCmtValidator(val, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return validators, nil
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
"cosmossdk.io/errors"
|
||||
"cosmossdk.io/math"
|
||||
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
)
|
||||
|
||||
// NewHistoricalInfo will create a historical information struct from header and valset
|
||||
// it will first sort valset before inclusion into historical info
|
||||
func NewHistoricalInfo(header cmtproto.Header, valSet Validators, powerReduction math.Int) HistoricalInfo {
|
||||
// Must sort in the same way that CometBFT does
|
||||
sort.SliceStable(valSet.Validators, func(i, j int) bool {
|
||||
return ValidatorsByVotingPower(valSet.Validators).Less(i, j, powerReduction)
|
||||
})
|
||||
|
||||
return HistoricalInfo{
|
||||
Header: header,
|
||||
Valset: valSet.Validators,
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateBasic will ensure HistoricalInfo is not nil and sorted
|
||||
func ValidateBasic(hi HistoricalInfo, valAc address.Codec) error {
|
||||
if len(hi.Valset) == 0 {
|
||||
return errors.Wrap(ErrInvalidHistoricalInfo, "validator set is empty")
|
||||
}
|
||||
|
||||
if !sort.IsSorted(Validators{Validators: hi.Valset, ValidatorCodec: valAc}) {
|
||||
return errors.Wrap(ErrInvalidHistoricalInfo, "validator set is not sorted by address")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
||||
func (hi HistoricalInfo) UnpackInterfaces(c codectypes.AnyUnpacker) error {
|
||||
for i := range hi.Valset {
|
||||
if err := hi.Valset[i].UnpackInterfaces(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package types_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/x/staking/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec/address"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
var header = cmtproto.Header{
|
||||
ChainID: "hello",
|
||||
Height: 5,
|
||||
}
|
||||
|
||||
func createValidators(t *testing.T) []types.Validator {
|
||||
t.Helper()
|
||||
return []types.Validator{
|
||||
newValidator(t, valAddr1, pk1),
|
||||
newValidator(t, valAddr2, pk2),
|
||||
newValidator(t, valAddr3, pk3),
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateBasic(t *testing.T) {
|
||||
validators := createValidators(t)
|
||||
hi := types.HistoricalInfo{
|
||||
Header: header,
|
||||
}
|
||||
ac := address.NewBech32Codec("cosmosvaloper")
|
||||
err := types.ValidateBasic(hi, ac)
|
||||
require.Error(t, err, "ValidateBasic passed on nil ValSet")
|
||||
|
||||
// Ensure validators are not sorted
|
||||
for sort.IsSorted(types.Validators{Validators: validators, ValidatorCodec: ac}) {
|
||||
rand.Shuffle(len(validators), func(i, j int) {
|
||||
validators[i], validators[j] = validators[j], validators[i]
|
||||
})
|
||||
}
|
||||
hi = types.HistoricalInfo{
|
||||
Header: header,
|
||||
Valset: validators,
|
||||
}
|
||||
err = types.ValidateBasic(hi, ac)
|
||||
require.Error(t, err, "ValidateBasic passed on unsorted ValSet")
|
||||
|
||||
hi = types.NewHistoricalInfo(header, types.Validators{Validators: validators, ValidatorCodec: ac}, sdk.DefaultPowerReduction)
|
||||
err = types.ValidateBasic(hi, ac)
|
||||
require.NoError(t, err, "ValidateBasic failed on valid HistoricalInfo")
|
||||
}
|
||||
@@ -5,18 +5,15 @@ import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
cmttypes "github.com/cometbft/cometbft/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"cosmossdk.io/x/staking/testutil"
|
||||
"cosmossdk.io/x/staking/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/codec/legacy"
|
||||
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
|
||||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
||||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -256,58 +253,6 @@ func TestValidatorsSortDeterminism(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Check SortCometBFT sorts the same as CometBFT
|
||||
func TestValidatorsSortCometBFT(t *testing.T) {
|
||||
vals := make([]types.Validator, 100)
|
||||
|
||||
for i := range vals {
|
||||
pk := ed25519.GenPrivKey().PubKey()
|
||||
pk2 := ed25519.GenPrivKey().PubKey()
|
||||
vals[i] = newValidator(t, sdk.ValAddress(pk2.Address()), pk)
|
||||
vals[i].Status = types.Bonded
|
||||
vals[i].Tokens = math.NewInt(rand.Int63())
|
||||
}
|
||||
// create some validators with the same power
|
||||
for i := 0; i < 10; i++ {
|
||||
vals[i].Tokens = math.NewInt(1000000)
|
||||
}
|
||||
|
||||
valz := types.Validators{Validators: vals, ValidatorCodec: address.NewBech32Codec("cosmosvaloper")}
|
||||
|
||||
// create expected CometBFT validators by converting to CometBFT then sorting
|
||||
expectedVals, err := testutil.ToCmtValidators(valz, sdk.DefaultPowerReduction)
|
||||
require.NoError(t, err)
|
||||
sort.Sort(cmttypes.ValidatorsByVotingPower(expectedVals))
|
||||
|
||||
// sort in SDK and then convert to CometBFT
|
||||
sort.SliceStable(valz.Validators, func(i, j int) bool {
|
||||
return types.ValidatorsByVotingPower(valz.Validators).Less(i, j, sdk.DefaultPowerReduction)
|
||||
})
|
||||
actualVals, err := testutil.ToCmtValidators(valz, sdk.DefaultPowerReduction)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, expectedVals, actualVals, "sorting in SDK is not the same as sorting in CometBFT")
|
||||
}
|
||||
|
||||
func TestValidatorToCmt(t *testing.T) {
|
||||
vals := types.Validators{}
|
||||
expected := make([]*cmttypes.Validator, 10)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
pk := ed25519.GenPrivKey().PubKey()
|
||||
val := newValidator(t, sdk.ValAddress(pk.Address()), pk)
|
||||
val.Status = types.Bonded
|
||||
val.Tokens = math.NewInt(rand.Int63())
|
||||
vals.Validators = append(vals.Validators, val)
|
||||
cmtPk, err := cryptocodec.ToCmtPubKeyInterface(pk)
|
||||
require.NoError(t, err)
|
||||
expected[i] = cmttypes.NewValidator(cmtPk, val.ConsensusPower(sdk.DefaultPowerReduction))
|
||||
}
|
||||
vs, err := testutil.ToCmtValidators(vals, sdk.DefaultPowerReduction)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, vs)
|
||||
}
|
||||
|
||||
func TestBondStatus(t *testing.T) {
|
||||
require.False(t, types.Unbonded == types.Bonded)
|
||||
require.False(t, types.Unbonded == types.Unbonding)
|
||||
|
||||
Reference in New Issue
Block a user