From bfb702009a6434f657500262a1f71e3d4acfe80d Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Mon, 16 May 2022 22:14:11 +0800 Subject: [PATCH] fix tx response decoding --- rpc/ethereum/namespaces/eth/filters/api.go | 4 +-- rpc/websockets.go | 2 +- x/evm/types/codec.go | 20 +++++++----- x/evm/types/utils.go | 36 +++++++++------------- x/evm/types/utils_test.go | 33 ++++++++++++-------- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/rpc/ethereum/namespaces/eth/filters/api.go b/rpc/ethereum/namespaces/eth/filters/api.go index 00d6defe..9bd9eafb 100644 --- a/rpc/ethereum/namespaces/eth/filters/api.go +++ b/rpc/ethereum/namespaces/eth/filters/api.go @@ -391,7 +391,7 @@ func (api *PublicFilterAPI) Logs(ctx context.Context, crit filters.FilterCriteri continue } - txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) + txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec) if err != nil { return } @@ -467,7 +467,7 @@ func (api *PublicFilterAPI) NewFilter(criteria filters.FilterCriteria) (rpc.ID, continue } - txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) + txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec) if err != nil { return } diff --git a/rpc/websockets.go b/rpc/websockets.go index c23f5944..e2d76abc 100644 --- a/rpc/websockets.go +++ b/rpc/websockets.go @@ -536,7 +536,7 @@ func (api *pubSubAPI) subscribeLogs(wsConn *wsConn, subID rpc.ID, extra interfac continue } - txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) + txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec) if err != nil { api.logger.Error("failed to decode tx response", "error", err.Error()) return diff --git a/x/evm/types/codec.go b/x/evm/types/codec.go index b44db514..793dd29a 100644 --- a/x/evm/types/codec.go +++ b/x/evm/types/codec.go @@ -1,7 +1,6 @@ package types import ( - "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -10,9 +9,8 @@ import ( proto "github.com/gogo/protobuf/proto" ) -var ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) - type ( + TxResponse interface{} ExtensionOptionsEthereumTxI interface{} ) @@ -22,15 +20,11 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { (*sdk.Msg)(nil), &MsgEthereumTx{}, ) - registry.RegisterInterface( - "ethermint.evm.v1.ExtensionOptionsEthereumTx", - (*ExtensionOptionsEthereumTxI)(nil), - &ExtensionOptionsEthereumTx{}, - ) registry.RegisterImplementations( (*tx.TxExtensionOptionI)(nil), &ExtensionOptionsEthereumTx{}, ) + registry.RegisterInterface( "ethermint.evm.v1.TxData", (*TxData)(nil), @@ -38,6 +32,16 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { &AccessListTx{}, &LegacyTx{}, ) + registry.RegisterInterface( + "ethermint.evm.v1.MsgEthereumTxResponse", + (*TxResponse)(nil), + &MsgEthereumTxResponse{}, + ) + registry.RegisterInterface( + "ethermint.evm.v1.ExtensionOptionsEthereumTx", + (*ExtensionOptionsEthereumTxI)(nil), + &ExtensionOptionsEthereumTx{}, + ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } diff --git a/x/evm/types/utils.go b/x/evm/types/utils.go index 16b1c780..5df470b9 100644 --- a/x/evm/types/utils.go +++ b/x/evm/types/utils.go @@ -6,8 +6,8 @@ import ( "github.com/gogo/protobuf/proto" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -18,25 +18,27 @@ const maxBitLen = 256 var EmptyCodeHash = crypto.Keccak256(nil) // DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse -func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) { +func DecodeTxResponse(in []byte, cdc codec.Codec) (*MsgEthereumTxResponse, error) { var txMsgData sdk.TxMsgData - if err := proto.Unmarshal(in, &txMsgData); err != nil { + if err := txMsgData.Unmarshal(in); err != nil { return nil, err } - data := txMsgData.GetData() - if len(data) == 0 { - return &MsgEthereumTxResponse{}, nil + responses := txMsgData.GetMsgResponses() + if len(responses) == 0 { + return nil, nil } - var res MsgEthereumTxResponse - - err := proto.Unmarshal(data[0].GetData(), &res) - if err != nil { - return nil, sdkerrors.Wrap(err, "failed to unmarshal tx response message data") + if err := cdc.UnpackAny(responses[0], new(TxResponse)); err != nil { + return nil, fmt.Errorf("failed to unmarshal tx response message: %w", err) } - return &res, nil + msgval := responses[0].GetCachedValue() + res, ok := msgval.(*MsgEthereumTxResponse) + if !ok { + return nil, fmt.Errorf("tx response message has invalid type: %T", msgval) + } + return res, nil } // EncodeTransactionLogs encodes TransactionLogs slice into a protobuf-encoded byte slice. @@ -44,16 +46,6 @@ func EncodeTransactionLogs(res *TransactionLogs) ([]byte, error) { return proto.Marshal(res) } -// DecodeTxResponse decodes an protobuf-encoded byte slice into TransactionLogs -func DecodeTransactionLogs(data []byte) (TransactionLogs, error) { - var logs TransactionLogs - err := proto.Unmarshal(data, &logs) - if err != nil { - return TransactionLogs{}, err - } - return logs, nil -} - // UnwrapEthereumMsg extract MsgEthereumTx from wrapping sdk.Tx func UnwrapEthereumMsg(tx *sdk.Tx, ethHash common.Hash) (*MsgEthereumTx, error) { if tx == nil { diff --git a/x/evm/types/utils_test.go b/x/evm/types/utils_test.go index d3ec00e5..d48a3c27 100644 --- a/x/evm/types/utils_test.go +++ b/x/evm/types/utils_test.go @@ -5,23 +5,33 @@ import ( "math/big" "testing" + "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" - proto "github.com/gogo/protobuf/proto" + "github.com/tharsis/ethermint/app" "github.com/tharsis/ethermint/encoding" evmtypes "github.com/tharsis/ethermint/x/evm/types" - "github.com/stretchr/testify/require" - "github.com/ethereum/go-ethereum/common" ) +var testCodec codec.Codec + +func init() { + registry := codectypes.NewInterfaceRegistry() + evmtypes.RegisterInterfaces(registry) + testCodec = codec.NewProtoCodec(registry) +} + func TestEvmDataEncoding(t *testing.T) { ret := []byte{0x5, 0x8} - data := &evmtypes.MsgEthereumTxResponse{ + resp := &evmtypes.MsgEthereumTxResponse{ Hash: common.BytesToHash([]byte("hash")).String(), Logs: []*evmtypes.Log{{ Data: []byte{1, 2, 3, 4}, @@ -30,21 +40,20 @@ func TestEvmDataEncoding(t *testing.T) { Ret: ret, } - enc, err := proto.Marshal(data) + any, err := codectypes.NewAnyWithValue(resp) require.NoError(t, err) - txData := &sdk.TxMsgData{ - Data: []*sdk.MsgData{{MsgType: evmtypes.TypeMsgEthereumTx, Data: enc}}, + MsgResponses: []*codectypes.Any{any}, } - txDataBz, err := proto.Marshal(txData) + txDataBz, err := txData.Marshal() require.NoError(t, err) - res, err := evmtypes.DecodeTxResponse(txDataBz) + decoded, err := evmtypes.DecodeTxResponse(txDataBz, testCodec) require.NoError(t, err) - require.NotNil(t, res) - require.Equal(t, data.Logs, res.Logs) - require.Equal(t, ret, res.Ret) + require.NotNil(t, decoded) + require.Equal(t, resp.Logs, decoded.Logs) + require.Equal(t, ret, decoded.Ret) } func TestUnwrapEthererumMsg(t *testing.T) {