Skip to content

Commit

Permalink
core: improve locks in txpool (ethereum#807)
Browse files Browse the repository at this point in the history
* added a write lock to the txs.filter method and a read lock to the txs.reheap method - both of which are called by Filter during reorg adjustments to txpool

* txpool reorg locks

* more locks

* locks

* linters

* params, packaging: update version for v0.3.8-beta release

* core: add logs in reheap

---------

Co-authored-by: Alex <dalexwatts@gmail.com>
Co-authored-by: Evgeny Danienko <6655321@bk.ru>
  • Loading branch information
3 people committed Apr 4, 2023
1 parent 72a2220 commit 73f3c75
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 16 deletions.
37 changes: 31 additions & 6 deletions core/tx_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/common"
cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)

// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
Expand Down Expand Up @@ -150,19 +151,38 @@ 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))

if withRlock {
m.m.RLock()
log.Info("[DEBUG] Acquired lock over txpool map while performing reheap")
}

for nonce := range m.items {
*m.index = append(*m.index, nonce)
index = append(index, nonce)
}

heap.Init(m.index)
if withRlock {
m.m.RUnlock()
}

heap.Init(&index)

if withRlock {
m.m.Lock()
}

m.index = &index

if withRlock {
m.m.Unlock()
}

m.cacheMu.Lock()
m.cache = nil
Expand Down Expand Up @@ -521,12 +541,17 @@ 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()
}
// Reset total cost
l.subTotalCost(removed)
l.subTotalCost(invalids)
l.txs.reheap()

l.txs.reheap(true)

return removed, invalids
}

Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: bor
Version: 0.3.7
Version: 0.3.8-beta
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control.arm64
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: bor
Version: 0.3.7
Version: 0.3.8-beta
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control.profile.amd64
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: bor-profile
Version: 0.3.7
Version: 0.3.8-beta
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control.profile.arm64
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: bor-profile
Version: 0.3.7
Version: 0.3.8-beta
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
2 changes: 1 addition & 1 deletion packaging/templates/package_scripts/control.validator
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: bor-profile
Version: 0.3.7
Version: 0.3.8-beta
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Source: bor-profile
Version: 0.3.7
Version: 0.3.8-beta
Section: develop
Priority: standard
Maintainer: Polygon <release-team@polygon.technology>
Expand Down
8 changes: 4 additions & 4 deletions params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
)

const (
VersionMajor = 0 // Major version component of the current release
VersionMinor = 3 // Minor version component of the current release
VersionPatch = 7 // Patch version component of the current release
VersionMeta = "stable" // Version metadata to append to the version string
VersionMajor = 0 // Major version component of the current release
VersionMinor = 3 // Minor version component of the current release
VersionPatch = 8 // Patch version component of the current release
VersionMeta = "beta" // Version metadata to append to the version string
)

// Version holds the textual version string.
Expand Down

0 comments on commit 73f3c75

Please sign in to comment.