Skip to content

Commit 8f80caf

Browse files
rjl493456442karalabe
authored andcommitted
core: fix write concurrency in txpool (#19835)
* core: fix write coucurrency in txpool * core: add rlock for pendingState read access * core: address comments
1 parent 31a1f16 commit 8f80caf

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

‎core/tx_noncer.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package core
1818

1919
import (
20+
"sync"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/ethereum/go-ethereum/core/state"
2224
)
@@ -27,6 +29,7 @@ import (
2729
type txNoncer struct {
2830
fallback *state.StateDB
2931
nonces map[common.Address]uint64
32+
lock sync.Mutex
3033
}
3134

3235
// newTxNoncer creates a new virtual state database to track the pool nonces.
@@ -40,6 +43,11 @@ func newTxNoncer(statedb *state.StateDB) *txNoncer {
4043
// get returns the current nonce of an account, falling back to a real state
4144
// database if the account is unknown.
4245
func (txn *txNoncer) get(addr common.Address) uint64 {
46+
// We use mutex for get operation is the underlying
47+
// state will mutate db even for read access.
48+
txn.lock.Lock()
49+
defer txn.lock.Unlock()
50+
4351
if _, ok := txn.nonces[addr]; !ok {
4452
txn.nonces[addr] = txn.fallback.GetNonce(addr)
4553
}
@@ -49,5 +57,23 @@ func (txn *txNoncer) get(addr common.Address) uint64 {
4957
// set inserts a new virtual nonce into the virtual state database to be returned
5058
// whenever the pool requests it instead of reaching into the real state database.
5159
func (txn *txNoncer) set(addr common.Address, nonce uint64) {
60+
txn.lock.Lock()
61+
defer txn.lock.Unlock()
62+
63+
txn.nonces[addr] = nonce
64+
}
65+
66+
// setIfLower updates a new virtual nonce into the virtual state database if the
67+
// the new one is lower.
68+
func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) {
69+
txn.lock.Lock()
70+
defer txn.lock.Unlock()
71+
72+
if _, ok := txn.nonces[addr]; !ok {
73+
txn.nonces[addr] = txn.fallback.GetNonce(addr)
74+
}
75+
if txn.nonces[addr] <= nonce {
76+
return
77+
}
5278
txn.nonces[addr] = nonce
5379
}

‎core/tx_pool.go‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,7 @@ func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
854854
pool.enqueueTx(tx.Hash(), tx)
855855
}
856856
// Update the account nonce if needed
857-
if nonce := tx.Nonce(); pool.pendingNonces.get(addr) > nonce {
858-
pool.pendingNonces.set(addr, nonce)
859-
}
857+
pool.pendingNonces.setIfLower(addr, tx.Nonce())
860858
// Reduce the pending counter
861859
pendingCounter.Dec(int64(1 + len(invalids)))
862860
return
@@ -1232,9 +1230,7 @@ func (pool *TxPool) truncatePending() {
12321230
pool.all.Remove(hash)
12331231

12341232
// Update the account nonce to the dropped transaction
1235-
if nonce := tx.Nonce(); pool.pendingNonces.get(offenders[i]) > nonce {
1236-
pool.pendingNonces.set(offenders[i], nonce)
1237-
}
1233+
pool.pendingNonces.setIfLower(offenders[i], tx.Nonce())
12381234
log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
12391235
}
12401236
pool.priced.Removed(len(caps))
@@ -1261,9 +1257,7 @@ func (pool *TxPool) truncatePending() {
12611257
pool.all.Remove(hash)
12621258

12631259
// Update the account nonce to the dropped transaction
1264-
if nonce := tx.Nonce(); pool.pendingNonces.get(addr) > nonce {
1265-
pool.pendingNonces.set(addr, nonce)
1266-
}
1260+
pool.pendingNonces.setIfLower(addr, tx.Nonce())
12671261
log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
12681262
}
12691263
pool.priced.Removed(len(caps))

0 commit comments

Comments
 (0)