Skip to content

Commit

Permalink
Quick mempool workaround.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Nov 5, 2024
1 parent 73e894a commit fad31f4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
4 changes: 4 additions & 0 deletions txcache/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ const senderGracePeriodLowerBound = 2
const senderGracePeriodUpperBound = 2

const numEvictedTxsToDisplay = 3

const maxGasRequested = 10_000_000_000

const overridenBatchSizePerSender = 1
1 change: 1 addition & 0 deletions txcache/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (cache *TxCache) monitorSelectionEnd(selection []*WrappedTransaction, stopW

type batchSelectionJournal struct {
copied int
copiedGas uint64
isFirstBatch bool
hasInitialGap bool
hasMiddleGap bool
Expand Down
6 changes: 6 additions & 0 deletions txcache/score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func TestDefaultScoreComputer_computeRawScore(t *testing.T) {

score = computer.computeRawScore(senderScoreParams{count: 10000, feeScore: 180000000, gas: 1000000000})
assert.InDelta(t, float64(1.4129614707), score, delta)

score = computer.computeRawScore(senderScoreParams{count: 1, feeScore: 87893196, gas: 500_000_000})
assert.InDelta(t, float64(15.005599854444185), score, delta)

score = computer.computeRawScore(senderScoreParams{count: 1, feeScore: 92786964, gas: 500_000_000})
assert.InDelta(t, float64(17.554738697485206), score, delta)
}

func BenchmarkScoreComputer_computeRawScore(b *testing.B) {
Expand Down
11 changes: 6 additions & 5 deletions txcache/txCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func (cache *TxCache) GetByTxHash(txHash []byte) (*WrappedTransaction, bool) {
// SelectTransactionsWithBandwidth selects a reasonably fair list of transactions to be included in the next miniblock
// It returns at most "numRequested" transactions
// Each sender gets the chance to give at least bandwidthPerSender gas worth of transactions, unless "numRequested" limit is reached before iterating over all senders
func (cache *TxCache) SelectTransactionsWithBandwidth(numRequested int, batchSizePerSender int, bandwidthPerSender uint64) []*WrappedTransaction {
result := cache.doSelectTransactions(numRequested, batchSizePerSender, bandwidthPerSender)
func (cache *TxCache) SelectTransactionsWithBandwidth(numRequested int, _ int, bandwidthPerSender uint64) []*WrappedTransaction {
result := cache.doSelectTransactions(numRequested, overridenBatchSizePerSender, bandwidthPerSender)
go cache.doAfterSelection()
return result
}
Expand All @@ -117,17 +117,17 @@ func (cache *TxCache) doSelectTransactions(numRequested int, batchSizePerSender
result := make([]*WrappedTransaction, numRequested)
resultFillIndex := 0
resultIsFull := false
accumulatedGas := uint64(0)

snapshotOfSenders := cache.getSendersEligibleForSelection()

for pass := 0; !resultIsFull; pass++ {
copiedInThisPass := 0

for _, txList := range snapshotOfSenders {
batchSizeWithScoreCoefficient := batchSizePerSender * int(txList.getLastComputedScore()+1)
// Reset happens on first pass only
isFirstBatch := pass == 0
journal := txList.selectBatchTo(isFirstBatch, result[resultFillIndex:], batchSizeWithScoreCoefficient, bandwidthPerSender)
journal := txList.selectBatchTo(isFirstBatch, result[resultFillIndex:], batchSizePerSender, bandwidthPerSender)
cache.monitorBatchSelectionEnd(journal)

if isFirstBatch {
Expand All @@ -136,7 +136,8 @@ func (cache *TxCache) doSelectTransactions(numRequested int, batchSizePerSender

resultFillIndex += journal.copied
copiedInThisPass += journal.copied
resultIsFull = resultFillIndex == numRequested
accumulatedGas += journal.copiedGas
resultIsFull = resultFillIndex == numRequested || accumulatedGas >= maxGasRequested
if resultIsFull {
break
}
Expand Down
4 changes: 4 additions & 0 deletions txcache/txListForSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func (listForSender *txListForSender) selectBatchTo(isFirstBatch bool, destinati
copiedBandwidth := uint64(0)
lastTxGasLimit := uint64(0)
copied := 0
copiedGas := uint64(0)

for ; ; copied, copiedBandwidth = copied+1, copiedBandwidth+lastTxGasLimit {
if element == nil || copied == batchSize || copied == availableSpace || copiedBandwidth >= bandwidth {
break
Expand All @@ -272,13 +274,15 @@ func (listForSender *txListForSender) selectBatchTo(isFirstBatch bool, destinati
}

destination[copied] = value
copiedGas += lastTxGasLimit
element = element.Next()
previousNonce = txNonce
}

listForSender.copyBatchIndex = element
listForSender.copyPreviousNonce = previousNonce
journal.copied = copied
journal.copiedGas = copiedGas
return journal
}

Expand Down
25 changes: 15 additions & 10 deletions txcache/wrappedTransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import (
)

func Test_estimateTxFeeScore(t *testing.T) {
txGasHandler, txFeeHelper := dummyParamsWithGasPrice(100 * oneBillion)
A := createTxWithParams([]byte("a"), "a", 1, 200, 50000, 100*oneBillion)
B := createTxWithParams([]byte("b"), "b", 1, 200, 50000000, 100*oneBillion)
C := createTxWithParams([]byte("C"), "c", 1, 200, 1500000000, 100*oneBillion)
txGasHandler, txFeeHelper := dummyParamsWithGasPrice(oneBillion)
A := createTxWithParams([]byte("a"), "a", 1, 200, 50_000, oneBillion)
B := createTxWithParams([]byte("b"), "b", 1, 200, 50_000_000, oneBillion)
C := createTxWithParams([]byte("C"), "c", 1, 200, 500_000_000, oneBillion)
D := createTxWithParams([]byte("D"), "d", 1, 200, 500_000_000, 2*oneBillion)

scoreA := estimateTxFeeScore(A, txGasHandler, txFeeHelper)
scoreB := estimateTxFeeScore(B, txGasHandler, txFeeHelper)
scoreC := estimateTxFeeScore(C, txGasHandler, txFeeHelper)
require.Equal(t, uint64(8940), scoreA)
require.Equal(t, uint64(8940), A.TxFeeScoreNormalized)
require.Equal(t, uint64(6837580), scoreB)
require.Equal(t, uint64(6837580), B.TxFeeScoreNormalized)
require.Equal(t, uint64(205079820), scoreC)
require.Equal(t, uint64(205079820), C.TxFeeScoreNormalized)
scoreD := estimateTxFeeScore(D, txGasHandler, txFeeHelper)

require.Equal(t, 11436, int(scoreA))
require.Equal(t, 11436, int(A.TxFeeScoreNormalized))
require.Equal(t, 8791116, int(scoreB))
require.Equal(t, 8791116, int(B.TxFeeScoreNormalized))
require.Equal(t, 87893196, int(scoreC))
require.Equal(t, 87893196, int(C.TxFeeScoreNormalized))
require.Equal(t, 92786964, int(scoreD))
require.Equal(t, 92786964, int(D.TxFeeScoreNormalized))
}

func Test_normalizeGasPriceProcessing(t *testing.T) {
Expand Down

0 comments on commit fad31f4

Please sign in to comment.