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

@ -1481,7 +1481,6 @@ func (p *BlobPool) Pending(filter txpool.PendingFilter) txpool.Pending {
for addr, txs := range p.index { for addr, txs := range p.index {
var ( var (
tail []*txpool.LazyTransaction tail []*txpool.LazyTransaction
first = true
) )
for i, tx := range txs { for i, tx := range txs {
var tip *uint256.Int var tip *uint256.Int
@ -1513,8 +1512,7 @@ func (p *BlobPool) Pending(filter txpool.PendingFilter) txpool.Pending {
Gas: tx.execGas, Gas: tx.execGas,
BlobGas: tx.blobGas, BlobGas: tx.blobGas,
} }
if first { if len(tail) == 0 {
first = false
tail = make([]*txpool.LazyTransaction, 0, len(txs)-i) tail = make([]*txpool.LazyTransaction, 0, len(txs)-i)
heads = append(heads, &txpool.TxTips{ heads = append(heads, &txpool.TxTips{
From: addr, From: addr,

View file

@ -550,7 +550,6 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) txpool.Pending {
} }
var ( var (
tail []*txpool.LazyTransaction tail []*txpool.LazyTransaction
first = true
txs = list.Flatten() txs = list.Flatten()
) )
for i, tx := range txs { for i, tx := range txs {
@ -575,8 +574,7 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) txpool.Pending {
Gas: txs[i].Gas(), Gas: txs[i].Gas(),
BlobGas: txs[i].BlobGas(), BlobGas: txs[i].BlobGas(),
} }
if first { if len(tail) == 0 {
first = false
tail = make([]*txpool.LazyTransaction, 0, len(txs)-i) tail = make([]*txpool.LazyTransaction, 0, len(txs)-i)
heads = append(heads, &txpool.TxTips{ heads = append(heads, &txpool.TxTips{
From: addr, From: addr,
@ -610,9 +608,7 @@ func (pool *LegacyPool) PendingHashes(filter txpool.PendingFilter) []common.Hash
if filter.OnlyLocals && !pool.locals.contains(addr) { if filter.OnlyLocals && !pool.locals.contains(addr) {
continue continue
} }
for _, tx := range list.Flatten() { hashes = list.AppendHashes(hashes)
hashes = append(hashes, tx.Hash())
}
} }
return hashes return hashes
} }

View file

@ -258,6 +258,25 @@ func (m *sortedMap) Flatten() types.Transactions {
return txs 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 // LastElement returns the last element of a flattened list, thus, the
// transaction with the highest nonce // transaction with the highest nonce
func (m *sortedMap) LastElement() *types.Transaction { func (m *sortedMap) LastElement() *types.Transaction {
@ -453,6 +472,12 @@ func (l *list) Flatten() types.Transactions {
return l.txs.Flatten() 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 // LastElement returns the last element of a flattened list, thus, the
// transaction with the highest nonce // transaction with the highest nonce
func (l *list) LastElement() *types.Transaction { func (l *list) LastElement() *types.Transaction {