From 5bdc6d3b886871782c57a2906b1986d9958d13f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zdyba=C5=82?= Date: Tue, 18 Jan 2022 14:43:33 +0100 Subject: [PATCH] Implement NumUnconfirmedTxs RPC call (#255) --- CHANGELOG-PENDING.md | 5 ++- rpc/client/client.go | 7 ++- rpc/client/client_test.go | 91 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/CHANGELOG-PENDING.md b/CHANGELOG-PENDING.md index 9e7afdda4f7..512b5cf2a84 100644 --- a/CHANGELOG-PENDING.md +++ b/CHANGELOG-PENDING.md @@ -11,12 +11,13 @@ Month, DD, YYYY ### FEATURES - [indexer] [Implement block and transaction indexing, enable TxSearch RPC endpoint #202](https://github.com/celestiaorg/optimint/pull/202) [@mattdf](https://github.com/mattdf) -- [rpc] [Tendermint URI RPC](https://github.com/celestiaorg/optimint/pull/224) [@tzdybal](https://github.com/tzdybal/) +- [rpc] [Tendermint URI RPC #224](https://github.com/celestiaorg/optimint/pull/224) [@tzdybal](https://github.com/tzdybal/) ### IMPROVEMENTS - [ci] [Add more linters #219](https://github.com/celestiaorg/optimint/pull/219) [@tzdybal](https://github.com/tzdybal/) -- [deps] [Update dependencies: grpc, cors, cobra, viper, tm-db](https://github.com/celestiaorg/optimint/pull/245) [@tzdybal](https://github.com/tzdybal/) +- [deps] [Update dependencies: grpc, cors, cobra, viper, tm-db #245](https://github.com/celestiaorg/optimint/pull/245) [@tzdybal](https://github.com/tzdybal/) +- [rpc] [Implement NumUnconfirmedTxs #255](https://github.com/celestiaorg/optimint/pull/255) [@tzdybal](https://github.com/tzdybal/) ### BUG FIXES diff --git a/rpc/client/client.go b/rpc/client/client.go index 96b2f8cf990..5b91236824f 100644 --- a/rpc/client/client.go +++ b/rpc/client/client.go @@ -471,7 +471,12 @@ func (c *Client) BroadcastEvidence(ctx context.Context, evidence types.Evidence) } func (c *Client) NumUnconfirmedTxs(ctx context.Context) (*ctypes.ResultUnconfirmedTxs, error) { - panic("NumUnconfiredTxs - not implemented!") + return &ctypes.ResultUnconfirmedTxs{ + Count: c.node.Mempool.Size(), + Total: c.node.Mempool.Size(), + TotalBytes: c.node.Mempool.TxsBytes(), + }, nil + } func (c *Client) UnconfirmedTxs(ctx context.Context, limitPtr *int) (*ctypes.ResultUnconfirmedTxs, error) { diff --git a/rpc/client/client_test.go b/rpc/client/client_test.go index a4af6c8afc7..152ab53045b 100644 --- a/rpc/client/client_test.go +++ b/rpc/client/client_test.go @@ -214,6 +214,97 @@ func TestGetBlock(t *testing.T) { require.NoError(err) } +func TestUnconfirmedTxs(t *testing.T) { + tx1 := tmtypes.Tx("tx1") + tx2 := tmtypes.Tx("another tx") + + cases := []struct { + name string + txs []tmtypes.Tx + expectedCount int + expectedTotal int + expectedTotalBytes int + }{ + {"no txs", nil, 0, 0, 0}, + {"one tx", []tmtypes.Tx{tx1}, 1, 1, len(tx1)}, + {"two txs", []tmtypes.Tx{tx1, tx2}, 2, 2, len(tx1) + len(tx2)}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + assert := assert.New(t) + require := require.New(t) + + mockApp, rpc := getRPC(t) + mockApp.On("BeginBlock", mock.Anything).Return(abci.ResponseBeginBlock{}) + mockApp.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{}) + + err := rpc.node.Start() + require.NoError(err) + + for _, tx := range c.txs { + res, err := rpc.BroadcastTxAsync(context.Background(), tx) + assert.NoError(err) + assert.NotNil(res) + } + + numRes, err := rpc.NumUnconfirmedTxs(context.Background()) + assert.NoError(err) + assert.NotNil(numRes) + assert.EqualValues(c.expectedCount, numRes.Count) + assert.EqualValues(c.expectedTotal, numRes.Total) + assert.EqualValues(c.expectedTotalBytes, numRes.TotalBytes) + + limit := -1 + txRes, err := rpc.UnconfirmedTxs(context.Background(), &limit) + assert.NoError(err) + assert.NotNil(txRes) + assert.EqualValues(c.expectedCount, txRes.Count) + assert.EqualValues(c.expectedTotal, txRes.Total) + assert.EqualValues(c.expectedTotalBytes, txRes.TotalBytes) + assert.Len(txRes.Txs, c.expectedCount) + }) + } +} + +func TestUnconfirmedTxsLimit(t *testing.T) { + t.Skip("Test disabled because of known bug") + // there's a bug in mempool implementation - count should be 1 + // TODO(tzdybal): uncomment after resolving https://github.com/celestiaorg/optimint/issues/191 + + assert := assert.New(t) + require := require.New(t) + + mockApp, rpc := getRPC(t) + mockApp.On("BeginBlock", mock.Anything).Return(abci.ResponseBeginBlock{}) + mockApp.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{}) + + err := rpc.node.Start() + require.NoError(err) + + tx1 := tmtypes.Tx("tx1") + tx2 := tmtypes.Tx("another tx") + + res, err := rpc.BroadcastTxAsync(context.Background(), tx1) + assert.NoError(err) + assert.NotNil(res) + + res, err = rpc.BroadcastTxAsync(context.Background(), tx2) + assert.NoError(err) + assert.NotNil(res) + + limit := 1 + txRes, err := rpc.UnconfirmedTxs(context.Background(), &limit) + assert.NoError(err) + assert.NotNil(txRes) + assert.EqualValues(1, txRes.Count) + assert.EqualValues(2, txRes.Total) + assert.EqualValues(len(tx1)+len(tx2), txRes.TotalBytes) + assert.Len(txRes.Txs, limit) + assert.Contains(txRes.Txs, tx1) + assert.NotContains(txRes.Txs, tx2) +} + // copy-pasted from store/store_test.go func getRandomBlock(height uint64, nTxs int) *types.Block { block := &types.Block{