Skip to content

Commit

Permalink
eth/tracers: add txHash field on txTraceResult (#27183)
Browse files Browse the repository at this point in the history
This PR modifies the interface for the results of `debug_traceBlock` and `debug_traceCall` by adding the `txHash`, allowing users to identify which transaction's trace result corresponds to.

---------

Co-authored-by: Martin Holst Swende <[email protected]>
  • Loading branch information
2 people authored and carterqw2 committed Feb 12, 2024
1 parent 6388f0e commit 213811e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 5 additions & 4 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ type StdTraceConfig struct {

// txTraceResult is the result of a single transaction trace.
type txTraceResult struct {
TxHash common.Hash `json:"txHash"` // transaction hash
Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer
Error string `json:"error,omitempty"` // Trace failure produced by the tracer
}
Expand Down Expand Up @@ -322,13 +323,13 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config
vmRunner := api.backend.NewEVMRunner(task.block.Header(), task.statedb)
res, err := api.traceTx(localctx, msg, txctx, blockCtx, vmRunner, task.statedb, sysCtx, config)
if err != nil {
task.results[i] = &txTraceResult{Error: err.Error()}
task.results[i] = &txTraceResult{TxHash: tx.Hash(), Error: err.Error()}
log.Warn("Tracing failed", "hash", tx.Hash(), "block", task.block.NumberU64(), "err", err)
break
}
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
task.statedb.Finalise(api.backend.ChainConfig().IsEIP158(task.block.Number()))
task.results[i] = &txTraceResult{Result: res}
task.results[i] = &txTraceResult{TxHash: tx.Hash(), Result: res}
}
// Stream the result back to the user or abort on teardown
select {
Expand Down Expand Up @@ -667,10 +668,10 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
vmRunner := api.backend.NewEVMRunner(block.Header(), task.statedb)
res, err := api.traceTx(ctx, msg, txctx, blockCtx, vmRunner, task.statedb, sysCtx, config)
if err != nil {
results[task.index] = &txTraceResult{Error: err.Error()}
results[task.index] = &txTraceResult{TxHash: txs[task.index].Hash(), Error: err.Error()}
continue
}
results[task.index] = &txTraceResult{Result: res}
results[task.index] = &txTraceResult{TxHash: txs[task.index].Hash(), Result: res}
}
}()
}
Expand Down
8 changes: 5 additions & 3 deletions eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,14 @@ func TestTraceBlock(t *testing.T) {
}}
genBlocks := 10
signer := types.HomesteadSigner{}
var txHash common.Hash
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1]
// value: 1000 wei
// fee: 0 wei
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.MinimumGasPrice(nil), nil), signer, accounts[0].key)
b.AddTx(tx)
txHash = tx.Hash()
}))

var testSuite = []struct {
Expand All @@ -388,7 +390,7 @@ func TestTraceBlock(t *testing.T) {
// Trace head block
{
blockNumber: rpc.BlockNumber(genBlocks),
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
want: fmt.Sprintf(`[{"txHash":"%v","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`, txHash),
},
// Trace non-existent block
{
Expand All @@ -398,12 +400,12 @@ func TestTraceBlock(t *testing.T) {
// Trace latest block
{
blockNumber: rpc.LatestBlockNumber,
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
want: fmt.Sprintf(`[{"txHash":"%v","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`, txHash),
},
// Trace pending block
{
blockNumber: rpc.PendingBlockNumber,
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
want: fmt.Sprintf(`[{"txHash":"%v","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`, txHash),
},
}
for i, tc := range testSuite {
Expand Down

0 comments on commit 213811e

Please sign in to comment.