core/txpool: simplify loop, implement AppendHashes

This commit is contained in:
Martin Holst Swende 2024-03-08 09:40:19 +01:00
parent 5ccec6f7f8
commit a249b78774
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 31 additions and 12 deletions

View file

@ -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,

View file

@ -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
}

View file

@ -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 {