Skip to content

Commit

Permalink
renepay: more informative renesendpay error reply
Browse files Browse the repository at this point in the history
Changelog-None.

Signed-off-by: Lagrang3 <[email protected]>
  • Loading branch information
Lagrang3 committed Jan 23, 2025
1 parent 2868948 commit ec71e53
Showing 1 changed file with 74 additions and 9 deletions.
83 changes: 74 additions & 9 deletions plugins/renepay/sendpay.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand All @@ -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);

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}

0 comments on commit ec71e53

Please sign in to comment.