From 460131f20a9f213e1f48b54062f298bf1cdff42e Mon Sep 17 00:00:00 2001 From: Evgeny Danienko <6655321@bk.ru> Date: Tue, 4 Apr 2023 09:44:44 +0400 Subject: [PATCH 1/4] txpool reorg locks --- core/tx_list.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/core/tx_list.go b/core/tx_list.go index fea4434b9b..e8aece7700 100644 --- a/core/tx_list.go +++ b/core/tx_list.go @@ -149,19 +149,25 @@ func (m *txSortedMap) Filter(filter func(*types.Transaction) bool) types.Transac removed := m.filter(filter) // If transactions were removed, the heap and cache are ruined if len(removed) > 0 { - m.reheap() + m.reheap(false) } return removed } -func (m *txSortedMap) reheap() { - *m.index = make([]uint64, 0, len(m.items)) +func (m *txSortedMap) reheap(withRlock bool) { + index := make(nonceHeap, 0, len(m.items)) for nonce := range m.items { - *m.index = append(*m.index, nonce) + index = append(index, nonce) } - heap.Init(m.index) + heap.Init(&index) + + if withRlock { + m.m.RLock() + m.index = &index + m.m.RUnlock() + } m.cacheMu.Lock() m.cache = nil @@ -510,9 +516,12 @@ func (l *txList) Filter(costLimit *uint256.Int, gasLimit uint64) (types.Transact lowest = nonce } } + + l.txs.m.Lock() invalids = l.txs.filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest }) + l.txs.m.Unlock() } - l.txs.reheap() + l.txs.reheap(true) return removed, invalids } From 5178b8397ca44b4c3cf9ffc114d0676beb9fbaf3 Mon Sep 17 00:00:00 2001 From: Evgeny Danienko <6655321@bk.ru> Date: Tue, 4 Apr 2023 10:12:13 +0400 Subject: [PATCH 2/4] more locks --- core/tx_list.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/tx_list.go b/core/tx_list.go index e8aece7700..6966d26a3c 100644 --- a/core/tx_list.go +++ b/core/tx_list.go @@ -157,10 +157,18 @@ func (m *txSortedMap) Filter(filter func(*types.Transaction) bool) types.Transac func (m *txSortedMap) reheap(withRlock bool) { index := make(nonceHeap, 0, len(m.items)) + if withRlock { + m.m.RLock() + } + for nonce := range m.items { index = append(index, nonce) } + if withRlock { + m.m.RUnlock() + } + heap.Init(&index) if withRlock { From e9426fc54f2dbdd98b61e7b3a77d93d93cde3e0b Mon Sep 17 00:00:00 2001 From: Evgeny Danienko <6655321@bk.ru> Date: Tue, 4 Apr 2023 13:31:35 +0400 Subject: [PATCH 3/4] locks --- core/tx_list.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/tx_list.go b/core/tx_list.go index 6966d26a3c..3447418718 100644 --- a/core/tx_list.go +++ b/core/tx_list.go @@ -172,9 +172,13 @@ func (m *txSortedMap) reheap(withRlock bool) { heap.Init(&index) if withRlock { - m.m.RLock() - m.index = &index - m.m.RUnlock() + m.m.Lock() + } + + m.index = &index + + if withRlock { + m.m.Unlock() } m.cacheMu.Lock() From dfb322ed2976e0c0d76a96edd8f6ae69a9163130 Mon Sep 17 00:00:00 2001 From: Evgeny Danienko <6655321@bk.ru> Date: Tue, 4 Apr 2023 14:49:56 +0400 Subject: [PATCH 4/4] linters --- core/tx_list.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/tx_list.go b/core/tx_list.go index 3447418718..88c2f5cf75 100644 --- a/core/tx_list.go +++ b/core/tx_list.go @@ -533,7 +533,9 @@ func (l *txList) Filter(costLimit *uint256.Int, gasLimit uint64) (types.Transact invalids = l.txs.filter(func(tx *types.Transaction) bool { return tx.Nonce() > lowest }) l.txs.m.Unlock() } + l.txs.reheap(true) + return removed, invalids }