Skip to content

Commit 1fc3473

Browse files
carterqw2sjlee1125holiman
authored
eth/tracers: add txHash field on txTraceResult (#2254)
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: sjlee1125 <[email protected]> Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 6388f0e commit 1fc3473

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

eth/tracers/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ type StdTraceConfig struct {
205205

206206
// txTraceResult is the result of a single transaction trace.
207207
type txTraceResult struct {
208+
TxHash common.Hash `json:"txHash"` // transaction hash
208209
Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer
209210
Error string `json:"error,omitempty"` // Trace failure produced by the tracer
210211
}
@@ -322,13 +323,13 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config
322323
vmRunner := api.backend.NewEVMRunner(task.block.Header(), task.statedb)
323324
res, err := api.traceTx(localctx, msg, txctx, blockCtx, vmRunner, task.statedb, sysCtx, config)
324325
if err != nil {
325-
task.results[i] = &txTraceResult{Error: err.Error()}
326+
task.results[i] = &txTraceResult{TxHash: tx.Hash(), Error: err.Error()}
326327
log.Warn("Tracing failed", "hash", tx.Hash(), "block", task.block.NumberU64(), "err", err)
327328
break
328329
}
329330
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
330331
task.statedb.Finalise(api.backend.ChainConfig().IsEIP158(task.block.Number()))
331-
task.results[i] = &txTraceResult{Result: res}
332+
task.results[i] = &txTraceResult{TxHash: tx.Hash(), Result: res}
332333
}
333334
// Stream the result back to the user or abort on teardown
334335
select {
@@ -667,10 +668,10 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
667668
vmRunner := api.backend.NewEVMRunner(block.Header(), task.statedb)
668669
res, err := api.traceTx(ctx, msg, txctx, blockCtx, vmRunner, task.statedb, sysCtx, config)
669670
if err != nil {
670-
results[task.index] = &txTraceResult{Error: err.Error()}
671+
results[task.index] = &txTraceResult{TxHash: txs[task.index].Hash(), Error: err.Error()}
671672
continue
672673
}
673-
results[task.index] = &txTraceResult{Result: res}
674+
results[task.index] = &txTraceResult{TxHash: txs[task.index].Hash(), Result: res}
674675
}
675676
}()
676677
}

eth/tracers/api_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,14 @@ func TestTraceBlock(t *testing.T) {
366366
}}
367367
genBlocks := 10
368368
signer := types.HomesteadSigner{}
369+
var txHash common.Hash
369370
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
370371
// Transfer from account[0] to account[1]
371372
// value: 1000 wei
372373
// fee: 0 wei
373374
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.MinimumGasPrice(nil), nil), signer, accounts[0].key)
374375
b.AddTx(tx)
376+
txHash = tx.Hash()
375377
}))
376378

377379
var testSuite = []struct {
@@ -388,7 +390,7 @@ func TestTraceBlock(t *testing.T) {
388390
// Trace head block
389391
{
390392
blockNumber: rpc.BlockNumber(genBlocks),
391-
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
393+
want: fmt.Sprintf(`[{"txHash":"%v","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`, txHash),
392394
},
393395
// Trace non-existent block
394396
{
@@ -398,12 +400,12 @@ func TestTraceBlock(t *testing.T) {
398400
// Trace latest block
399401
{
400402
blockNumber: rpc.LatestBlockNumber,
401-
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
403+
want: fmt.Sprintf(`[{"txHash":"%v","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`, txHash),
402404
},
403405
// Trace pending block
404406
{
405407
blockNumber: rpc.PendingBlockNumber,
406-
want: `[{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`,
408+
want: fmt.Sprintf(`[{"txHash":"%v","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}]`, txHash),
407409
},
408410
}
409411
for i, tc := range testSuite {

0 commit comments

Comments
 (0)