From 7b71f95068368ad895691dadd49f0c04ec8208fb Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 17 Jun 2025 15:03:15 +0900 Subject: [PATCH 01/12] add support for eth_getblockreceipts --- CHANGELOG.md | 1 + rpc/backend/backend.go | 1 + rpc/backend/blocks.go | 46 ++++++++++++++++++++++++++++++ rpc/namespaces/ethereum/eth/api.go | 8 +++++- tests/solidity/init-test-node.sh | 2 +- 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acc00b92a0..1516749970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (rpx) [#414](https://github.com/crypto-org-chain/ethermint/pull/414) Add support for eth_getBlockReceipts. * (evm) [#414](https://github.com/crypto-org-chain/ethermint/pull/414) Integrate go-block-stm for parallel tx execution. * (block-stm) [#498](https://github.com/crypto-org-chain/ethermint/pull/498) Enable incarnation cache for block-stm executor. diff --git a/rpc/backend/backend.go b/rpc/backend/backend.go index 68090e4767..92ae9ab91e 100644 --- a/rpc/backend/backend.go +++ b/rpc/backend/backend.go @@ -85,6 +85,7 @@ type EVMBackend interface { GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) *hexutil.Uint + GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string]interface{}, error) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpctypes.ResultBlock, error) TendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error) TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index 131c8407ff..e1dca9ebd4 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -91,6 +91,37 @@ func (b *Backend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) ( return res, nil } +// GetBlockReceipts returns a list of Ethereum transaction receipts given a block number + +func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string]interface{}, error) { + resBlock, err := b.TendermintBlockByNumber(blockNum) + if err != nil { + return nil, nil + } + // return if requested block height is greater than the current one + if resBlock == nil || resBlock.Block == nil { + return nil, nil + } + blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height) + if err != nil { + b.logger.Debug("failed to fetch block result from Tendermint", "height", blockNum, "error", err.Error()) + return nil, nil + } + + txHashes := b.TransactionHashesFromTendermintBlock(resBlock, blockRes) + + res := make([]map[string]interface{}, 0) + for _, txHash := range txHashes { + receipt, err := b.GetTransactionReceipt(txHash) + if err != nil { + return nil, err + } + res = append(res, receipt) + } + + return res, nil +} + // GetBlockByHash returns the JSON-RPC compatible Ethereum block identified by // hash. func (b *Backend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) { @@ -513,6 +544,21 @@ func (b *Backend) RPCBlockFromTendermintBlock( return formattedBlock, nil } +// TransactionHashesFromTendermintBlock returns list of eth transaction hashes +// given Tendermint block and its block result. +func (b *Backend) TransactionHashesFromTendermintBlock( + resBlock *tmrpctypes.ResultBlock, + blockRes *tmrpctypes.ResultBlockResults, +) []common.Hash { + var ethHashes []common.Hash + msgs := b.EthMsgsFromTendermintBlock(resBlock, blockRes) + for _, ethMsg := range msgs { + ethHashes = append(ethHashes, ethMsg.Hash()) + } + + return ethHashes +} + // EthBlockByNumber returns the Ethereum Block identified by number. func (b *Backend) EthBlockByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Block, error) { resBlock, err := b.TendermintBlockByNumber(blockNum) diff --git a/rpc/namespaces/ethereum/eth/api.go b/rpc/namespaces/ethereum/eth/api.go index 2572f1726e..002f2a1c90 100644 --- a/rpc/namespaces/ethereum/eth/api.go +++ b/rpc/namespaces/ethereum/eth/api.go @@ -62,7 +62,7 @@ type EthereumAPI interface { GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNumber, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) - // eth_getBlockReceipts + GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string]interface{}, error) // Writing Transactions // @@ -222,6 +222,12 @@ func (e *PublicAPI) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockN return e.backend.GetTransactionByBlockNumberAndIndex(blockNum, idx) } +// GetBlockReceipts returns a list of transaction receipts given a block number. +func (e *PublicAPI) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string]interface{}, error) { + e.logger.Debug("eth_getBlockReceipts", "number", blockNum) + return e.backend.GetBlockReceipts(blockNum) +} + /////////////////////////////////////////////////////////////////////////////// /// Write Txs /// /////////////////////////////////////////////////////////////////////////////// diff --git a/tests/solidity/init-test-node.sh b/tests/solidity/init-test-node.sh index 635287cbb5..ceeff523e4 100755 --- a/tests/solidity/init-test-node.sh +++ b/tests/solidity/init-test-node.sh @@ -36,7 +36,7 @@ echo $USER4_MNEMONIC | ethermintd keys add $USER4_KEY --recover --keyring-backen ethermintd init $MONIKER --chain-id $CHAINID # Set gas limit in genesis -cat $HOME/.ethermintd/config/genesis.json | jq '.consensus_params["block"]["max_gas"]="10000000"' > $HOME/.ethermintd/config/tmp_genesis.json && mv $HOME/.ethermintd/config/tmp_genesis.json $HOME/.ethermintd/config/genesis.json +cat $HOME/.ethermintd/config/genesis.json | jq '.consensus["params"]["block"]["max_gas"]="10000000"' > $HOME/.ethermintd/config/tmp_genesis.json && mv $HOME/.ethermintd/config/tmp_genesis.json $HOME/.ethermintd/config/genesis.json # modified default configs if [[ "$OSTYPE" == "darwin"* ]]; then From ffe889165f8eb5c466e8d3d29deebf10b8df7c8b Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 17 Jun 2025 15:12:54 +0900 Subject: [PATCH 02/12] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1516749970..5d99c91673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features -* (rpx) [#414](https://github.com/crypto-org-chain/ethermint/pull/414) Add support for eth_getBlockReceipts. +* (rpx) [#629](https://github.com/crypto-org-chain/ethermint/pull/629) Add support for eth_getBlockReceipts. * (evm) [#414](https://github.com/crypto-org-chain/ethermint/pull/414) Integrate go-block-stm for parallel tx execution. * (block-stm) [#498](https://github.com/crypto-org-chain/ethermint/pull/498) Enable incarnation cache for block-stm executor. From f6e56e4ca0cc5374635c684d7c385a5e5647a3a0 Mon Sep 17 00:00:00 2001 From: Thomas Nguy <81727899+thomas-nguy@users.noreply.github.com> Date: Tue, 17 Jun 2025 16:47:11 +0900 Subject: [PATCH 03/12] Update rpc/backend/blocks.go Co-authored-by: Xinyu <91446598+XinyuCRO@users.noreply.github.com> Signed-off-by: Thomas Nguy <81727899+thomas-nguy@users.noreply.github.com> --- rpc/backend/blocks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index e1dca9ebd4..8fd9e2b5a7 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -110,7 +110,7 @@ func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string] txHashes := b.TransactionHashesFromTendermintBlock(resBlock, blockRes) - res := make([]map[string]interface{}, 0) + res := make([]map[string]interface{}, len(txHashes) for _, txHash := range txHashes { receipt, err := b.GetTransactionReceipt(txHash) if err != nil { From abda9272c1b5b09c14ffaecdf8cec530a0f097bf Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 17 Jun 2025 17:09:21 +0900 Subject: [PATCH 04/12] fix lint --- rpc/backend/blocks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index 8fd9e2b5a7..e94b49a5c4 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -110,7 +110,7 @@ func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string] txHashes := b.TransactionHashesFromTendermintBlock(resBlock, blockRes) - res := make([]map[string]interface{}, len(txHashes) + res := make([]map[string]interface{}, len(txHashes)) for _, txHash := range txHashes { receipt, err := b.GetTransactionReceipt(txHash) if err != nil { From aee2feeeeaaeffdc2f4ca87bf6231e5b7e1db820 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 17 Jun 2025 17:20:44 +0900 Subject: [PATCH 05/12] optimization --- rpc/backend/backend.go | 2 +- rpc/backend/blocks.go | 4 ++-- rpc/backend/tx_info.go | 14 ++++++++------ rpc/backend/tx_info_test.go | 2 +- rpc/namespaces/ethereum/eth/api.go | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rpc/backend/backend.go b/rpc/backend/backend.go index 92ae9ab91e..9e29ef391e 100644 --- a/rpc/backend/backend.go +++ b/rpc/backend/backend.go @@ -122,7 +122,7 @@ type EVMBackend interface { GetTxByEthHash(txHash common.Hash) (*ethermint.TxResult, error) GetTxByTxIndex(height int64, txIndex uint) (*ethermint.TxResult, error) GetTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) - GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) + GetTransactionReceipt(hash common.Hash, resBlock *tmrpctypes.ResultBlock) (map[string]interface{}, error) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNumber, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index e94b49a5c4..bfd21e01c7 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -112,7 +112,7 @@ func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string] res := make([]map[string]interface{}, len(txHashes)) for _, txHash := range txHashes { - receipt, err := b.GetTransactionReceipt(txHash) + receipt, err := b.GetTransactionReceipt(txHash, resBlock) if err != nil { return nil, err } @@ -550,8 +550,8 @@ func (b *Backend) TransactionHashesFromTendermintBlock( resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults, ) []common.Hash { - var ethHashes []common.Hash msgs := b.EthMsgsFromTendermintBlock(resBlock, blockRes) + ethHashes := make([]common.Hash, len(msgs)) for _, ethMsg := range msgs { ethHashes = append(ethHashes, ethMsg.Hash()) } diff --git a/rpc/backend/tx_info.go b/rpc/backend/tx_info.go index b33ccab66f..35d4ea0719 100644 --- a/rpc/backend/tx_info.go +++ b/rpc/backend/tx_info.go @@ -149,8 +149,8 @@ func (b *Backend) GetGasUsed(res *ethermint.TxResult, gas uint64) uint64 { return res.GasUsed } -// GetTransactionReceipt returns the transaction receipt identified by hash. -func (b *Backend) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) { +// GetTransactionReceipt returns the transaction receipt identified by hash. It takes an optional resBlock, if nil then the method will fetch it. +func (b *Backend) GetTransactionReceipt(hash common.Hash, resBlock *tmrpctypes.ResultBlock) (map[string]interface{}, error) { b.logger.Debug("eth_getTransactionReceipt", "hash", hash) res, err := b.GetTxByEthHash(hash) @@ -158,10 +158,12 @@ func (b *Backend) GetTransactionReceipt(hash common.Hash) (map[string]interface{ b.logger.Debug("tx not found", "hash", hash, "error", err.Error()) return nil, nil } - resBlock, err := b.TendermintBlockByNumber(rpctypes.BlockNumber(res.Height)) - if err != nil { - b.logger.Debug("block not found", "height", res.Height, "error", err.Error()) - return nil, nil + if resBlock == nil { + resBlock, err = b.TendermintBlockByNumber(rpctypes.BlockNumber(res.Height)) + if err != nil { + b.logger.Debug("block not found", "height", res.Height, "error", err.Error()) + return nil, nil + } } tx, err := b.clientCtx.TxConfig.TxDecoder()(resBlock.Block.Txs[res.TxIndex]) if err != nil { diff --git a/rpc/backend/tx_info_test.go b/rpc/backend/tx_info_test.go index fd0471303f..afb8aa36c5 100644 --- a/rpc/backend/tx_info_test.go +++ b/rpc/backend/tx_info_test.go @@ -586,7 +586,7 @@ func (suite *BackendTestSuite) TestGetTransactionReceipt() { err := suite.backend.indexer.IndexBlock(tc.block, tc.blockResult) suite.Require().NoError(err) - txReceipt, err := suite.backend.GetTransactionReceipt(tc.tx.Hash()) + txReceipt, err := suite.backend.GetTransactionReceipt(tc.tx.Hash(), nil) if tc.expPass { suite.Require().NoError(err) suite.Require().Equal(txReceipt, tc.expTxReceipt) diff --git a/rpc/namespaces/ethereum/eth/api.go b/rpc/namespaces/ethereum/eth/api.go index 002f2a1c90..b07841512e 100644 --- a/rpc/namespaces/ethereum/eth/api.go +++ b/rpc/namespaces/ethereum/eth/api.go @@ -195,7 +195,7 @@ func (e *PublicAPI) GetTransactionCount(address common.Address, blockNrOrHash rp func (e *PublicAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) { hexTx := hash.Hex() e.logger.Debug("eth_getTransactionReceipt", "hash", hexTx) - return e.backend.GetTransactionReceipt(hash) + return e.backend.GetTransactionReceipt(hash, nil) } // GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash. From eb67425433a4addc775578c668f4f22bca63d418 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 17 Jun 2025 17:21:57 +0900 Subject: [PATCH 06/12] remove empty space --- rpc/backend/blocks.go | 1 - 1 file changed, 1 deletion(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index bfd21e01c7..20fec10a95 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -92,7 +92,6 @@ func (b *Backend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) ( } // GetBlockReceipts returns a list of Ethereum transaction receipts given a block number - func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string]interface{}, error) { resBlock, err := b.TendermintBlockByNumber(blockNum) if err != nil { From 98b30789fa0c7dc0b4471567a769ce772a101efe Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 17 Jun 2025 18:18:30 +0900 Subject: [PATCH 07/12] add test case --- rpc/backend/blocks_test.go | 107 +++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/rpc/backend/blocks_test.go b/rpc/backend/blocks_test.go index f40e96d6c8..991b9f6842 100644 --- a/rpc/backend/blocks_test.go +++ b/rpc/backend/blocks_test.go @@ -1,13 +1,17 @@ package backend import ( + tmlog "cosmossdk.io/log" "encoding/json" "fmt" + dbm "github.com/cosmos/cosmos-db" + "github.com/evmos/ethermint/indexer" "math/big" sdkmath "cosmossdk.io/math" "github.com/cometbft/cometbft/abci/types" tmrpctypes "github.com/cometbft/cometbft/rpc/core/types" + comettypes "github.com/cometbft/cometbft/types" tmtypes "github.com/cometbft/cometbft/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" @@ -1644,3 +1648,106 @@ func (suite *BackendTestSuite) TestEthBlockFromTendermintBlock() { }) } } + +// TODO fix this test case and TestGetTransactionReceipt +func (suite *BackendTestSuite) TestEthBlockReceipts() { + msgEthereumTx, _ := suite.buildEthereumTx() + txBz := suite.signAndEncodeEthTx(msgEthereumTx) + txHash := msgEthereumTx.Hash() + + testCases := []struct { + name string + registerMock func() + tx *evmtypes.MsgEthereumTx + block *comettypes.Block + blockResult []*types.ExecTxResult + expTxReceipt map[string]interface{} + expPass bool + }{ + { + "fail - Receipts do not match ", + func() { + var header metadata.MD + queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) + client := suite.backend.clientCtx.Client.(*mocks.Client) + RegisterParams(queryClient, &header, 1) + RegisterParamsWithoutHeader(queryClient, 1) + RegisterBlock(client, 1, txBz) + RegisterBlockResults(client, 1) + }, + msgEthereumTx, + &comettypes.Block{Header: comettypes.Header{Height: 1}, Data: comettypes.Data{Txs: []comettypes.Tx{txBz}}}, + []*types.ExecTxResult{ + { + Code: 0, + Events: []types.Event{ + {Type: evmtypes.EventTypeEthereumTx, Attributes: []types.EventAttribute{ + {Key: "ethereumTxHash", Value: txHash.Hex()}, + {Key: "txIndex", Value: "0"}, + {Key: "amount", Value: "1000"}, + {Key: "txGasUsed", Value: "21000"}, + {Key: "txHash", Value: ""}, + {Key: "recipient", Value: "0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7"}, + }}, + }, + }, + }, + map[string]interface{}(nil), + false, + }, + { + "Success - Receipts match", + func() { + var header metadata.MD + queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) + client := suite.backend.clientCtx.Client.(*mocks.Client) + RegisterParams(queryClient, &header, 1) + RegisterParamsWithoutHeader(queryClient, 1) + RegisterBlock(client, 1, txBz) + RegisterBlockResults(client, 1) + }, + msgEthereumTx, + &comettypes.Block{Header: comettypes.Header{Height: 1}, Data: comettypes.Data{Txs: []comettypes.Tx{txBz}}}, + []*types.ExecTxResult{ + { + Code: 0, + Events: []types.Event{ + {Type: evmtypes.EventTypeEthereumTx, Attributes: []types.EventAttribute{ + {Key: "ethereumTxHash", Value: txHash.Hex()}, + {Key: "txIndex", Value: "0"}, + {Key: "amount", Value: "1000"}, + {Key: "txGasUsed", Value: "21000"}, + {Key: "txHash", Value: ""}, + {Key: "recipient", Value: "0x775b87ef5D82ca211811C1a02CE0fE0CA3a455d7"}, + }}, + }, + }, + }, + map[string]interface{}(nil), + false, //needs to be set to true + }, + } + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.name), func() { + suite.SetupTest() // reset test and queries + tc.registerMock() + + db := dbm.NewMemDB() + suite.backend.indexer = indexer.NewKVIndexer(db, tmlog.NewNopLogger(), suite.backend.clientCtx) + err := suite.backend.indexer.IndexBlock(tc.block, tc.blockResult) + suite.Require().NoError(err) + + receipts, err := suite.backend.GetBlockReceipts(ethrpc.BlockNumber(1)) + + for receipt := range receipts { + if tc.expPass { + suite.Require().NoError(err) + suite.Require().Equal(receipt, tc.expTxReceipt) + } else { + suite.Require().NotEqual(receipt, tc.expTxReceipt) + } + } + + }) + } +} From 7e5ed3a0e930f9a8b0190a8c8faf862c618c0ffb Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 17 Jun 2025 19:19:19 +0900 Subject: [PATCH 08/12] fix length and add test --- rpc/backend/blocks.go | 4 +-- rpc/backend/blocks_test.go | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index 20fec10a95..b98f2b26b9 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -109,7 +109,7 @@ func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string] txHashes := b.TransactionHashesFromTendermintBlock(resBlock, blockRes) - res := make([]map[string]interface{}, len(txHashes)) + res := make([]map[string]interface{}, 0, len(txHashes)) for _, txHash := range txHashes { receipt, err := b.GetTransactionReceipt(txHash, resBlock) if err != nil { @@ -550,7 +550,7 @@ func (b *Backend) TransactionHashesFromTendermintBlock( blockRes *tmrpctypes.ResultBlockResults, ) []common.Hash { msgs := b.EthMsgsFromTendermintBlock(resBlock, blockRes) - ethHashes := make([]common.Hash, len(msgs)) + ethHashes := make([]common.Hash, 0, len(msgs)) for _, ethMsg := range msgs { ethHashes = append(ethHashes, ethMsg.Hash()) } diff --git a/rpc/backend/blocks_test.go b/rpc/backend/blocks_test.go index 991b9f6842..d51a4d9b99 100644 --- a/rpc/backend/blocks_test.go +++ b/rpc/backend/blocks_test.go @@ -1751,3 +1751,53 @@ func (suite *BackendTestSuite) TestEthBlockReceipts() { }) } } + +func (suite *BackendTestSuite) TestTransactionHashesFromTendermintBlock() { + msgEthereumTx, bz := suite.buildEthereumTx() + emptyBlock := tmtypes.MakeBlock(1, []tmtypes.Tx{}, nil, nil) + testCases := []struct { + name string + resBlock *tmrpctypes.ResultBlock + blockRes *tmrpctypes.ResultBlockResults + expHashes []common.Hash + }{ + { + "empty block", + &tmrpctypes.ResultBlock{ + Block: emptyBlock, + }, + &tmrpctypes.ResultBlockResults{ + Height: 1, + TxsResults: []*types.ExecTxResult{{Code: 0, GasUsed: 0}}, + }, + []common.Hash{}, + }, + { + "block with tx", + &tmrpctypes.ResultBlock{ + Block: tmtypes.MakeBlock(1, []tmtypes.Tx{bz}, nil, nil), + }, + &tmrpctypes.ResultBlockResults{ + Height: 1, + TxsResults: []*types.ExecTxResult{{Code: 0, GasUsed: 0}}, + FinalizeBlockEvents: []types.Event{ + { + Type: evmtypes.EventTypeBlockBloom, + Attributes: []types.EventAttribute{ + {Key: string(bAttributeKeyEthereumBloom)}, + }, + }, + }, + }, + []common.Hash{msgEthereumTx.Hash()}, + }, + } + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.name), func() { + suite.SetupTest() // reset test and queries + hashes := suite.backend.TransactionHashesFromTendermintBlock(tc.resBlock, tc.blockRes) + + suite.Require().Equal(tc.expHashes, hashes) + }) + } +} From e7e74cd3fd6c79fbaf40270d44bb4ee48fdd4938 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Wed, 18 Jun 2025 09:39:15 +0900 Subject: [PATCH 09/12] return err details --- rpc/backend/blocks.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index b98f2b26b9..39f6f33754 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -68,18 +68,18 @@ func (b *Backend) BlockNumber() (hexutil.Uint64, error) { func (b *Backend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) (map[string]interface{}, error) { resBlock, err := b.TendermintBlockByNumber(blockNum) if err != nil { - return nil, nil + return nil, err } // return if requested block height is greater than the current one if resBlock == nil || resBlock.Block == nil { - return nil, nil + return nil, fmt.Errorf("unexpected block number") } blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height) if err != nil { b.logger.Debug("failed to fetch block result from Tendermint", "height", blockNum, "error", err.Error()) - return nil, nil + return nil, err } res, err := b.RPCBlockFromTendermintBlock(resBlock, blockRes, fullTx) @@ -95,16 +95,16 @@ func (b *Backend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) ( func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string]interface{}, error) { resBlock, err := b.TendermintBlockByNumber(blockNum) if err != nil { - return nil, nil + return nil, err } // return if requested block height is greater than the current one if resBlock == nil || resBlock.Block == nil { - return nil, nil + return nil, fmt.Errorf("unexpected block number") } blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height) if err != nil { b.logger.Debug("failed to fetch block result from Tendermint", "height", blockNum, "error", err.Error()) - return nil, nil + return nil, err } txHashes := b.TransactionHashesFromTendermintBlock(resBlock, blockRes) @@ -131,13 +131,13 @@ func (b *Backend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]inte if resBlock == nil { // block not found - return nil, nil + return nil, fmt.Errorf("block not found") } blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height) if err != nil { b.logger.Debug("failed to fetch block result from Tendermint", "block-hash", hash.String(), "error", err.Error()) - return nil, nil + return nil, err } res, err := b.RPCBlockFromTendermintBlock(resBlock, blockRes, fullTx) @@ -215,7 +215,7 @@ func (b *Backend) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpc if resBlock.Block == nil { b.logger.Debug("TendermintBlockByNumber block not found", "height", height) - return nil, nil + return nil, fmt.Errorf("block not found") } return resBlock, nil @@ -275,7 +275,7 @@ func (b *Backend) TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.Resu if resBlock == nil || resBlock.Block == nil { b.logger.Debug("TendermintBlockByHash block not found", "blockHash", blockHash.Hex()) - return nil, nil + return nil, fmt.Errorf("block not found") } return resBlock, nil From 8f9614ca365f8e29c2fdd9ed7cc9e04b0a3d7bcb Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Wed, 18 Jun 2025 13:15:42 +0900 Subject: [PATCH 10/12] fix tests --- rpc/backend/blocks.go | 14 +++++++------- rpc/backend/blocks_test.go | 10 +++++----- rpc/backend/chain_info_test.go | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index 39f6f33754..287aa79934 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -68,12 +68,12 @@ func (b *Backend) BlockNumber() (hexutil.Uint64, error) { func (b *Backend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) (map[string]interface{}, error) { resBlock, err := b.TendermintBlockByNumber(blockNum) if err != nil { - return nil, err + return nil, nil } // return if requested block height is greater than the current one if resBlock == nil || resBlock.Block == nil { - return nil, fmt.Errorf("unexpected block number") + return nil, nil } blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height) @@ -95,11 +95,11 @@ func (b *Backend) GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) ( func (b *Backend) GetBlockReceipts(blockNum rpctypes.BlockNumber) ([]map[string]interface{}, error) { resBlock, err := b.TendermintBlockByNumber(blockNum) if err != nil { - return nil, err + return nil, nil } // return if requested block height is greater than the current one if resBlock == nil || resBlock.Block == nil { - return nil, fmt.Errorf("unexpected block number") + return nil, nil } blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height) if err != nil { @@ -131,7 +131,7 @@ func (b *Backend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]inte if resBlock == nil { // block not found - return nil, fmt.Errorf("block not found") + return nil, nil } blockRes, err := b.TendermintBlockResultByNumber(&resBlock.Block.Height) @@ -215,7 +215,7 @@ func (b *Backend) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpc if resBlock.Block == nil { b.logger.Debug("TendermintBlockByNumber block not found", "height", height) - return nil, fmt.Errorf("block not found") + return nil, fmt.Errorf("Tendermint block not found") } return resBlock, nil @@ -275,7 +275,7 @@ func (b *Backend) TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.Resu if resBlock == nil || resBlock.Block == nil { b.logger.Debug("TendermintBlockByHash block not found", "blockHash", blockHash.Hex()) - return nil, fmt.Errorf("block not found") + return nil, fmt.Errorf("Tendermint block not found") } return resBlock, nil diff --git a/rpc/backend/blocks_test.go b/rpc/backend/blocks_test.go index d51a4d9b99..4c00cd860a 100644 --- a/rpc/backend/blocks_test.go +++ b/rpc/backend/blocks_test.go @@ -136,7 +136,7 @@ func (suite *BackendTestSuite) TestGetBlockByNumber() { true, }, { - "pass - block results error", + "fail - block results error", ethrpc.BlockNumber(1), true, sdkmath.NewInt(1).BigInt(), @@ -150,7 +150,7 @@ func (suite *BackendTestSuite) TestGetBlockByNumber() { RegisterBlockResultsError(client, blockNum.Int64()) }, true, - true, + false, }, { "pass - without tx", @@ -276,7 +276,7 @@ func (suite *BackendTestSuite) TestGetBlockByHash() { RegisterBlockByHashNotFound(client, hash, txBz) }, true, - true, + false, }, { "noop - tendermint failed to fetch block result", @@ -294,7 +294,7 @@ func (suite *BackendTestSuite) TestGetBlockByHash() { RegisterBlockResultsError(client, height) }, true, - true, + false, }, { "pass - without tx", @@ -551,7 +551,7 @@ func (suite *BackendTestSuite) TestTendermintBlockByNumber() { RegisterBlockNotFound(client, height) }, false, - true, + false, }, { "fail - blockNum < 0 with app state height error", diff --git a/rpc/backend/chain_info_test.go b/rpc/backend/chain_info_test.go index 0c2025116d..4eb01ce378 100644 --- a/rpc/backend/chain_info_test.go +++ b/rpc/backend/chain_info_test.go @@ -410,7 +410,7 @@ func (suite *BackendTestSuite) TestFeeHistory() { 1, nil, nil, - true, + false, nil, }, { From b4cf2059a3f6fe1f3fd17853476d228b616092c6 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Wed, 18 Jun 2025 13:30:06 +0900 Subject: [PATCH 11/12] fix lint --- rpc/backend/blocks.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index 287aa79934..ec63cb3834 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -215,7 +215,7 @@ func (b *Backend) TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpc if resBlock.Block == nil { b.logger.Debug("TendermintBlockByNumber block not found", "height", height) - return nil, fmt.Errorf("Tendermint block not found") + return nil, fmt.Errorf("tendermint block not found") } return resBlock, nil @@ -275,7 +275,7 @@ func (b *Backend) TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.Resu if resBlock == nil || resBlock.Block == nil { b.logger.Debug("TendermintBlockByHash block not found", "blockHash", blockHash.Hex()) - return nil, fmt.Errorf("Tendermint block not found") + return nil, fmt.Errorf("tendermint block not found") } return resBlock, nil From e29673e898f8f06454649d06388743354ad3b075 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Wed, 18 Jun 2025 14:06:16 +0900 Subject: [PATCH 12/12] fix tests --- rpc/backend/blocks.go | 2 +- rpc/backend/blocks_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/backend/blocks.go b/rpc/backend/blocks.go index ec63cb3834..73dd7d481b 100644 --- a/rpc/backend/blocks.go +++ b/rpc/backend/blocks.go @@ -275,7 +275,7 @@ func (b *Backend) TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.Resu if resBlock == nil || resBlock.Block == nil { b.logger.Debug("TendermintBlockByHash block not found", "blockHash", blockHash.Hex()) - return nil, fmt.Errorf("tendermint block not found") + return nil, nil } return resBlock, nil diff --git a/rpc/backend/blocks_test.go b/rpc/backend/blocks_test.go index 4c00cd860a..9b84939cae 100644 --- a/rpc/backend/blocks_test.go +++ b/rpc/backend/blocks_test.go @@ -276,7 +276,7 @@ func (suite *BackendTestSuite) TestGetBlockByHash() { RegisterBlockByHashNotFound(client, hash, txBz) }, true, - false, + true, }, { "noop - tendermint failed to fetch block result",