Add mutex for currentState in TxPool

This commit is contained in:
Jerry 2023-08-04 12:25:27 -07:00
parent 560620fa89
commit 75fe9c6c84
No known key found for this signature in database
GPG key ID: 5B33FA23CB103211

View file

@ -280,6 +280,7 @@ type TxPool struct {
shanghai atomic.Bool // Fork indicator whether we are in the Shanghai stage. shanghai atomic.Bool // Fork indicator whether we are in the Shanghai stage.
currentState *state.StateDB // Current state in the blockchain head currentState *state.StateDB // Current state in the blockchain head
currentStateMutex sync.Mutex // Mutex to protect currentState
pendingNonces *noncer // Pending state tracking virtual nonces pendingNonces *noncer // Pending state tracking virtual nonces
currentMaxGas atomic.Uint64 // Current gas limit for transaction caps currentMaxGas atomic.Uint64 // Current gas limit for transaction caps
@ -745,6 +746,9 @@ func (pool *TxPool) local() map[common.Address]types.Transactions {
// and does not require the pool mutex to be held. // and does not require the pool mutex to be held.
// nolint:gocognit // nolint:gocognit
func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error { func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error {
pool.currentStateMutex.Lock()
defer pool.currentStateMutex.Unlock()
// Accept only legacy transactions until EIP-2718/2930 activates. // Accept only legacy transactions until EIP-2718/2930 activates.
if !pool.eip2718.Load() && tx.Type() != types.LegacyTxType { if !pool.eip2718.Load() && tx.Type() != types.LegacyTxType {
return core.ErrTxTypeNotSupported return core.ErrTxTypeNotSupported
@ -1915,6 +1919,9 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Trans
balance := uint256.NewInt(0) balance := uint256.NewInt(0)
pool.currentStateMutex.Lock()
defer pool.currentStateMutex.Unlock()
// Iterate over all accounts and promote any executable transactions // Iterate over all accounts and promote any executable transactions
for _, addr := range accounts { for _, addr := range accounts {
list = pool.queue[addr] list = pool.queue[addr]
@ -2252,6 +2259,9 @@ func (pool *TxPool) demoteUnexecutables() {
// Iterate over all accounts and demote any non-executable transactions // Iterate over all accounts and demote any non-executable transactions
pool.pendingMu.RLock() pool.pendingMu.RLock()
pool.currentStateMutex.Lock()
defer pool.currentStateMutex.Unlock()
for addr, list := range pool.pending { for addr, list := range pool.pending {
nonce := pool.currentState.GetNonce(addr) nonce := pool.currentState.GetNonce(addr)