Skip to content

Commit 23814ec

Browse files
Ganesha UpadhyayaGanesha Upadhyaya
authored andcommitted
Fix mempool batching for v0.11.x
1 parent 53e0691 commit 23814ec

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

mempool/clist_mempool.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -530,27 +530,18 @@ func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
530530
txs := make([]types.Tx, 0, mem.txs.Len())
531531
for e := mem.txs.Front(); e != nil; e = e.Next() {
532532
memTx := e.Value.(*mempoolTx)
533-
534-
txs = append(txs, memTx.tx)
535-
536-
dataSize := types.ComputeProtoSizeForTxs([]types.Tx{memTx.tx})
537-
538-
// Check total size requirement
539-
if maxBytes > -1 && runningSize+dataSize > maxBytes {
540-
return txs[:len(txs)-1]
541-
}
542-
543-
runningSize += dataSize
544-
545-
// Check total gas requirement.
533+
// Check total gas requirement and total size requirement.
546534
// If maxGas is negative, skip this check.
547535
// Since newTotalGas < masGas, which
548536
// must be non-negative, it follows that this won't overflow.
549537
newTotalGas := totalGas + memTx.gasWanted
550-
if maxGas > -1 && newTotalGas > maxGas {
551-
return txs[:len(txs)-1]
538+
totalDataSize := runningSize + types.ComputeProtoSizeForTxs([]types.Tx{memTx.tx})
539+
if (maxGas > -1 && newTotalGas > maxGas) || (maxBytes > -1 && totalDataSize > maxBytes) {
540+
continue
552541
}
553542
totalGas = newTotalGas
543+
runningSize = totalDataSize
544+
txs = append(txs, memTx.tx)
554545
}
555546
return txs
556547
}

mempool/clist_mempool_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ func TestReapMaxBytesMaxGas(t *testing.T) {
256256
}
257257
}
258258

259+
func TestTxMempoolTxLargerThanMaxBytes(t *testing.T) {
260+
app := kvstore.NewInMemoryApplication()
261+
cc := proxy.NewLocalClientCreator(app)
262+
txmp, cleanup := newMempoolWithApp(cc)
263+
defer cleanup()
264+
265+
// large high priority tx
266+
bigTx := kvstore.NewRandomTx(100)
267+
smallTx := kvstore.NewRandomTx(20)
268+
require.NoError(t, txmp.CheckTx(bigTx, nil, TxInfo{SenderID: 1}))
269+
require.NoError(t, txmp.CheckTx(smallTx, nil, TxInfo{SenderID: 2}))
270+
271+
// reap by max bytes less than the large tx
272+
reapedTxs := txmp.ReapMaxBytesMaxGas(100, -1)
273+
require.Len(t, reapedTxs, 1)
274+
require.Equal(t, types.Tx(smallTx), reapedTxs[0])
275+
}
276+
259277
func TestMempoolFilters(t *testing.T) {
260278
app := kvstore.NewInMemoryApplication()
261279
cc := proxy.NewLocalClientCreator(app)

0 commit comments

Comments
 (0)