From 5ef7297e51791d8085552b5af4377c214050f7d6 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 13 May 2020 17:44:31 +0200 Subject: [PATCH] paymod: Add a `getinfo` call on payment_start to get the blockheight This is necessary so we can build the absolute locktimes in the next step. For now just fetch the blockheight on each (sub-)payment, later we can reuse the root blockheight if it ends up using too much traffic. --- plugins/libplugin-pay.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index 60d8cc36b..8ebfff25d 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -37,11 +37,44 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd, return p; } +/* Generic handler for RPC failures that should end up failing the payment. */ +static struct command_result *payment_rpc_failure(struct command *cmd, + const char *buffer, + const jsmntok_t *toks, + struct payment *p) +{ + plugin_log(p->cmd->plugin, LOG_DBG, + "Failing a partial payment due to a failed RPC call: %.*s", + toks->end - toks->start, buffer + toks->start); + + p->step = PAYMENT_STEP_FAILED; + payment_continue(p); + return command_still_pending(cmd); +} + +static struct command_result *payment_getinfo_success(struct command *cmd, + const char *buffer, + const jsmntok_t *toks, + struct payment *p) +{ + const jsmntok_t *blockheighttok = + json_get_member(buffer, toks, "blockheight"); + json_to_number(buffer, blockheighttok, &p->start_block); + payment_continue(p); + return command_still_pending(cmd); +} + void payment_start(struct payment *p) { p->step = PAYMENT_STEP_INITIALIZED; p->current_modifier = -1; - payment_continue(p); + + /* TODO If this is not the root, we can actually skip the getinfo call + * and just reuse the parent's value. */ + send_outreq(p->cmd->plugin, + jsonrpc_request_start(p->cmd->plugin, NULL, "getinfo", + payment_getinfo_success, + payment_rpc_failure, p)); } static bool route_hop_from_json(struct route_hop *dst, const char *buffer,