Skip to content

Commit

Permalink
pool: refactoring, addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
emailtovamos committed Oct 15, 2024
1 parent 0e67514 commit f41bb13
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 40 deletions.
2 changes: 1 addition & 1 deletion core/txpool/legacypool/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

// txHeapItem implements the Interface interface of heap so that it can be heapified
// txHeapItem implements the Interface interface (https://pkg.go.dev/container/heap#Interface) of heap so that it can be heapified
type txHeapItem struct {
tx *types.Transaction
timestamp int64 // Unix timestamp (nanoseconds) of when the transaction was added
Expand Down
47 changes: 8 additions & 39 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,6 @@ func (pool *LegacyPool) add(tx *types.Transaction, local bool) (replaced bool, e
return false, txpool.ErrAlreadyKnown
}

maxMainPoolSize := pool.config.GlobalSlots + pool.config.GlobalQueue
txPoolSizeAfterCurrentTx := uint64(pool.all.Slots() + numSlots(tx))

// Make the local flag. If it's from local source or it's from the network but
// the sender is marked as local previously, treat it as the local transaction.
isLocal := local || pool.locals.containsTx(tx)
Expand Down Expand Up @@ -806,7 +803,7 @@ func (pool *LegacyPool) add(tx *types.Transaction, local bool) (replaced bool, e
}

// If the transaction pool is full, discard underpriced transactions
if txPoolSizeAfterCurrentTx > maxMainPoolSize {
if uint64(pool.all.Slots()+numSlots(tx)) > pool.config.GlobalSlots+pool.config.GlobalQueue {
// If the new transaction is underpriced, don't accept it
if !isLocal && pool.priced.Underpriced(tx) {
log.Trace("Discarding underpriced transaction", "hash", hash, "gasTipCap", tx.GasTipCap(), "gasFeeCap", tx.GasFeeCap())
Expand All @@ -826,8 +823,7 @@ func (pool *LegacyPool) add(tx *types.Transaction, local bool) (replaced bool, e
// New transaction is better than our worse ones, make room for it.
// If it's a local transaction, forcibly discard all available transactions.
// Otherwise if we can't make enough room for new one, abort the operation.
toBeDiscarded := pool.all.Slots() - int(pool.config.GlobalSlots+pool.config.GlobalQueue) + numSlots(tx)
drop, success := pool.priced.Discard(toBeDiscarded, isLocal)
drop, success := pool.priced.Discard(pool.all.Slots()-int(pool.config.GlobalSlots+pool.config.GlobalQueue)+numSlots(tx), isLocal)

// Special case, we still can't make the room for the new remote one.
if !isLocal && !success {
Expand Down Expand Up @@ -1727,8 +1723,7 @@ func (pool *LegacyPool) truncateQueue() {
for _, list := range pool.queue {
queued += uint64(list.Len())
}
queueMax := pool.config.GlobalQueue
if queued <= queueMax {
if queued <= pool.config.GlobalQueue {
return
}

Expand All @@ -1742,7 +1737,7 @@ func (pool *LegacyPool) truncateQueue() {
sort.Sort(sort.Reverse(addresses))

// Drop transactions until the total is below the limit or only locals remain
for drop := queued - queueMax; drop > 0 && len(addresses) > 0; {
for drop := queued - pool.config.GlobalQueue; drop > 0 && len(addresses) > 0; {
addr := addresses[len(addresses)-1]
list := pool.queue[addr.address]

Expand Down Expand Up @@ -2097,27 +2092,6 @@ func numSlots(tx *types.Transaction) int {
return int((tx.Size() + txSlotSize - 1) / txSlotSize)
}

func (pool *LegacyPool) startPeriodicTransfer(t time.Duration) {
ticker := time.NewTicker(time.Minute) // Adjust the interval as needed
if t != 0 {
ticker.Reset(t)
}

go func() {
for {
select {
case <-ticker.C:
pool.mu.Lock()
pool.transferTransactions()
pool.mu.Unlock()
case <-pool.reorgShutdownCh:
ticker.Stop()
return
}
}
}()
}

// transferTransactions mainly moves from OverflowPool to MainPool
func (pool *LegacyPool) transferTransactions() {
maxMainPoolSize := int(pool.config.GlobalSlots + pool.config.GlobalQueue)
Expand All @@ -2133,19 +2107,14 @@ func (pool *LegacyPool) transferTransactions() {
}
extraSlots := maxMainPoolSize - currentMainPoolSize
extraTransactions := (extraSlots + 3) / 4 // Since maximum slots per transaction is 4
// So now we can take out extraTransactions number of transactions from OverflowPool and put in MainPool
if extraTransactions < 1 {
return
}

log.Debug("Will attempt to transfer from OverflowPool to MainPool", "transactions", extraTransactions)

tx := pool.localBufferPool.Flush(extraTransactions)
if len(tx) == 0 {
txs := pool.localBufferPool.Flush(extraTransactions)
if len(txs) == 0 {
return
}

pool.Add(tx, true, false)
pool.Add(txs, true, false)
}

func (pool *LegacyPool) availableSlotsOverflowPool() int {
Expand All @@ -2157,7 +2126,7 @@ func (pool *LegacyPool) availableSlotsOverflowPool() int {
return 0
}

func (pool *LegacyPool) printTxStats() {
func (pool *LegacyPool) PrintTxStats() {
for _, l := range pool.pending {
for _, transaction := range l.txs.items {
from, _ := types.Sender(pool.signer, transaction)
Expand Down

0 comments on commit f41bb13

Please sign in to comment.