pool: remove old buffer for pool3
This commit is contained in:
parent
4e69ac4a0f
commit
31c9465eac
@ -1,115 +0,0 @@
|
||||
package legacypool
|
||||
|
||||
import (
|
||||
containerList "container/list"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
type LRUBuffer struct {
|
||||
capacity int
|
||||
buffer *containerList.List
|
||||
index map[common.Hash]*containerList.Element
|
||||
mu sync.Mutex
|
||||
size int // Total number of slots used
|
||||
}
|
||||
|
||||
func NewLRUBuffer(capacity int) *LRUBuffer {
|
||||
return &LRUBuffer{
|
||||
capacity: capacity,
|
||||
buffer: containerList.New(),
|
||||
index: make(map[common.Hash]*containerList.Element),
|
||||
size: 0, // Initialize size to 0
|
||||
}
|
||||
}
|
||||
|
||||
func (lru *LRUBuffer) Add(tx *types.Transaction) {
|
||||
lru.mu.Lock()
|
||||
defer lru.mu.Unlock()
|
||||
|
||||
if elem, ok := lru.index[tx.Hash()]; ok {
|
||||
lru.buffer.MoveToFront(elem)
|
||||
return
|
||||
}
|
||||
|
||||
txSlots := numSlots(tx)
|
||||
|
||||
// Remove elements until there is enough capacity
|
||||
for lru.size+txSlots > lru.capacity && lru.buffer.Len() > 0 {
|
||||
back := lru.buffer.Back()
|
||||
removedTx := back.Value.(*types.Transaction)
|
||||
lru.buffer.Remove(back)
|
||||
delete(lru.index, removedTx.Hash())
|
||||
lru.size -= numSlots(removedTx) // Decrease size by the slots of the removed transaction
|
||||
}
|
||||
|
||||
elem := lru.buffer.PushFront(tx)
|
||||
lru.index[tx.Hash()] = elem
|
||||
lru.size += txSlots // Increase size by the slots of the new transaction
|
||||
// Update pool3Gauge
|
||||
pool3Gauge.Inc(1)
|
||||
}
|
||||
|
||||
func (lru *LRUBuffer) Get(hash common.Hash) (*types.Transaction, bool) {
|
||||
lru.mu.Lock()
|
||||
defer lru.mu.Unlock()
|
||||
|
||||
if elem, ok := lru.index[hash]; ok {
|
||||
lru.buffer.MoveToFront(elem)
|
||||
return elem.Value.(*types.Transaction), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (lru *LRUBuffer) Flush(maxTransactions int) []*types.Transaction {
|
||||
lru.mu.Lock()
|
||||
defer lru.mu.Unlock()
|
||||
|
||||
txs := make([]*types.Transaction, 0, maxTransactions)
|
||||
count := 0
|
||||
for count < maxTransactions && lru.buffer.Len() > 0 {
|
||||
back := lru.buffer.Back()
|
||||
removedTx := back.Value.(*types.Transaction)
|
||||
txs = append(txs, removedTx)
|
||||
lru.buffer.Remove(back)
|
||||
delete(lru.index, removedTx.Hash())
|
||||
lru.size -= numSlots(removedTx) // Decrease size by the slots of the removed transaction
|
||||
count++
|
||||
// Update pool3Gauge
|
||||
pool3Gauge.Dec(1)
|
||||
}
|
||||
return txs
|
||||
}
|
||||
|
||||
// New method to get the current size of the buffer in terms of slots
|
||||
func (lru *LRUBuffer) Size() int {
|
||||
lru.mu.Lock()
|
||||
defer lru.mu.Unlock()
|
||||
return lru.size
|
||||
}
|
||||
|
||||
// New iterator method to iterate over all transactions, ONLY used for printing and debugging
|
||||
func (lru *LRUBuffer) iterate() <-chan *types.Transaction {
|
||||
ch := make(chan *types.Transaction)
|
||||
go func() {
|
||||
lru.mu.Lock()
|
||||
defer lru.mu.Unlock()
|
||||
defer close(ch)
|
||||
|
||||
for e := lru.buffer.Front(); e != nil; e = e.Next() {
|
||||
ch <- e.Value.(*types.Transaction)
|
||||
}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
func (lru *LRUBuffer) PrintTxStats() {
|
||||
// Iterating over the transactions
|
||||
for tx := range lru.iterate() {
|
||||
// Print transaction details or process them as needed
|
||||
fmt.Println(tx.Hash().String(), tx.GasFeeCap().String(), tx.GasTipCap().String())
|
||||
}
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
package legacypool
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Helper function to create a dummy transaction of specified size
|
||||
func createDummyTransaction(size int) *types.Transaction {
|
||||
data := make([]byte, size)
|
||||
return types.NewTransaction(0, common.Address{}, nil, 0, nil, data)
|
||||
}
|
||||
|
||||
func TestNewLRUBuffer(t *testing.T) {
|
||||
capacity := 10
|
||||
lru := NewLRUBuffer(capacity)
|
||||
|
||||
require.Equal(t, capacity, lru.capacity, "expected capacity to match")
|
||||
require.Zero(t, lru.buffer.Len(), "expected buffer length to be zero")
|
||||
require.Zero(t, len(lru.index), "expected index length to be zero")
|
||||
require.Zero(t, lru.size, "expected size to be zero")
|
||||
}
|
||||
|
||||
func TestAddAndGet(t *testing.T) {
|
||||
lru := NewLRUBuffer(10)
|
||||
|
||||
tx1 := createDummyTransaction(500)
|
||||
tx2 := createDummyTransaction(1500)
|
||||
|
||||
lru.Add(tx1)
|
||||
lru.Add(tx2)
|
||||
|
||||
require.Equal(t, 2, lru.Size(), "expected size to be 2")
|
||||
|
||||
retrievedTx, ok := lru.Get(tx1.Hash())
|
||||
require.True(t, ok, "expected to retrieve tx1")
|
||||
require.Equal(t, tx1.Hash(), retrievedTx.Hash(), "retrieved tx1 hash does not match")
|
||||
|
||||
retrievedTx, ok = lru.Get(tx2.Hash())
|
||||
require.True(t, ok, "expected to retrieve tx2")
|
||||
require.Equal(t, tx2.Hash(), retrievedTx.Hash(), "retrieved tx2 hash does not match")
|
||||
}
|
||||
|
||||
func TestBufferCapacity(t *testing.T) {
|
||||
lru := NewLRUBuffer(2) // Capacity in slots
|
||||
|
||||
tx1 := createDummyTransaction(500) // 1 slot
|
||||
tx2 := createDummyTransaction(1500) // 1 slot
|
||||
tx3 := createDummyTransaction(1000) // 1 slot
|
||||
|
||||
lru.Add(tx1)
|
||||
lru.Add(tx2)
|
||||
|
||||
require.Equal(t, 2, lru.Size(), "expected size to be 2")
|
||||
|
||||
lru.Add(tx3)
|
||||
|
||||
require.Equal(t, 2, lru.Size(), "expected size to remain 2 after adding tx3")
|
||||
_, ok := lru.Get(tx1.Hash())
|
||||
require.False(t, ok, "expected tx1 to be evicted")
|
||||
}
|
||||
|
||||
func TestFlush(t *testing.T) {
|
||||
lru := NewLRUBuffer(10)
|
||||
|
||||
tx1 := createDummyTransaction(500)
|
||||
tx2 := createDummyTransaction(1500)
|
||||
tx3 := createDummyTransaction(1000)
|
||||
|
||||
lru.Add(tx1)
|
||||
lru.Add(tx2)
|
||||
lru.Add(tx3)
|
||||
|
||||
flushedTxs := lru.Flush(2)
|
||||
|
||||
require.Len(t, flushedTxs, 2, "expected to flush 2 transactions")
|
||||
|
||||
expectedSize := 1
|
||||
actualSize := lru.Size()
|
||||
require.Equal(t, expectedSize, actualSize, "expected size after flush to match")
|
||||
}
|
||||
|
||||
func TestSize(t *testing.T) {
|
||||
lru := NewLRUBuffer(10)
|
||||
|
||||
tx1 := createDummyTransaction(500) // 1 slot
|
||||
tx2 := createDummyTransaction(1500) // 2 slots
|
||||
|
||||
lru.Add(tx1)
|
||||
require.Equal(t, 1, lru.Size(), "expected size to be 1")
|
||||
|
||||
lru.Add(tx2)
|
||||
require.Equal(t, 2, lru.Size(), "expected size to be 2")
|
||||
|
||||
lru.Flush(1)
|
||||
require.Equal(t, 1, lru.Size(), "expected size to be 1 after flush")
|
||||
}
|
Loading…
Reference in New Issue
Block a user