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

On NotifyAccountNonce, remove transactions with lower nonces #49

Closed
wants to merge 1 commit into from
Closed
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
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
Loading