eth/tracers: add txHash field on txTraceResult (#27183)

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 <martin@swende.se>
This commit is contained in:
sjlee1125 2023-05-05 21:59:13 +09:00 committed by GitHub
parent ba09403113
commit 604e215d1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 15 deletions

View File

@ -176,6 +176,7 @@ type StdTraceConfig struct {
// txTraceResult is the result of a single transaction trace. // txTraceResult is the result of a single transaction trace.
type txTraceResult struct { type txTraceResult struct {
TxHash common.Hash `json:"txHash"` // transaction hash
Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer
Error string `json:"error,omitempty"` // Trace failure produced by the tracer Error string `json:"error,omitempty"` // Trace failure produced by the tracer
} }
@ -278,13 +279,13 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed
} }
res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config) res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config)
if err != nil { 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) log.Warn("Tracing failed", "hash", tx.Hash(), "block", task.block.NumberU64(), "err", err)
break break
} }
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect // 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.statedb.Finalise(api.backend.ChainConfig().IsEIP158(task.block.Number()))
task.results[i] = &txTraceResult{Result: res} task.results[i] = &txTraceResult{TxHash: tx.Hash(), Result: res}
} }
// Tracing state is used up, queue it for de-referencing. Note the // Tracing state is used up, queue it for de-referencing. Note the
// state is the parent state of trace block, use block.number-1 as // state is the parent state of trace block, use block.number-1 as
@ -615,7 +616,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
if err != nil { if err != nil {
return nil, err return nil, err
} }
results[i] = &txTraceResult{Result: res} results[i] = &txTraceResult{TxHash: tx.Hash(), Result: res}
// Finalize the state so any modifications are written to the trie // Finalize the state so any modifications are written to the trie
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
statedb.Finalise(is158) statedb.Finalise(is158)
@ -656,10 +657,10 @@ func (api *API) traceBlockParallel(ctx context.Context, block *types.Block, stat
} }
res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config) res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config)
if err != nil { if err != nil {
results[task.index] = &txTraceResult{Error: err.Error()} results[task.index] = &txTraceResult{TxHash: txs[task.index].Hash(), Error: err.Error()}
continue continue
} }
results[task.index] = &txTraceResult{Result: res} results[task.index] = &txTraceResult{TxHash: txs[task.index].Hash(), Result: res}
} }
}() }()
} }

View File

@ -384,12 +384,14 @@ func TestTraceBlock(t *testing.T) {
} }
genBlocks := 10 genBlocks := 10
signer := types.HomesteadSigner{} signer := types.HomesteadSigner{}
var txHash common.Hash
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) { backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1] // Transfer from account[0] to account[1]
// value: 1000 wei // value: 1000 wei
// fee: 0 wei // fee: 0 wei
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key) tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
b.AddTx(tx) b.AddTx(tx)
txHash = tx.Hash()
}) })
defer backend.chain.Stop() defer backend.chain.Stop()
api := NewAPI(backend) api := NewAPI(backend)
@ -408,7 +410,7 @@ func TestTraceBlock(t *testing.T) {
// Trace head block // Trace head block
{ {
blockNumber: rpc.BlockNumber(genBlocks), 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 // Trace non-existent block
{ {
@ -418,12 +420,12 @@ func TestTraceBlock(t *testing.T) {
// Trace latest block // Trace latest block
{ {
blockNumber: rpc.LatestBlockNumber, 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 // Trace pending block
{ {
blockNumber: rpc.PendingBlockNumber, 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 { for i, tc := range testSuite {
@ -853,7 +855,7 @@ func TestTraceChain(t *testing.T) {
backend.relHook = func() { rel.Add(1) } backend.relHook = func() { rel.Add(1) }
api := NewAPI(backend) api := NewAPI(backend)
single := `{"result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}` single := `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000000","result":{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}}`
var cases = []struct { var cases = []struct {
start uint64 start uint64
end uint64 end uint64
@ -872,16 +874,17 @@ func TestTraceChain(t *testing.T) {
next := c.start + 1 next := c.start + 1
for result := range resCh { for result := range resCh {
if next != uint64(result.Block) { if have, want := uint64(result.Block), next; have != want {
t.Error("Unexpected tracing block") t.Fatalf("unexpected tracing block, have %d want %d", have, want)
} }
if len(result.Traces) != int(next) { if have, want := len(result.Traces), int(next); have != want {
t.Error("Unexpected tracing result") t.Fatalf("unexpected result length, have %d want %d", have, want)
} }
for _, trace := range result.Traces { for _, trace := range result.Traces {
trace.TxHash = common.Hash{}
blob, _ := json.Marshal(trace) blob, _ := json.Marshal(trace)
if string(blob) != single { if have, want := string(blob), single; have != want {
t.Error("Unexpected tracing result") t.Fatalf("unexpected tracing result, have\n%v\nwant:\n%v", have, want)
} }
} }
next += 1 next += 1