From 27da8f77b5cfd57583219ba0dee22715004eefc1 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 29 Jun 2016 06:49:21 +0930 Subject: [PATCH] daemon: expose find_peer(), rename other to find_peer_json(). This is the more normal case; find by ID. The low-level json commands are really just for testing. Signed-off-by: Rusty Russell --- daemon/peer.c | 36 +++++++++++++++++++++--------------- daemon/peer.h | 2 ++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/daemon/peer.c b/daemon/peer.c index 4de9f11be..d8c2c115f 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -50,23 +50,29 @@ struct json_connecting { struct anchor_input *input; }; -static struct peer *find_peer(struct lightningd_state *dstate, +struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id) +{ + struct peer *peer; + + list_for_each(&dstate->peers, peer, list) { + if (peer->state != STATE_INIT && pubkey_eq(&peer->id, id)) + return peer; + } + return NULL; +} + +static struct peer *find_peer_json(struct lightningd_state *dstate, const char *buffer, jsmntok_t *peeridtok) { struct pubkey peerid; - struct peer *peer; if (!pubkey_from_hexstr(dstate->secpctx, buffer + peeridtok->start, peeridtok->end - peeridtok->start, &peerid)) return NULL; - list_for_each(&dstate->peers, peer, list) { - if (peer->state != STATE_INIT && pubkey_eq(&peer->id, &peerid)) - return peer; - } - return NULL; + return find_peer(dstate, &peerid); } static struct json_result *null_response(const tal_t *ctx) @@ -2499,7 +2505,7 @@ static void json_newhtlc(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; @@ -2592,7 +2598,7 @@ static void json_fulfillhtlc(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; @@ -2653,7 +2659,7 @@ static void json_failhtlc(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; @@ -2709,7 +2715,7 @@ static void json_commit(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; @@ -2748,7 +2754,7 @@ static void json_close(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; @@ -2788,7 +2794,7 @@ static void json_disconnect(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; @@ -2825,7 +2831,7 @@ static void json_signcommit(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; @@ -2864,7 +2870,7 @@ static void json_output(struct command *cmd, return; } - peer = find_peer(cmd->dstate, buffer, peeridtok); + peer = find_peer_json(cmd->dstate, buffer, peeridtok); if (!peer) { command_fail(cmd, "Could not find peer with that peerid"); return; diff --git a/daemon/peer.h b/daemon/peer.h index 6333fcfc4..70c2b01c2 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -220,6 +220,8 @@ struct peer { void setup_listeners(struct lightningd_state *dstate, unsigned int portnum); +struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id); + /* Populates very first peer->{local,remote}.commit->{tx,cstate} */ bool setup_first_commit(struct peer *peer);