Skip to content

Commit b0478f7

Browse files
committed
eth/tracers: improve flaky tests ethereum#25918
1 parent 8164281 commit b0478f7

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

eth/tracers/api_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ type testBackend struct {
6464
relHook func() // Hook is invoked when the requested state is released
6565
}
6666

67+
// testBackend creates a new test backend. OBS: After test is done, teardown must be
68+
// invoked in order to release associated resources.
6769
func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend {
6870
backend := &testBackend{
6971
chainConfig: gspec.Config,
@@ -141,6 +143,11 @@ func (b *testBackend) ChainDb() ethdb.Database {
141143
return b.chaindb
142144
}
143145

146+
// teardown releases the associated resources.
147+
func (b *testBackend) teardown() {
148+
b.chain.Stop()
149+
}
150+
144151
func (b *testBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error) {
145152
statedb, err := b.chain.StateAt(block.Root())
146153
if err != nil {
@@ -204,13 +211,15 @@ func TestTraceCall(t *testing.T) {
204211
}}
205212
genBlocks := 10
206213
signer := types.HomesteadSigner{}
207-
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
214+
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
208215
// Transfer from account[0] to account[1]
209216
// value: 1000 wei
210217
// fee: 0 wei
211218
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
212219
b.AddTx(tx)
213-
}))
220+
})
221+
defer backend.teardown()
222+
api := NewAPI(backend)
214223
var testSuite = []struct {
215224
blockNumber rpc.BlockNumber
216225
call ethapi.TransactionArgs
@@ -337,14 +346,16 @@ func TestTraceTransaction(t *testing.T) {
337346
}}
338347
target := common.Hash{}
339348
signer := types.HomesteadSigner{}
340-
api := NewAPI(newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
349+
backend := newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
341350
// Transfer from account[0] to account[1]
342351
// value: 1000 wei
343352
// fee: 0 wei
344353
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
345354
b.AddTx(tx)
346355
target = tx.Hash()
347-
}))
356+
})
357+
defer backend.chain.Stop()
358+
api := NewAPI(backend)
348359
result, err := api.TraceTransaction(context.Background(), target, nil)
349360
if err != nil {
350361
t.Errorf("Failed to trace transaction %v", err)
@@ -379,13 +390,15 @@ func TestTraceBlock(t *testing.T) {
379390
}}
380391
genBlocks := 10
381392
signer := types.HomesteadSigner{}
382-
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
393+
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
383394
// Transfer from account[0] to account[1]
384395
// value: 1000 wei
385396
// fee: 0 wei
386397
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
387398
b.AddTx(tx)
388-
}))
399+
})
400+
defer backend.chain.Stop()
401+
api := NewAPI(backend)
389402

390403
var testSuite = []struct {
391404
blockNumber rpc.BlockNumber

eth/tracers/internal/tracetest/calltrace_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
138138
if _, err = st.TransitionDb(common.Address{}); err != nil {
139139
t.Fatalf("failed to execute transaction: %v", err)
140140
}
141-
// Retrieve the trace result and compare against the etalon
141+
// Retrieve the trace result and compare against the expected.
142142
res, err := tracer.GetResult()
143143
if err != nil {
144144
t.Fatalf("failed to retrieve trace result: %v", err)
@@ -291,7 +291,7 @@ func TestZeroValueToNotExitCall(t *testing.T) {
291291
if _, err = st.TransitionDb(common.Address{}); err != nil {
292292
t.Fatalf("failed to execute transaction: %v", err)
293293
}
294-
// Retrieve the trace result and compare against the etalon
294+
// Retrieve the trace result and compare against the expected.
295295
res, err := tracer.GetResult()
296296
if err != nil {
297297
t.Fatalf("failed to retrieve trace result: %v", err)
@@ -373,7 +373,7 @@ func testContractTracer(tracerName string, dirPath string, t *testing.T) {
373373
if _, err = st.TransitionDb(common.Address{}); err != nil {
374374
t.Fatalf("failed to execute transaction: %v", err)
375375
}
376-
// Retrieve the trace result and compare against the etalon
376+
// Retrieve the trace result and compare against the expected.
377377
res, err := tracer.GetResult()
378378
if err != nil {
379379
t.Fatalf("failed to retrieve trace result: %v", err)

eth/tracers/internal/tracetest/prestate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typ
122122
if _, err = st.TransitionDb(common.Address{}); err != nil {
123123
t.Fatalf("failed to execute transaction: %v", err)
124124
}
125-
// Retrieve the trace result and compare against the etalon
125+
// Retrieve the trace result and compare against the expected.
126126
res, err := tracer.GetResult()
127127
if err != nil {
128128
t.Fatalf("failed to retrieve trace result: %v", err)

eth/tracers/internal/tracetest/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77
"unicode"
88

9-
// Force-load native and js pacakges, to trigger registration
9+
// Force-load native and js packages, to trigger registration
1010
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/js"
1111
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/native"
1212
)

0 commit comments

Comments
 (0)