Skip to content

Commit

Permalink
renepay: pay selfpayments on spot
Browse files Browse the repository at this point in the history
Resolve a selfpayment right from the result of `sendpay` instead of
waiting for the notification.
  • Loading branch information
Lagrang3 committed Apr 10, 2024
1 parent 9baeb04 commit b067667
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
49 changes: 20 additions & 29 deletions plugins/renepay/mods.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,44 +327,38 @@ REGISTER_PAYMENT_MODIFIER(initial_sanity_checks, initial_sanity_checks_cb);

static struct command_result *selfpay_success(struct command *cmd,
const char *buf,
const jsmntok_t *result,
struct payment *payment)
const jsmntok_t *tok,
struct route *route)
{
struct payment *payment = route->payment;
assert(payment);
struct preimage preimage;
const char *err;
err = json_scan(tmpctx, buf, result, "{payment_preimage:%}",
err = json_scan(tmpctx, buf, tok, "{payment_preimage:%}",
JSON_SCAN(json_to_preimage, &preimage));
if (err)
plugin_err(
cmd->plugin, "selfpay didn't have payment_preimage: %.*s",
json_tok_full_len(result), json_tok_full(buf, result));
json_tok_full_len(tok), json_tok_full(buf, tok));


payment_note(payment, LOG_DBG, "Paid with self-pay.");
/* FIXME: shouldn't we process selfpay in the same way a regular payment? */
return payment_success(payment, &preimage);
}

static void route_selfpaypending(const struct route *route)
static struct command_result *selfpay_failure(struct command *cmd,
const char *buf,
const jsmntok_t *tok,
struct route *route)
{
assert(route);
struct payment *payment = route->payment;
assert(payment);
struct routetracker *routetracker = payment->routetracker;
assert(routetracker);

/* we already keep track of this route */
assert(!route_map_get(routetracker->pending_routes, &route->key));
route_map_add(routetracker->pending_routes, route);
tal_steal(routetracker, route);

if (!amount_msat_add(&payment->total_sent, payment->total_sent,
payment->amount) ||
!amount_msat_add(&payment->total_delivering,
payment->total_delivering, payment->amount)) {
struct payment_result *result = tal_sendpay_result_from_json(tmpctx, buf, tok);
if (result == NULL)
plugin_err(pay_plugin->plugin,
"%s: amount_msat arithmetic overflow.",
__PRETTY_FUNCTION__);
}
"Unable to parse sendpay failure: %.*s",
json_tok_full_len(tok), json_tok_full(buf, tok));

return payment_fail(payment, result->code, "%s", result->message);
}

static struct command_result *selfpay_cb(struct payment *payment)
Expand All @@ -377,18 +371,15 @@ static struct command_result *selfpay_cb(struct payment *payment)
if (!cmd)
plugin_err(pay_plugin->plugin,
"Selfpay: cannot get a valid cmd.");
struct out_req *req;
req =
jsonrpc_request_start(cmd->plugin, cmd, "sendpay", selfpay_success,
payment_rpc_failure, payment);

struct route *route = new_route(payment, payment, payment->groupid,
/*partid=*/0, payment->payment_hash,
payment->amount, payment->amount);
struct out_req *req;
req = jsonrpc_request_start(cmd->plugin, cmd, "sendpay",
selfpay_success, selfpay_failure, route);
route->hops = tal_arr(route, struct route_hop, 0);
json_add_route(req->js, route);

route_selfpaypending(route);
return send_outreq(cmd->plugin, req);
}

Expand Down
16 changes: 12 additions & 4 deletions plugins/renepay/routetracker.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,14 @@ struct command_result *notification_sendpay_failure(struct command *cmd,
assert(payment->routetracker);
struct route *route =
route_map_get(payment->routetracker->pending_routes, key);
if (!route)
plugin_err(pay_plugin->plugin,
if (!route) {
/* This can happen if payment is first tried with renepay and
* then retried using another payment plugin. */
plugin_log(pay_plugin->plugin, LOG_UNUSUAL,
"%s: key %s is not found in pending_routes",
__PRETTY_FUNCTION__, fmt_routekey(tmpctx, key));
return notification_handled(cmd);
}

assert(route->result == NULL);
route->result = tal_sendpay_result_from_json(route, buf, sub);
Expand Down Expand Up @@ -338,10 +342,14 @@ struct command_result *notification_sendpay_success(struct command *cmd,
assert(payment->routetracker);
struct route *route =
route_map_get(payment->routetracker->pending_routes, key);
if (!route)
plugin_err(pay_plugin->plugin,
if (!route) {
/* This can happen if payment is first tried with renepay and
* then retried using another payment plugin. */
plugin_log(pay_plugin->plugin, LOG_UNUSUAL,
"%s: key %s is not found in pending_routes",
__PRETTY_FUNCTION__, fmt_routekey(tmpctx, key));
return notification_handled(cmd);
}

assert(route->result == NULL);
route->result = tal_sendpay_result_from_json(route, buf, sub);
Expand Down

0 comments on commit b067667

Please sign in to comment.