Skip to content

Commit

Permalink
Fix after self-review.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Nov 5, 2024
1 parent 5ee99a2 commit 09f30ed
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 40 deletions.
32 changes: 0 additions & 32 deletions testscommon/txcachemocks/txGasHandlerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()))
Expand Down
6 changes: 3 additions & 3 deletions txcache/txCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion txcache/txListBySenderMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions txcache/txListForSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 09f30ed

Please sign in to comment.