Repository: ethereum/go-ethereum
Pull request #31763: eth/tracers: fix `standardTraceBlockToFile`
Author: login: jwasinger
Created: 2025-05-05T06:22:37Z | Merged: 2025-05-05T14:15:59Z

Previously, we were only setting a default `vm.Config` on the EVM instance which kept the tracer disabled.

With this PR, the tracer is always enabled.  However, the output is discarded unless we are tracing a transaction that we want the dump of the output from.


Diff:
diff --git a/eth/tracers/api.go b/eth/tracers/api.go
index 17a0ad687a89..fe72924828e6 100644
--- a/eth/tracers/api.go
+++ b/eth/tracers/api.go
@@ -778,6 +778,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
 		// Note: This copies the config, to not screw up the main config
 		chainConfig, canon = overrideConfig(chainConfig, config.Overrides)
 	}
+
 	evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{})
 	if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
 		core.ProcessBeaconBlockRoot(*beaconRoot, evm)
@@ -787,42 +788,45 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
 	}
 	for i, tx := range block.Transactions() {
 		// Prepare the transaction for un-traced execution
-		var (
-			msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee())
-			vmConf vm.Config
-			dump   *os.File
-			writer *bufio.Writer
-			err    error
-		)
-		// If the transaction needs tracing, swap out the configs
-		if tx.Hash() == txHash || txHash == (common.Hash{}) {
-			// Generate a unique temporary file to dump it into
-			prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
-			if !canon {
-				prefix = fmt.Sprintf("%valt-", prefix)
-			}
-			dump, err = os.CreateTemp(os.TempDir(), prefix)
+		msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
+		if txHash != (common.Hash{}) && tx.Hash() != txHash {
+			// Process the tx to update state, but don't trace it.
+			_, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
 			if err != nil {
-				return nil, err
-			}
-			dumps = append(dumps, dump.Name())
-
-			// Swap out the noop logger to the standard tracer
-			writer = bufio.NewWriter(dump)
-			vmConf = vm.Config{
-				Tracer:                  logger.NewJSONLogger(&logConfig, writer),
-				EnablePreimageRecording: true,
+				return dumps, err
 			}
+			// 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
+			statedb.Finalise(evm.ChainConfig().IsEIP158(block.Number()))
+			continue
 		}
+		// The transaction should be traced.
+		// Generate a unique temporary file to dump it into.
+		prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
+		if !canon {
+			prefix = fmt.Sprintf("%valt-", prefix)
+		}
+		var dump *os.File
+		dump, err := os.CreateTemp(os.TempDir(), prefix)
+		if err != nil {
+			return nil, err
+		}
+		dumps = append(dumps, dump.Name())
+		// Set up the tracer and EVM for the transaction.
+		var (
+			writer = bufio.NewWriter(dump)
+			tracer = logger.NewJSONLogger(&logConfig, writer)
+			evm    = vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{
+				Tracer:    tracer,
+				NoBaseFee: true,
+			})
+		)
 		// Execute the transaction and flush any traces to disk
 		statedb.SetTxContext(tx.Hash(), i)
-		if vmConf.Tracer.OnTxStart != nil {
-			vmConf.Tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
-		}
-		vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
-		if vmConf.Tracer.OnTxEnd != nil {
-			vmConf.Tracer.OnTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, err)
+		if tracer.OnTxStart != nil {
+			tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
 		}
+		_, err = core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
 		if writer != nil {
 			writer.Flush()
 		}
diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go
index fa3918769422..d20d5eaff656 100644
--- a/eth/tracers/api_test.go
+++ b/eth/tracers/api_test.go
@@ -23,6 +23,7 @@ import (
 	"errors"
 	"fmt"
 	"math/big"
+	"os"
 	"reflect"
 	"slices"
 	"sync/atomic"
@@ -1218,3 +1219,118 @@ func TestTraceBlockWithBasefee(t *testing.T) {
 		}
 	}
 }
