Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: use LRU cache in txNoncer to avoid memory leaks #25603

Merged
merged 5 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions core/tx_noncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -64,13 +66,15 @@ func (txn *txNoncer) set(addr common.Address, nonce uint64) {
}

// setIfLower updates a new virtual nonce into the virtual state database if the
// the new one is lower.
// new one is lower.
func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) {
txn.lock.Lock()
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
Expand Down
3 changes: 0 additions & 3 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,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 {
holiman marked this conversation as resolved.
Show resolved Hide resolved
pool.mu.RLock()
defer pool.mu.RUnlock()

return pool.pendingNonces.get(addr)
}

Expand Down