Skip to content

Commit

Permalink
Merge PR: fix tx decode in query to make compatible with all tx encode (
Browse files Browse the repository at this point in the history
#1424)

* fix tx decode

* optimize code
  • Loading branch information
KamiD authored Jan 12, 2022
1 parent b1c50ff commit bbfe781
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/rpc/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion x/evm/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
32 changes: 30 additions & 2 deletions x/evm/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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, &ethTx); 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.
//
Expand Down

5 comments on commit bbfe781

@ceilwoo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新后eth_getTransactionByHash拿不到数据是什么回事?

@KamiD
Copy link
Member Author

@KamiD KamiD commented on bbfe781 Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新后eth_getTransactionByHash拿不到数据是什么回事?

你使用的什么版本? 主网还是测试网,你使用的什么hash? 是链上返回的,还是本地计算的,给个示例

@ceilwoo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新后eth_getTransactionByHash拿不到数据是什么回事?

你使用的什么版本? 主网还是测试网,你使用的什么hash? 是链上返回的,还是本地计算的,给个示例

大概14日凌晨开始,我在v1.1.4的自建fullnode和wss://exchainws.okex.org:8443上,用eth_getTransactionByHash查询pending的hash没有任何返回。
在此之前都是正常的,在其他eth兼容链上也正常,只有在新版本的oec上用不了。

大概代码:
const provider = new ethers.providers.WebSocketProvider("wss://exchainws.okex.org:8443");
provider.on("pending", (tx:string)=>{
const trans = await provider.getTransaction(tx); //trans只返回null,之前的版本能返回TransactionResponse
});

@architectcoder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新后eth_getTransactionByHash拿不到数据是什么回事?

你使用的什么版本? 主网还是测试网,你使用的什么hash? 是链上返回的,还是本地计算的,给个示例

大概14日凌晨开始,我在v1.1.4的自建fullnode和wss://exchainws.okex.org:8443上,用eth_getTransactionByHash查询pending的hash没有任何返回。 在此之前都是正常的,在其他eth兼容链上也正常,只有在新版本的oec上用不了。

大概代码: const provider = new ethers.providers.WebSocketProvider("wss://exchainws.okex.org:8443"); provider.on("pending", (tx:string)=>{ const trans = await provider.getTransaction(tx); //trans只返回null,之前的版本能返回TransactionResponse });

我也遭遇了相同的问题 :(

@myuniswap2000
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我也遇到了。。

Please sign in to comment.