diff --git a/core/txpool/blobpool/blobpool.go b/core/txpool/blobpool/blobpool.go index 619aeb40de..ba6308b4bb 100644 --- a/core/txpool/blobpool/blobpool.go +++ b/core/txpool/blobpool/blobpool.go @@ -1480,8 +1480,7 @@ func (p *BlobPool) Pending(filter txpool.PendingFilter) txpool.Pending { } for addr, txs := range p.index { var ( - tail []*txpool.LazyTransaction - first = true + tail []*txpool.LazyTransaction ) for i, tx := range txs { var tip *uint256.Int @@ -1513,8 +1512,7 @@ func (p *BlobPool) Pending(filter txpool.PendingFilter) txpool.Pending { Gas: tx.execGas, BlobGas: tx.blobGas, } - if first { - first = false + if len(tail) == 0 { tail = make([]*txpool.LazyTransaction, 0, len(txs)-i) heads = append(heads, &txpool.TxTips{ From: addr, diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 677b18014a..652045dad9 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -549,9 +549,8 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) txpool.Pending { continue } var ( - tail []*txpool.LazyTransaction - first = true - txs = list.Flatten() + tail []*txpool.LazyTransaction + txs = list.Flatten() ) for i, tx := range txs { if tx.GasFeeCapIntCmp(baseFeeBig) < 0 { @@ -575,8 +574,7 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) txpool.Pending { Gas: txs[i].Gas(), BlobGas: txs[i].BlobGas(), } - if first { - first = false + if len(tail) == 0 { tail = make([]*txpool.LazyTransaction, 0, len(txs)-i) heads = append(heads, &txpool.TxTips{ From: addr, @@ -610,9 +608,7 @@ func (pool *LegacyPool) PendingHashes(filter txpool.PendingFilter) []common.Hash if filter.OnlyLocals && !pool.locals.contains(addr) { continue } - for _, tx := range list.Flatten() { - hashes = append(hashes, tx.Hash()) - } + hashes = list.AppendHashes(hashes) } return hashes } diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 7db9c98ace..51f4dbc70a 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -258,6 +258,25 @@ func (m *sortedMap) Flatten() types.Transactions { return txs } +// AppendHashes uses the flattened slice of transactions and appends the hashes +// to the destination slice. +func (m *sortedMap) AppendHashes(dst []common.Hash) []common.Hash { + m.cacheMu.Lock() + defer m.cacheMu.Unlock() + // If the sorting was not cached yet, create and cache it + if m.cache == nil { + m.cache = make(types.Transactions, 0, len(m.items)) + for _, tx := range m.items { + m.cache = append(m.cache, tx) + } + sort.Sort(types.TxByNonce(m.cache)) + } + for _, tx := range m.items { + dst = append(dst, tx.Hash()) + } + return dst +} + // LastElement returns the last element of a flattened list, thus, the // transaction with the highest nonce func (m *sortedMap) LastElement() *types.Transaction { @@ -453,6 +472,12 @@ func (l *list) Flatten() types.Transactions { return l.txs.Flatten() } +// AppendHashes flattens a nonce-sorted slice of transcations, and appends +// the hashes to dst. The destination slice might be reallocated, and is returned. +func (l *list) AppendHashes(dst []common.Hash) []common.Hash { + return l.txs.AppendHashes(dst) +} + // LastElement returns the last element of a flattened list, thus, the // transaction with the highest nonce func (l *list) LastElement() *types.Transaction {