Skip to content

Commit 5e45db7

Browse files
authored
Merge pull request #21021 from karalabe/tests-snapshot-gen-cleanup
tests: cleanup snapshot generator goroutine leak
2 parents ab72803 + 65cd28a commit 5e45db7

5 files changed

Lines changed: 30 additions & 19 deletions

File tree

‎cmd/evm/staterunner.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func stateTestCmd(ctx *cli.Context) error {
9696
for _, st := range test.Subtests() {
9797
// Run the test and aggregate the result
9898
result := &StatetestResult{Name: key, Fork: st.Fork, Pass: true}
99-
state, err := test.Run(st, cfg, false)
99+
_, state, err := test.Run(st, cfg, false)
100100
// print state root for evmlab tracing
101101
if ctx.GlobalBool(MachineFlag.Name) && state != nil {
102102
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", state.IntermediateRoot(false))

‎eth/tracers/tracers_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func TestPrestateTracerCreate2(t *testing.T) {
168168
Code: []byte{},
169169
Balance: big.NewInt(500000000000000),
170170
}
171-
statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)
171+
_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)
172172

173173
// Create the tracer, the EVM environment and run it
174174
tracer, err := New("prestateTracer")
@@ -242,7 +242,7 @@ func TestCallTracer(t *testing.T) {
242242
GasLimit: uint64(test.Context.GasLimit),
243243
GasPrice: tx.GasPrice(),
244244
}
245-
statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false)
245+
_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false)
246246

247247
// Create the tracer, the EVM environment and run it
248248
tracer, err := New("callTracer")

‎tests/state_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ func TestState(t *testing.T) {
6666

6767
t.Run(key+"/trie", func(t *testing.T) {
6868
withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
69-
_, err := test.Run(subtest, vmconfig, false)
69+
_, _, err := test.Run(subtest, vmconfig, false)
7070
return st.checkFailure(t, name+"/trie", err)
7171
})
7272
})
7373
t.Run(key+"/snap", func(t *testing.T) {
7474
withTrace(t, test.gasLimit(subtest), func(vmconfig vm.Config) error {
75-
_, err := test.Run(subtest, vmconfig, true)
75+
snaps, statedb, err := test.Run(subtest, vmconfig, true)
76+
if _, err := snaps.Journal(statedb.IntermediateRoot(false)); err != nil {
77+
return err
78+
}
7679
return st.checkFailure(t, name+"/snap", err)
7780
})
7881
})

‎tests/state_test_util.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,37 +147,37 @@ func (t *StateTest) Subtests() []StateSubtest {
147147
}
148148

149149
// Run executes a specific subtest and verifies the post-state and logs
150-
func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*state.StateDB, error) {
151-
statedb, root, err := t.RunNoVerify(subtest, vmconfig, snapshotter)
150+
func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*snapshot.Tree, *state.StateDB, error) {
151+
snaps, statedb, root, err := t.RunNoVerify(subtest, vmconfig, snapshotter)
152152
if err != nil {
153-
return statedb, err
153+
return snaps, statedb, err
154154
}
155155
post := t.json.Post[subtest.Fork][subtest.Index]
156156
// N.B: We need to do this in a two-step process, because the first Commit takes care
157157
// of suicides, and we need to touch the coinbase _after_ it has potentially suicided.
158158
if root != common.Hash(post.Root) {
159-
return statedb, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
159+
return snaps, statedb, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
160160
}
161161
if logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) {
162-
return statedb, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
162+
return snaps, statedb, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
163163
}
164-
return statedb, nil
164+
return snaps, statedb, nil
165165
}
166166

167167
// RunNoVerify runs a specific subtest and returns the statedb and post-state root
168-
func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*state.StateDB, common.Hash, error) {
168+
func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*snapshot.Tree, *state.StateDB, common.Hash, error) {
169169
config, eips, err := getVMConfig(subtest.Fork)
170170
if err != nil {
171-
return nil, common.Hash{}, UnsupportedForkError{subtest.Fork}
171+
return nil, nil, common.Hash{}, UnsupportedForkError{subtest.Fork}
172172
}
173173
vmconfig.ExtraEips = eips
174174
block := t.genesis(config).ToBlock(nil)
175-
statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter)
175+
snaps, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter)
176176

177177
post := t.json.Post[subtest.Fork][subtest.Index]
178178
msg, err := t.json.Tx.toMessage(post)
179179
if err != nil {
180-
return nil, common.Hash{}, err
180+
return nil, nil, common.Hash{}, err
181181
}
182182
context := core.NewEVMContext(msg, block.Header(), nil, &t.json.Env.Coinbase)
183183
context.GetHash = vmTestBlockHash
@@ -199,14 +199,14 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh
199199
statedb.AddBalance(block.Coinbase(), new(big.Int))
200200
// And _now_ get the state root
201201
root := statedb.IntermediateRoot(config.IsEIP158(block.Number()))
202-
return statedb, root, nil
202+
return snaps, statedb, root, nil
203203
}
204204

205205
func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {
206206
return t.json.Tx.GasLimit[t.json.Post[subtest.Fork][subtest.Index].Indexes.Gas]
207207
}
208208

209-
func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter bool) *state.StateDB {
209+
func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter bool) (*snapshot.Tree, *state.StateDB) {
210210
sdb := state.NewDatabase(db)
211211
statedb, _ := state.New(common.Hash{}, sdb, nil)
212212
for addr, a := range accounts {
@@ -225,7 +225,7 @@ func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter boo
225225
snaps = snapshot.New(db, sdb.TrieDB(), 1, root, false)
226226
}
227227
statedb, _ = state.New(root, sdb, snaps)
228-
return statedb
228+
return snaps, statedb
229229
}
230230

231231
func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis {

‎tests/vm_test_util.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ type vmExecMarshaling struct {
7979
}
8080

8181
func (t *VMTest) Run(vmconfig vm.Config, snapshotter bool) error {
82-
statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter)
82+
snaps, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter)
83+
if snapshotter {
84+
preRoot := statedb.IntermediateRoot(false)
85+
defer func() {
86+
if _, err := snaps.Journal(preRoot); err != nil {
87+
panic(err)
88+
}
89+
}()
90+
}
8391
ret, gasRemaining, err := t.exec(statedb, vmconfig)
8492

8593
if t.json.GasRemaining == nil {

0 commit comments

Comments
 (0)