Skip to content

Commit 7eb3beb

Browse files
committed
renepay: more informative renesendpay error reply
Changelog-None. Signed-off-by: Lagrang3 <[email protected]>
1 parent 42ddc30 commit 7eb3beb

File tree

1 file changed

+74
-9
lines changed

1 file changed

+74
-9
lines changed

plugins/renepay/sendpay.c

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,42 @@ static const u8 *create_onion(const tal_t *ctx,
261261
return onion;
262262
}
263263

264-
static struct command_result *sendonion_done(struct command *cmd,
265-
const char *method UNUSED,
266-
const char *buffer,
267-
const jsmntok_t *toks,
268-
struct renesendpay *renesendpay)
264+
static struct command_result *sendpay_fail(struct command *cmd,
265+
const char *method UNUSED,
266+
const char *buffer,
267+
const jsmntok_t *toks,
268+
struct renesendpay *renesendpay)
269+
{
270+
plugin_log(cmd->plugin, LOG_UNUSUAL,
271+
"renesendpay failed calling sendpay: %.*s",
272+
json_tok_full_len(toks), json_tok_full(buffer, toks));
273+
const jsmntok_t *codetok = json_get_member(buffer, toks, "code");
274+
const jsmntok_t *msgtok = json_get_member(buffer, toks, "message");
275+
const char *msg = NULL;
276+
if (msgtok)
277+
msg = json_strdup(tmpctx, buffer, msgtok);
278+
u32 errcode;
279+
if (codetok != NULL)
280+
json_to_u32(buffer, codetok, &errcode);
281+
else
282+
errcode = PLUGIN_ERROR;
283+
struct json_stream *response = jsonrpc_stream_fail(
284+
cmd, errcode,
285+
tal_fmt(tmpctx, "sendpay failed: %s", msg ? msg : "\"\""));
286+
json_object_start(response, "data");
287+
json_add_sha256(response, "payment_hash", &renesendpay->payment_hash);
288+
json_add_string(response, "status", "failed");
289+
json_add_amount_msat(response, "amount_sent_msat",
290+
renesendpay->sent_amount);
291+
json_object_end(response);
292+
return command_finished(cmd, response);
293+
}
294+
295+
static struct command_result *renesendpay_done(struct command *cmd,
296+
const char *method UNUSED,
297+
const char *buffer,
298+
const jsmntok_t *toks,
299+
struct renesendpay *renesendpay)
269300
{
270301
const char *err;
271302
u64 created_index;
@@ -296,6 +327,7 @@ static struct command_result *sendonion_done(struct command *cmd,
296327
json_add_amount_msat(response, "amount_total_msat",
297328
renesendpay->total_amount);
298329
json_add_string(response, "invoice", renesendpay->invoice);
330+
json_add_string(response, "status", "pending");
299331

300332
const jsmntok_t *preimagetok =
301333
json_get_member(buffer, toks, "payment_preimage");
@@ -325,6 +357,39 @@ static struct command_result *sendonion_done(struct command *cmd,
325357
return command_finished(cmd, response);
326358
}
327359

360+
static struct command_result *waitblockheight_fail(struct command *cmd,
361+
const char *method UNUSED,
362+
const char *buffer,
363+
const jsmntok_t *toks,
364+
struct renesendpay *renesendpay)
365+
{
366+
plugin_log(cmd->plugin, LOG_UNUSUAL,
367+
"renesendpay failed calling waitblockheight: %.*s",
368+
json_tok_full_len(toks), json_tok_full(buffer, toks));
369+
const jsmntok_t *codetok = json_get_member(buffer, toks, "code");
370+
const jsmntok_t *msgtok = json_get_member(buffer, toks, "message");
371+
const char *msg;
372+
if (msgtok)
373+
msg = json_strdup(tmpctx, buffer, msgtok);
374+
else
375+
msg = "";
376+
u32 errcode;
377+
if (codetok != NULL)
378+
json_to_u32(buffer, codetok, &errcode);
379+
else
380+
errcode = PLUGIN_ERROR;
381+
struct json_stream *response = jsonrpc_stream_fail(
382+
cmd, errcode,
383+
tal_fmt(tmpctx, "waitblockheight failed: %s", msg ? msg : "\"\""));
384+
json_object_start(response, "data");
385+
json_add_sha256(response, "payment_hash", &renesendpay->payment_hash);
386+
json_add_string(response, "status", "failed");
387+
json_add_amount_msat(response, "amount_sent_msat",
388+
renesendpay->sent_amount);
389+
json_object_end(response);
390+
return command_finished(cmd, response);
391+
}
392+
328393
static struct command_result *waitblockheight_done(struct command *cmd,
329394
const char *method UNUSED,
330395
const char *buffer,
@@ -346,7 +411,7 @@ static struct command_result *waitblockheight_done(struct command *cmd,
346411
if (tal_count(renesendpay->route) > 0) {
347412
onion = create_onion(tmpctx, renesendpay,
348413
renesendpay->route[0].node_id, 1);
349-
req = jsonrpc_request_start(cmd, "sendonion", sendonion_done,
414+
req = jsonrpc_request_start(cmd, "sendonion", renesendpay_done,
350415
sendpay_rpc_failure, renesendpay);
351416
json_add_hex_talarr(req->js, "onion", onion);
352417

@@ -376,8 +441,8 @@ static struct command_result *waitblockheight_done(struct command *cmd,
376441
} else {
377442
/* self payment */
378443
onion = NULL;
379-
req = jsonrpc_request_start(cmd, "sendpay", sendonion_done,
380-
sendpay_rpc_failure, renesendpay);
444+
req = jsonrpc_request_start(cmd, "sendpay", renesendpay_done,
445+
sendpay_fail, renesendpay);
381446
json_array_start(req->js, "route");
382447
json_array_end(req->js);
383448
json_add_amount_msat(req->js, "amount_msat",
@@ -482,7 +547,7 @@ struct command_result *json_renesendpay(struct command *cmd,
482547

483548
struct out_req *req =
484549
jsonrpc_request_start(cmd, "waitblockheight", waitblockheight_done,
485-
sendpay_rpc_failure, renesendpay);
550+
waitblockheight_fail, renesendpay);
486551
json_add_num(req->js, "blockheight", 0);
487552
return send_outreq(req);
488553
}

0 commit comments

Comments
 (0)