Skip to content

Commit

Permalink
feat: Move ValidateBasic logic from method func of msg to msg handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed Apr 26, 2024
1 parent 4ae15fe commit b156e23
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 185 deletions.
5 changes: 1 addition & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ import (
_ "github.com/evmos/ethermint/client/docs/statik"

evmv1 "github.com/evmos/ethermint/api/ethermint/evm/v1"
feemarketv1 "github.com/evmos/ethermint/api/ethermint/feemarket/v1"
"github.com/evmos/ethermint/app/ante"
enccodec "github.com/evmos/ethermint/encoding/codec"
"github.com/evmos/ethermint/ethereum/eip712"
Expand Down Expand Up @@ -267,10 +266,8 @@ func NewEthermintApp(
},
}

// evm/MsgEthereumTx, evm/MsgUpdateParams, feemarket/MsgUpdateParams
// evm/MsgEthereumTx
signingOptions.DefineCustomGetSigners(protov2.MessageName(&evmv1.MsgEthereumTx{}), evmtypes.GetSignersFromMsgEthereumTxV2)
signingOptions.DefineCustomGetSigners(protov2.MessageName(&evmv1.MsgUpdateParams{}), evmtypes.GetSignersFromMsgUpdateParamsV2)
signingOptions.DefineCustomGetSigners(protov2.MessageName(&feemarketv1.MsgUpdateParams{}), feemarkettypes.GetSignersFromMsgUpdateParamsV2)

interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
Expand Down
6 changes: 1 addition & 5 deletions encoding/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import (
protov2 "google.golang.org/protobuf/proto"

evmv1 "github.com/evmos/ethermint/api/ethermint/evm/v1"
feemarketv1 "github.com/evmos/ethermint/api/ethermint/feemarket/v1"
enccodec "github.com/evmos/ethermint/encoding/codec"
evmtypes "github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
)

// MakeTestEncodingConfig creates an EncodingConfig for testing
Expand All @@ -43,10 +41,8 @@ func MakeTestEncodingConfig(modules ...module.AppModuleBasic) params.EncodingCon
ValidatorAddressCodec: address.Bech32Codec{Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix()},
}

// evm/MsgEthereumTx, evm/MsgUpdateParams, feemarket/MsgUpdateParams
// evm/MsgEthereumTx
signingOptions.DefineCustomGetSigners(protov2.MessageName(&evmv1.MsgEthereumTx{}), evmtypes.GetSignersFromMsgEthereumTxV2)
signingOptions.DefineCustomGetSigners(protov2.MessageName(&evmv1.MsgUpdateParams{}), evmtypes.GetSignersFromMsgUpdateParamsV2)
signingOptions.DefineCustomGetSigners(protov2.MessageName(&feemarketv1.MsgUpdateParams{}), feemarkettypes.GetSignersFromMsgUpdateParamsV2)

interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
Expand Down
4 changes: 2 additions & 2 deletions rpc/backend/tx_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (b *Backend) GetTxByEthHash(hash common.Hash) (*ethermint.TxResult, error)
}

