eth/tracers: fix standardTraceBlockToFile

This commit is contained in:
Jared Wasinger 2025-05-05 13:53:38 +08:00
parent 1b18ba2423
commit bdddb82d7e

View file

@ -22,6 +22,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"math/big" "math/big"
"os" "os"
"runtime" "runtime"
@ -778,7 +779,13 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
// Note: This copies the config, to not screw up the main config // Note: This copies the config, to not screw up the main config
chainConfig, canon = overrideConfig(chainConfig, config.Overrides) chainConfig, canon = overrideConfig(chainConfig, config.Overrides)
} }
evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{})
writer := bufio.NewWriter(io.Discard)
tracer := logger.NewJSONLogger(&logConfig, writer)
evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{
Tracer: tracer,
EnablePreimageRecording: true,
})
if beaconRoot := block.BeaconRoot(); beaconRoot != nil { if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
core.ProcessBeaconBlockRoot(*beaconRoot, evm) core.ProcessBeaconBlockRoot(*beaconRoot, evm)
} }
@ -789,9 +796,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
// Prepare the transaction for un-traced execution // Prepare the transaction for un-traced execution
var ( var (
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee()) msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee())
vmConf vm.Config
dump *os.File dump *os.File
writer *bufio.Writer
err error err error
) )
// If the transaction needs tracing, swap out the configs // If the transaction needs tracing, swap out the configs
@ -809,19 +814,20 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
// Swap out the noop logger to the standard tracer // Swap out the noop logger to the standard tracer
writer = bufio.NewWriter(dump) writer = bufio.NewWriter(dump)
vmConf = vm.Config{ *tracer = *logger.NewJSONLogger(&logConfig, writer)
Tracer: logger.NewJSONLogger(&logConfig, writer), } else {
EnablePreimageRecording: true, writer = bufio.NewWriter(io.Discard)
} *tracer = *logger.NewJSONLogger(&logConfig, writer)
} }
// Execute the transaction and flush any traces to disk // Execute the transaction and flush any traces to disk
statedb.SetTxContext(tx.Hash(), i) statedb.SetTxContext(tx.Hash(), i)
if vmConf.Tracer.OnTxStart != nil { if tracer.OnTxStart != nil {
vmConf.Tracer.OnTxStart(evm.GetVMContext(), tx, msg.From) tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
} }
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit)) vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
if vmConf.Tracer.OnTxEnd != nil { if tracer.OnTxEnd != nil {
vmConf.Tracer.OnTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, err) tracer.OnTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, err)
} }
if writer != nil { if writer != nil {
writer.Flush() writer.Flush()