Skip to content

Commit 88c6962

Browse files
authored
core/txpool: remove "local" notion from the txpool price heap (#21478)
* core: separate the local notion from the pricedHeap * core: add benchmarks * core: improve tests * core: address comments * core: degrade the panic to error message * core: fix typo * core: address comments * core: address comment * core: use PEAK instead of POP * core: address comments
1 parent b47f4ca commit 88c6962

3 files changed

Lines changed: 271 additions & 145 deletions

File tree

‎core/tx_list.go‎

Lines changed: 65 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/core/types"
27-
"github.com/ethereum/go-ethereum/log"
2827
)
2928

3029
// nonceHeap is a heap.Interface implementation over 64bit unsigned integers for
@@ -439,24 +438,29 @@ func (h *priceHeap) Pop() interface{} {
439438
}
440439

441440
// txPricedList is a price-sorted heap to allow operating on transactions pool
442-
// contents in a price-incrementing way.
441+
// contents in a price-incrementing way. It's built opon the all transactions
442+
// in txpool but only interested in the remote part. It means only remote transactions
443+
// will be considered for tracking, sorting, eviction, etc.
443444
type txPricedList struct {
444-
all *txLookup // Pointer to the map of all transactions
445-
items *priceHeap // Heap of prices of all the stored transactions
446-
stales int // Number of stale price points to (re-heap trigger)
445+
all *txLookup // Pointer to the map of all transactions
446+
remotes *priceHeap // Heap of prices of all the stored **remote** transactions
447+
stales int // Number of stale price points to (re-heap trigger)
447448
}
448449

449450
// newTxPricedList creates a new price-sorted transaction heap.
450451
func newTxPricedList(all *txLookup) *txPricedList {
451452
return &txPricedList{
452-
all: all,
453-
items: new(priceHeap),
453+
all: all,
454+
remotes: new(priceHeap),
454455
}
455456
}
456457

