Skip to content

Commit

Permalink
pool: fail fast, disable by default, no interface
Browse files Browse the repository at this point in the history
  • Loading branch information
emailtovamos committed Oct 16, 2024
1 parent 0dd0bd7 commit 60bdc25
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
20 changes: 10 additions & 10 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ var DefaultConfig = Config{
GlobalSlots: 4096 + 1024, // urgent + floating queue capacity with 4:1 ratio
AccountQueue: 64,
GlobalQueue: 1024,
OverflowPoolSlots: 1024,
OverflowPoolSlots: 0,

Lifetime: 3 * time.Hour,
ReannounceTime: 10 * 365 * 24 * time.Hour,
Expand Down Expand Up @@ -239,7 +239,7 @@ type LegacyPool struct {
all *lookup // All transactions to allow lookups
priced *pricedList // All transactions sorted by price

localBufferPool OverflowPool // Local buffer transactions
localBufferPool *TxOverflowPool // Local buffer transactions

reqResetCh chan *txpoolResetRequest
reqPromoteCh chan *accountSet
Expand Down Expand Up @@ -2083,23 +2083,23 @@ func numSlots(tx *types.Transaction) int {
return int((tx.Size() + txSlotSize - 1) / txSlotSize)
}

// transferTransactions mainly moves from OverflowPool to MainPool
// transferTransactions moves transactions from OverflowPool to MainPool
func (pool *LegacyPool) transferTransactions() {
maxMainPoolSize := int(pool.config.GlobalSlots + pool.config.GlobalQueue)
extraSizeMainPool := maxMainPoolSize - int(uint64(len(pool.pending))+uint64(len(pool.queue)))
if extraSizeMainPool <= 0 {
// Fail fast if the overflow pool is empty
if pool.localBufferPool.Size() == 0 {
return
}

maxMainPoolSize := int(pool.config.GlobalSlots + pool.config.GlobalQueue)
// Use pool.all.Slots() to get the total slots used by all transactions
currentMainPoolSize := pool.all.Slots()
canTransferOverflowPoolToMainPool := maxMainPoolSize > currentMainPoolSize
if !canTransferOverflowPoolToMainPool {
if currentMainPoolSize >= maxMainPoolSize {
return
}

extraSlots := maxMainPoolSize - currentMainPoolSize
extraTransactions := (extraSlots + 3) / 4 // Since maximum slots per transaction is 4
extraTransactions := (extraSlots + 3) / 4 // Since a transaction can take up to 4 slots
log.Debug("Will attempt to transfer from OverflowPool to MainPool", "transactions", extraTransactions)

txs := pool.localBufferPool.Flush(extraTransactions)
if len(txs) == 0 {
return
Expand Down
2 changes: 1 addition & 1 deletion core/txpool/legacypool/legacypool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2247,12 +2247,12 @@ func TestReplacement(t *testing.T) {

func TestTransferTransactions(t *testing.T) {
t.Parallel()
testTxPoolConfig.OverflowPoolSlots = 1
pool, _ := setupPoolWithConfig(eip1559Config)
defer pool.Close()

pool.config.GlobalSlots = 1
pool.config.GlobalQueue = 2
pool.config.OverflowPoolSlots = 1

// Create a number of test accounts and fund them
keys := make([]*ecdsa.PrivateKey, 5)
Expand Down
15 changes: 0 additions & 15 deletions core/txpool/legacypool/overflowpool.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ func (h *txHeap) Pop() interface{} {
return item
}

type TxOverflowPoolHeap struct {
type TxOverflowPool struct {
txHeap txHeap
index map[common.Hash]*txHeapItem
mu sync.RWMutex
maxSize uint64
totalSize int
}

func NewTxOverflowPoolHeap(estimatedMaxSize uint64) *TxOverflowPoolHeap {
return &TxOverflowPoolHeap{
func NewTxOverflowPoolHeap(estimatedMaxSize uint64) *TxOverflowPool {
return &TxOverflowPool{
txHeap: make(txHeap, 0, estimatedMaxSize),
index: make(map[common.Hash]*txHeapItem, estimatedMaxSize),
maxSize: estimatedMaxSize,
}
}

func (tp *TxOverflowPoolHeap) Add(tx *types.Transaction) {
func (tp *TxOverflowPool) Add(tx *types.Transaction) {
tp.mu.Lock()
defer tp.mu.Unlock()

Expand Down Expand Up @@ -107,7 +107,7 @@ func (tp *TxOverflowPoolHeap) Add(tx *types.Transaction) {
OverflowPoolGauge.Inc(1)
}

func (tp *TxOverflowPoolHeap) Get(hash common.Hash) (*types.Transaction, bool) {
func (tp *TxOverflowPool) Get(hash common.Hash) (*types.Transaction, bool) {
tp.mu.RLock()
defer tp.mu.RUnlock()
if item, ok := tp.index[hash]; ok {
Expand All @@ -116,7 +116,7 @@ func (tp *TxOverflowPoolHeap) Get(hash common.Hash) (*types.Transaction, bool) {
return nil, false
}

func (tp *TxOverflowPoolHeap) Remove(hash common.Hash) {
func (tp *TxOverflowPool) Remove(hash common.Hash) {
tp.mu.Lock()
defer tp.mu.Unlock()
if item, ok := tp.index[hash]; ok {
Expand All @@ -127,7 +127,7 @@ func (tp *TxOverflowPoolHeap) Remove(hash common.Hash) {
}
}

func (tp *TxOverflowPoolHeap) Flush(n int) []*types.Transaction {
func (tp *TxOverflowPool) Flush(n int) []*types.Transaction {
tp.mu.Lock()
defer tp.mu.Unlock()
if n > tp.txHeap.Len() {
Expand All @@ -148,19 +148,19 @@ func (tp *TxOverflowPoolHeap) Flush(n int) []*types.Transaction {
return txs
}

func (tp *TxOverflowPoolHeap) Len() int {
func (tp *TxOverflowPool) Len() int {
tp.mu.RLock()
defer tp.mu.RUnlock()
return tp.txHeap.Len()
}

func (tp *TxOverflowPoolHeap) Size() int {
func (tp *TxOverflowPool) Size() int {
tp.mu.RLock()
defer tp.mu.RUnlock()
return tp.totalSize
}

func (tp *TxOverflowPoolHeap) PrintTxStats() {
func (tp *TxOverflowPool) PrintTxStats() {
tp.mu.RLock()
defer tp.mu.RUnlock()
for _, item := range tp.txHeap {
Expand Down
File renamed without changes.

0 comments on commit 60bdc25

Please sign in to comment.