+
+func TestStandardTraceBlockToFile(t *testing.T) {
+	var (
+		// A sender who makes transactions, has some funds
+		key, _  = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
+		address = crypto.PubkeyToAddress(key.PublicKey)
+		funds   = big.NewInt(1000000000000000)
+
+		// first contract the sender transacts with
+		aa     = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43")
+		aaCode = []byte{byte(vm.PUSH1), 0x00, byte(vm.POP)}
+
+		// second contract the sender transacts with
+		bb     = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f44")
+		bbCode = []byte{byte(vm.PUSH2), 0x00, 0x01, byte(vm.POP)}
+	)
+
+	genesis := &core.Genesis{
+		Config: params.TestChainConfig,
+		Alloc: types.GenesisAlloc{
+			address: {Balance: funds},
+			aa: {
+				Code:    aaCode,
+				Nonce:   1,
+				Balance: big.NewInt(0),
+			},
+			bb: {
+				Code:    bbCode,
+				Nonce:   1,
+				Balance: big.NewInt(0),
+			},
+		},
+	}
+	txHashs := make([]common.Hash, 0, 2)
+	backend := newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
+		b.SetCoinbase(common.Address{1})
+		// first tx to aa
+		tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
+			Nonce:    0,
+			To:       &aa,
+			Value:    big.NewInt(0),
+			Gas:      50000,
+			GasPrice: b.BaseFee(),
+			Data:     nil,
+		}), types.HomesteadSigner{}, key)
+		b.AddTx(tx)
+		txHashs = append(txHashs, tx.Hash())
+		// second tx to bb
+		tx, _ = types.SignTx(types.NewTx(&types.LegacyTx{
+			Nonce:    1,
+			To:       &bb,
+			Value:    big.NewInt(1),
+			Gas:      100000,
+			GasPrice: b.BaseFee(),
+			Data:     nil,
+		}), types.HomesteadSigner{}, key)
+		b.AddTx(tx)
+		txHashs = append(txHashs, tx.Hash())
+	})
+	defer backend.chain.Stop()
+
+	var testSuite = []struct {
+		blockNumber rpc.BlockNumber
+		config      *StdTraceConfig
+		want        []string
+	}{
+		{
+			// test that all traces in the block were outputted if no trace config is specified
+			blockNumber: rpc.LatestBlockNumber,
+			config:      nil,
+			want: []string{
+				`{"pc":0,"op":96,"gas":"0x7148","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}
+{"pc":2,"op":80,"gas":"0x7145","gasCost":"0x2","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"POP"}
+{"pc":3,"op":0,"gas":"0x7143","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
+{"output":"","gasUsed":"0x5"}
+`,
+				`{"pc":0,"op":97,"gas":"0x13498","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH2"}
+{"pc":3,"op":80,"gas":"0x13495","gasCost":"0x2","memSize":0,"stack":["0x1"],"depth":1,"refund":0,"opName":"POP"}
+{"pc":4,"op":0,"gas":"0x13493","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
+{"output":"","gasUsed":"0x5"}
+`,
+			},
+		},
+		{
+			// test that only a specific tx is traced if specified
+			blockNumber: rpc.LatestBlockNumber,
+			config:      &StdTraceConfig{TxHash: txHashs[1]},
+			want: []string{
+				`{"pc":0,"op":97,"gas":"0x13498","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH2"}
+{"pc":3,"op":80,"gas":"0x13495","gasCost":"0x2","memSize":0,"stack":["0x1"],"depth":1,"refund":0,"opName":"POP"}
+{"pc":4,"op":0,"gas":"0x13493","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
+{"output":"","gasUsed":"0x5"}
+`,
+			},
+		},
+	}
+
+	api := NewAPI(backend)
+	for i, tc := range testSuite {
+		block, _ := api.blockByNumber(context.Background(), tc.blockNumber)
+		txTraces, err := api.StandardTraceBlockToFile(context.Background(), block.Hash(), tc.config)
+		if err != nil {
+			t.Fatalf("test index %d received error %v", i, err)
+		}
+		for j, traceFileName := range txTraces {
+			traceReceived, err := os.ReadFile(traceFileName)
+			if err != nil {
+				t.Fatalf("could not read trace file: %v", err)
+			}
+			if tc.want[j] != string(traceReceived) {
+				t.Fatalf("unexpected trace result.  expected\n'%s'\n\nreceived\n'%s'\n", tc.want[j], string(traceReceived))
+			}
+		}
+	}
+}