// fallback to tendermint tx indexer
query := fmt.Sprintf("%s.%s='%s'", evmtypes.TypeMsgEthereumTx, evmtypes.AttributeKeyEthereumTxHash, hash.Hex())
query := fmt.Sprintf("%s.%s='%s'", evmtypes.EventTypeEthereumTx, evmtypes.AttributeKeyEthereumTxHash, hash.Hex())
txResult, err := b.queryTendermintTxIndexer(query, func(txs *rpctypes.ParsedTxs) *rpctypes.ParsedTx {
return txs.GetTxByHash(hash)
})
Expand All @@ -335,7 +335,7 @@ func (b *Backend) GetTxByTxIndex(height int64, index uint) (*ethermint.TxResult,

// fallback to tendermint tx indexer
query := fmt.Sprintf("tx.height=%d AND %s.%s=%d",
height, evmtypes.TypeMsgEthereumTx,
height, evmtypes.EventTypeEthereumTx,
evmtypes.AttributeKeyTxIndex, index,
)
txResult, err := b.queryTendermintTxIndexer(query, func(txs *rpctypes.ParsedTxs) *rpctypes.ParsedTx {
Expand Down
2 changes: 1 addition & 1 deletion rpc/backend/tx_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (suite *BackendTestSuite) TestGetTxByEthHash() {
func() {
suite.backend.indexer = nil
client := suite.backend.clientCtx.Client.(*mocks.Client)
query := fmt.Sprintf("%s.%s='%s'", evmtypes.TypeMsgEthereumTx, evmtypes.AttributeKeyEthereumTxHash, common.HexToHash(msgEthereumTx.Hash).Hex())
query := fmt.Sprintf("%s.%s='%s'", evmtypes.EventTypeEthereumTx, evmtypes.AttributeKeyEthereumTxHash, common.HexToHash(msgEthereumTx.Hash).Hex())
RegisterTxSearch(client, query, bz)
},
msgEthereumTx,
Expand Down
2 changes: 1 addition & 1 deletion rpc/namespaces/ethereum/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (api *PublicFilterAPI) Logs(ctx context.Context, crit filters.FilterCriteri
}

// filter only events from EVM module txs
_, isMsgEthereumTx := ev.Events[evmtypes.TypeMsgEthereumTx]
_, isMsgEthereumTx := ev.Events[evmtypes.EventTypeEthereumTx]

if !isMsgEthereumTx {
// ignore transaction as it's not from the evm module
Expand Down
8 changes: 8 additions & 0 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t
// performed if the requested authority is the Cosmos SDK governance module
// account.
func (k *Keeper) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if _, err := sdk.AccAddressFromBech32(req.Authority); err != nil {
return nil, errorsmod.Wrap(err, "invalid authority address")
}

if k.authority.String() != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority, expected %s, got %s", k.authority.String(), req.Authority)
}

if err := req.Params.Validate(); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SetParams(ctx, req.Params); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion x/evm/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package types

// Evm module events
const (
EventTypeEthereumTx = TypeMsgEthereumTx
EventTypeEthereumTx = "ethereum_tx"
EventTypeBlockBloom = "block_bloom"
EventTypeTxLog = "tx_log"

Expand Down
60 changes: 0 additions & 60 deletions x/evm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ var (
_ codectypes.UnpackInterfacesMessage = MsgEthereumTx{}
)

// message type and route constants
const (
// TypeMsgEthereumTx defines the type string of an Ethereum transaction
TypeMsgEthereumTx = "ethereum_tx"
)

// NewTx returns a reference to a new Ethereum transaction message.
func NewTx(
chainID *big.Int, nonce uint64, to *common.Address, amount *big.Int,
Expand Down Expand Up @@ -176,12 +170,6 @@ func (msg *MsgEthereumTx) FromEthereumTx(tx *ethtypes.Transaction) error {
return nil
}

// Route returns the route value of an MsgEthereumTx.
func (msg MsgEthereumTx) Route() string { return RouterKey }

// Type returns the type value of an MsgEthereumTx.
func (msg MsgEthereumTx) Type() string { return TypeMsgEthereumTx }

// ValidateBasic implements the sdk.Msg interface. It performs basic validation
// checks of a Transaction. If returns an error if validation fails.
func (msg MsgEthereumTx) ValidateBasic() error {
Expand Down Expand Up @@ -448,27 +436,6 @@ func (msg *MsgEthereumTx) BuildTx(b client.TxBuilder, evmDenom string) (authsign
return tx, nil
}

// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m MsgUpdateParams) GetSigners() []sdk.AccAddress {
//#nosec G703 -- gosec raises a warning about a non-handled error which we deliberately ignore here
addr, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}

// ValidateBasic does a sanity check of the provided data
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}

return m.Params.Validate()
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&m))
}

func GetSignersFromMsgEthereumTxV2(msg protov2.Message) ([][]byte, error) {
msgv1, err := GetMsgEthereumTxFromMsgV2(msg)
if err != nil {
Expand Down Expand Up @@ -518,30 +485,3 @@ func GetMsgEthereumTxFromMsgV2(msg protov2.Message) (MsgEthereumTx, error) {
msgv1.Hash = msgv1.AsTransaction().Hash().Hex()
return msgv1, nil
}

func GetSignersFromMsgUpdateParamsV2(msg protov2.Message) ([][]byte, error) {
msgv1, err := GetMsgUpdateParamsFromMsgV2(msg)
if err != nil {
return nil, err
}

signers := [][]byte{}
for _, signer := range msgv1.GetSigners() {
signers = append(signers, signer.Bytes())
}

return signers, nil
}

func GetMsgUpdateParamsFromMsgV2(msg protov2.Message) (MsgUpdateParams, error) {
msgv2, ok := msg.(*evmapi.MsgUpdateParams)
if !ok {
return MsgUpdateParams{}, fmt.Errorf("invalid x/evm/MsgUpdateParams msg v2: %v", msg)
}

msgv1 := MsgUpdateParams{
Authority: msgv2.Authority,
}

return msgv1, nil
}
3 changes: 1 addition & 2 deletions x/evm/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_Constructor() {
msg := types.NewTx(nil, 0, &suite.to, nil, 100000, nil, nil, nil, []byte("test"), nil)

// suite.Require().Equal(msg.Data.To, suite.to.Hex())
suite.Require().Equal(msg.Route(), types.RouterKey)
suite.Require().Equal(msg.Type(), types.TypeMsgEthereumTx)
suite.Require().Equal(sdk.MsgTypeURL(msg), "/ethermint.evm.v1.MsgEthereumTx")
// suite.Require().NotNil(msg.To())
suite.Require().Equal(msg.GetMsgs(), []sdk.Msg{msg})
suite.Require().Panics(func() { msg.GetSigners() })
Expand Down
8 changes: 8 additions & 0 deletions x/feemarket/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ import (
// performed if the requested authority is the Cosmos SDK governance module
// account.
func (k *Keeper) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if _, err := sdk.AccAddressFromBech32(req.Authority); err != nil {
return nil, errorsmod.Wrap(err, "invalid authority address")
}

if k.authority.String() != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority.String(), req.Authority)
}

if err := req.Params.Validate(); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.SetParams(ctx, req.Params); err != nil {
return nil, err
Expand Down
57 changes: 1 addition & 56 deletions x/feemarket/types/msg.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,5 @@
package types

import (
"fmt"

protov2 "google.golang.org/protobuf/proto"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
feemarketapi "github.com/evmos/ethermint/api/ethermint/feemarket/v1"
)
import sdk "github.com/cosmos/cosmos-sdk/types"

var _ sdk.Msg = &MsgUpdateParams{}

// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m *MsgUpdateParams) GetSigners() []sdk.AccAddress {
addr := sdk.MustAccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}

// ValidateBasic does a sanity check of the provided data
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}

return m.Params.Validate()
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&m))
}

func GetSignersFromMsgUpdateParamsV2(msg protov2.Message) ([][]byte, error) {
msgv1, err := GetMsgUpdateParamsFromMsgV2(msg)
if err != nil {
return nil, err
}

signers := [][]byte{}
for _, signer := range msgv1.GetSigners() {
signers = append(signers, signer.Bytes())
}

return signers, nil
}

func GetMsgUpdateParamsFromMsgV2(msg protov2.Message) (MsgUpdateParams, error) {
msgv2, ok := msg.(*feemarketapi.MsgUpdateParams)
if !ok {
return MsgUpdateParams{}, fmt.Errorf("invalid x/feemarket/MsgUpdateParams msg v2: %v", msg)
}

msgv1 := MsgUpdateParams{
Authority: msgv2.Authority,
}

return msgv1, nil
}
53 changes: 0 additions & 53 deletions x/feemarket/types/msg_test.go

This file was deleted.

0 comments on commit b156e23

Please sign in to comment.