From b1ac490bea0f7a6ce3bbf0de6085d96702b8f8c0 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 29 Jun 2016 06:49:21 +0930 Subject: [PATCH] daemon: add-route RPC command. Signed-off-by: Rusty Russell --- daemon/jsonrpc.c | 11 ++++++++ daemon/jsonrpc.h | 2 ++ daemon/peer.c | 10 -------- daemon/routing.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 10 deletions(-) diff --git a/daemon/jsonrpc.c b/daemon/jsonrpc.c index 2bb2e7b0c..ece03117b 100644 --- a/daemon/jsonrpc.c +++ b/daemon/jsonrpc.c @@ -264,6 +264,7 @@ static const struct json_command *cmdlist[] = { &disconnect_command, &signcommit_command, &output_command, + &add_route_command, }; static void json_help(struct command *cmd, @@ -314,6 +315,16 @@ static void json_result(struct json_connection *jcon, io_wake(jcon); } +struct json_result *null_response(const tal_t *ctx) +{ + struct json_result *response; + + response = new_json_result(ctx); + json_object_start(response, NULL); + json_object_end(response); + return response; +} + void command_success(struct command *cmd, struct json_result *result) { struct json_connection *jcon = cmd->jcon; diff --git a/daemon/jsonrpc.h b/daemon/jsonrpc.h index ff4acc72a..7214665e1 100644 --- a/daemon/jsonrpc.h +++ b/daemon/jsonrpc.h @@ -49,6 +49,7 @@ struct json_command { const char *help; }; +struct json_result *null_response(const tal_t *ctx); void command_success(struct command *cmd, struct json_result *response); void PRINTF_FMT(2, 3) command_fail(struct command *cmd, const char *fmt, ...); @@ -69,4 +70,5 @@ extern const struct json_command disconnect_command; extern const struct json_command signcommit_command; extern const struct json_command output_command; extern const struct json_command accept_payment_command; +extern const struct json_command add_route_command; #endif /* LIGHTNING_DAEMON_JSONRPC_H */ diff --git a/daemon/peer.c b/daemon/peer.c index d8c2c115f..d2b1dedff 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -75,16 +75,6 @@ static struct peer *find_peer_json(struct lightningd_state *dstate, return find_peer(dstate, &peerid); } -static struct json_result *null_response(const tal_t *ctx) -{ - struct json_result *response; - - response = new_json_result(ctx); - json_object_start(response, NULL); - json_object_end(response); - return response; -} - static bool peer_uncommitted_changes(const struct peer *peer) { /* Not initialized yet? */ diff --git a/daemon/routing.c b/daemon/routing.c index 3c248c916..79e3a49fd 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -1,3 +1,4 @@ +#include "jsonrpc.h" #include "lightningd.h" #include "log.h" #include "overflows.h" @@ -232,3 +233,69 @@ struct peer *find_route(struct lightningd_state *dstate, return first; } + +static void json_add_route(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + jsmntok_t *srctok, *dsttok, *basetok, *vartok, *delaytok, *minblockstok; + struct pubkey src, dst; + struct node *from, *to; + u32 base, var, delay, minblocks; + + if (!json_get_params(buffer, params, + "src", &srctok, + "dst", &dsttok, + "base", &basetok, + "var", &vartok, + "delay", &delaytok, + "minblocks", &minblockstok, + NULL)) { + command_fail(cmd, "Need src, dst, base, var, delay & minblocks"); + return; + } + + if (!pubkey_from_hexstr(cmd->dstate->secpctx, + buffer + srctok->start, + srctok->end - srctok->start, &src)) { + command_fail(cmd, "src %.*s not valid", + srctok->end - srctok->start, + buffer + srctok->start); + return; + } + + if (!pubkey_from_hexstr(cmd->dstate->secpctx, + buffer + dsttok->start, + dsttok->end - dsttok->start, &dst)) { + command_fail(cmd, "dst %.*s not valid", + dsttok->end - dsttok->start, + buffer + dsttok->start); + return; + } + + if (!json_tok_number(buffer, basetok, &base) + || !json_tok_number(buffer, vartok, &var) + || !json_tok_number(buffer, delaytok, &delay) + || !json_tok_number(buffer, minblockstok, &minblocks)) { + command_fail(cmd, + "base, var, delay and minblocks must be numbers"); + return; + } + + from = get_node(cmd->dstate, &src); + if (!from) + from = new_node(cmd->dstate, &src); + to = get_node(cmd->dstate, &dst); + if (!to) + to = new_node(cmd->dstate, &dst); + + add_connection(cmd->dstate, from, to, base, var, delay, minblocks); + command_success(cmd, null_response(cmd)); +} + +const struct json_command add_route_command = { + "add-route", + json_add_route, + "Add route from {src} to {dst}, {base} rate in msatoshi, {var} rate in msatoshi, {delay} blocks delay and {minblocks} minimum timeout", + "Returns an empty result on success" +}; +