Skip to content

Commit

Permalink
Pending Txs Update Condition (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbhat1 authored and udpatil committed Mar 26, 2024
1 parent e2af783 commit 9aa0b20
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/mempool/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
58 changes: 58 additions & 0 deletions internal/mempool/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

0 comments on commit 9aa0b20

Please sign in to comment.