From c8da420a9de0abaa65300f3e68f4ea649b9f9a60 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 15 Mar 2017 13:46:29 +0100 Subject: [PATCH] routing: Fill in the `getroute` functionality Copied the JSON-request parsing from `pay.c`, passing through to `gossipd`, filling the reply with the `route_hop` serialization, and serializing as JSON-RPC response. --- lightningd/gossip/gossip.c | 27 ++++++++++++--- lightningd/gossip_control.c | 65 +++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/lightningd/gossip/gossip.c b/lightningd/gossip/gossip.c index a96883410..79edba8ff 100644 --- a/lightningd/gossip/gossip.c +++ b/lightningd/gossip/gossip.c @@ -451,11 +451,30 @@ static struct io_plan *release_peer(struct io_conn *conn, struct daemon *daemon, "Unknown peer %"PRIu64, unique_id); } -static struct io_plan *getroute(struct io_conn *conn, struct daemon *daemon, u8 *msg) +static struct io_plan *getroute_req(struct io_conn *conn, struct daemon *daemon, + u8 *msg) { - return next_req_in(conn, daemon); -} + tal_t *tmpctx = tal_tmpctx(msg); + struct pubkey source, destination; + u32 msatoshi; + u16 riskfactor; + u8 *out; + struct route_hop *hops; + + fromwire_gossip_getroute_request(msg, NULL, &source, &destination, + &msatoshi, &riskfactor); + status_trace("Trying to find a route from %s to %s for %d msatoshi", + pubkey_to_hexstr(tmpctx, &source), + pubkey_to_hexstr(tmpctx, &destination), msatoshi); + + hops = get_route(tmpctx, daemon->rstate, &source, &destination, + msatoshi, 1); + out = towire_gossip_getroute_reply(msg, hops); + tal_free(tmpctx); + daemon_conn_send(&daemon->master, out); + return daemon_conn_read_next(conn, &daemon->master); +} static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon) { @@ -500,7 +519,7 @@ static struct io_plan *recv_req(struct io_conn *conn, struct daemon_conn *master return getnodes(conn, daemon); case WIRE_GOSSIP_GETROUTE_REQUEST: - return getroute(conn, daemon, daemon->msg_in); + return getroute_req(conn, daemon, daemon->master.msg_in); case WIRE_GOSSIPCTL_RELEASE_PEER_REPLY: case WIRE_GOSSIP_GETNODES_REPLY: diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 1e7d79f71..b9c02b5ff 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -221,8 +221,73 @@ static const struct json_command getnodes_command = { "Returns a list of all nodes that we know about"}; AUTODATA(json_command, &getnodes_command); +static bool json_getroute_reply(struct subd *gossip, const u8 *reply, const int *fds, + struct command *cmd) +{ + struct json_result *response; + struct route_hop *hops; + size_t i; + + fromwire_gossip_getroute_reply(reply, reply, NULL, &hops); + + if (tal_count(hops) == 0) { + command_fail(cmd, "Could not find a route"); + return true; + } + + response = new_json_result(cmd); + json_object_start(response, NULL); + json_array_start(response, "route"); + for (i=0; idstate); + if (!json_get_params(buffer, params, + "id", &idtok, + "msatoshi", &msatoshitok, + "riskfactor", &riskfactortok, + NULL)) { + command_fail(cmd, "Need id, msatoshi and riskfactor"); + return; + } + + if (!pubkey_from_hexstr(buffer + idtok->start, + idtok->end - idtok->start, &id)) { + command_fail(cmd, "Invalid id"); + return; + } + + if (!json_tok_u64(buffer, msatoshitok, &msatoshi)) { + command_fail(cmd, "'%.*s' is not a valid number", + (int)(msatoshitok->end - msatoshitok->start), + buffer + msatoshitok->start); + return; + } + + if (!json_tok_double(buffer, riskfactortok, &riskfactor)) { + command_fail(cmd, "'%.*s' is not a valid double", + (int)(riskfactortok->end - riskfactortok->start), + buffer + riskfactortok->start); + return; + } + u8 *req = towire_gossip_getroute_request(cmd, &cmd->dstate->id, &id, msatoshi, riskfactor*1000); + subd_req(ld->gossip, req, -1, 0, json_getroute_reply, cmd); } static const struct json_command getroute_command = {