From 5d113eea3216e7c28d74faccb4f6caaafb075b46 Mon Sep 17 00:00:00 2001 From: Miro Date: Mon, 14 Apr 2025 16:57:19 -0400 Subject: [PATCH 1/3] core/txpool/legacypool: fix data race in Add --- core/txpool/legacypool/legacypool.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 04f1a2234c49..08134fef62f3 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -943,8 +943,11 @@ func (pool *LegacyPool) Add(txs []*types.Transaction, sync bool) []error { news = make([]*types.Transaction, 0, len(txs)) ) for i, tx := range txs { + pool.mu.Lock() + hash := pool.all.Get(tx.Hash()) + pool.mu.Unlock() // If the transaction is known, pre-set the error slot - if pool.all.Get(tx.Hash()) != nil { + if hash != nil { errs[i] = txpool.ErrAlreadyKnown knownTxMeter.Mark(1) continue From 847532f28710c376c6fa8e1ff4c040a75c62c1b8 Mon Sep 17 00:00:00 2001 From: Miro Date: Wed, 16 Apr 2025 08:38:19 -0400 Subject: [PATCH 2/3] core/txpool/legacypool: Clear lookup state with lock --- core/txpool/legacypool/legacypool.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 08134fef62f3..696a21121a86 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -943,11 +943,8 @@ func (pool *LegacyPool) Add(txs []*types.Transaction, sync bool) []error { news = make([]*types.Transaction, 0, len(txs)) ) for i, tx := range txs { - pool.mu.Lock() - hash := pool.all.Get(tx.Hash()) - pool.mu.Unlock() // If the transaction is known, pre-set the error slot - if hash != nil { + if pool.all.Get(tx.Hash()) != nil { errs[i] = txpool.ErrAlreadyKnown knownTxMeter.Mark(1) continue @@ -1830,6 +1827,15 @@ func (t *lookup) Remove(hash common.Hash) { delete(t.txs, hash) } +func (t *lookup) Clear() { + t.lock.Lock() + defer t.lock.Unlock() + + t.slots = 0 + t.txs = make(map[common.Hash]*types.Transaction) + t.auths = make(map[common.Address][]common.Hash) +} + // TxsBelowTip finds all remote transactions below the given tip threshold. func (t *lookup) TxsBelowTip(threshold *big.Int) types.Transactions { found := make(types.Transactions, 0, 128) @@ -1926,7 +1932,7 @@ func (pool *LegacyPool) Clear() { for addr := range pool.queue { pool.reserver.Release(addr) } - pool.all = newLookup() + pool.all.Clear() pool.priced = newPricedList(pool.all) pool.pending = make(map[common.Address]*list) pool.queue = make(map[common.Address]*list) From d5d1ec391aae04608b3314f575007d68e2223e15 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Thu, 17 Apr 2025 10:35:41 +0800 Subject: [PATCH 3/3] Update legacypool.go --- core/txpool/legacypool/legacypool.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 696a21121a86..7bf360ff65f5 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -1827,6 +1827,7 @@ func (t *lookup) Remove(hash common.Hash) { delete(t.txs, hash) } +// Clear resets the lookup structure, removing all stored entries. func (t *lookup) Clear() { t.lock.Lock() defer t.lock.Unlock()