From 62a8c2ec034d0296fd63e7c1d6ba6bc44ec5bac3 Mon Sep 17 00:00:00 2001 From: Seungbae Yu Date: Sat, 17 Sep 2022 01:23:13 +0900 Subject: [PATCH] core: don't cache zero nonce in txNoncer (#25603) This changes the nonce cache used by TxPool to not store cached nonces for non-existing accounts. --- core/tx_noncer.go | 8 ++++++-- core/tx_pool.go | 3 --- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/tx_noncer.go b/core/tx_noncer.go index 60779dd3121e..83c3118c0ac5 100644 --- a/core/tx_noncer.go +++ b/core/tx_noncer.go @@ -49,7 +49,9 @@ func (txn *txNoncer) get(addr common.Address) uint64 { defer txn.lock.Unlock() if _, ok := txn.nonces[addr]; !ok { - txn.nonces[addr] = txn.fallback.GetNonce(addr) + if nonce := txn.fallback.GetNonce(addr); nonce != 0 { + txn.nonces[addr] = nonce + } } return txn.nonces[addr] } @@ -70,7 +72,9 @@ func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) { defer txn.lock.Unlock() if _, ok := txn.nonces[addr]; !ok { - txn.nonces[addr] = txn.fallback.GetNonce(addr) + if nonce := txn.fallback.GetNonce(addr); nonce != 0 { + txn.nonces[addr] = nonce + } } if txn.nonces[addr] <= nonce { return diff --git a/core/tx_pool.go b/core/tx_pool.go index d12ab4c3c886..b6911c2c9ecc 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -499,9 +499,6 @@ func (pool *TxPool) SetGasPrice(price *big.Int) { // Nonce returns the next nonce of an account, with all transactions executable // by the pool already applied on top. func (pool *TxPool) Nonce(addr common.Address) uint64 { - pool.mu.RLock() - defer pool.mu.RUnlock() - return pool.pendingNonces.get(addr) }