From fbc1bc013510168512ca0dd1daea53196fe1d92c Mon Sep 17 00:00:00 2001 From: Kartik Bhat Date: Fri, 22 Mar 2024 12:30:41 -0400 Subject: [PATCH] Pending Txs Update Condition (#214) --- internal/mempool/tx.go | 2 +- internal/mempool/tx_test.go | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/internal/mempool/tx.go b/internal/mempool/tx.go index 17846d5bc..9366ac7fa 100644 --- a/internal/mempool/tx.go +++ b/internal/mempool/tx.go @@ -377,7 +377,7 @@ func (p *PendingTxs) Insert(tx *WrappedTx, resCheckTx *abci.ResponseCheckTxV2, t p.mtx.Lock() defer p.mtx.Unlock() - if len(p.txs) >= p.config.PendingSize && uint64(tx.Size())+p.sizeBytes > uint64(p.config.MaxPendingTxsBytes) { + if len(p.txs) >= p.config.PendingSize || uint64(tx.Size())+p.sizeBytes > uint64(p.config.MaxPendingTxsBytes) { return errors.New("pending store is full") } diff --git a/internal/mempool/tx_test.go b/internal/mempool/tx_test.go index 834beeda1..152730b33 100644 --- a/internal/mempool/tx_test.go +++ b/internal/mempool/tx_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/types" ) @@ -304,3 +305,60 @@ func TestPendingTxsPopTxsBad(t *testing.T) { // duplicate require.Panics(t, func() { pendingTxs.popTxsAtIndices([]int{2, 2}) }) } + +func TestPendingTxs_InsertCondition(t *testing.T) { + mempoolCfg := config.TestMempoolConfig() + + // First test exceeding number of txs + mempoolCfg.PendingSize = 2 + + pendingTxs := NewPendingTxs(mempoolCfg) + + // Transaction setup + tx1 := &WrappedTx{ + tx: types.Tx("tx1_data"), + priority: 1, + } + tx1Size := tx1.Size() + + tx2 := &WrappedTx{ + tx: types.Tx("tx2_data"), + priority: 2, + } + tx2Size := tx2.Size() + + err := pendingTxs.Insert(tx1, &abci.ResponseCheckTxV2{}, TxInfo{}) + require.Nil(t, err) + + err = pendingTxs.Insert(tx2, &abci.ResponseCheckTxV2{}, TxInfo{}) + require.Nil(t, err) + + // Should fail due to pending store size limit + tx3 := &WrappedTx{ + tx: types.Tx("tx3_data_exceeding_pending_size"), + priority: 3, + } + + err = pendingTxs.Insert(tx3, &abci.ResponseCheckTxV2{}, TxInfo{}) + require.NotNil(t, err) + + // Second test exceeding byte size condition + mempoolCfg.PendingSize = 5 + pendingTxs = NewPendingTxs(mempoolCfg) + mempoolCfg.MaxPendingTxsBytes = int64(tx1Size + tx2Size) + + err = pendingTxs.Insert(tx1, &abci.ResponseCheckTxV2{}, TxInfo{}) + require.Nil(t, err) + + err = pendingTxs.Insert(tx2, &abci.ResponseCheckTxV2{}, TxInfo{}) + require.Nil(t, err) + + // Should fail due to exceeding max pending transaction bytes + tx3 = &WrappedTx{ + tx: types.Tx("tx3_small_but_exceeds_byte_limit"), + priority: 3, + } + + err = pendingTxs.Insert(tx3, &abci.ResponseCheckTxV2{}, TxInfo{}) + require.NotNil(t, err) +}