Skip to content

Commit 89270d0

Browse files
authored
feat: purpose -1 to return all unconfirmed transactions in UnconfirmedTxs RPC (#1675)
## Description Instead of creating a new RPC method that returns all the unconfirmed mempool transactions, I used -1 as a special flag to do that. I think it's fine to do so. If it's not, can always define a new endpoint. #### PR checklist - [ ] Tests written/updated - [ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog) - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments
1 parent 1d1d65b commit 89270d0

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

rpc/client/rpc_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,38 @@ func TestUnconfirmedTxs(t *testing.T) {
404404
mempool.Flush()
405405
}
406406

407+
func TestUncappedUnconfirmedTxs(t *testing.T) {
408+
mempool := node.Mempool()
409+
numberOfTransactions := 120 // needs to be greater than maxPerPage const
410+
for i := 0; i < numberOfTransactions; i++ {
411+
_, _, tx := MakeTxKV()
412+
413+
ch := make(chan *abci.Response, 1)
414+
err := mempool.CheckTx(tx, func(resp *abci.Response) { ch <- resp }, mempl.TxInfo{})
415+
require.NoError(t, err)
416+
417+
// wait for tx to arrive in mempoool.
418+
select {
419+
case <-ch:
420+
case <-time.After(5 * time.Second):
421+
t.Error("Timed out waiting for CheckTx callback")
422+
}
423+
}
424+
425+
for _, c := range GetClients() {
426+
mc := c.(client.MempoolClient)
427+
limit := -1 // set the limit to -1 to return everything
428+
res, err := mc.UnconfirmedTxs(context.Background(), &limit)
429+
require.NoError(t, err)
430+
431+
assert.Equal(t, numberOfTransactions, res.Count)
432+
assert.Equal(t, numberOfTransactions, res.Total)
433+
assert.Equal(t, mempool.SizeBytes(), res.TotalBytes)
434+
}
435+
436+
mempool.Flush()
437+
}
438+
407439
func TestNumUnconfirmedTxs(t *testing.T) {
408440
_, _, tx := MakeTxKV()
409441

rpc/core/mempool.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,13 @@ func BroadcastTxCommit(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadc
154154
// UnconfirmedTxs gets unconfirmed transactions (maximum ?limit entries)
155155
// including their number.
156156
// More: https://docs.cometbft.com/v0.34/rpc/#/Info/unconfirmed_txs
157+
// If limitPtr == -1, it will return all the unconfirmed transactions in the mempool.
157158
func UnconfirmedTxs(ctx *rpctypes.Context, limitPtr *int) (*ctypes.ResultUnconfirmedTxs, error) {
158-
// reuse per_page validator
159-
limit := validatePerPage(limitPtr)
159+
limit := *limitPtr
160+
if limit != -1 {
161+
// reuse per_page validator
162+
limit = validatePerPage(limitPtr)
163+
}
160164
env := GetEnvironment()
161165

162166
txs := env.Mempool.ReapMaxTxs(limit)

rpc/openapi/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ paths:
824824
parameters:
825825
- in: query
826826
name: limit
827-
description: Maximum number of unconfirmed transactions to return (max 100)
827+
description: Maximum number of unconfirmed transactions to return (max 100). If set to -1, it will return all the unconfirmed mempool transactions.
828828
required: false
829829
schema:
830830
type: integer

0 commit comments

Comments
 (0)