Skip to content

Commit

Permalink
pool: make maxSize & totalSize same type
Browse files Browse the repository at this point in the history
  • Loading branch information
emailtovamos committed Nov 21, 2024
1 parent 86484a1 commit b00a6c3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func (pool *LegacyPool) Stats() (int, int) {
return pool.stats()
}

func (pool *LegacyPool) statsOverflowPool() int {
func (pool *LegacyPool) statsOverflowPool() uint64 {
pool.mu.RLock()
defer pool.mu.RUnlock()

Expand Down
8 changes: 4 additions & 4 deletions core/txpool/legacypool/legacypool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2268,25 +2268,25 @@ func TestTransferTransactions(t *testing.T) {

assert.Equal(t, 0, pending, "pending transactions mismatched")
assert.Equal(t, 0, queue, "queued transactions mismatched")
assert.Equal(t, 1, pool.statsOverflowPool(), "OverflowPool size unexpected")
assert.Equal(t, uint64(1), pool.statsOverflowPool(), "OverflowPool size unexpected")

tx2 := dynamicFeeTx(0, 100000, big.NewInt(3), big.NewInt(2), keys[1])
pool.addToOverflowPool([]*types.Transaction{tx2}, true)
assert.Equal(t, 1, pool.statsOverflowPool(), "OverflowPool size unexpected")
assert.Equal(t, uint64(1), pool.statsOverflowPool(), "OverflowPool size unexpected")
<-pool.requestPromoteExecutables(newAccountSet(pool.signer, from))
pending, queue = pool.Stats()

assert.Equal(t, 0, pending, "pending transactions mismatched")
assert.Equal(t, 1, queue, "queued transactions mismatched")
assert.Equal(t, 0, pool.statsOverflowPool(), "OverflowPool size unexpected")
assert.Equal(t, uint64(0), pool.statsOverflowPool(), "OverflowPool size unexpected")

tx3 := dynamicFeeTx(0, 100000, big.NewInt(3), big.NewInt(2), keys[2])
pool.addToOverflowPool([]*types.Transaction{tx3}, true)
pending, queue = pool.Stats()

assert.Equal(t, 1, pending, "pending transactions mismatched")
assert.Equal(t, 0, queue, "queued transactions mismatched")
assert.Equal(t, 1, pool.statsOverflowPool(), "OverflowPool size unexpected")
assert.Equal(t, uint64(1), pool.statsOverflowPool(), "OverflowPool size unexpected")
}

// Tests that the pool rejects replacement dynamic fee transactions that don't
Expand Down
16 changes: 8 additions & 8 deletions core/txpool/legacypool/tx_overflowpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type TxOverflowPool struct {
index map[common.Hash]*txHeapItem
mu sync.RWMutex
maxSize uint64 // Maximum slots
totalSize int // Total number of slots currently
totalSize uint64 // Total number of slots currently
}

func NewTxOverflowPoolHeap(estimatedMaxSize uint64) *TxOverflowPool {
Expand All @@ -87,16 +87,16 @@ func (tp *TxOverflowPool) Add(tx *types.Transaction) bool {
return false
}

txSlots := numSlots(tx)
txSlots := uint64(numSlots(tx))

// If the transaction is too big to ever fit (and the pool isn't empty right now), reject it
if (uint64(txSlots) > tp.maxSize) || (uint64(txSlots) == tp.maxSize && tp.totalSize != 0) {
if (txSlots > tp.maxSize) || (txSlots == tp.maxSize && tp.totalSize != 0) {
log.Warn("Transaction too large to fit in OverflowPool", "transaction", tx.Hash().String(), "requiredSlots", txSlots, "maxSlots", tp.maxSize)
return false
}

// Remove transactions until there is room for the new transaction
for uint64(tp.totalSize+txSlots) > tp.maxSize {
for tp.totalSize+txSlots > tp.maxSize {
if tp.txHeap.Len() == 0 {
// No transactions left to remove, cannot make room
log.Warn("Not enough space in OverflowPool even after clearing", "transaction", tx.Hash().String())
Expand All @@ -109,7 +109,7 @@ func (tp *TxOverflowPool) Add(tx *types.Transaction) bool {
return false
}
delete(tp.index, oldestItem.tx.Hash())
tp.totalSize -= numSlots(oldestItem.tx)
tp.totalSize -= uint64(numSlots(oldestItem.tx))
OverflowPoolGauge.Dec(1)
}

Expand Down Expand Up @@ -141,7 +141,7 @@ func (tp *TxOverflowPool) Remove(hash common.Hash) {
if item, ok := tp.index[hash]; ok {
heap.Remove(&tp.txHeap, item.index)
delete(tp.index, hash)
tp.totalSize -= numSlots(item.tx)
tp.totalSize -= uint64(numSlots(item.tx))
OverflowPoolGauge.Dec(1)
}
}
Expand All @@ -160,7 +160,7 @@ func (tp *TxOverflowPool) Flush(n int) []*types.Transaction {
}
txs[i] = item.tx
delete(tp.index, item.tx.Hash())
tp.totalSize -= numSlots(item.tx)
tp.totalSize -= uint64(numSlots(item.tx))
}

OverflowPoolGauge.Dec(int64(n))
Expand All @@ -173,7 +173,7 @@ func (tp *TxOverflowPool) Len() int {
return tp.txHeap.Len()
}

func (tp *TxOverflowPool) Size() int {
func (tp *TxOverflowPool) Size() uint64 {
tp.mu.RLock()
defer tp.mu.RUnlock()
return tp.totalSize
Expand Down
2 changes: 1 addition & 1 deletion core/txpool/legacypool/tx_overflowpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestTxOverflowPoolSlotCalculation(t *testing.T) {
// Create a third transaction with more slots than tx1
tx3Added := pool.Add(tx3)
assert.Equal(t, false, tx3Added)
assert.Equal(t, 2, pool.totalSize)
assert.Equal(t, uint64(2), pool.totalSize)

// Verify that the pool length remains at 2
assert.Equal(t, 2, pool.Len(), "Expected pool size 2 after overflow")
Expand Down

0 comments on commit b00a6c3

Please sign in to comment.