Skip to content

Commit

Permalink
Add mutex for currentState in TxPool
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls authored and 0xsharma committed Nov 29, 2023
1 parent 3eb8577 commit 453ec37
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions core/txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 453ec37

Please sign in to comment.