From 77ece0b4b2c373b7733d440e3f9f015da8a44802 Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Thu, 26 Dec 2024 16:32:32 +0800 Subject: [PATCH] revert opt of reheap frequency; close async pricedlist when txpool is shutdown --- core/txpool/legacypool/async_priced_list.go | 2 +- core/txpool/legacypool/legacypool.go | 12 +++--------- core/txpool/legacypool/legacypool_test.go | 1 - core/txpool/legacypool/list.go | 12 ++++++------ 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/core/txpool/legacypool/async_priced_list.go b/core/txpool/legacypool/async_priced_list.go index e988ad922..613a25cfa 100644 --- a/core/txpool/legacypool/async_priced_list.go +++ b/core/txpool/legacypool/async_priced_list.go @@ -182,7 +182,7 @@ func (a *asyncPricedList) GetBaseFee() *big.Int { return a.priced.floating.baseFee } -func (a *asyncPricedList) Stop() { +func (a *asyncPricedList) Close() { close(a.quit) } diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 4381a85bc..3e84c93ad 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -432,6 +432,7 @@ func (pool *LegacyPool) loop() { defer evict.Stop() defer journal.Stop() defer reannounce.Stop() + defer pool.priced.Close() // Notify tests that the init phase is done close(pool.initDoneCh) @@ -845,15 +846,6 @@ func (pool *LegacyPool) add(tx *types.Transaction, local bool) (replaced bool, e } // If the transaction pool is full, discard underpriced transactions if uint64(pool.all.Slots()+numSlots(tx)) > pool.config.GlobalSlots+pool.config.GlobalQueue { - currHead := pool.currentHead.Load() - if currHead != nil && currHead.BaseFee != nil && pool.priced.NeedReheap(currHead) { - if pool.chainconfig.IsLondon(new(big.Int).Add(currHead.Number, big.NewInt(1))) { - baseFee := eip1559.CalcBaseFee(pool.chainconfig, currHead, currHead.Time+1) - pool.priced.SetBaseFee(baseFee) - } - pool.priced.Reheap() - } - // If the new transaction is underpriced, don't accept it if !isLocal && pool.priced.Underpriced(tx) { log.Trace("Discarding underpriced transaction", "hash", hash, "gasTipCap", tx.GasTipCap(), "gasFeeCap", tx.GasFeeCap()) @@ -1472,6 +1464,8 @@ func (pool *LegacyPool) runReorg(done chan struct{}, reset *txpoolResetRequest, if pool.chainconfig.IsLondon(new(big.Int).Add(reset.newHead.Number, big.NewInt(1))) { pendingBaseFee = eip1559.CalcBaseFee(pool.chainconfig, reset.newHead, reset.newHead.Time+1) pool.priced.SetBaseFee(pendingBaseFee) + } else { + pool.priced.Reheap() } } gasTip, baseFee := pool.gasTip.Load(), pendingBaseFee diff --git a/core/txpool/legacypool/legacypool_test.go b/core/txpool/legacypool/legacypool_test.go index 7388ecca9..41dcb64fe 100644 --- a/core/txpool/legacypool/legacypool_test.go +++ b/core/txpool/legacypool/legacypool_test.go @@ -2304,7 +2304,6 @@ func TestDualHeapEviction(t *testing.T) { add(false) for baseFee = 0; baseFee <= 1000; baseFee += 100 { pool.priced.SetBaseFee(big.NewInt(int64(baseFee))) - pool.priced.Reheap() add(true) check(highCap, "fee cap") add(false) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 80e32edea..181fadb47 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -544,12 +544,12 @@ type pricedListInterface interface { Removed(count int) Underpriced(tx *types.Transaction) bool Discard(slots int, force bool) (types.Transactions, bool) - NeedReheap(currHead *types.Header) bool Reheap() SetBaseFee(baseFee *big.Int) GetBaseFee() *big.Int Staled() int TxCount() int + Close() } // pricedList is a price-sorted heap to allow operating on transactions pool @@ -564,7 +564,6 @@ type pricedListInterface interface { // better candidates for inclusion while in other cases (at the top of the baseFee peak) // the floating heap is better. When baseFee is decreasing they behave similarly. type pricedList struct { - currHead *types.Header // Current block header for effective tip calculation // Number of stale price points to (re-heap trigger). stales atomic.Int64 @@ -687,10 +686,6 @@ func (l *pricedList) Discard(slots int, force bool) (types.Transactions, bool) { return drop, true } -func (l *pricedList) NeedReheap(currHead *types.Header) bool { - return l.currHead == nil || currHead == nil || currHead.Hash().Cmp(l.currHead.Hash()) != 0 -} - // Reheap forcibly rebuilds the heap based on the current remote transaction set. func (l *pricedList) Reheap() { l.reheapMu.Lock() @@ -722,6 +717,7 @@ func (l *pricedList) Reheap() { // necessary to call right before SetBaseFee when processing a new block. func (l *pricedList) SetBaseFee(baseFee *big.Int) { l.urgent.baseFee = baseFee + l.Reheap() } // GetBaseFee returns the current base fee used for sorting the urgent heap. @@ -732,3 +728,7 @@ func (l *pricedList) GetBaseFee() *big.Int { func (l *pricedList) TxCount() int { return len(l.urgent.list) + len(l.floating.list) } + +func (l *pricedList) Close() { + //do nothing +}