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
}
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
}

View File

@ -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

View File

@ -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)
}

View File

@ -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 {

View File

@ -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) {