From bbfe781245a08cdf32113a224d48512476690b3e Mon Sep 17 00:00:00 2001 From: KamiD <44460798+KamiD@users.noreply.github.com> Date: Thu, 13 Jan 2022 06:14:03 +0800 Subject: [PATCH] Merge PR: fix tx decode in query to make compatible with all tx encode (#1424) * fix tx decode * optimize code --- app/rpc/types/utils.go | 4 ++-- x/evm/client/rest/rest.go | 2 +- x/evm/types/utils.go | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/rpc/types/utils.go b/app/rpc/types/utils.go index a003ae3d9e..be4a76e373 100644 --- a/app/rpc/types/utils.go +++ b/app/rpc/types/utils.go @@ -31,7 +31,7 @@ var ( // RawTxToEthTx returns a evm MsgEthereum transaction from raw tx bytes. func RawTxToEthTx(clientCtx clientcontext.CLIContext, bz []byte) (*evmtypes.MsgEthereumTx, error) { - tx, err := evmtypes.TxDecoder(clientCtx.Codec)(bz) + tx, err := evmtypes.TxDecoderOnlyInQuery(clientCtx.Codec)(bz) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } @@ -235,7 +235,7 @@ func GetKeyByAddress(keys []ethsecp256k1.PrivKey, address common.Address) (key * // EVM module transactions. func GetBlockCumulativeGas(cdc *codec.Codec, block *tmtypes.Block, idx int) uint64 { var gasUsed uint64 - txDecoder := evmtypes.TxDecoder(cdc) + txDecoder := evmtypes.TxDecoderOnlyInQuery(cdc) for i := 0; i < idx && i < len(block.Txs); i++ { txi, err := txDecoder(block.Txs[i]) diff --git a/x/evm/client/rest/rest.go b/x/evm/client/rest/rest.go index fd4a1fe3aa..3a4a694d03 100644 --- a/x/evm/client/rest/rest.go +++ b/x/evm/client/rest/rest.go @@ -90,7 +90,7 @@ func QueryTx(cliCtx context.CLIContext, hashHexStr string) (interface{}, error) } } - tx, err := evmtypes.TxDecoder(cliCtx.Codec)(resTx.Tx) + tx, err := evmtypes.TxDecoderOnlyInQuery(cliCtx.Codec)(resTx.Tx) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) } diff --git a/x/evm/types/utils.go b/x/evm/types/utils.go index 55839fa157..560c2b86f6 100644 --- a/x/evm/types/utils.go +++ b/x/evm/types/utils.go @@ -4,11 +4,12 @@ import ( "bytes" "encoding/binary" "fmt" + "math/big" + "strings" + authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types" "github.com/okex/exchain/libs/tendermint/global" "github.com/okex/exchain/libs/tendermint/types" - "math/big" - "strings" "github.com/tendermint/go-amino" @@ -579,6 +580,33 @@ func TxDecoder(cdc *codec.Codec) sdk.TxDecoder { } } +// +func TxDecoderOnlyInQuery(cdc *codec.Codec) sdk.TxDecoder { + return func(txBytes []byte, heights ...int64) (sdk.Tx, error) { + var tx sdk.Tx + var err error + if len(txBytes) == 0 { + return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx bytes are empty") + } + + var ethTx MsgEthereumTx + if err = authtypes.EthereumTxDecode(txBytes, ðTx); err == nil { + return ethTx, nil + } + + // sdk.Tx is an interface. The concrete message types + // are registered by MakeTxCodec + // TODO: switch to UnmarshalBinaryBare on SDK v0.40.0 + if v, err := cdc.UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller(txBytes, &tx); err == nil { + return v.(sdk.Tx), nil + } + if err = cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx); err == nil { + return tx, nil + } + return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, err.Error()) + } +} + // recoverEthSig recovers a signature according to the Ethereum specification and // returns the sender or an error. //