Skip to content

Commit

Permalink
pool: refactor to overflow pool
Browse files Browse the repository at this point in the history
  • Loading branch information
emailtovamos committed Oct 14, 2024
1 parent 5d44ba9 commit 7a929d6
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 124 deletions.
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var (
utils.TxPoolGlobalSlotsFlag,
utils.TxPoolAccountQueueFlag,
utils.TxPoolGlobalQueueFlag,
utils.TxPoolPool3SlotsFlag,
utils.TxPoolOverflowPoolSlotsFlag,
utils.TxPoolLifetimeFlag,
utils.TxPoolReannounceTimeFlag,
utils.BlobPoolDataDirFlag,
Expand Down
34 changes: 17 additions & 17 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ var (
Value: ethconfig.Defaults.TxPool.GlobalQueue,
Category: flags.TxPoolCategory,
}
TxPoolPool3SlotsFlag = &cli.Uint64Flag{
Name: "txpool.pool3slots",
Usage: "Maximum number of transaction slots in pool 3",
Value: ethconfig.Defaults.TxPool.Pool3Slots,
TxPoolOverflowPoolSlotsFlag = &cli.Uint64Flag{
Name: "txpool.overflowpoolslots",
Usage: "Maximum number of transaction slots in overflow pool",
Value: ethconfig.Defaults.TxPool.OverflowPoolSlots,
Category: flags.TxPoolCategory,
}
TxPoolLifetimeFlag = &cli.DurationFlag{
Expand Down Expand Up @@ -1768,8 +1768,8 @@ func setTxPool(ctx *cli.Context, cfg *legacypool.Config) {
if ctx.IsSet(TxPoolGlobalQueueFlag.Name) {
cfg.GlobalQueue = ctx.Uint64(TxPoolGlobalQueueFlag.Name)
}
if ctx.IsSet(TxPoolPool3SlotsFlag.Name) {
cfg.Pool3Slots = ctx.Uint64(TxPoolPool3SlotsFlag.Name)
if ctx.IsSet(TxPoolOverflowPoolSlotsFlag.Name) {
cfg.OverflowPoolSlots = ctx.Uint64(TxPoolOverflowPoolSlotsFlag.Name)
}
if ctx.IsSet(TxPoolLifetimeFlag.Name) {
cfg.Lifetime = ctx.Duration(TxPoolLifetimeFlag.Name)
Expand Down Expand Up @@ -2292,17 +2292,17 @@ func EnableNodeInfo(poolConfig *legacypool.Config, nodeInfo *p2p.NodeInfo) Setup
return func() {
// register node info into metrics
metrics.NewRegisteredLabel("node-info", nil).Mark(map[string]interface{}{
"Enode": nodeInfo.Enode,
"ENR": nodeInfo.ENR,
"ID": nodeInfo.ID,
"PriceLimit": poolConfig.PriceLimit,
"PriceBump": poolConfig.PriceBump,
"AccountSlots": poolConfig.AccountSlots,
"GlobalSlots": poolConfig.GlobalSlots,
"AccountQueue": poolConfig.AccountQueue,
"GlobalQueue": poolConfig.GlobalQueue,
"Pool3Slots": poolConfig.Pool3Slots,
"Lifetime": poolConfig.Lifetime,
"Enode": nodeInfo.Enode,
"ENR": nodeInfo.ENR,
"ID": nodeInfo.ID,
"PriceLimit": poolConfig.PriceLimit,
"PriceBump": poolConfig.PriceBump,
"AccountSlots": poolConfig.AccountSlots,
"GlobalSlots": poolConfig.GlobalSlots,
"AccountQueue": poolConfig.AccountQueue,
"GlobalQueue": poolConfig.GlobalQueue,
"OverflowPoolSlots": poolConfig.OverflowPoolSlots,
"Lifetime": poolConfig.Lifetime,
})
}
}
Expand Down
25 changes: 15 additions & 10 deletions core/txpool/legacypool/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ func (h *txHeap) Pop() interface{} {
return item
}

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

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

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

Expand All @@ -94,6 +94,7 @@ func (tp *TxPool3Heap) Add(tx *types.Transaction) {
}
delete(tp.index, oldestItem.tx.Hash())
tp.totalSize -= numSlots(oldestItem.tx)
OverflowPoolGauge.Dec(1)
}

item := &txHeapItem{
Expand All @@ -103,9 +104,10 @@ func (tp *TxPool3Heap) Add(tx *types.Transaction) {
heap.Push(&tp.txHeap, item)
tp.index[tx.Hash()] = item
tp.totalSize += numSlots(tx)
OverflowPoolGauge.Inc(1)
}

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

func (tp *TxPool3Heap) Remove(hash common.Hash) {
func (tp *TxOverflowPoolHeap) Remove(hash common.Hash) {
tp.mu.Lock()
defer tp.mu.Unlock()
if item, ok := tp.index[hash]; ok {
heap.Remove(&tp.txHeap, item.index)
delete(tp.index, hash)
tp.totalSize -= numSlots(item.tx)
OverflowPoolGauge.Dec(1)
}
}

func (tp *TxPool3Heap) Flush(n int) []*types.Transaction {
func (tp *TxOverflowPoolHeap) Flush(n int) []*types.Transaction {
tp.mu.Lock()
defer tp.mu.Unlock()
if n > tp.txHeap.Len() {
Expand All @@ -140,22 +143,24 @@ func (tp *TxPool3Heap) Flush(n int) []*types.Transaction {
delete(tp.index, item.tx.Hash())
tp.totalSize -= numSlots(item.tx)
}

OverflowPoolGauge.Dec(int64(n))
return txs
}

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

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

func (tp *TxPool3Heap) PrintTxStats() {
func (tp *TxOverflowPoolHeap) PrintTxStats() {
tp.mu.RLock()
defer tp.mu.RUnlock()
for _, item := range tp.txHeap {
Expand Down
72 changes: 36 additions & 36 deletions core/txpool/legacypool/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ func createTestTx(nonce uint64, gasPrice *big.Int) *types.Transaction {
return types.NewTransaction(nonce, to, big.NewInt(1000), 21000, gasPrice, nil)
}

func TestNewTxPool3Heap(t *testing.T) {
pool := NewTxPool3Heap(0)
func TestNewTxOverflowPoolHeap(t *testing.T) {
pool := NewTxOverflowPoolHeap(0)
if pool == nil {
t.Fatal("NewTxPool3Heap returned nil")
t.Fatal("NewTxOverflowPoolHeap returned nil")
}
if pool.Len() != 0 {
t.Errorf("New pool should be empty, got length %d", pool.Len())
}
}

func TestTxPool3HeapAdd(t *testing.T) {
pool := NewTxPool3Heap(1)
func TestTxOverflowPoolHeapAdd(t *testing.T) {
pool := NewTxOverflowPoolHeap(1)
tx := createTestTx(1, big.NewInt(1000))

pool.Add(tx)
Expand All @@ -43,8 +43,8 @@ func TestTxPool3HeapAdd(t *testing.T) {
}
}

func TestTxPool3HeapGet(t *testing.T) {
pool := NewTxPool3Heap(1)
func TestTxOverflowPoolHeapGet(t *testing.T) {
pool := NewTxOverflowPoolHeap(1)
tx := createTestTx(1, big.NewInt(1000))
pool.Add(tx)

Expand All @@ -62,8 +62,8 @@ func TestTxPool3HeapGet(t *testing.T) {
}
}

func TestTxPool3HeapRemove(t *testing.T) {
pool := NewTxPool3Heap(1)
func TestTxOverflowPoolHeapRemove(t *testing.T) {
pool := NewTxOverflowPoolHeap(1)
tx := createTestTx(1, big.NewInt(1000))
pool.Add(tx)

Expand All @@ -79,8 +79,8 @@ func TestTxPool3HeapRemove(t *testing.T) {
}
}

func TestTxPool3HeapPopN(t *testing.T) {
pool := NewTxPool3Heap(3)
func TestTxOverflowPoolHeapPopN(t *testing.T) {
pool := NewTxOverflowPoolHeap(3)
tx1 := createTestTx(1, big.NewInt(1000))
tx2 := createTestTx(2, big.NewInt(2000))
tx3 := createTestTx(3, big.NewInt(3000))
Expand Down Expand Up @@ -115,8 +115,8 @@ func TestTxPool3HeapPopN(t *testing.T) {
}
}

func TestTxPool3HeapOrdering(t *testing.T) {
pool := NewTxPool3Heap(3)
func TestTxOverflowPoolHeapOrdering(t *testing.T) {
pool := NewTxOverflowPoolHeap(3)
tx1 := createTestTx(1, big.NewInt(1000))
tx2 := createTestTx(2, big.NewInt(2000))
tx3 := createTestTx(3, big.NewInt(3000))
Expand All @@ -135,8 +135,8 @@ func TestTxPool3HeapOrdering(t *testing.T) {
}
}

func TestTxPool3HeapLen(t *testing.T) {
pool := NewTxPool3Heap(2)
func TestTxOverflowPoolHeapLen(t *testing.T) {
pool := NewTxOverflowPoolHeap(2)
if pool.Len() != 0 {
t.Errorf("New pool should have length 0, got %d", pool.Len())
}
Expand Down Expand Up @@ -179,19 +179,19 @@ func createRandomTestTxs(n int) []*types.Transaction {
// goos: darwin
// goarch: arm64
// pkg: github.com/ethereum/go-ethereum/core/txpool/legacypool
// BenchmarkTxPool3HeapAdd-8 813326 2858 ns/op
func BenchmarkTxPool3HeapAdd(b *testing.B) {
pool := NewTxPool3Heap(uint64(b.N))
// BenchmarkTxOverflowPoolHeapAdd-8 813326 2858 ns/op
func BenchmarkTxOverflowPoolHeapAdd(b *testing.B) {
pool := NewTxOverflowPoolHeap(uint64(b.N))
txs := createRandomTestTxs(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
pool.Add(txs[i])
}
}

// BenchmarkTxPool3HeapGet-8 32613938 35.63 ns/op
func BenchmarkTxPool3HeapGet(b *testing.B) {
pool := NewTxPool3Heap(1000)
// BenchmarkTxOverflowPoolHeapGet-8 32613938 35.63 ns/op
func BenchmarkTxOverflowPoolHeapGet(b *testing.B) {
pool := NewTxOverflowPoolHeap(1000)
txs := createRandomTestTxs(1000)
for _, tx := range txs {
pool.Add(tx)
Expand All @@ -202,9 +202,9 @@ func BenchmarkTxPool3HeapGet(b *testing.B) {
}
}

// BenchmarkTxPool3HeapRemove-8 3020841 417.8 ns/op
func BenchmarkTxPool3HeapRemove(b *testing.B) {
pool := NewTxPool3Heap(uint64(b.N))
// BenchmarkTxOverflowPoolHeapRemove-8 3020841 417.8 ns/op
func BenchmarkTxOverflowPoolHeapRemove(b *testing.B) {
pool := NewTxOverflowPoolHeap(uint64(b.N))
txs := createRandomTestTxs(b.N)
for _, tx := range txs {
pool.Add(tx)
Expand All @@ -215,9 +215,9 @@ func BenchmarkTxPool3HeapRemove(b *testing.B) {
}
}

// BenchmarkTxPool3HeapFlush-8 42963656 29.90 ns/op
func BenchmarkTxPool3HeapFlush(b *testing.B) {
pool := NewTxPool3Heap(1000)
// BenchmarkTxOverflowPoolHeapFlush-8 42963656 29.90 ns/op
func BenchmarkTxOverflowPoolHeapFlush(b *testing.B) {
pool := NewTxOverflowPoolHeap(1000)
txs := createRandomTestTxs(1000)
for _, tx := range txs {
pool.Add(tx)
Expand All @@ -228,9 +228,9 @@ func BenchmarkTxPool3HeapFlush(b *testing.B) {
}
}

// BenchmarkTxPool3HeapLen-8 79147188 20.07 ns/op
func BenchmarkTxPool3HeapLen(b *testing.B) {
pool := NewTxPool3Heap(1000)
// BenchmarkTxOverflowPoolHeapLen-8 79147188 20.07 ns/op
func BenchmarkTxOverflowPoolHeapLen(b *testing.B) {
pool := NewTxOverflowPoolHeap(1000)
txs := createRandomTestTxs(1000)
for _, tx := range txs {
pool.Add(tx)
Expand All @@ -241,9 +241,9 @@ func BenchmarkTxPool3HeapLen(b *testing.B) {
}
}

// BenchmarkTxPool3HeapAddRemove-8 902896 1546 ns/op
func BenchmarkTxPool3HeapAddRemove(b *testing.B) {
pool := NewTxPool3Heap(uint64(b.N))
// BenchmarkTxOverflowPoolHeapAddRemove-8 902896 1546 ns/op
func BenchmarkTxOverflowPoolHeapAddRemove(b *testing.B) {
pool := NewTxOverflowPoolHeap(uint64(b.N))
txs := createRandomTestTxs(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -252,9 +252,9 @@ func BenchmarkTxPool3HeapAddRemove(b *testing.B) {
}
}

// BenchmarkTxPool3HeapAddFlush-8 84417 14899 ns/op
func BenchmarkTxPool3HeapAddFlush(b *testing.B) {
pool := NewTxPool3Heap(uint64(b.N * 10))
// BenchmarkTxOverflowPoolHeapAddFlush-8 84417 14899 ns/op
func BenchmarkTxOverflowPoolHeapAddFlush(b *testing.B) {
pool := NewTxOverflowPoolHeap(uint64(b.N * 10))
txs := createRandomTestTxs(b.N * 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down
Loading

0 comments on commit 7a929d6

Please sign in to comment.