Skip to content

Commit

Permalink
add UnconfirmedTxs/NumUnconfirmedTxs methods to HTTP/Local clients (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-lashin authored and melekes committed Dec 11, 2018
1 parent df32ea4 commit 2594cec
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Special thanks to external contributors on this release:
### FEATURES:

### IMPROVEMENTS:
- [rpc] Add `UnconfirmedTxs(limit)` and `NumUnconfirmedTxs()` methods to HTTP/Local clients (@danil-lashin)

### BUG FIXES:
- [kv indexer] \#2912 don't ignore key when executing CONTAINS
Expand Down
18 changes: 18 additions & 0 deletions rpc/client/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ func (c *HTTP) broadcastTX(route string, tx types.Tx) (*ctypes.ResultBroadcastTx
return result, nil
}

func (c *HTTP) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) {
result := new(ctypes.ResultUnconfirmedTxs)
_, err := c.rpc.Call("unconfirmed_txs", map[string]interface{}{"limit": limit}, result)
if err != nil {
return nil, errors.Wrap(err, "unconfirmed_txs")
}
return result, nil
}

func (c *HTTP) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) {
result := new(ctypes.ResultUnconfirmedTxs)
_, err := c.rpc.Call("num_unconfirmed_txs", map[string]interface{}{}, result)
if err != nil {
return nil, errors.Wrap(err, "num_unconfirmed_txs")
}
return result, nil
}

func (c *HTTP) NetInfo() (*ctypes.ResultNetInfo, error) {
result := new(ctypes.ResultNetInfo)
_, err := c.rpc.Call("net_info", map[string]interface{}{}, result)
Expand Down
6 changes: 6 additions & 0 deletions rpc/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,9 @@ type NetworkClient interface {
type EventsClient interface {
types.EventBusSubscriber
}

// MempoolClient shows us data about current mempool state.
type MempoolClient interface {
UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error)
NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error)
}
8 changes: 8 additions & 0 deletions rpc/client/localclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func (Local) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
return core.BroadcastTxSync(tx)
}

func (Local) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) {
return core.UnconfirmedTxs(limit)
}

func (Local) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) {
return core.NumUnconfirmedTxs()
}

func (Local) NetInfo() (*ctypes.ResultNetInfo, error) {
return core.NetInfo()
}
Expand Down
36 changes: 36 additions & 0 deletions rpc/client/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,42 @@ func TestBroadcastTxCommit(t *testing.T) {
}
}

func TestUnconfirmedTxs(t *testing.T) {
_, _, tx := MakeTxKV()

mempool := node.MempoolReactor().Mempool
_ = mempool.CheckTx(tx, nil)

for i, c := range GetClients() {
mc, ok := c.(client.MempoolClient)
require.True(t, ok, "%d", i)
txs, err := mc.UnconfirmedTxs(1)
require.Nil(t, err, "%d: %+v", i, err)
assert.Exactly(t, types.Txs{tx}, types.Txs(txs.Txs))
}

mempool.Flush()
}

func TestNumUnconfirmedTxs(t *testing.T) {
_, _, tx := MakeTxKV()

mempool := node.MempoolReactor().Mempool
_ = mempool.CheckTx(tx, nil)
mempoolSize := mempool.Size()

for i, c := range GetClients() {
mc, ok := c.(client.MempoolClient)
require.True(t, ok, "%d", i)
res, err := mc.NumUnconfirmedTxs()
require.Nil(t, err, "%d: %+v", i, err)

assert.Equal(t, mempoolSize, res.N)
}

mempool.Flush()
}

func TestTx(t *testing.T) {
// first we broadcast a tx
c := getHTTPClient()
Expand Down

0 comments on commit 2594cec

Please sign in to comment.