From 453ec37a8d7aa1e83a6ae46d978976a220587523 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 4 Aug 2023 12:25:27 -0700 Subject: [PATCH] Add mutex for currentState in TxPool --- core/txpool/txpool.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index 6e6762fe51..83be37019d 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -279,9 +279,10 @@ type TxPool struct { eip1559 atomic.Bool // Fork indicator whether we are using EIP-1559 type transactions. shanghai atomic.Bool // Fork indicator whether we are in the Shanghai stage. - currentState *state.StateDB // Current state in the blockchain head - pendingNonces *noncer // Pending state tracking virtual nonces - currentMaxGas atomic.Uint64 // Current gas limit for transaction caps + currentState *state.StateDB // Current state in the blockchain head + currentStateMutex sync.Mutex // Mutex to protect currentState + pendingNonces *noncer // Pending state tracking virtual nonces + currentMaxGas atomic.Uint64 // Current gas limit for transaction caps locals *accountSet // Set of local transaction to exempt from eviction rules journal *journal // Journal of local transaction to back up to disk @@ -745,6 +746,9 @@ func (pool *TxPool) local() map[common.Address]types.Transactions { // and does not require the pool mutex to be held. // nolint:gocognit 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. if !pool.eip2718.Load() && tx.Type() != types.LegacyTxType { return core.ErrTxTypeNotSupported @@ -1924,6 +1928,9 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Trans balance := uint256.NewInt(0) + pool.currentStateMutex.Lock() + defer pool.currentStateMutex.Unlock() + // Iterate over all accounts and promote any executable transactions for _, addr := range accounts { list = pool.queue[addr] @@ -2261,6 +2268,9 @@ func (pool *TxPool) demoteUnexecutables() { // Iterate over all accounts and demote any non-executable transactions pool.pendingMu.RLock() + pool.currentStateMutex.Lock() + defer pool.currentStateMutex.Unlock() + for addr, list := range pool.pending { nonce := pool.currentState.GetNonce(addr)