fix tx response decoding

This commit is contained in:
Roy Crihfield 2022-05-16 22:14:11 +08:00
parent 54e49b83e7
commit bfb702009a
5 changed files with 50 additions and 45 deletions

View File

@ -391,7 +391,7 @@ func (api *PublicFilterAPI) Logs(ctx context.Context, crit filters.FilterCriteri
continue continue
} }
txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec)
if err != nil { if err != nil {
return return
} }
@ -467,7 +467,7 @@ func (api *PublicFilterAPI) NewFilter(criteria filters.FilterCriteria) (rpc.ID,
continue continue
} }
txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec)
if err != nil { if err != nil {
return return
} }

View File

@ -536,7 +536,7 @@ func (api *pubSubAPI) subscribeLogs(wsConn *wsConn, subID rpc.ID, extra interfac
continue continue
} }
txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data) txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data, api.clientCtx.Codec)
if err != nil { if err != nil {
api.logger.Error("failed to decode tx response", "error", err.Error()) api.logger.Error("failed to decode tx response", "error", err.Error())
return return

View File

@ -1,7 +1,6 @@
package types package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types" codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@ -10,9 +9,8 @@ import (
proto "github.com/gogo/protobuf/proto" proto "github.com/gogo/protobuf/proto"
) )
var ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
type ( type (
TxResponse interface{}
ExtensionOptionsEthereumTxI interface{} ExtensionOptionsEthereumTxI interface{}
) )
@ -22,15 +20,11 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
(*sdk.Msg)(nil), (*sdk.Msg)(nil),
&MsgEthereumTx{}, &MsgEthereumTx{},
) )
registry.RegisterInterface(
"ethermint.evm.v1.ExtensionOptionsEthereumTx",
(*ExtensionOptionsEthereumTxI)(nil),
&ExtensionOptionsEthereumTx{},
)
registry.RegisterImplementations( registry.RegisterImplementations(
(*tx.TxExtensionOptionI)(nil), (*tx.TxExtensionOptionI)(nil),
&ExtensionOptionsEthereumTx{}, &ExtensionOptionsEthereumTx{},
) )
registry.RegisterInterface( registry.RegisterInterface(
"ethermint.evm.v1.TxData", "ethermint.evm.v1.TxData",
(*TxData)(nil), (*TxData)(nil),
@ -38,6 +32,16 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
&AccessListTx{}, &AccessListTx{},
&LegacyTx{}, &LegacyTx{},
) )
registry.RegisterInterface(
"ethermint.evm.v1.MsgEthereumTxResponse",
(*TxResponse)(nil),
&MsgEthereumTxResponse{},
)
registry.RegisterInterface(
"ethermint.evm.v1.ExtensionOptionsEthereumTx",
(*ExtensionOptionsEthereumTxI)(nil),
&ExtensionOptionsEthereumTx{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
} }

View File

@ -6,8 +6,8 @@ import (
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" 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/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -18,25 +18,27 @@ const maxBitLen = 256
var EmptyCodeHash = crypto.Keccak256(nil) var EmptyCodeHash = crypto.Keccak256(nil)
// DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse // 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 var txMsgData sdk.TxMsgData
if err := proto.Unmarshal(in, &txMsgData); err != nil { if err := txMsgData.Unmarshal(in); err != nil {
return nil, err return nil, err
} }
data := txMsgData.GetData() responses := txMsgData.GetMsgResponses()
if len(data) == 0 { if len(responses) == 0 {
return &MsgEthereumTxResponse{}, nil return nil, nil
} }
var res MsgEthereumTxResponse if err := cdc.UnpackAny(responses[0], new(TxResponse)); err != nil {
return nil, fmt.Errorf("failed to unmarshal tx response message: %w", err)
err := proto.Unmarshal(data[0].GetData(), &res)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to unmarshal tx response message data")
} }
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. // EncodeTransactionLogs encodes TransactionLogs slice into a protobuf-encoded byte slice.
@ -44,16 +46,6 @@ func EncodeTransactionLogs(res *TransactionLogs) ([]byte, error) {
return proto.Marshal(res) 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 // UnwrapEthereumMsg extract MsgEthereumTx from wrapping sdk.Tx
func UnwrapEthereumMsg(tx *sdk.Tx, ethHash common.Hash) (*MsgEthereumTx, error) { func UnwrapEthereumMsg(tx *sdk.Tx, ethHash common.Hash) (*MsgEthereumTx, error) {
if tx == nil { if tx == nil {

View File

@ -5,23 +5,33 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client" "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" sdk "github.com/cosmos/cosmos-sdk/types"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
proto "github.com/gogo/protobuf/proto"
"github.com/tharsis/ethermint/app" "github.com/tharsis/ethermint/app"
"github.com/tharsis/ethermint/encoding" "github.com/tharsis/ethermint/encoding"
evmtypes "github.com/tharsis/ethermint/x/evm/types" evmtypes "github.com/tharsis/ethermint/x/evm/types"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common" "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) { func TestEvmDataEncoding(t *testing.T) {
ret := []byte{0x5, 0x8} ret := []byte{0x5, 0x8}
data := &evmtypes.MsgEthereumTxResponse{ resp := &evmtypes.MsgEthereumTxResponse{
Hash: common.BytesToHash([]byte("hash")).String(), Hash: common.BytesToHash([]byte("hash")).String(),
Logs: []*evmtypes.Log{{ Logs: []*evmtypes.Log{{
Data: []byte{1, 2, 3, 4}, Data: []byte{1, 2, 3, 4},
@ -30,21 +40,20 @@ func TestEvmDataEncoding(t *testing.T) {
Ret: ret, Ret: ret,
} }
enc, err := proto.Marshal(data) any, err := codectypes.NewAnyWithValue(resp)
require.NoError(t, err) require.NoError(t, err)
txData := &sdk.TxMsgData{ 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) require.NoError(t, err)
res, err := evmtypes.DecodeTxResponse(txDataBz) decoded, err := evmtypes.DecodeTxResponse(txDataBz, testCodec)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, res) require.NotNil(t, decoded)
require.Equal(t, data.Logs, res.Logs) require.Equal(t, resp.Logs, decoded.Logs)
require.Equal(t, ret, res.Ret) require.Equal(t, ret, decoded.Ret)
} }
func TestUnwrapEthererumMsg(t *testing.T) { func TestUnwrapEthererumMsg(t *testing.T) {