Browse Source

JSONRPC: add id argument to listpeers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
parent
commit
cf54f23947
  1. 11
      contrib/pylightning/lightning/lightning.py
  2. 5
      gossipd/gossip.c
  3. 3
      gossipd/gossip_wire.csv
  4. 23
      lightningd/peer_control.c

11
contrib/pylightning/lightning/lightning.py

@ -79,13 +79,12 @@ class LightningRpc(UnixDomainSocketRpc):
"""Get info about a specific peer, optionally with its log. """Get info about a specific peer, optionally with its log.
""" """
if log_level: if log_level:
peers = self.listpeers(log_level)['peers'] peers = self.listpeers(peer_id, log_level)['peers']
else: else:
peers = self.listpeers()['peers'] peers = self.listpeers(peer_id)['peers']
for p in peers: if len(peers) == 0:
if p['peerid'] == peer_id: return None
return p return peers[0]
return None
def getlog(self, level=None): def getlog(self, level=None):
args = [] args = []

5
gossipd/gossip.c

@ -1733,11 +1733,14 @@ static struct io_plan *get_peers(struct io_conn *conn,
size_t n = 0; size_t n = 0;
struct pubkey *id = tal_arr(conn, struct pubkey, n); struct pubkey *id = tal_arr(conn, struct pubkey, n);
struct wireaddr *wireaddr = tal_arr(conn, struct wireaddr, n); struct wireaddr *wireaddr = tal_arr(conn, struct wireaddr, n);
struct pubkey *specific_id = NULL;
if (!fromwire_gossip_getpeers_request(msg, NULL)) if (!fromwire_gossip_getpeers_request(msg, msg, NULL, &specific_id))
master_badmsg(WIRE_GOSSIPCTL_PEER_ADDRHINT, msg); master_badmsg(WIRE_GOSSIPCTL_PEER_ADDRHINT, msg);
list_for_each(&daemon->peers, peer, list) { list_for_each(&daemon->peers, peer, list) {
if (specific_id && !pubkey_eq(specific_id, &peer->id))
continue;
tal_resize(&id, n+1); tal_resize(&id, n+1);
tal_resize(&wireaddr, n+1); tal_resize(&wireaddr, n+1);

3
gossipd/gossip_wire.csv

@ -135,6 +135,9 @@ gossip_resolve_channel_reply,,keys,num_keys*struct pubkey
# The main daemon asks for peers # The main daemon asks for peers
gossip_getpeers_request,3011 gossip_getpeers_request,3011
# 0 or 1
gossip_getpeers_request,,num,u16
gossip_getpeers_request,,id,num*struct pubkey
gossip_getpeers_reply,3111 gossip_getpeers_reply,3111
gossip_getpeers_reply,,num,u16 gossip_getpeers_reply,,num,u16

Can't render this file because it has a wrong number of fields in line 5.

23
lightningd/peer_control.c

@ -790,6 +790,8 @@ struct getpeers_args {
struct command *cmd; struct command *cmd;
/* If non-NULL, they want logs too */ /* If non-NULL, they want logs too */
enum log_level *ll; enum log_level *ll;
/* If set, only report on a specific id. */
struct pubkey *specific_id;
}; };
static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg, static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg,
@ -811,6 +813,9 @@ static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg,
json_object_start(response, NULL); json_object_start(response, NULL);
json_array_start(response, "peers"); json_array_start(response, "peers");
list_for_each(&gpa->cmd->ld->peers, p, list) { list_for_each(&gpa->cmd->ld->peers, p, list) {
if (gpa->specific_id && !pubkey_eq(gpa->specific_id, &p->id))
continue;
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_string(response, "state", peer_state_name(p->state)); json_add_string(response, "state", peer_state_name(p->state));
json_array_start(response, "netaddr"); json_array_start(response, "netaddr");
@ -874,13 +879,27 @@ static void json_listpeers(struct command *cmd,
{ {
jsmntok_t *leveltok; jsmntok_t *leveltok;
struct getpeers_args *gpa = tal(cmd, struct getpeers_args); struct getpeers_args *gpa = tal(cmd, struct getpeers_args);
jsmntok_t *idtok;
gpa->cmd = cmd; gpa->cmd = cmd;
if (!json_get_params(buffer, params, "?level", &leveltok, NULL)) { gpa->specific_id = NULL;
if (!json_get_params(buffer, params,
"?id", &idtok,
"?level", &leveltok,
NULL)) {
command_fail(cmd, "Invalid arguments"); command_fail(cmd, "Invalid arguments");
return; return;
} }
if (idtok) {
gpa->specific_id = tal_arr(cmd, struct pubkey, 1);
if (!json_tok_pubkey(buffer, idtok, gpa->specific_id)) {
command_fail(cmd, "id %.*s not valid",
idtok->end - idtok->start,
buffer + idtok->start);
return;
}
}
if (leveltok) { if (leveltok) {
gpa->ll = tal(gpa, enum log_level); gpa->ll = tal(gpa, enum log_level);
if (json_tok_streq(buffer, leveltok, "debug")) if (json_tok_streq(buffer, leveltok, "debug"))
@ -900,7 +919,7 @@ static void json_listpeers(struct command *cmd,
/* Get peers from gossipd. */ /* Get peers from gossipd. */
subd_req(cmd, cmd->ld->gossip, subd_req(cmd, cmd->ld->gossip,
take(towire_gossip_getpeers_request(cmd)), take(towire_gossip_getpeers_request(cmd, gpa->specific_id)),
-1, 0, gossipd_getpeers_complete, gpa); -1, 0, gossipd_getpeers_complete, gpa);
command_still_pending(cmd); command_still_pending(cmd);
} }

Loading…
Cancel
Save