Skip to content

Commit

Permalink
fix: 'nonce too low' list should be shift with a initial index of -1,…
Browse files Browse the repository at this point in the history
… but not 0

add more logs and metrics
  • Loading branch information
andyzhang2023 committed Nov 7, 2024
1 parent 34bba5b commit 1abebd6
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ var (
reorgresetTimer = metrics.NewRegisteredTimer("txpool/reorgresettime", nil)
truncateTimer = metrics.NewRegisteredTimer("txpool/truncatetime", nil)
reorgresetNoblockingTimer = metrics.NewRegisteredTimer("txpool/noblocking/reorgresettime", nil)

//nonce too low
nonceTooLowHeaderTimer = metrics.NewRegisteredTimer("txpool/nonce/too/low/header/duration", nil)
nonceTooLowBlockTimer = metrics.NewRegisteredTimer("txpool/nonce/too/low/block/duration", nil)
nonceTooLowTxMeter = metrics.NewRegisteredMeter("txpool/nonce/too/low/tx", nil)
)

// BlockChain defines the minimal set of methods needed to back a tx pool with
Expand Down Expand Up @@ -644,33 +649,50 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) map[common.Address]
var (
minTipBig *big.Int
baseFeeBig *big.Int

blockNumber uint64 = 0
blockHash common.Hash = common.Hash{}
nonceTooLowCount = 0
currBlockDuration time.Duration = 0
currHeaderDuration time.Duration = 0
txHashesDuration time.Duration = 0
staled = make(map[common.Hash]struct{})
)
defer func() {
log.Info("perf-trace Pending() nonce too low", "blockNumber", blockNumber, "blockHash", blockHash, "nonceTooLowCount", nonceTooLowCount, "staled", len(staled), "currHeaderDuration", currHeaderDuration, "currBlockDuration", currBlockDuration, "txHashesDuration", txHashesDuration)
}()
if filter.MinTip != nil {
minTipBig = filter.MinTip.ToBig()
}
if filter.BaseFee != nil {
baseFeeBig = filter.BaseFee.ToBig()
}
pending := make(map[common.Address][]*txpool.LazyTransaction, len(pool.pending))
var staled map[common.Hash]struct{}
t0 := time.Now()
if currHeader := pool.chain.CurrentBlock(); currHeader != nil {
currBlock := pool.chain.GetBlock(currHeader.Hash(), currHeader.Number.Uint64())
currHeaderDuration = time.Since(t0)
blockNumber = currHeader.Number.Uint64()
blockHash = currHeader.Hash()
currBlock := pool.chain.GetBlock(blockHash, currHeader.Number.Uint64())
currBlockDuration = time.Since(t0) - currHeaderDuration
staled = make(map[common.Hash]struct{}, len(currBlock.Transactions()))
for _, tx := range currBlock.Transactions() {
staled[tx.Hash()] = struct{}{}
}
txHashesDuration = time.Since(t0) - currBlockDuration - currHeaderDuration
}
for addr, txs := range pool.pendingCache.dump() {
// remove nonce too low transactions
if len(staled) > 0 {
noncetoolow := 0
noncetoolow := -1
for i, tx := range txs {
if _, hit := staled[tx.Hash()]; !hit {
break
}
noncetoolow = i
}
txs = txs[noncetoolow:]
nonceTooLowCount += noncetoolow + 1
txs = txs[noncetoolow+1:]
}

// If the miner requests tip enforcement, cap the lists now
Expand Down

0 comments on commit 1abebd6

Please sign in to comment.