refactor!(staking): remove comet crypto as a dep (#20295)

This commit is contained in:
Marko 2024-05-07 12:27:47 +02:00 committed by GitHub
parent 48d5ab41b9
commit fc84ff6f40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 47 additions and 58 deletions

View File

@ -38,6 +38,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
"github.com/cosmos/cosmos-sdk/baseapp/testutil/mock"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -1799,8 +1800,11 @@ func TestABCI_PrepareProposal_VoteExtensions(t *testing.T) {
},
}
pk, err := cryptocodec.FromCmtProtoPublicKey(tmPk)
require.NoError(t, err)
consAddr := sdk.ConsAddress(addr.String())
valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), consAddr.Bytes()).Return(tmPk, nil)
valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), consAddr.Bytes()).Return(pk, nil)
// set up baseapp
prepareOpt := func(bapp *baseapp.BaseApp) {
@ -1828,7 +1832,7 @@ func TestABCI_PrepareProposal_VoteExtensions(t *testing.T) {
suite := NewBaseAppSuite(t, prepareOpt)
_, err := suite.baseApp.InitChain(&abci.InitChainRequest{
_, err = suite.baseApp.InitChain(&abci.InitChainRequest{
InitialHeight: 1,
ConsensusParams: &cmtproto.ConsensusParams{
Feature: &cmtproto.FeatureParams{
@ -2092,7 +2096,10 @@ func TestBaseApp_VoteExtensions(t *testing.T) {
Secp256K1: pubKey.Bytes(),
},
}
valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), val).Return(tmPk, nil)
pk, err := cryptocodec.FromCmtProtoPublicKey(tmPk)
require.NoError(t, err)
valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), val).Return(pk, nil)
}
baseappOpts := func(app *baseapp.BaseApp) {

View File

@ -8,7 +8,6 @@ import (
"github.com/cockroachdb/errors"
abci "github.com/cometbft/cometbft/abci/types"
cmtprotocrypto "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cryptoenc "github.com/cometbft/cometbft/crypto/encoding"
cmttypes "github.com/cometbft/cometbft/types"
@ -17,6 +16,8 @@ import (
"cosmossdk.io/core/comet"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
)
@ -26,7 +27,7 @@ type (
// extension signatures. Typically, this will be implemented by the x/staking
// module, which has knowledge of the CometBFT public key.
ValidatorStore interface {
GetPubKeyByConsAddr(context.Context, sdk.ConsAddress) (cmtprotocrypto.PublicKey, error)
GetPubKeyByConsAddr(context.Context, sdk.ConsAddress) (cryptotypes.PubKey, error)
}
// GasTx defines the contract that a transaction with a gas limit must implement.
@ -110,7 +111,12 @@ func ValidateVoteExtensions(
return fmt.Errorf("failed to get validator %X public key: %w", valConsAddr, err)
}
cmtPubKey, err := cryptoenc.PubKeyFromProto(pubKeyProto)
cmtpk, err := cryptocodec.ToCmtProtoPublicKey(pubKeyProto)
if err != nil {
return fmt.Errorf("failed to convert validator %X public key: %w", valConsAddr, err)
}
cmtPubKey, err := cryptoenc.PubKeyFromProto(cmtpk)
if err != nil {
return fmt.Errorf("failed to convert validator %X public key: %w", valConsAddr, err)
}

View File

@ -28,6 +28,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp/testutil/mock"
"github.com/cosmos/cosmos-sdk/client"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -93,9 +94,11 @@ func NewABCIUtilsTestSuite(t *testing.T) *ABCIUtilsTestSuite {
s.valStore = valStore
// set up mock
s.valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), s.vals[0].consAddr.Bytes()).Return(s.vals[0].tmPk, nil).AnyTimes()
s.valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), s.vals[1].consAddr.Bytes()).Return(s.vals[1].tmPk, nil).AnyTimes()
s.valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), s.vals[2].consAddr.Bytes()).Return(s.vals[2].tmPk, nil).AnyTimes()
for _, val := range s.vals {
pk, err := cryptocodec.FromCmtProtoPublicKey(val.tmPk)
require.NoError(t, err)
valStore.EXPECT().GetPubKeyByConsAddr(gomock.Any(), val.consAddr.Bytes()).Return(pk, nil).AnyTimes()
}
// create context
s.ctx = sdk.Context{}.WithConsensusParams(cmtproto.ConsensusParams{

View File

@ -8,8 +8,8 @@ import (
context "context"
reflect "reflect"
v1 "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
types "github.com/cosmos/cosmos-sdk/types"
types "github.com/cosmos/cosmos-sdk/crypto/types"
types0 "github.com/cosmos/cosmos-sdk/types"
gomock "github.com/golang/mock/gomock"
)
@ -37,10 +37,10 @@ func (m *MockValidatorStore) EXPECT() *MockValidatorStoreMockRecorder {
}
// GetPubKeyByConsAddr mocks base method.
func (m *MockValidatorStore) GetPubKeyByConsAddr(arg0 context.Context, arg1 types.ConsAddress) (v1.PublicKey, error) {
func (m *MockValidatorStore) GetPubKeyByConsAddr(arg0 context.Context, arg1 types0.ConsAddress) (types.PubKey, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetPubKeyByConsAddr", arg0, arg1)
ret0, _ := ret[0].(v1.PublicKey)
ret0, _ := ret[0].(types.PubKey)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -112,7 +112,7 @@ func (m *MockProposalTxVerifier) EXPECT() *MockProposalTxVerifierMockRecorder {
}
// PrepareProposalVerifyTx mocks base method.
func (m *MockProposalTxVerifier) PrepareProposalVerifyTx(tx types.Tx) ([]byte, error) {
func (m *MockProposalTxVerifier) PrepareProposalVerifyTx(tx types0.Tx) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "PrepareProposalVerifyTx", tx)
ret0, _ := ret[0].([]byte)
@ -127,10 +127,10 @@ func (mr *MockProposalTxVerifierMockRecorder) PrepareProposalVerifyTx(tx interfa
}
// ProcessProposalVerifyTx mocks base method.
func (m *MockProposalTxVerifier) ProcessProposalVerifyTx(txBz []byte) (types.Tx, error) {
func (m *MockProposalTxVerifier) ProcessProposalVerifyTx(txBz []byte) (types0.Tx, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ProcessProposalVerifyTx", txBz)
ret0, _ := ret[0].(types.Tx)
ret0, _ := ret[0].(types0.Tx)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -142,10 +142,10 @@ func (mr *MockProposalTxVerifierMockRecorder) ProcessProposalVerifyTx(txBz inter
}
// TxDecode mocks base method.
func (m *MockProposalTxVerifier) TxDecode(txBz []byte) (types.Tx, error) {
func (m *MockProposalTxVerifier) TxDecode(txBz []byte) (types0.Tx, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "TxDecode", txBz)
ret0, _ := ret[0].(types.Tx)
ret0, _ := ret[0].(types0.Tx)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -157,7 +157,7 @@ func (mr *MockProposalTxVerifierMockRecorder) TxDecode(txBz interface{}) *gomock
}
// TxEncode mocks base method.
func (m *MockProposalTxVerifier) TxEncode(tx types.Tx) ([]byte, error) {
func (m *MockProposalTxVerifier) TxEncode(tx types0.Tx) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "TxEncode", tx)
ret0, _ := ret[0].([]byte)
@ -207,7 +207,7 @@ func (mr *MockTxSelectorMockRecorder) Clear() *gomock.Call {
}
// SelectTxForProposal mocks base method.
func (m *MockTxSelector) SelectTxForProposal(ctx context.Context, maxTxBytes, maxBlockGas uint64, memTx types.Tx, txBz []byte) bool {
func (m *MockTxSelector) SelectTxForProposal(ctx context.Context, maxTxBytes, maxBlockGas uint64, memTx types0.Tx, txBz []byte) bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SelectTxForProposal", ctx, maxTxBytes, maxBlockGas, memTx, txBz)
ret0, _ := ret[0].(bool)

View File

@ -1,8 +1,6 @@
package types
import (
cmtprotocrypto "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
"cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
@ -87,7 +85,6 @@ type ValidatorI interface {
IsUnbonding() bool // check if has status unbonding
GetOperator() string // operator address to receive/return validators coins
ConsPubKey() (cryptotypes.PubKey, error) // validation consensus pubkey (cryptotypes.PubKey)
TmConsPublicKey() (cmtprotocrypto.PublicKey, error) // validation consensus pubkey (CometBFT)
GetConsAddr() ([]byte, error) // validation consensus address
GetTokens() math.Int // validation tokens
GetBondedTokens() math.Int // validator bonded tokens

View File

@ -94,6 +94,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* remove `Keeper`: `SetLastTotalPower`, `GetLastTotalPower`
* [#17335](https://github.com/cosmos/cosmos-sdk/pull/17335) Remove usage of `"cosmossdk.io/x/staking/types".Infraction_*` in favour of `"cosmossdk.io/api/cosmos/staking/v1beta1".Infraction_` in order to remove dependency between modules on staking
* [#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.
### State Breaking changes

View File

@ -7,7 +7,6 @@ import (
"fmt"
"time"
cmtprotocrypto "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
gogotypes "github.com/cosmos/gogoproto/types"
"cosmossdk.io/collections"
@ -17,6 +16,7 @@ import (
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/staking/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -594,15 +594,16 @@ func (k Keeper) IsValidatorJailed(ctx context.Context, addr sdk.ConsAddress) (bo
}
// GetPubKeyByConsAddr returns the consensus public key by consensus address.
func (k Keeper) GetPubKeyByConsAddr(ctx context.Context, addr sdk.ConsAddress) (cmtprotocrypto.PublicKey, error) {
// Caller receives a Cosmos SDK Pubkey type and must cast it to a comet type
func (k Keeper) GetPubKeyByConsAddr(ctx context.Context, addr sdk.ConsAddress) (cryptotypes.PubKey, error) {
v, err := k.GetValidatorByConsAddr(ctx, addr)
if err != nil {
return cmtprotocrypto.PublicKey{}, err
return nil, err
}
pubkey, err := v.CmtConsPublicKey()
pubkey, err := v.ConsPubKey()
if err != nil {
return cmtprotocrypto.PublicKey{}, err
return nil, err
}
return pubkey, nil

View File

@ -13,7 +13,6 @@ import (
math "cosmossdk.io/math"
types "cosmossdk.io/x/consensus/types"
types0 "cosmossdk.io/x/staking/types"
v1 "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
types1 "github.com/cosmos/cosmos-sdk/crypto/types"
types2 "github.com/cosmos/cosmos-sdk/types"
gomock "github.com/golang/mock/gomock"
@ -312,10 +311,10 @@ func (mr *MockValidatorSetMockRecorder) Delegation(arg0, arg1, arg2 interface{})
}
// GetPubKeyByConsAddr mocks base method.
func (m *MockValidatorSet) GetPubKeyByConsAddr(arg0 context.Context, arg1 types2.ConsAddress) (v1.PublicKey, error) {
func (m *MockValidatorSet) GetPubKeyByConsAddr(arg0 context.Context, arg1 types2.ConsAddress) (types1.PubKey, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetPubKeyByConsAddr", arg0, arg1)
ret0, _ := ret[0].(v1.PublicKey)
ret0, _ := ret[0].(types1.PubKey)
ret1, _ := ret[1].(error)
return ret0, ret1
}

View File

@ -3,8 +3,6 @@ package types
import (
"context"
cmtprotocrypto "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
st "cosmossdk.io/api/cosmos/staking/v1beta1"
"cosmossdk.io/core/address"
"cosmossdk.io/math"
@ -74,7 +72,7 @@ type ValidatorSet interface {
// GetPubKeyByConsAddr returns the consensus public key for a validator. Used in vote
// extension validation.
GetPubKeyByConsAddr(context.Context, sdk.ConsAddress) (cmtprotocrypto.PublicKey, error)
GetPubKeyByConsAddr(context.Context, sdk.ConsAddress) (cryptotypes.PubKey, error)
}
// DelegationSet expected properties for the set of all delegations for a particular (noalias)

View File

@ -7,8 +7,6 @@ import (
"strings"
"time"
cmtprotocrypto "github.com/cometbft/cometbft/api/cometbft/crypto/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/errors"
@ -16,7 +14,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@ -477,27 +474,6 @@ func (v Validator) ConsPubKey() (cryptotypes.PubKey, error) {
return pk, nil
}
// CmtConsPublicKey casts Validator.ConsensusPubkey to cmtprotocrypto.PubKey.
func (v Validator) CmtConsPublicKey() (cmtprotocrypto.PublicKey, error) {
pk, err := v.ConsPubKey()
if err != nil {
return cmtprotocrypto.PublicKey{}, err
}
tmPk, err := cryptocodec.ToCmtProtoPublicKey(pk)
if err != nil {
return cmtprotocrypto.PublicKey{}, err
}
return tmPk, nil
}
// Deprecated: use CmtConsPublicKey instead
// We do not delete this function as it is part of the ValidatorI interface
func (v Validator) TmConsPublicKey() (cmtprotocrypto.PublicKey, error) {
return v.CmtConsPublicKey()
}
// GetConsAddr extracts Consensus key address
func (v Validator) GetConsAddr() ([]byte, error) {
pk, ok := v.ConsensusPubkey.GetCachedValue().(cryptotypes.PubKey)