eth/tracers: improve flaky tests #25918 (#1301)

This commit is contained in:
Daniel Liu 2025-09-09 07:10:44 +08:00 committed by GitHub
parent 51f4757eed
commit c51f227944
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 11 deletions

View file

@ -64,6 +64,8 @@ type testBackend struct {
relHook func() // Hook is invoked when the requested state is released relHook func() // Hook is invoked when the requested state is released
} }
// testBackend creates a new test backend. OBS: After test is done, teardown must be
// invoked in order to release associated resources.
func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend { func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend {
backend := &testBackend{ backend := &testBackend{
chainConfig: gspec.Config, chainConfig: gspec.Config,
@ -141,6 +143,11 @@ func (b *testBackend) ChainDb() ethdb.Database {
return b.chaindb return b.chaindb
} }
// teardown releases the associated resources.
func (b *testBackend) teardown() {
b.chain.Stop()
}
func (b *testBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error) { func (b *testBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error) {
statedb, err := b.chain.StateAt(block.Root()) statedb, err := b.chain.StateAt(block.Root())
if err != nil { if err != nil {
@ -204,13 +211,15 @@ func TestTraceCall(t *testing.T) {
}} }}
genBlocks := 10 genBlocks := 10
signer := types.HomesteadSigner{} signer := types.HomesteadSigner{}
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) { backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1] // Transfer from account[0] to account[1]
// value: 1000 wei // value: 1000 wei
// fee: 0 wei // fee: 0 wei
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key) tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
b.AddTx(tx) b.AddTx(tx)
})) })
defer backend.teardown()
api := NewAPI(backend)
var testSuite = []struct { var testSuite = []struct {
blockNumber rpc.BlockNumber blockNumber rpc.BlockNumber
call ethapi.TransactionArgs call ethapi.TransactionArgs
@ -337,14 +346,16 @@ func TestTraceTransaction(t *testing.T) {
}} }}
target := common.Hash{} target := common.Hash{}
signer := types.HomesteadSigner{} signer := types.HomesteadSigner{}
api := NewAPI(newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) { backend := newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1] // Transfer from account[0] to account[1]
// value: 1000 wei // value: 1000 wei
// fee: 0 wei // fee: 0 wei
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key) tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
b.AddTx(tx) b.AddTx(tx)
target = tx.Hash() target = tx.Hash()
})) })
defer backend.chain.Stop()
api := NewAPI(backend)
result, err := api.TraceTransaction(context.Background(), target, nil) result, err := api.TraceTransaction(context.Background(), target, nil)
if err != nil { if err != nil {
t.Errorf("Failed to trace transaction %v", err) t.Errorf("Failed to trace transaction %v", err)
@ -379,13 +390,15 @@ func TestTraceBlock(t *testing.T) {
}} }}
genBlocks := 10 genBlocks := 10
signer := types.HomesteadSigner{} signer := types.HomesteadSigner{}
api := NewAPI(newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) { backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
// Transfer from account[0] to account[1] // Transfer from account[0] to account[1]
// value: 1000 wei // value: 1000 wei
// fee: 0 wei // fee: 0 wei
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key) tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
b.AddTx(tx) b.AddTx(tx)
})) })
defer backend.chain.Stop()
api := NewAPI(backend)
var testSuite = []struct { var testSuite = []struct {
blockNumber rpc.BlockNumber blockNumber rpc.BlockNumber

View file

@ -138,7 +138,7 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
if _, err = st.TransitionDb(common.Address{}); err != nil { if _, err = st.TransitionDb(common.Address{}); err != nil {
t.Fatalf("failed to execute transaction: %v", err) t.Fatalf("failed to execute transaction: %v", err)
} }
// Retrieve the trace result and compare against the etalon // Retrieve the trace result and compare against the expected.
res, err := tracer.GetResult() res, err := tracer.GetResult()
if err != nil { if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err) t.Fatalf("failed to retrieve trace result: %v", err)
@ -291,7 +291,7 @@ func TestZeroValueToNotExitCall(t *testing.T) {
if _, err = st.TransitionDb(common.Address{}); err != nil { if _, err = st.TransitionDb(common.Address{}); err != nil {
t.Fatalf("failed to execute transaction: %v", err) t.Fatalf("failed to execute transaction: %v", err)
} }
// Retrieve the trace result and compare against the etalon // Retrieve the trace result and compare against the expected.
res, err := tracer.GetResult() res, err := tracer.GetResult()
if err != nil { if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err) t.Fatalf("failed to retrieve trace result: %v", err)
@ -373,7 +373,7 @@ func testContractTracer(tracerName string, dirPath string, t *testing.T) {
if _, err = st.TransitionDb(common.Address{}); err != nil { if _, err = st.TransitionDb(common.Address{}); err != nil {
t.Fatalf("failed to execute transaction: %v", err) t.Fatalf("failed to execute transaction: %v", err)
} }
// Retrieve the trace result and compare against the etalon // Retrieve the trace result and compare against the expected.
res, err := tracer.GetResult() res, err := tracer.GetResult()
if err != nil { if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err) t.Fatalf("failed to retrieve trace result: %v", err)

View file

@ -122,7 +122,7 @@ func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typ
if _, err = st.TransitionDb(common.Address{}); err != nil { if _, err = st.TransitionDb(common.Address{}); err != nil {
t.Fatalf("failed to execute transaction: %v", err) t.Fatalf("failed to execute transaction: %v", err)
} }
// Retrieve the trace result and compare against the etalon // Retrieve the trace result and compare against the expected.
res, err := tracer.GetResult() res, err := tracer.GetResult()
if err != nil { if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err) t.Fatalf("failed to retrieve trace result: %v", err)

View file

@ -6,7 +6,7 @@ import (
"strings" "strings"
"unicode" "unicode"
// Force-load native and js pacakges, to trigger registration // Force-load native and js packages, to trigger registration
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/js" _ "github.com/XinFinOrg/XDPoSChain/eth/tracers/js"
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/native" _ "github.com/XinFinOrg/XDPoSChain/eth/tracers/native"
) )