JSONRPC: add id argument to listpeers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-01-17 10:23:20 +10:30
parent 5698a133c2
commit cf54f23947
4 changed files with 33 additions and 9 deletions

View File

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

View File

@ -1733,11 +1733,14 @@ static struct io_plan *get_peers(struct io_conn *conn,
size_t n = 0;
struct pubkey *id = tal_arr(conn, struct pubkey, 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);
list_for_each(&daemon->peers, peer, list) {
if (specific_id && !pubkey_eq(specific_id, &peer->id))
continue;
tal_resize(&id, n+1);
tal_resize(&wireaddr, n+1);

View File

@ -135,6 +135,9 @@ gossip_resolve_channel_reply,,keys,num_keys*struct pubkey
# The main daemon asks for peers
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,,num,u16

1 #include <common/cryptomsg.h>
135 # yet reached the announcement depth. So we add the channel locally so gossip_send_gossip,,len,u16
136 # we can use it already. gossip_send_gossip,,gossip,len*u8
137 gossip_local_add_channel,3017 # Both sides have seen the funding tx being locked, but we have not
138 # yet reached the announcement depth. So we add the channel locally so
139 # we can use it already.
140 gossip_local_add_channel,3017
141 gossip_local_add_channel,,short_channel_id,struct short_channel_id
142 gossip_local_add_channel,,chain_hash,struct bitcoin_blkid
143 gossip_local_add_channel,,remote_node_id,struct pubkey

View File

@ -790,6 +790,8 @@ struct getpeers_args {
struct command *cmd;
/* If non-NULL, they want logs too */
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,
@ -811,6 +813,9 @@ static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg,
json_object_start(response, NULL);
json_array_start(response, "peers");
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_add_string(response, "state", peer_state_name(p->state));
json_array_start(response, "netaddr");
@ -874,13 +879,27 @@ static void json_listpeers(struct command *cmd,
{
jsmntok_t *leveltok;
struct getpeers_args *gpa = tal(cmd, struct getpeers_args);
jsmntok_t *idtok;
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");
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) {
gpa->ll = tal(gpa, enum log_level);
if (json_tok_streq(buffer, leveltok, "debug"))
@ -900,7 +919,7 @@ static void json_listpeers(struct command *cmd,
/* Get peers from gossipd. */
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);
command_still_pending(cmd);
}