457458
// Put inserts a new transaction into the heap.
458-
func (l *txPricedList) Put(tx *types.Transaction) {
459-
heap.Push(l.items, tx)
459+
func (l *txPricedList) Put(tx *types.Transaction, local bool) {
460+
if local {
461+
return
462+
}
463+
heap.Push(l.remotes, tx)
460464
}
461465

462466
// Removed notifies the prices transaction list that an old transaction dropped
@@ -465,121 +469,95 @@ func (l *txPricedList) Put(tx *types.Transaction) {
465469
func (l *txPricedList) Removed(count int) {
466470
// Bump the stale counter, but exit if still too low (< 25%)
467471
l.stales += count
468-
if l.stales <= len(*l.items)/4 {
472+
if l.stales <= len(*l.remotes)/4 {
469473
return
470474
}
471475
// Seems we've reached a critical number of stale transactions, reheap
472-
reheap := make(priceHeap, 0, l.all.Count())
473-
474-
l.stales, l.items = 0, &reheap
475-
l.all.Range(func(hash common.Hash, tx *types.Transaction) bool {
476-
*l.items = append(*l.items, tx)
477-
return true
478-
})
479-
heap.Init(l.items)
476+
l.Reheap()
480477
}
481478

482479
// Cap finds all the transactions below the given price threshold, drops them
483480
// from the priced list and returns them for further removal from the entire pool.
484-
func (l *txPricedList) Cap(threshold *big.Int, local *accountSet) types.Transactions {
481+
//
482+
// Note: only remote transactions will be considered for eviction.
483+
func (l *txPricedList) Cap(threshold *big.Int) types.Transactions {
485484
drop := make(types.Transactions, 0, 128) // Remote underpriced transactions to drop
486-
save := make(types.Transactions, 0, 64) // Local underpriced transactions to keep
487-
488-
for len(*l.items) > 0 {
485+
for len(*l.remotes) > 0 {
489486
// Discard stale transactions if found during cleanup
490-
tx := heap.Pop(l.items).(*types.Transaction)
491-
if l.all.Get(tx.Hash()) == nil {
487+
cheapest := (*l.remotes)[0]
488+
if l.all.GetRemote(cheapest.Hash()) == nil { // Removed or migrated
489+
heap.Pop(l.remotes)
492490
l.stales--
493491
continue
494492
}
495493
// Stop the discards if we've reached the threshold
496-
if tx.GasPriceIntCmp(threshold) >= 0 {
497-
save = append(save, tx)
494+
if cheapest.GasPriceIntCmp(threshold) >= 0 {
498495
break
499496
}
500-
// Non stale transaction found, discard unless local
501-
if local.containsTx(tx) {
502-
save = append(save, tx)
503-
} else {
504-
drop = append(drop, tx)
505-
}
506-
}
507-
for _, tx := range save {
508-
heap.Push(l.items, tx)
497+
heap.Pop(l.remotes)
498+
drop = append(drop, cheapest)
509499
}
510500
return drop
511501
}
512502

513503
// Underpriced checks whether a transaction is cheaper than (or as cheap as) the
514-
// lowest priced transaction currently being tracked.
515-
func (l *txPricedList) Underpriced(tx *types.Transaction, local *accountSet) bool {
516-
// Local transactions cannot be underpriced
517-
if local.containsTx(tx) {
518-
return false
519-
}
504+
// lowest priced (remote) transaction currently being tracked.
505+
func (l *txPricedList) Underpriced(tx *types.Transaction) bool {
520506
// Discard stale price points if found at the heap start
521-
for len(*l.items) > 0 {
522-
head := []*types.Transaction(*l.items)[0]
523-
if l.all.Get(head.Hash()) == nil {
507+
for len(*l.remotes) > 0 {
508+
head := []*types.Transaction(*l.remotes)[0]
509+
if l.all.GetRemote(head.Hash()) == nil { // Removed or migrated
524510
l.stales--
525-
heap.Pop(l.items)
511+
heap.Pop(l.remotes)
526512
continue
527513
}
528514
break
529515
}
530516
// Check if the transaction is underpriced or not
531-
if len(*l.items) == 0 {
532-
log.Error("Pricing query for empty pool") // This cannot happen, print to catch programming errors
533-
return false
517+
if len(*l.remotes) == 0 {
518+
return false // There is no remote transaction at all.
534519
}
535-
cheapest := []*types.Transaction(*l.items)[0]
520+
// If the remote transaction is even cheaper than the
521+
// cheapest one tracked locally, reject it.
522+
cheapest := []*types.Transaction(*l.remotes)[0]
536523
return cheapest.GasPriceCmp(tx) >= 0
537524
}
538525

539526
// Discard finds a number of most underpriced transactions, removes them from the
540527
// priced list and returns them for further removal from the entire pool.
541-
func (l *txPricedList) Discard(slots int, local *accountSet) types.Transactions {
542-
// If we have some local accountset, those will not be discarded
543-
if !local.empty() {
544-
// In case the list is filled to the brim with 'local' txs, we do this
545-
// little check to avoid unpacking / repacking the heap later on, which
546-
// is very expensive
547-
discardable := 0
548-
for _, tx := range *l.items {
549-
if !local.containsTx(tx) {
550-
discardable++
551-
}
552-
if discardable >= slots {
553-
break
554-
}
555-
}
556-
if slots > discardable {
557-
slots = discardable
558-
}
559-
}
560-
if slots == 0 {
561-
return nil
562-
}
563-
drop := make(types.Transactions, 0, slots) // Remote underpriced transactions to drop
564-
save := make(types.Transactions, 0, len(*l.items)-slots) // Local underpriced transactions to keep
565-
566-
for len(*l.items) > 0 && slots > 0 {
528+
//
529+
// Note local transaction won't be considered for eviction.
530+
func (l *txPricedList) Discard(slots int, force bool) (types.Transactions, bool) {
531+
drop := make(types.Transactions, 0, slots) // Remote underpriced transactions to drop
532+
for len(*l.remotes) > 0 && slots > 0 {
567533
// Discard stale transactions if found during cleanup
568-
tx := heap.Pop(l.items).(*types.Transaction)
569-
if l.all.Get(tx.Hash()) == nil {
534+
tx := heap.Pop(l.remotes).(*types.Transaction)
535+
if l.all.GetRemote(tx.Hash()) == nil { // Removed or migrated
570536
l.stales--
571537
continue
572538
}
573-
// Non stale transaction found, discard unless local
574-
if local.containsTx(tx) {
575-
save = append(save, tx)
576-
} else {
577-
drop = append(drop, tx)
578-
slots -= numSlots(tx)
539+
// Non stale transaction found, discard it
540+
drop = append(drop, tx)
541+
slots -= numSlots(tx)
542+
}
543+
// If we still can't make enough room for the new transaction
544+
if slots > 0 && !force {
545+
for _, tx := range drop {
546+
heap.Push(l.remotes, tx)
579547
}
548+
return nil, false
580549
}
581-
for _, tx := range save {
582-
heap.Push(l.items, tx)
583-
}
584-
return drop
550+
return drop, true
551+
}
552+
553+
// Reheap forcibly rebuilds the heap based on the current remote transaction set.
554+
func (l *txPricedList) Reheap() {
555+
reheap := make(priceHeap, 0, l.all.RemoteCount())
556+
557+
l.stales, l.remotes = 0, &reheap
558+
l.all.Range(func(hash common.Hash, tx *types.Transaction, local bool) bool {
559+
*l.remotes = append(*l.remotes, tx)
560+
return true
561+
}, false, true) // Only iterate remotes
562+
heap.Init(l.remotes)
585563
}

0 commit comments

Comments
 (0)