Skip to content

Commit 45ac11c

Browse files
committed
renepay: simplify rpc error callback functions
Changelog-None. Signed-off-by: Lagrang3 <[email protected]>
1 parent ec71e53 commit 45ac11c

File tree

1 file changed

+29
-80
lines changed

1 file changed

+29
-80
lines changed

Diff for: plugins/renepay/sendpay.c

+29-80
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <common/json_param.h>
33
#include <common/json_stream.h>
44
#include <common/onion_encode.h>
5+
#include <plugins/renepay/json.h>
56
#include <plugins/renepay/payplugin.h>
67
#include <plugins/renepay/sendpay.h>
78

@@ -133,22 +134,37 @@ struct renesendpay {
133134
unsigned int blockheight;
134135
};
135136

136-
static struct command_result *sendpay_rpc_failure(struct command *cmd,
137-
const char *method UNUSED,
138-
const char *buffer,
139-
const jsmntok_t *toks,
140-
struct renesendpay *renesendpay)
137+
static struct command_result *rpc_fail(struct command *cmd,
138+
const char *method,
139+
const char *buffer,
140+
const jsmntok_t *toks,
141+
struct renesendpay *renesendpay)
141142
{
143+
plugin_log(cmd->plugin, LOG_UNUSUAL,
144+
"renesendpay failed calling %s: %.*s", method,
145+
json_tok_full_len(toks), json_tok_full(buffer, toks));
142146
const jsmntok_t *codetok = json_get_member(buffer, toks, "code");
147+
const jsmntok_t *msgtok = json_get_member(buffer, toks, "message");
148+
const char *msg;
149+
if (msgtok)
150+
msg = json_strdup(tmpctx, buffer, msgtok);
151+
else
152+
msg = "";
143153
u32 errcode;
144154
if (codetok != NULL)
145155
json_to_u32(buffer, codetok, &errcode);
146156
else
147-
errcode = LIGHTNINGD;
148-
// FIXME: make this response more useful, maybe similar to waitsendpay
149-
return command_fail(
150-
cmd, errcode, "renesendpay failed to error in RPC: %.*s",
151-
json_tok_full_len(toks), json_tok_full(buffer, toks));
157+
errcode = PLUGIN_ERROR;
158+
struct json_stream *response = jsonrpc_stream_fail(
159+
cmd, errcode,
160+
tal_fmt(tmpctx, "%s failed: %s", method, msg ? msg : "\"\""));
161+
json_object_start(response, "data");
162+
json_add_sha256(response, "payment_hash", &renesendpay->payment_hash);
163+
json_add_string(response, "status", "failed");
164+
json_add_amount_msat(response, "amount_sent_msat",
165+
renesendpay->sent_amount);
166+
json_object_end(response);
167+
return command_finished(cmd, response);
152168
}
153169

154170
static void sphinx_append_blinded_path(const tal_t *ctx,
@@ -261,37 +277,6 @@ static const u8 *create_onion(const tal_t *ctx,
261277
return onion;
262278
}
263279

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-
295280
static struct command_result *renesendpay_done(struct command *cmd,
296281
const char *method UNUSED,
297282
const char *buffer,
@@ -357,39 +342,6 @@ static struct command_result *renesendpay_done(struct command *cmd,
357342
return command_finished(cmd, response);
358343
}
359344

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-
393345
static struct command_result *waitblockheight_done(struct command *cmd,
394346
const char *method UNUSED,
395347
const char *buffer,
@@ -412,7 +364,7 @@ static struct command_result *waitblockheight_done(struct command *cmd,
412364
onion = create_onion(tmpctx, renesendpay,
413365
renesendpay->route[0].node_id, 1);
414366
req = jsonrpc_request_start(cmd, "sendonion", renesendpay_done,
415-
sendpay_rpc_failure, renesendpay);
367+
rpc_fail, renesendpay);
416368
json_add_hex_talarr(req->js, "onion", onion);
417369

418370
json_add_amount_msat(req->js, "amount_msat",
@@ -442,16 +394,13 @@ static struct command_result *waitblockheight_done(struct command *cmd,
442394
/* self payment */
443395
onion = NULL;
444396
req = jsonrpc_request_start(cmd, "sendpay", renesendpay_done,
445-
sendpay_fail, renesendpay);
397+
rpc_fail, renesendpay);
446398
json_array_start(req->js, "route");
447399
json_array_end(req->js);
448400
json_add_amount_msat(req->js, "amount_msat",
449401
renesendpay->total_amount);
450402
if(renesendpay->payment_secret)
451403
json_add_secret(req->js, "payment_secret", renesendpay->payment_secret);
452-
else{
453-
// FIXME: get payment_secret from blinded path
454-
}
455404
}
456405

457406
json_add_sha256(req->js, "payment_hash", &renesendpay->payment_hash);
@@ -547,7 +496,7 @@ struct command_result *json_renesendpay(struct command *cmd,
547496

548497
struct out_req *req =
549498
jsonrpc_request_start(cmd, "waitblockheight", waitblockheight_done,
550-
waitblockheight_fail, renesendpay);
499+
rpc_fail, renesendpay);
551500
json_add_num(req->js, "blockheight", 0);
552501
return send_outreq(req);
553502
}

0 commit comments

Comments
 (0)