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

Commit 481e3b8

Browse files
calvinacofedekunze
andauthored
rpc: add debug_tranceBlockByHash, fix debug_traceBlock* crash on non-existing block (#743)
* Problem: Missing debug_tranceBlockByHash RPC method Solution: (Fix #742) Add the missing method * Problem: debug_traceBlockByNumber crashed when block height not found Solution: (Fix #744) Fix memory reference error * Run go fmt * Revert comment on init.sh * Apply suggestions from code review Co-authored-by: Federico Kunze Küllmer <[email protected]> * Update rpc/ethereum/backend/backend.go * Update rpc/ethereum/backend/backend.go Co-authored-by: Federico Kunze Küllmer <[email protected]>
1 parent 72c1098 commit 481e3b8

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
4949
* (rpc) [tharsis#624](https://github.com/tharsis/ethermint/pull/624) Implement new JSON-RPC endpoints from latest geth version
5050
* (evm) [tharsis#662](https://github.com/tharsis/ethermint/pull/662) Disable basefee for non london blocks
5151
* (cmd) [tharsis#712](https://github.com/tharsis/ethermint/pull/712) add tx cli to build evm transaction
52-
* (rpc) [tharsis#733](https://github.com/tharsis/ethermint/pull/733) add JSON_RPC endpoint personal_unpair
53-
* (rpc) [tharsis#740](https://github.com/tharsis/ethermint/pull/740) add JSON_RPC endpoint personal_initializeWallet
52+
* (rpc) [tharsis#733](https://github.com/tharsis/ethermint/pull/733) add JSON_RPC endpoint `personal_unpair`
53+
* (rpc) [tharsis#740](https://github.com/tharsis/ethermint/pull/740) add JSON_RPC endpoint `personal_initializeWallet`
54+
* (rpc) [tharsis#743](https://github.com/tharsis/ethermint/pull/743) add JSON_RPC endpoint `debug_traceBlockByHash`
5455

5556
### Bug Fixes
5657

@@ -61,6 +62,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
6162
* (evm, test) [tharsis#649](https://github.com/tharsis/ethermint/pull/649) Test DynamicFeeTx.
6263
* (evm) [tharsis#702](https://github.com/tharsis/ethermint/pull/702) Fix panic in web3 RPC handlers
6364
* (rpc) [tharsis#720](https://github.com/tharsis/ethermint/pull/720) Fix `debug_traceTransaction` failure
65+
* (rpc) [tharsis#743](https://github.com/tharsis/ethermint/pull/743) Fix debug JSON RPC handler crash on non-existing block
6466

6567
### Improvements
6668

init.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ethermintd config chain-id $CHAINID
2525
ethermintd keys add $KEY --keyring-backend $KEYRING --algo $KEYALGO
2626

2727
# Set moniker and chain-id for Ethermint (Moniker can be anything, chain-id must be an integer)
28-
ethermintd init $MONIKER --chain-id $CHAINID
28+
ethermintd init $MONIKER --chain-id $CHAINID
2929

3030
# Change parameter token denominations to aphoton
3131
cat $HOME/.ethermintd/config/genesis.json | jq '.app_state["staking"]["params"]["bond_denom"]="aphoton"' > $HOME/.ethermintd/config/tmp_genesis.json && mv $HOME/.ethermintd/config/tmp_genesis.json $HOME/.ethermintd/config/genesis.json

rpc/ethereum/backend/backend.go

+16
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Backend interface {
5353
// Blockchain API
5454
BlockNumber() (hexutil.Uint64, error)
5555
GetTendermintBlockByNumber(blockNum types.BlockNumber) (*tmrpctypes.ResultBlock, error)
56+
GetTendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error)
5657
GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (map[string]interface{}, error)
5758
GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error)
5859
BlockByNumber(blockNum types.BlockNumber) (*ethtypes.Block, error)
@@ -306,6 +307,21 @@ func (e *EVMBackend) GetTendermintBlockByNumber(blockNum types.BlockNumber) (*tm
306307
return resBlock, nil
307308
}
308309

310+
// GetTendermintBlockByHash returns a Tendermint format block by block number
311+
func (e *EVMBackend) GetTendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error) {
312+
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, blockHash.Bytes())
313+
if err != nil {
314+
e.logger.Debug("tendermint client failed to get block", "blockHash", blockHash.Hex(), "error", err.Error())
315+
}
316+
317+
if resBlock == nil || resBlock.Block == nil {
318+
e.logger.Debug("GetBlockByNumber block not found", "blockHash", blockHash.Hex())
319+
return nil, nil
320+
}
321+
322+
return resBlock, nil
323+
}
324+
309325
// BlockBloom query block bloom filter from block results
310326
func (e *EVMBackend) BlockBloom(height *int64) (ethtypes.Bloom, error) {
311327
result, err := e.clientCtx.Client.BlockResults(e.ctx, height)

rpc/ethereum/namespaces/debug/api.go

+20
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,32 @@ func (a *API) TraceBlockByNumber(height rpctypes.BlockNumber, config *evmtypes.T
167167
// Get Tendermint Block
168168
resBlock, err := a.backend.GetTendermintBlockByNumber(height)
169169
if err != nil {
170+
a.logger.Debug("get block failed", "height", height, "error", err.Error())
170171
return nil, err
171172
}
172173

173174
return a.traceBlock(height, config, resBlock)
174175
}
175176

177+
// TraceBlockByHash returns the structured logs created during the execution of
178+
// EVM and returns them as a JSON object.
179+
func (a *API) TraceBlockByHash(hash common.Hash, config *evmtypes.TraceConfig) ([]*evmtypes.TxTraceResult, error) {
180+
a.logger.Debug("debug_traceBlockByHash", "hash", hash)
181+
// Get Tendermint Block
182+
resBlock, err := a.backend.GetTendermintBlockByHash(hash)
183+
if err != nil {
184+
a.logger.Debug("get block failed", "hash", hash.Hex(), "error", err.Error())
185+
return nil, err
186+
}
187+
188+
if resBlock == nil || resBlock.Block == nil {
189+
a.logger.Debug("block not found", "hash", hash.Hex())
190+
return nil, errors.New("block not found")
191+
}
192+
193+
return a.traceBlock(rpctypes.BlockNumber(resBlock.Block.Height), config, resBlock)
194+
}
195+
176196
// traceBlock configures a new tracer according to the provided configuration, and
177197
// executes all the transactions contained within. The return value will be one item
178198
// per transaction, dependent on the requested tracer.

0 commit comments

Comments
 (0)