From 06c4f6ddcafda83e3ab689740b2a8f2840338147 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 17 Jan 2018 06:14:32 +1030 Subject: [PATCH] JSONRPC: add optional short_channel_id argument to listchannels In order to just list one (though it may return two entries, one for each channel direction!). Signed-off-by: Rusty Russell --- gossipd/gossip.c | 8 ++++++++ gossipd/gossip_wire.csv | 3 +++ lightningd/gossip_control.c | 21 ++++++++++++++++++++- tests/test_lightningd.py | 11 +++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 55248a036..6f4d45ddd 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -1066,11 +1066,19 @@ static struct io_plan *getchannels_req(struct io_conn *conn, struct daemon *daem struct gossip_getchannels_entry *entries; struct node *n; struct node_map_iter i; + struct short_channel_id *scid; + + fromwire_gossip_getchannels_request(msg, msg, NULL, &scid); entries = tal_arr(tmpctx, struct gossip_getchannels_entry, num_chans); n = node_map_first(daemon->rstate->nodes, &i); while (n != NULL) { for (j=0; jout); j++){ + if (scid && + !short_channel_id_eq(scid, + &n->out[j]->short_channel_id)) { + continue; + } tal_resize(&entries, num_chans + 1); entries[num_chans].source = n->out[j]->src->id; entries[num_chans].destination = n->out[j]->dst->id; diff --git a/gossipd/gossip_wire.csv b/gossipd/gossip_wire.csv index d7dd0bce5..dfdc6f9a6 100644 --- a/gossipd/gossip_wire.csv +++ b/gossipd/gossip_wire.csv @@ -105,6 +105,9 @@ gossip_getroute_reply,,num_hops,u16 gossip_getroute_reply,,hops,num_hops*struct route_hop gossip_getchannels_request,3007 +# In practice, 0 or 1. +gossip_getchannels_request,,num,u16 +gossip_getchannels_request,,short_channel_id,num*struct short_channel_id gossip_getchannels_reply,3107 gossip_getchannels_reply,,num_channels,u16 diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 63e0e3bba..d4b54b5c4 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -386,7 +386,26 @@ static void json_listchannels_reply(struct subd *gossip, const u8 *reply, static void json_listchannels(struct command *cmd, const char *buffer, const jsmntok_t *params) { - u8 *req = towire_gossip_getchannels_request(cmd); + u8 *req; + jsmntok_t *idtok; + struct short_channel_id *id = NULL; + + if (!json_get_params(buffer, params, + "?short_channel_id", &idtok, + NULL)) { + command_fail(cmd, "Invalid arguments"); + return; + } + + if (idtok) { + id = tal_arr(cmd, struct short_channel_id, 1); + if (!json_tok_short_channel_id(buffer, idtok, id)) { + command_fail(cmd, "Invalid short_channel_id"); + return; + } + } + + req = towire_gossip_getchannels_request(cmd, id); subd_req(cmd->ld->gossip, cmd->ld->gossip, req, -1, 0, json_listchannels_reply, cmd); command_still_pending(cmd); diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index 295e3f91f..22620c041 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -1775,6 +1775,17 @@ class LightningDTests(BaseLightningDTests): .format(bitcoind.rpc.getblockcount() + 9 + shadow_route)) assert l3.rpc.listinvoice('test_forward_different_fees_and_cltv')[0]['complete'] == True + # Check that we see all the channels + shortids = set(c['short_channel_id'] for c in l2.rpc.listchannels()['channels']) + for scid in shortids: + c = l1.rpc.listchannels(scid)['channels'] + # We get one entry for each direction. + assert len(c) == 2 + assert c[0]['short_channel_id'] == scid + assert c[1]['short_channel_id'] == scid + assert c[0]['source'] == c[1]['destination'] + assert c[1]['source'] == c[0]['destination'] + @unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1 for --dev-broadcast-interval") def test_forward_pad_fees_and_cltv(self): """Test that we are allowed extra locktime delta, and fees"""