From 07c6ff00558b43137218ba160e2e54afbdbd60a7 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Tue, 14 Mar 2023 10:42:54 +0800 Subject: [PATCH] fix: align filter rule for debug trace block (#220) * align filter rule for debug_traceBlockByNumber * test debug_traceBlockByNumber * fix lint * update doc --- CHANGELOG.md | 4 ++ rpc/backend/tracing.go | 11 ++++- tests/integration_tests/test_debug_trace.py | 51 +++++++++++++++++++++ tests/integration_tests/utils.py | 7 +++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/integration_tests/test_debug_trace.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ae0045552..900e1e632c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [#1682](https://github.com/evmos/ethermint/pull/1682) Add config for maximum number of bytes returned from eth_call. +### Bug Fixes + +* (rpc) [#1688](https://github.com/evmos/ethermint/pull/1688) Align filter rule for `debug_traceBlockByNumber` + ### Improvements * (cli) [#1615](https://github.com/evmos/ethermint/pull/1615) Support customize db opener in `StartCmd`. diff --git a/rpc/backend/tracing.go b/rpc/backend/tracing.go index e616c6f092..60998d517c 100644 --- a/rpc/backend/tracing.go +++ b/rpc/backend/tracing.go @@ -9,6 +9,7 @@ import ( 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/hexutil" rpctypes "github.com/evmos/ethermint/rpc/types" ethermint "github.com/evmos/ethermint/types" evmtypes "github.com/evmos/ethermint/x/evm/types" @@ -137,11 +138,19 @@ func (b *Backend) TraceBlock(height rpctypes.BlockNumber, // If there are no transactions return empty array return []*evmtypes.TxTraceResult{}, nil } - + blockRes, err := b.TendermintBlockResultByNumber(&block.Block.Height) + if err != nil { + b.logger.Debug("block result not found", "height", block.Block.Height, "error", err.Error()) + return nil, nil + } txDecoder := b.clientCtx.TxConfig.TxDecoder() var txsMessages []*evmtypes.MsgEthereumTx for i, tx := range txs { + if !rpctypes.TxSuccessOrExceedsBlockGasLimit(blockRes.TxsResults[i]) { + b.logger.Debug("invalid tx result code", "cosmos-hash", hexutil.Encode(tx.Hash())) + continue + } decodedTx, err := txDecoder(tx) if err != nil { b.logger.Error("failed to decode transaction", "hash", txs[i].Hash(), "error", err.Error()) diff --git a/tests/integration_tests/test_debug_trace.py b/tests/integration_tests/test_debug_trace.py new file mode 100644 index 0000000000..d96b055510 --- /dev/null +++ b/tests/integration_tests/test_debug_trace.py @@ -0,0 +1,51 @@ +import requests +from pystarport import ports + +from .utils import ( + derive_new_account, + send_transaction, + sign_transaction, + wait_for_new_blocks, +) + + +def test_trace_blk(ethermint): + w3 = ethermint.w3 + cli = ethermint.cosmos_cli() + acc = derive_new_account(3) + sender = acc.address + # fund new sender + fund = 3000000000000000000 + tx = {"to": sender, "value": fund, "gasPrice": w3.eth.gas_price} + send_transaction(w3, tx) + assert w3.eth.get_balance(sender, "latest") == fund + nonce = w3.eth.get_transaction_count(sender) + blk = wait_for_new_blocks(cli, 1, sleep=0.1) + txhashes = [] + total = 3 + for n in range(total): + tx = { + "to": "0x2956c404227Cc544Ea6c3f4a36702D0FD73d20A2", + "value": fund // total, + "gas": 21000, + "maxFeePerGas": 6556868066901, + "maxPriorityFeePerGas": 1500000000, + "nonce": nonce + n, + } + signed = sign_transaction(w3, tx, acc.key) + txhash = w3.eth.send_raw_transaction(signed.rawTransaction) + txhashes.append(txhash) + for txhash in txhashes[0 : total - 1]: + res = w3.eth.wait_for_transaction_receipt(txhash) + assert res.status == 1 + + url = f"http://127.0.0.1:{ports.evmrpc_port(ethermint.base_port(0))}" + params = { + "method": "debug_traceBlockByNumber", + "params": [hex(blk + 1)], + "id": 1, + "jsonrpc": "2.0", + } + rsp = requests.post(url, json=params) + assert rsp.status_code == 200 + assert len(rsp.json()["result"]) == 2 diff --git a/tests/integration_tests/utils.py b/tests/integration_tests/utils.py index eda82706e4..6fcaf16cf8 100644 --- a/tests/integration_tests/utils.py +++ b/tests/integration_tests/utils.py @@ -190,3 +190,10 @@ def parse_events(logs): ev["type"]: {attr["key"]: attr["value"] for attr in ev["attributes"]} for ev in logs[0]["events"] } + + +def derive_new_account(n=1): + # derive a new address + account_path = f"m/44'/60'/0'/0/{n}" + mnemonic = os.getenv("COMMUNITY_MNEMONIC") + return Account.from_mnemonic(mnemonic, account_path=account_path)