core/txpool/locals: fix flaky test

This commit is contained in:
Gary Rong 2025-03-24 21:19:59 +08:00
parent 4946b9603b
commit 277c7a0ba2

View file

@ -67,16 +67,24 @@ func newTestEnv(t *testing.T, n int, gasTip uint64, journal string) *testEnv {
db := rawdb.NewMemoryDatabase() db := rawdb.NewMemoryDatabase()
chain, _ := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil) chain, _ := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil)
ch := make(chan core.ChainHeadEvent, 1)
sub := chain.SubscribeChainHeadEvent(ch)
defer sub.Unsubscribe()
if n, err := chain.InsertChain(blocks); err != nil { if n, err := chain.InsertChain(blocks); err != nil {
t.Fatalf("failed to process block %d: %v", n, err) t.Fatalf("failed to process block %d: %v", n, err)
} }
for ev := range ch {
if ev.Header.Number.Uint64() == blocks[len(blocks)-1].NumberU64() {
break
}
}
legacyPool := legacypool.New(legacypool.DefaultConfig, chain) legacyPool := legacypool.New(legacypool.DefaultConfig, chain)
pool, err := txpool.New(gasTip, chain, []txpool.SubPool{legacyPool}) pool, err := txpool.New(gasTip, chain, []txpool.SubPool{legacyPool})
if err != nil { if err != nil {
t.Fatalf("Failed to create tx pool: %v", err) t.Fatalf("Failed to create tx pool: %v", err)
} }
time.Sleep(time.Second) // hack, make sure txpool initialization is done
return &testEnv{ return &testEnv{
chain: chain, chain: chain,
pool: pool, pool: pool,
@ -107,6 +115,10 @@ func (env *testEnv) makeTx(nonce uint64, gasPrice *big.Int) *types.Transaction {
} }
func (env *testEnv) commit() { func (env *testEnv) commit() {
ch := make(chan core.ChainHeadEvent, 1)
sub := env.chain.SubscribeChainHeadEvent(ch)
defer sub.Unsubscribe()
head := env.chain.CurrentBlock() head := env.chain.CurrentBlock()
block := env.chain.GetBlock(head.Hash(), head.Number.Uint64()) block := env.chain.GetBlock(head.Hash(), head.Number.Uint64())
blocks, _ := core.GenerateChain(env.chain.Config(), block, ethash.NewFaker(), env.genDb, 1, func(i int, gen *core.BlockGen) { blocks, _ := core.GenerateChain(env.chain.Config(), block, ethash.NewFaker(), env.genDb, 1, func(i int, gen *core.BlockGen) {
@ -117,6 +129,12 @@ func (env *testEnv) commit() {
gen.AddTx(tx) gen.AddTx(tx)
}) })
env.chain.InsertChain(blocks) env.chain.InsertChain(blocks)
for ev := range ch {
if ev.Header.Number.Uint64() == blocks[len(blocks)-1].NumberU64() {
break
}
}
} }
func TestRejectInvalids(t *testing.T) { func TestRejectInvalids(t *testing.T) {
@ -127,7 +145,7 @@ func TestRejectInvalids(t *testing.T) {
gasTip uint64 gasTip uint64
tx *types.Transaction tx *types.Transaction
expErr error expErr error
op func() commit bool
}{ }{
{ {
tx: env.makeTx(5, nil), // stale tx: env.makeTx(5, nil), // stale
@ -147,9 +165,7 @@ func TestRejectInvalids(t *testing.T) {
expErr: types.ErrInvalidSig, expErr: types.ErrInvalidSig,
}, },
{ {
op: func() { commit: true,
env.commit()
},
tx: env.makeTx(10, nil), // stale tx: env.makeTx(10, nil), // stale
expErr: core.ErrNonceTooLow, expErr: core.ErrNonceTooLow,
}, },
@ -162,8 +178,8 @@ func TestRejectInvalids(t *testing.T) {
if c.gasTip != 0 { if c.gasTip != 0 {
env.setGasTip(c.gasTip) env.setGasTip(c.gasTip)
} }
if c.op != nil { if c.commit {
c.op() env.commit()
} }
gotErr := env.tracker.Track(c.tx) gotErr := env.tracker.Track(c.tx)
if c.expErr == nil && gotErr != nil { if c.expErr == nil && gotErr != nil {