Skip to content

Commit c6d4500

Browse files
authored
eth, les: properly init statedb accesslist during tracing (#22480)
* eth/state, les/state: properly init statedb accesslist when tracing, fixes #22475 * eth: review comments * eth/tracers: fix compilation err * eth/tracers: apply @karalabe's suggested fix
1 parent 3f74c8e commit c6d4500

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

‎eth/state_accessor.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ func (eth *Ethereum) stateAtTransaction(block *types.Block, txIndex int, reexec
217217
}
218218
// Not yet the searched for transaction, execute on top of the current state
219219
vmenv := vm.NewEVM(context, txContext, statedb, eth.blockchain.Config(), vm.Config{})
220+
statedb.Prepare(tx.Hash(), block.Hash(), idx)
220221
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
221222
release()
222223
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)

‎eth/tracers/api.go‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ type StdTraceConfig struct {
169169
TxHash common.Hash
170170
}
171171

172+
// txTraceContext is the contextual infos about a transaction before it gets run.
173+
type txTraceContext struct {
174+
index int // Index of the transaction within the block
175+
hash common.Hash // Hash of the transaction
176+
block common.Hash // Hash of the block containing the transaction
177+
}
178+
172179
// txTraceResult is the result of a single transaction trace.
173180
type txTraceResult struct {
174181
Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer
@@ -266,7 +273,12 @@ func (api *API) traceChain(ctx context.Context, start, end *types.Block, config
266273
// Trace all the transactions contained within
267274
for i, tx := range task.block.Transactions() {
268275
msg, _ := tx.AsMessage(signer)
269-
res, err := api.traceTx(ctx, msg, blockCtx, task.statedb, config)
276+
txctx := &txTraceContext{
277+
index: i,
278+
hash: tx.Hash(),
279+
block: task.block.Hash(),
280+
}
281+
res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config)
270282
if err != nil {
271283
task.results[i] = &txTraceResult{Error: err.Error()}
272284
log.Warn("Tracing failed", "hash", tx.Hash(), "block", task.block.NumberU64(), "err", err)
@@ -478,14 +490,20 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
478490
threads = len(txs)
479491
}
480492
blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
493+
blockHash := block.Hash()
481494
for th := 0; th < threads; th++ {
482495
pend.Add(1)
483496
go func() {
484497
defer pend.Done()
485498
// Fetch and execute the next transaction trace tasks
486499
for task := range jobs {
487500
msg, _ := txs[task.index].AsMessage(signer)
488-
res, err := api.traceTx(ctx, msg, blockCtx, task.statedb, config)
501+
txctx := &txTraceContext{
502+
index: task.index,
503+
hash: txs[task.index].Hash(),
504+
block: blockHash,
505+
}
506+
res, err := api.traceTx(ctx, msg, txctx, blockCtx, task.statedb, config)
489507
if err != nil {
490508
results[task.index] = &txTraceResult{Error: err.Error()}
491509
continue
@@ -502,9 +520,8 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
502520

503521
// Generate the next state snapshot fast without tracing
504522
msg, _ := tx.AsMessage(signer)
505-
txContext := core.NewEVMTxContext(msg)
506-
507-
vmenv := vm.NewEVM(blockCtx, txContext, statedb, api.backend.ChainConfig(), vm.Config{})
523+
statedb.Prepare(tx.Hash(), block.Hash(), i)
524+
vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{})
508525
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil {
509526
failed = err
510527
break
@@ -619,6 +636,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
619636
}
620637
// Execute the transaction and flush any traces to disk
621638
vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf)
639+
statedb.Prepare(tx.Hash(), block.Hash(), i)
622640
_, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()))
623641
if writer != nil {
624642
writer.Flush()
@@ -678,7 +696,12 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *
678696
}
679697
defer release()
680698

681-
return api.traceTx(ctx, msg, vmctx, statedb, config)
699+
txctx := &txTraceContext{
700+
index: int(index),
701+
hash: hash,
702+
block: blockHash,
703+
}
704+
return api.traceTx(ctx, msg, txctx, vmctx, statedb, config)
682705
}
683706

684707
// TraceCall lets you trace a given eth_call. It collects the structured logs
@@ -713,13 +736,14 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.CallArgs, blockNrOrHa
713736
// Execute the trace
714737
msg := args.ToMessage(api.backend.RPCGasCap())
715738
vmctx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
716-
return api.traceTx(ctx, msg, vmctx, statedb, config)
739+
740+
return api.traceTx(ctx, msg, new(txTraceContext), vmctx, statedb, config)
717741
}
718742

719743
// traceTx configures a new tracer according to the provided configuration, and
720744
// executes the given message in the provided environment. The return value will
721745
// be tracer dependent.
722-
func (api *API) traceTx(ctx context.Context, message core.Message, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig) (interface{}, error) {
746+
func (api *API) traceTx(ctx context.Context, message core.Message, txctx *txTraceContext, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig) (interface{}, error) {
723747
// Assemble the structured logger or the JavaScript tracer
724748
var (
725749
tracer vm.Tracer
@@ -753,9 +777,12 @@ func (api *API) traceTx(ctx context.Context, message core.Message, vmctx vm.Bloc
753777
default:
754778
tracer = vm.NewStructLogger(config.LogConfig)
755779
}
756-
757780
// Run the transaction with tracing enabled.
758781
vmenv := vm.NewEVM(vmctx, txContext, statedb, api.backend.ChainConfig(), vm.Config{Debug: true, Tracer: tracer})
782+
783+
// Call Prepare to clear out the statedb access list
784+
statedb.Prepare(txctx.hash, txctx.block, txctx.index)
785+
759786
result, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()))
760787
if err != nil {
761788
return nil, fmt.Errorf("tracing failed: %v", err)

‎les/state_accessor.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (leth *LightEthereum) stateAtTransaction(ctx context.Context, block *types.
7272
msg, _ := tx.AsMessage(signer)
7373
txContext := core.NewEVMTxContext(msg)
7474
context := core.NewEVMBlockContext(block.Header(), leth.blockchain, nil)
75+
statedb.Prepare(tx.Hash(), block.Hash(), idx)
7576
if idx == txIndex {
7677
return msg, context, statedb, func() {}, nil
7778
}

0 commit comments

Comments
 (0)