Skip to content

Commit

Permalink
Rename "TxGasHandler" to "MempoolHost".
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Nov 28, 2024
1 parent a3a849d commit 77bbd7a
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"github.com/multiversx/mx-chain-core-go/data"
)

// TxGasHandlerMock -
type TxGasHandlerMock struct {
// MempoolHostMock -
type MempoolHostMock struct {
minGasLimit uint64
minGasPrice uint64
gasPerDataByte uint64
gasPriceModifier float64
}

// NewTxGasHandlerMock -
func NewTxGasHandlerMock() *TxGasHandlerMock {
return &TxGasHandlerMock{
// NewMempoolHostMock -
func NewMempoolHostMock() *MempoolHostMock {
return &MempoolHostMock{
minGasLimit: 50000,
minGasPrice: 1000000000,
gasPerDataByte: 1500,
Expand All @@ -26,18 +26,18 @@ func NewTxGasHandlerMock() *TxGasHandlerMock {
}

// WithGasPriceModifier -
func (ghm *TxGasHandlerMock) WithGasPriceModifier(gasPriceModifier float64) *TxGasHandlerMock {
ghm.gasPriceModifier = gasPriceModifier
return ghm
func (mock *MempoolHostMock) WithGasPriceModifier(gasPriceModifier float64) *MempoolHostMock {
mock.gasPriceModifier = gasPriceModifier
return mock
}

// ComputeTxFee -
func (ghm *TxGasHandlerMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int {
func (mock *MempoolHostMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int {
dataLength := uint64(len(tx.GetData()))
gasPriceForMovement := tx.GetGasPrice()
gasPriceForProcessing := uint64(float64(gasPriceForMovement) * ghm.gasPriceModifier)
gasPriceForProcessing := uint64(float64(gasPriceForMovement) * mock.gasPriceModifier)

gasLimitForMovement := ghm.minGasLimit + dataLength*ghm.gasPerDataByte
gasLimitForMovement := mock.minGasLimit + dataLength*mock.gasPerDataByte
if tx.GetGasLimit() < gasLimitForMovement {
panic("tx.GetGasLimit() < gasLimitForMovement")
}
Expand All @@ -50,6 +50,6 @@ func (ghm *TxGasHandlerMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *bi
}

// IsInterfaceNil -
func (ghm *TxGasHandlerMock) IsInterfaceNil() bool {
return ghm == nil
func (mock *MempoolHostMock) IsInterfaceNil() bool {
return mock == nil
}
2 changes: 1 addition & 1 deletion txcache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package txcache

import "errors"

var errNilTxGasHandler = errors.New("nil tx gas handler")
var errNilMempoolHost = errors.New("nil mempool host")
var errNilSelectionSession = errors.New("nil selection session")
var errItemAlreadyInCache = errors.New("item already in cache")
var errEmptyBunchOfTransactions = errors.New("empty bunch of transactions")
22 changes: 11 additions & 11 deletions txcache/eviction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func TestTxCache_DoEviction_BecauseOfCount(t *testing.T) {
NumItemsToPreemptivelyEvict: 1,
}

txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()

cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)
require.NotNil(t, cache)

Expand Down Expand Up @@ -57,9 +57,9 @@ func TestTxCache_DoEviction_BecauseOfSize(t *testing.T) {
NumItemsToPreemptivelyEvict: 1,
}

txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()

cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)
require.NotNil(t, cache)

Expand Down Expand Up @@ -93,9 +93,9 @@ func TestTxCache_DoEviction_DoesNothingWhenAlreadyInProgress(t *testing.T) {
NumItemsToPreemptivelyEvict: 1,
}

txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()

cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)
require.NotNil(t, cache)

Expand Down Expand Up @@ -132,12 +132,12 @@ func TestBenchmarkTxCache_DoEviction(t *testing.T) {
NumItemsToPreemptivelyEvict: 50000,
}

txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()

sw := core.NewStopWatch()

t.Run("numSenders = 35000, numTransactions = 10", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

cache.config.EvictionEnabled = false
Expand All @@ -155,7 +155,7 @@ func TestBenchmarkTxCache_DoEviction(t *testing.T) {
})

t.Run("numSenders = 100000, numTransactions = 5", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

cache.config.EvictionEnabled = false
Expand All @@ -173,7 +173,7 @@ func TestBenchmarkTxCache_DoEviction(t *testing.T) {
})

t.Run("numSenders = 400000, numTransactions = 1", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

cache.config.EvictionEnabled = false
Expand All @@ -191,7 +191,7 @@ func TestBenchmarkTxCache_DoEviction(t *testing.T) {
})

t.Run("numSenders = 10000, numTransactions = 100", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

cache.config.EvictionEnabled = false
Expand Down
4 changes: 2 additions & 2 deletions txcache/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/multiversx/mx-chain-storage-go/types"
)

// TxGasHandler handles a transaction gas and gas cost
type TxGasHandler interface {
// MempoolHost provides blockchain information for mempool operations
type MempoolHost interface {
ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int
IsInterfaceNil() bool
}
Expand Down
20 changes: 10 additions & 10 deletions txcache/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ func TestBenchmarkTxCache_acquireBunchesOfTransactions(t *testing.T) {
NumItemsToPreemptivelyEvict: 1,
}

txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()

sw := core.NewStopWatch()

t.Run("numSenders = 10000, numTransactions = 100", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 10000, 100)
Expand All @@ -331,7 +331,7 @@ func TestBenchmarkTxCache_acquireBunchesOfTransactions(t *testing.T) {
})

t.Run("numSenders = 50000, numTransactions = 2", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 50000, 2)
Expand All @@ -348,7 +348,7 @@ func TestBenchmarkTxCache_acquireBunchesOfTransactions(t *testing.T) {
})

t.Run("numSenders = 100000, numTransactions = 1", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 100000, 1)
Expand All @@ -365,7 +365,7 @@ func TestBenchmarkTxCache_acquireBunchesOfTransactions(t *testing.T) {
})

t.Run("numSenders = 300000, numTransactions = 1", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 300000, 1)
Expand Down Expand Up @@ -491,13 +491,13 @@ func TestBenchmarkTxCache_doSelectTransactions(t *testing.T) {
NumItemsToPreemptivelyEvict: 1,
}

txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()
session := txcachemocks.NewSelectionSessionMock()

sw := core.NewStopWatch()

t.Run("numSenders = 10000, numTransactions = 100, maxNum = 50_000", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 10000, 100)
Expand All @@ -513,7 +513,7 @@ func TestBenchmarkTxCache_doSelectTransactions(t *testing.T) {
})

t.Run("numSenders = 50000, numTransactions = 2, maxNum = 50_000", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 50000, 2)
Expand All @@ -529,7 +529,7 @@ func TestBenchmarkTxCache_doSelectTransactions(t *testing.T) {
})

t.Run("numSenders = 100000, numTransactions = 1, maxNum = 50_000", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 100000, 1)
Expand All @@ -545,7 +545,7 @@ func TestBenchmarkTxCache_doSelectTransactions(t *testing.T) {
})

t.Run("numSenders = 300000, numTransactions = 1, maxNum = 50_000", func(t *testing.T) {
cache, err := NewTxCache(config, txGasHandler)
cache, err := NewTxCache(config, host)
require.Nil(t, err)

addManyTransactionsWithUniformDistribution(cache, 300000, 1)
Expand Down
4 changes: 2 additions & 2 deletions txcache/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func addManyTransactionsWithUniformDistribution(cache *TxCache, nSenders int, nT

func createBunchesOfTransactionsWithUniformDistribution(nSenders int, nTransactionsPerSender int) []bunchOfTransactions {
bunches := make([]bunchOfTransactions, 0, nSenders)
txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()

for senderTag := 0; senderTag < nSenders; senderTag++ {
bunch := make(bunchOfTransactions, 0, nTransactionsPerSender)
Expand All @@ -122,7 +122,7 @@ func createBunchesOfTransactionsWithUniformDistribution(nSenders int, nTransacti
transactionHash := createFakeTxHash(sender, nonce)
gasPrice := oneBillion + rand.Intn(3*oneBillion)
transaction := createTx(transactionHash, string(sender), uint64(nonce)).withGasPrice(uint64(gasPrice))
transaction.precomputeFields(txGasHandler)
transaction.precomputeFields(host)

bunch = append(bunch, transaction)
}
Expand Down
16 changes: 8 additions & 8 deletions txcache/transactionsHeapItem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func TestNewTransactionsHeapItem(t *testing.T) {
}

func TestTransactionsHeapItem_selectTransaction(t *testing.T) {
txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()
session := txcachemocks.NewSelectionSessionMock()

a := createTx([]byte("tx-1"), "alice", 42)
b := createTx([]byte("tx-2"), "alice", 43)
a.precomputeFields(txGasHandler)
b.precomputeFields(txGasHandler)
a.precomputeFields(host)
b.precomputeFields(host)

item, err := newTransactionsHeapItem(bunchOfTransactions{a, b})
require.NoError(t, err)
Expand Down Expand Up @@ -135,17 +135,17 @@ func TestTransactionsHeapItem_detectMiddleGap(t *testing.T) {
}

func TestTransactionsHeapItem_detectWillFeeExceedBalance(t *testing.T) {
txGasHandler := txcachemocks.NewTxGasHandlerMock()
host := txcachemocks.NewMempoolHostMock()

a := createTx([]byte("tx-1"), "alice", 42)
b := createTx([]byte("tx-2"), "alice", 43)
c := createTx([]byte("tx-3"), "alice", 44).withValue(big.NewInt(1000000000000000000))
d := createTx([]byte("tx-4"), "alice", 45)

a.precomputeFields(txGasHandler)
b.precomputeFields(txGasHandler)
c.precomputeFields(txGasHandler)
d.precomputeFields(txGasHandler)
a.precomputeFields(host)
b.precomputeFields(host)
c.precomputeFields(host)
d.precomputeFields(host)

t.Run("unknown", func(t *testing.T) {
item, err := newTransactionsHeapItem(bunchOfTransactions{a, b})
Expand Down
12 changes: 6 additions & 6 deletions txcache/txCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ type TxCache struct {
txListBySender *txListBySenderMap
txByHash *txByHashMap
config ConfigSourceMe
txGasHandler TxGasHandler
host MempoolHost
evictionMutex sync.Mutex
isEvictionInProgress atomic.Flag
mutTxOperation sync.Mutex
}

// NewTxCache creates a new transaction cache
func NewTxCache(config ConfigSourceMe, txGasHandler TxGasHandler) (*TxCache, error) {
func NewTxCache(config ConfigSourceMe, host MempoolHost) (*TxCache, error) {
log.Debug("NewTxCache", "config", config.String())
monitoring.MonitorNewCache(config.Name, uint64(config.NumBytesThreshold))

err := config.verify()
if err != nil {
return nil, err
}
if check.IfNil(txGasHandler) {
return nil, errNilTxGasHandler
if check.IfNil(host) {
return nil, errNilMempoolHost
}

// Note: for simplicity, we use the same "numChunks" for both internal concurrent maps
Expand All @@ -47,7 +47,7 @@ func NewTxCache(config ConfigSourceMe, txGasHandler TxGasHandler) (*TxCache, err
txListBySender: newTxListBySenderMap(numChunks, senderConstraintsObj),
txByHash: newTxByHashMap(numChunks),
config: config,
txGasHandler: txGasHandler,
host: host,
}

return txCache, nil
Expand All @@ -62,7 +62,7 @@ func (cache *TxCache) AddTx(tx *WrappedTransaction) (ok bool, added bool) {

logAdd.Trace("TxCache.AddTx", "tx", tx.TxHash, "nonce", tx.Tx.GetNonce(), "sender", tx.Tx.GetSndAddr())

tx.precomputeFields(cache.txGasHandler)
tx.precomputeFields(cache.host)

if cache.config.EvictionEnabled {
_ = cache.doEviction()
Expand Down
Loading

0 comments on commit 77bbd7a

Please sign in to comment.