Skip to content

Commit

Permalink
Merge pull request #60 from multiversx/MX-16211-optimize-get-value
Browse files Browse the repository at this point in the history
Mempool optimization: on transaction addition, call "GetTransferredValue" and retain its value
  • Loading branch information
andreibancioiu authored Nov 29, 2024
2 parents a3a849d + ea3ee2d commit da4eff4
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,38 @@ 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

ComputeTxFeeCalled func(tx data.TransactionWithFeeHandler) *big.Int
GetTransferredValueCalled func(tx data.TransactionHandler) *big.Int
}

// NewTxGasHandlerMock -
func NewTxGasHandlerMock() *TxGasHandlerMock {
return &TxGasHandlerMock{
// NewMempoolHostMock -
func NewMempoolHostMock() *MempoolHostMock {
return &MempoolHostMock{
minGasLimit: 50000,
minGasPrice: 1000000000,
gasPerDataByte: 1500,
gasPriceModifier: 0.01,
}
}

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

// ComputeTxFee -
func (ghm *TxGasHandlerMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int {
func (mock *MempoolHostMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int {
if mock.ComputeTxFeeCalled != nil {
return mock.ComputeTxFeeCalled(tx)
}

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 @@ -49,7 +50,16 @@ func (ghm *TxGasHandlerMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *bi
return fee
}

// GetTransferredValue -
func (mock *MempoolHostMock) GetTransferredValue(tx data.TransactionHandler) *big.Int {
if mock.GetTransferredValueCalled != nil {
return mock.GetTransferredValueCalled(tx)
}

return tx.GetValue()
}

// IsInterfaceNil -
func (ghm *TxGasHandlerMock) IsInterfaceNil() bool {
return ghm == nil
func (mock *MempoolHostMock) IsInterfaceNil() bool {
return mock == nil
}
10 changes: 0 additions & 10 deletions testscommon/txcachemocks/selectionSessionMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type SelectionSessionMock struct {
AccountStateByAddress map[string]*types.AccountState
GetAccountStateCalled func(address []byte) (*types.AccountState, error)
IsIncorrectlyGuardedCalled func(tx data.TransactionHandler) bool
GetTransferredValueCalled func(tx data.TransactionHandler) *big.Int
}

// NewSelectionSessionMock -
Expand Down Expand Up @@ -79,15 +78,6 @@ func (mock *SelectionSessionMock) IsIncorrectlyGuarded(tx data.TransactionHandle
return false
}

// GetTransferredValue -
func (mock *SelectionSessionMock) GetTransferredValue(tx data.TransactionHandler) *big.Int {
if mock.GetTransferredValueCalled != nil {
return mock.GetTransferredValueCalled(tx)
}

return tx.GetValue()
}

// IsInterfaceNil -
func (mock *SelectionSessionMock) IsInterfaceNil() bool {
return mock == nil
Expand Down
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
6 changes: 3 additions & 3 deletions txcache/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ 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
GetTransferredValue(tx data.TransactionHandler) *big.Int
IsInterfaceNil() bool
}

// SelectionSession provides blockchain information for transaction selection
type SelectionSession interface {
GetAccountState(accountKey []byte) (*types.AccountState, error)
IsIncorrectlyGuarded(tx data.TransactionHandler) bool
GetTransferredValue(tx data.TransactionHandler) *big.Int
IsInterfaceNil() bool
}

Expand Down
2 changes: 1 addition & 1 deletion txcache/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func selectTransactionsFromBunches(session SelectionSession, bunches []bunchOfTr
shouldSkipTransaction := detectSkippableTransaction(session, item)
if !shouldSkipTransaction {
accumulatedGas += gasLimit
selectedTransactions = append(selectedTransactions, item.selectCurrentTransaction(session))
selectedTransactions = append(selectedTransactions, item.selectCurrentTransaction())
}

// If there are more transactions in the same bunch (same sender as the popped item),
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
7 changes: 5 additions & 2 deletions txcache/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (

const oneMilion = 1000000
const oneBillion = oneMilion * 1000
const oneQuintillion = 1_000_000_000_000_000_000
const estimatedSizeOfBoundedTxFields = uint64(128)

var oneQuintillionBig = big.NewInt(oneQuintillion)

// The GitHub Actions runners are (extremely) slow.
const selectionLoopMaximumDuration = 30 * time.Second

Expand Down Expand Up @@ -112,7 +115,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 +125,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
8 changes: 4 additions & 4 deletions txcache/transactionsHeapItem.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ func newTransactionsHeapItem(bunch bunchOfTransactions) (*transactionsHeapItem,
}, nil
}

func (item *transactionsHeapItem) selectCurrentTransaction(session SelectionSession) *WrappedTransaction {
item.accumulateConsumedBalance(session)
func (item *transactionsHeapItem) selectCurrentTransaction() *WrappedTransaction {
item.accumulateConsumedBalance()

item.latestSelectedTransaction = item.currentTransaction
item.latestSelectedTransactionNonce = item.currentTransactionNonce

return item.currentTransaction
}

func (item *transactionsHeapItem) accumulateConsumedBalance(session SelectionSession) {
func (item *transactionsHeapItem) accumulateConsumedBalance() {
fee := item.currentTransaction.Fee
if fee != nil {
item.consumedBalance.Add(item.consumedBalance, fee)
}

transferredValue := session.GetTransferredValue(item.currentTransaction.Tx)
transferredValue := item.currentTransaction.TransferredValue
if transferredValue != nil {
item.consumedBalance.Add(item.consumedBalance, transferredValue)
}
Expand Down
Loading

0 comments on commit da4eff4

Please sign in to comment.