Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

filter non eth txs in block rpc response #741

Merged
merged 17 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (evm, test) [tharsis#649](https://github.com/tharsis/ethermint/pull/649) Test DynamicFeeTx.
* (evm) [tharsis#702](https://github.com/tharsis/ethermint/pull/702) Fix panic in web3 RPC handlers
* (rpc) [tharsis#720](https://github.com/tharsis/ethermint/pull/720) Fix `debug_traceTransaction` failure
* (rpc) [tharsis#741](https://github.com/tharsis/ethermint/pull/741) Fix `eth_getBlockByNumberAndHash` return with non eth txs

### Improvements

Expand Down
7 changes: 7 additions & 0 deletions rpc/ethereum/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ func (e *EVMBackend) EthBlockFromTendermint(

tx := ethMsg.AsTransaction()

// check tx exists on EVM
_, err := e.GetTxByEthHash(tx.Hash())
if err != nil {
e.logger.Debug("failed to query eth tx", "hash", tx.Hash().Hex(), "error", err.Error())
continue
}

if !fullTx {
hash := tx.Hash()
ethRPCTxs = append(ethRPCTxs, hash)
Expand Down
58 changes: 41 additions & 17 deletions rpc/ethereum/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"

ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/tharsis/ethermint/crypto/hd"
"github.com/tharsis/ethermint/rpc/ethereum/backend"
rpctypes "github.com/tharsis/ethermint/rpc/ethereum/types"
Expand Down Expand Up @@ -315,7 +316,8 @@ func (e *PublicAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Ui
return nil
}

n := hexutil.Uint(len(resBlock.Block.Txs))
ethMsgs := e.getEthereumMsgFromTendermintBlock(resBlock)
n := hexutil.Uint(len(ethMsgs))
return &n
}

Expand All @@ -333,7 +335,8 @@ func (e *PublicAPI) GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumb
return nil
}

n := hexutil.Uint(len(resBlock.Block.Txs))
ethMsgs := e.getEthereumMsgFromTendermintBlock(resBlock)
n := hexutil.Uint(len(ethMsgs))
return &n
}

Expand Down Expand Up @@ -673,23 +676,13 @@ func (e *PublicAPI) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexu
}

i := int(idx)
if i >= len(resBlock.Block.Txs) {
ethMsgs := e.getEthereumMsgFromTendermintBlock(resBlock)
if i >= len(ethMsgs) {
e.logger.Debug("block txs index out of bound", "index", i)
return nil, nil
}

txBz := resBlock.Block.Txs[i]
tx, err := e.clientCtx.TxConfig.TxDecoder()(txBz)
if err != nil {
e.logger.Debug("decoding failed", "error", err.Error())
return nil, fmt.Errorf("failed to decode tx: %w", err)
}

msg, err := evmtypes.UnwrapEthereumMsg(&tx)
if err != nil {
e.logger.Debug("invalid tx", "error", err.Error())
return nil, err
}
msg := ethMsgs[i]
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

baseFee, err := e.backend.BaseFee(resBlock.Block.Height)
if err != nil {
Expand Down Expand Up @@ -721,7 +714,8 @@ func (e *PublicAPI) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockN
}

i := int(idx)
if i >= len(resBlock.Block.Txs) {
ethMsgs := e.getEthereumMsgFromTendermintBlock(resBlock)
if i >= len(ethMsgs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

L723 (txBz := resBlock.Block.Txs[i] ) is inconsistent with the L685. See comment about assuming the index corresponds to an ethereum tx

Copy link
Contributor

Choose a reason for hiding this comment

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

line 723-730 needs to be removed @crypto-facs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! If we agree this #741 (comment) is the right approach to handle index across the endpoints I will add the filter here too.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok please remove

e.logger.Debug("block txs index out of bound", "index", i)
return nil, nil
}
Expand Down Expand Up @@ -848,7 +842,7 @@ func (e *PublicAPI) GetTransactionReceipt(hash common.Hash) (map[string]interfac
return receipt, nil
}

// PendingTransactions returns the transactions that are in the transaction pool
// GetPendingTransactions returns the transactions that are in the transaction pool
// and have a from address that is one of the accounts this node manages.
func (e *PublicAPI) GetPendingTransactions() ([]*rpctypes.RPCTransaction, error) {
e.logger.Debug("eth_getPendingTransactions")
Expand Down Expand Up @@ -1000,3 +994,33 @@ func (e *PublicAPI) getBlockNumber(blockNrOrHash rpctypes.BlockNumberOrHash) (rp
return rpctypes.EthEarliestBlockNumber, nil
}
}

func (e *PublicAPI) getEthereumMsgFromTendermintBlock(block *ctypes.ResultBlock) []*evmtypes.MsgEthereumTx {
var result []*evmtypes.MsgEthereumTx
for _, tx := range block.Block.Txs {
tx, err := e.clientCtx.TxConfig.TxDecoder()(tx)
if err != nil {
e.logger.Debug("failed to decode transaction in block", "height", block.Block.Height, "error", err.Error())
continue
}

for _, msg := range tx.GetMsgs() {
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)

crypto-facs marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
continue
}

hash := ethMsg.AsTransaction().Hash()
// check tx exists on EVM
_, err := e.backend.GetTxByEthHash(hash)
if err != nil {
e.logger.Debug("failed to query eth tx hash", "hash", hash.Hex(), "error", err.Error())
continue
}

result = append(result, ethMsg)
}
}
return result
}