Skip to content

Commit

Permalink
On NotifyAccountNonce, remove transactions with lower nonces.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Jul 1, 2024
1 parent 73e894a commit 41fb6b8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
12 changes: 10 additions & 2 deletions txcache/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ import (
var log = logger.GetOrCreate("txcache")

func (cache *TxCache) monitorEvictionWrtSenderLimit(sender []byte, evicted [][]byte) {
log.Trace("TxCache.AddTx() evict transactions wrt. limit by sender", "name", cache.name, "sender", sender, "num", len(evicted))
log.Trace("TxCache.monitorEvictionWrtSenderLimit()", "name", cache.name, "sender", sender, "num", len(evicted))

for i := 0; i < core.MinInt(len(evicted), numEvictedTxsToDisplay); i++ {
log.Trace("TxCache.AddTx() evict transactions wrt. limit by sender", "name", cache.name, "sender", sender, "tx", evicted[i])
log.Trace("TxCache.monitorEvictionWrtSenderLimit()", "name", cache.name, "sender", sender, "tx", evicted[i])
}
}

func (cache *TxCache) monitorEvictionWrtSenderNonce(sender []byte, senderNonce uint64, evicted [][]byte) {
log.Trace("TxCache.monitorEvictionWrtSenderNonce()", "name", cache.name, "sender", sender, "nonce", senderNonce, "num", len(evicted))

for i := 0; i < core.MinInt(len(evicted), numEvictedTxsToDisplay); i++ {
log.Trace("TxCache.monitorEvictionWrtSenderNonce()", "name", cache.name, "sender", sender, "nonce", senderNonce, "tx", evicted[i])
}
}

Expand Down
7 changes: 6 additions & 1 deletion txcache/txCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ func (cache *TxCache) UnRegisterHandler(string) {
// NotifyAccountNonce should be called by external components (such as interceptors and transactions processor)
// in order to inform the cache about initial nonce gap phenomena
func (cache *TxCache) NotifyAccountNonce(accountKey []byte, nonce uint64) {
cache.txListBySender.notifyAccountNonce(accountKey, nonce)
evicted := cache.txListBySender.notifyAccountNonce(accountKey, nonce)

if len(evicted) > 0 {
cache.monitorEvictionWrtSenderNonce(accountKey, nonce, evicted)
cache.txByHash.RemoveTxsBulk(evicted)
}
}

// ImmunizeTxsAgainstEviction does nothing for this type of cache
Expand Down
6 changes: 3 additions & 3 deletions txcache/txListBySenderMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ func (txMap *txListBySenderMap) RemoveSendersBulk(senders []string) uint32 {
return numRemoved
}

func (txMap *txListBySenderMap) notifyAccountNonce(accountKey []byte, nonce uint64) {
func (txMap *txListBySenderMap) notifyAccountNonce(accountKey []byte, nonce uint64) [][]byte {
sender := string(accountKey)
listForSender, ok := txMap.getListForSender(sender)
if !ok {
return
return nil
}

listForSender.notifyAccountNonce(nonce)
return listForSender.notifyAccountNonce(nonce)
}

func (txMap *txListBySenderMap) getSnapshotAscending() []*txListForSender {
Expand Down
30 changes: 29 additions & 1 deletion txcache/txListForSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,37 @@ func approximatelyCountTxInLists(lists []*txListForSender) uint64 {

// notifyAccountNonce does not update the "numFailedSelections" counter,
// since the notification comes at a time when we cannot actually detect whether the initial gap still exists or it was resolved.
func (listForSender *txListForSender) notifyAccountNonce(nonce uint64) {
// Removes transactions with lower nonces and returns their hashes.
func (listForSender *txListForSender) notifyAccountNonce(nonce uint64) [][]byte {
listForSender.mutex.Lock()
defer listForSender.mutex.Unlock()

listForSender.accountNonce.Set(nonce)
_ = listForSender.accountNonceKnown.SetReturningPrevious()

return listForSender.evictTransactionsWithLowerNonces(nonce)
}

// This function should only be used in critical section (listForSender.mutex)
func (listForSender *txListForSender) evictTransactionsWithLowerNonces(accountNonce uint64) [][]byte {
evictedTxHashes := make([][]byte, 0)

for element := listForSender.items.Front(); element != nil; element = element.Next() {
tx := element.Value.(*WrappedTransaction)
txNonce := tx.Tx.GetNonce()

if txNonce >= accountNonce {
break
}

listForSender.items.Remove(element)
listForSender.onRemovedListElement(element)

// Keep track of removed transactions
evictedTxHashes = append(evictedTxHashes, tx.TxHash)
}

return evictedTxHashes
}

// This function should only be used in critical section (listForSender.mutex)
Expand Down

0 comments on commit 41fb6b8

Please sign in to comment.