Skip to content

Commit

Permalink
Implement NumUnconfirmedTxs RPC call (cosmos#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzdybal authored Jan 18, 2022
1 parent 7c42e2d commit 5bdc6d3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG-PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
91 changes: 91 additions & 0 deletions rpc/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 5bdc6d3

Please sign in to comment.