From ec71e535060abc159f153754ebdf7e159aaa5238 Mon Sep 17 00:00:00 2001 From: Lagrang3 Date: Thu, 23 Jan 2025 11:53:17 +0100 Subject: [PATCH] renepay: more informative renesendpay error reply Changelog-None. Signed-off-by: Lagrang3 --- plugins/renepay/sendpay.c | 83 ++++++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/plugins/renepay/sendpay.c b/plugins/renepay/sendpay.c index 90787c7f7b4e..6ab7d2a62854 100644 --- a/plugins/renepay/sendpay.c +++ b/plugins/renepay/sendpay.c @@ -261,11 +261,42 @@ static const u8 *create_onion(const tal_t *ctx, return onion; } -static struct command_result *sendonion_done(struct command *cmd, - const char *method UNUSED, - const char *buffer, - const jsmntok_t *toks, - struct renesendpay *renesendpay) +static struct command_result *sendpay_fail(struct command *cmd, + const char *method UNUSED, + const char *buffer, + const jsmntok_t *toks, + struct renesendpay *renesendpay) +{ + plugin_log(cmd->plugin, LOG_UNUSUAL, + "renesendpay failed calling sendpay: %.*s", + json_tok_full_len(toks), json_tok_full(buffer, toks)); + const jsmntok_t *codetok = json_get_member(buffer, toks, "code"); + const jsmntok_t *msgtok = json_get_member(buffer, toks, "message"); + const char *msg = NULL; + if (msgtok) + msg = json_strdup(tmpctx, buffer, msgtok); + u32 errcode; + if (codetok != NULL) + json_to_u32(buffer, codetok, &errcode); + else + errcode = PLUGIN_ERROR; + struct json_stream *response = jsonrpc_stream_fail( + cmd, errcode, + tal_fmt(tmpctx, "sendpay failed: %s", msg ? msg : "\"\"")); + json_object_start(response, "data"); + json_add_sha256(response, "payment_hash", &renesendpay->payment_hash); + json_add_string(response, "status", "failed"); + json_add_amount_msat(response, "amount_sent_msat", + renesendpay->sent_amount); + json_object_end(response); + return command_finished(cmd, response); +} + +static struct command_result *renesendpay_done(struct command *cmd, + const char *method UNUSED, + const char *buffer, + const jsmntok_t *toks, + struct renesendpay *renesendpay) { const char *err; u64 created_index; @@ -296,6 +327,7 @@ static struct command_result *sendonion_done(struct command *cmd, json_add_amount_msat(response, "amount_total_msat", renesendpay->total_amount); json_add_string(response, "invoice", renesendpay->invoice); + json_add_string(response, "status", "pending"); const jsmntok_t *preimagetok = json_get_member(buffer, toks, "payment_preimage"); @@ -325,6 +357,39 @@ static struct command_result *sendonion_done(struct command *cmd, return command_finished(cmd, response); } +static struct command_result *waitblockheight_fail(struct command *cmd, + const char *method UNUSED, + const char *buffer, + const jsmntok_t *toks, + struct renesendpay *renesendpay) +{ + plugin_log(cmd->plugin, LOG_UNUSUAL, + "renesendpay failed calling waitblockheight: %.*s", + json_tok_full_len(toks), json_tok_full(buffer, toks)); + const jsmntok_t *codetok = json_get_member(buffer, toks, "code"); + const jsmntok_t *msgtok = json_get_member(buffer, toks, "message"); + const char *msg; + if (msgtok) + msg = json_strdup(tmpctx, buffer, msgtok); + else + msg = ""; + u32 errcode; + if (codetok != NULL) + json_to_u32(buffer, codetok, &errcode); + else + errcode = PLUGIN_ERROR; + struct json_stream *response = jsonrpc_stream_fail( + cmd, errcode, + tal_fmt(tmpctx, "waitblockheight failed: %s", msg ? msg : "\"\"")); + json_object_start(response, "data"); + json_add_sha256(response, "payment_hash", &renesendpay->payment_hash); + json_add_string(response, "status", "failed"); + json_add_amount_msat(response, "amount_sent_msat", + renesendpay->sent_amount); + json_object_end(response); + return command_finished(cmd, response); +} + static struct command_result *waitblockheight_done(struct command *cmd, const char *method UNUSED, const char *buffer, @@ -346,7 +411,7 @@ static struct command_result *waitblockheight_done(struct command *cmd, if (tal_count(renesendpay->route) > 0) { onion = create_onion(tmpctx, renesendpay, renesendpay->route[0].node_id, 1); - req = jsonrpc_request_start(cmd, "sendonion", sendonion_done, + req = jsonrpc_request_start(cmd, "sendonion", renesendpay_done, sendpay_rpc_failure, renesendpay); json_add_hex_talarr(req->js, "onion", onion); @@ -376,8 +441,8 @@ static struct command_result *waitblockheight_done(struct command *cmd, } else { /* self payment */ onion = NULL; - req = jsonrpc_request_start(cmd, "sendpay", sendonion_done, - sendpay_rpc_failure, renesendpay); + req = jsonrpc_request_start(cmd, "sendpay", renesendpay_done, + sendpay_fail, renesendpay); json_array_start(req->js, "route"); json_array_end(req->js); json_add_amount_msat(req->js, "amount_msat", @@ -482,7 +547,7 @@ struct command_result *json_renesendpay(struct command *cmd, struct out_req *req = jsonrpc_request_start(cmd, "waitblockheight", waitblockheight_done, - sendpay_rpc_failure, renesendpay); + waitblockheight_fail, renesendpay); json_add_num(req->js, "blockheight", 0); return send_outreq(req); }