From 09f30ed055218e9c1290aa2b00c038e702dd37e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 5 Nov 2024 21:10:25 +0200 Subject: [PATCH] Fix after self-review. --- testscommon/txcachemocks/txGasHandlerMock.go | 32 -------------------- txcache/txCache.go | 6 ++-- txcache/txListBySenderMap.go | 4 ++- txcache/txListForSender.go | 8 ++--- 4 files changed, 10 insertions(+), 40 deletions(-) diff --git a/testscommon/txcachemocks/txGasHandlerMock.go b/testscommon/txcachemocks/txGasHandlerMock.go index ba0e849a..46e18141 100644 --- a/testscommon/txcachemocks/txGasHandlerMock.go +++ b/testscommon/txcachemocks/txGasHandlerMock.go @@ -7,19 +7,10 @@ import ( "github.com/multiversx/mx-chain-core-go/data" ) -// TxGasHandler - -type TxGasHandler interface { - MinGasPrice() uint64 - MaxGasLimitPerTx() uint64 - ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int - IsInterfaceNil() bool -} - // TxGasHandlerMock - type TxGasHandlerMock struct { minGasLimit uint64 minGasPrice uint64 - maxGasLimitPerTx uint64 gasPerDataByte uint64 gasPriceModifier float64 } @@ -29,40 +20,17 @@ func NewTxGasHandlerMock() *TxGasHandlerMock { return &TxGasHandlerMock{ minGasLimit: 50000, minGasPrice: 1000000000, - maxGasLimitPerTx: 600000000, gasPerDataByte: 1500, gasPriceModifier: 0.01, } } -// WithMinGasLimit - -func (ghm *TxGasHandlerMock) WithMinGasLimit(minGasLimit uint64) *TxGasHandlerMock { - ghm.minGasLimit = minGasLimit - return ghm -} - -// WithMinGasPrice - -func (ghm *TxGasHandlerMock) WithMinGasPrice(minGasPrice uint64) *TxGasHandlerMock { - ghm.minGasPrice = minGasPrice - return ghm -} - // WithGasPriceModifier - func (ghm *TxGasHandlerMock) WithGasPriceModifier(gasPriceModifier float64) *TxGasHandlerMock { ghm.gasPriceModifier = gasPriceModifier return ghm } -// MinGasPrice - -func (ghm *TxGasHandlerMock) MinGasPrice() uint64 { - return ghm.minGasPrice -} - -// MaxGasLimitPerTx - -func (ghm *TxGasHandlerMock) MaxGasLimitPerTx() uint64 { - return ghm.maxGasLimitPerTx -} - // ComputeTxFee - func (ghm *TxGasHandlerMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int { dataLength := uint64(len(tx.GetData())) diff --git a/txcache/txCache.go b/txcache/txCache.go index 08c1343f..0a5e5de8 100644 --- a/txcache/txCache.go +++ b/txcache/txCache.go @@ -93,8 +93,8 @@ func (cache *TxCache) GetByTxHash(txHash []byte) (*WrappedTransaction, bool) { return tx, ok } -// SelectTransactions selects a reasonably fair list of transactions to be included in the next miniblock -// It returns transactions with total gas ~ "gasRequested". +// SelectTransactions selects the best transactions to be included in the next miniblock. +// It returns up to "maxNum" transactions, with total gas <= "gasRequested". func (cache *TxCache) SelectTransactions(gasRequested uint64, maxNum int) ([]*WrappedTransaction, uint64) { stopWatch := core.NewStopWatch() stopWatch.Start("selection") @@ -134,7 +134,7 @@ func (cache *TxCache) RemoveTxByHash(txHash []byte) bool { tx, foundInByHash := cache.txByHash.removeTx(string(txHash)) if !foundInByHash { - // Could have been previously removed (e.g. due to NotifyAccountNonce). + // Transaction might have been removed in the meantime (e.g. due to NotifyAccountNonce). return false } diff --git a/txcache/txListBySenderMap.go b/txcache/txListBySenderMap.go index ca53bd7f..ac3592f9 100644 --- a/txcache/txListBySenderMap.go +++ b/txcache/txListBySenderMap.go @@ -107,7 +107,7 @@ func (txMap *txListBySenderMap) removeTx(tx *WrappedTransaction) bool { return isFound } -// Important: this doesn't remove the transactions from txCache.txByHash. That's done by the caller. +// Important note: this doesn't remove the transactions from txCache.txByHash. That is the responsibility of the caller (of this function). func (txMap *txListBySenderMap) removeSender(sender string) bool { logRemove.Trace("txListBySenderMap.removeSender", "sender", sender) @@ -148,6 +148,8 @@ func (txMap *txListBySenderMap) notifyAccountNonceReturnEvictedTransactions(acco return evictedTxHashes } +// evictTransactionsWithHigherOrEqualNonces removes transactions with nonces higher or equal to the given nonce. +// Useful for the eviction flow. func (txMap *txListBySenderMap) evictTransactionsWithHigherOrEqualNonces(accountKey []byte, nonce uint64) { sender := string(accountKey) listForSender, ok := txMap.getListForSender(sender) diff --git a/txcache/txListForSender.go b/txcache/txListForSender.go index f3ab8d2b..77f023ec 100644 --- a/txcache/txListForSender.go +++ b/txcache/txListForSender.go @@ -182,7 +182,7 @@ func (listForSender *txListForSender) IsEmpty() bool { return listForSender.countTxWithLock() == 0 } -// getTxs returns the transactions in the list +// getTxs returns the transactions of the sender func (listForSender *txListForSender) getTxs() []*WrappedTransaction { listForSender.mutex.RLock() defer listForSender.mutex.RUnlock() @@ -197,7 +197,7 @@ func (listForSender *txListForSender) getTxs() []*WrappedTransaction { return result } -// getTxsReversed returns the transactions in the list, in reverse nonce order +// getTxsReversed returns the transactions of the sender, in reverse nonce order func (listForSender *txListForSender) getTxsReversed() []*WrappedTransaction { listForSender.mutex.RLock() defer listForSender.mutex.RUnlock() @@ -212,7 +212,7 @@ func (listForSender *txListForSender) getTxsReversed() []*WrappedTransaction { return result } -// getTxsWithoutGaps returns the transactions in the list (gaps are handled, affected transactions are excluded) +// getTxsWithoutGaps returns the transactions of the sender (gaps are handled, affected transactions are excluded) func (listForSender *txListForSender) getTxsWithoutGaps() []*WrappedTransaction { listForSender.mutex.RLock() defer listForSender.mutex.RUnlock() @@ -257,7 +257,7 @@ func (listForSender *txListForSender) countTxWithLock() uint64 { return uint64(listForSender.items.Len()) } -// Removes transactions with lower nonces and returns their hashes. +// notifyAccountNonceReturnEvictedTransactions sets the known account nonce, removes the transactions with lower nonces, and returns their hashes func (listForSender *txListForSender) notifyAccountNonceReturnEvictedTransactions(nonce uint64) [][]byte { // Optimization: if nonce is the same, do nothing. if listForSender.accountNonce.Get() == nonce {