Reviews:
user: login: mask-pp
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T10:03:59Z
user: login: jwasinger
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T10:19:32Z
user: login: mask-pp
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T10:33:41Z
user: login: s1na
body: LGTM
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T13:28:20Z
user: login: rjl493456442
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-05T13:59:15Z
user: login: s1na
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T14:08:33Z
user: login: s1na
body: Approve
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T14:10:01Z

Review comments:
diff_hunk: @@ -809,19 +814,20 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
 
 			// Swap out the noop logger to the standard tracer
 			writer = bufio.NewWriter(dump)
-			vmConf = vm.Config{
-				Tracer:                  logger.NewJSONLogger(&logConfig, writer),
-				EnablePreimageRecording: true,
-			}
+			*tracer = *logger.NewJSONLogger(&logConfig, writer)
+		} else {
path: eth/tracers/api.go
user: login: mask-pp
body: The `else` condition doesn't need to be added. If the specified `TxHash` using the if condition once and break at the end of the loop, if the `TxHash` is not set using the if condition at each loop.
created_at: 2025-05-05T10:03:59Z
author_association: CONTRIBUTOR
diff_hunk: @@ -809,19 +814,20 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
 
 			// Swap out the noop logger to the standard tracer
 			writer = bufio.NewWriter(dump)
-			vmConf = vm.Config{
-				Tracer:                  logger.NewJSONLogger(&logConfig, writer),
-				EnablePreimageRecording: true,
-			}
+			*tracer = *logger.NewJSONLogger(&logConfig, writer)
+		} else {
path: eth/tracers/api.go
user: login: jwasinger
body: Thanks
created_at: 2025-05-05T10:19:32Z
author_association: CONTRIBUTOR
diff_hunk: @@ -778,7 +778,8 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
 		// Note: This copies the config, to not screw up the main config
 		chainConfig, canon = overrideConfig(chainConfig, config.Overrides)
 	}
-	evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{})
+
+	evm := vm.NewEVM(vmctx, statedb, api.backend.ChainConfig(), vm.Config{})
path: eth/tracers/api.go
user: login: rjl493456442
body: Why to use `api.backend.ChainConfig()` here? Rather than the overridden one
created_at: 2025-05-05T13:59:15Z
author_association: MEMBER
diff_hunk: @@ -778,7 +778,8 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
 		// Note: This copies the config, to not screw up the main config
 		chainConfig, canon = overrideConfig(chainConfig, config.Overrides)
 	}
-	evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{})
+
+	evm := vm.NewEVM(vmctx, statedb, api.backend.ChainConfig(), vm.Config{})
path: eth/tracers/api.go
user: login: s1na
body: thx for the catch! fixed
created_at: 2025-05-05T14:08:33Z
author_association: CONTRIBUTOR

PR comments:
user: login: jwasinger
created_at: 2025-05-05T06:29:24Z
body: https://github.com/ethereum/go-ethereum/pull/31748 aims to do this in a cleaner way: split the trace-one-tx/trace-all-txs paths into two separate functions:  trace-one-tx retrieves the state at that tx, and traces only it. whereas trace-all-txs will trace all txs in a block with tracer set and dump enabled.

However, that PR is a lot larger than this, because it also tries to unify/deduplicate block/transaction tracing logic to the same underlying functions for tracers (standard vs js/native).
author_association: CONTRIBUTOR
user: login: mask-pp
created_at: 2025-05-05T06:53:38Z
body: Hi friend, your PR is similar to [mine](https://github.com/ethereum/go-ethereum/pull/31717). Maybe my PR was not discovered in time, which led to your repeated work. Sorry about that, and I also hope my work will be respected. 
cc @s1na 
author_association: CONTRIBUTOR
user: login: s1na
created_at: 2025-05-05T13:28:10Z
body: I made some changes to separate processing for transactions that don't need tracing.
author_association: CONTRIBUTOR
user: login: jwasinger
created_at: 2025-05-05T13:53:11Z
body: LGTM, I will give it another pass over and merge it tomorrow.
author_association: CONTRIBUTOR
user: login: rjl493456442
created_at: 2025-05-05T14:03:59Z
body: Do you think it makes sense to define a tracer wrapper, where the results are only tracked if tx.Hash == target, and otherwise silently ignored?

I want to preserve the property that the EVM is only created once per block. I know it's not true in many places, but would be nice to gradually fix them.
author_association: MEMBER