mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
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.
This commit is contained in:
parent
2d198e22d0
commit
c8da420a9d
@ -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:
|
||||
|
@ -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; i<tal_count(hops); i++) {
|
||||
json_object_start(response, NULL);
|
||||
json_add_pubkey(response, "id", &hops[i].nodeid);
|
||||
json_add_u64(response, "msatoshi", hops[i].amount);
|
||||
json_add_num(response, "delay", hops[i].delay);
|
||||
json_object_end(response);
|
||||
}
|
||||
json_array_end(response);
|
||||
json_object_end(response);
|
||||
command_success(cmd, response);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void json_getroute(struct command *cmd, const char *buffer, const jsmntok_t *params)
|
||||
{
|
||||
struct pubkey id;
|
||||
jsmntok_t *idtok, *msatoshitok, *riskfactortok;
|
||||
u64 msatoshi;
|
||||
double riskfactor;
|
||||
struct lightningd *ld = ld_from_dstate(cmd->dstate);
|
||||
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 = {
|
||||
|
Loading…
Reference in New Issue
Block a user