From 072865b85c7255b5ee5ede581d947cf121a18442 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Thu, 7 Mar 2024 11:41:40 -0800 Subject: [PATCH] fix test reliability --- core/txpool/txpool.go | 22 ++++++++++++---------- internal/ethapi/api_test.go | 1 + 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index 252aca0cf8..ce3f5f7e73 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -114,7 +114,16 @@ func New(gasTip *big.Int, chain BlockChain, subpools []SubPool) (*TxPool, error) return nil, err } } - go pool.loop(head, chain) + + // Subscribe to chain head events to trigger subpool resets + var ( + newHeadCh = make(chan core.ChainHeadEvent) + newHeadSub = chain.SubscribeChainHeadEvent(newHeadCh) + ) + go func() { + pool.loop(head, newHeadCh) + newHeadSub.Unsubscribe() + }() return pool, nil } @@ -192,14 +201,7 @@ func (p *TxPool) Close() error { // loop is the transaction pool's main event loop, waiting for and reacting to // outside blockchain events as well as for various reporting and transaction // eviction events. -func (p *TxPool) loop(head *types.Header, chain BlockChain) { - // Subscribe to chain head events to trigger subpool resets - var ( - newHeadCh = make(chan core.ChainHeadEvent) - newHeadSub = chain.SubscribeChainHeadEvent(newHeadCh) - ) - defer newHeadSub.Unsubscribe() - +func (p *TxPool) loop(head *types.Header, newHeadCh <-chan core.ChainHeadEvent) { // Track the previous and current head to feed to an idle reset var ( oldHead = head @@ -224,8 +226,8 @@ func (p *TxPool) loop(head *types.Header, chain BlockChain) { for _, subpool := range p.subpools { subpool.Reset(oldHead, newHead) } - resetDone <- newHead p.reorgFeed.Send(core.NewTxPoolReorgEvent{Head: newHead}) + resetDone <- newHead }(oldHead, newHead) default: diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index f168777153..6b7251266d 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -377,6 +377,7 @@ func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i i t.Fatalf("block %d: failed to accept into chain: %v", block.NumberU64(), err) } } + chain.DrainAcceptorQueue() backend := &testBackend{db: db, chain: chain} return backend