Skip to content

Commit

Permalink
renepay: refactor send routes
Browse files Browse the repository at this point in the history
We do not move to the next payment step until all sendpay RPCs are
finished. We flag routes that fail the sendpay call.

Signed-off-by: Lagrang3 <[email protected]>
  • Loading branch information
Lagrang3 committed Sep 19, 2024
1 parent 5242435 commit fa243fe
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
80 changes: 78 additions & 2 deletions plugins/renepay/mods.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,31 @@ REGISTER_PAYMENT_MODIFIER(compute_routes, compute_routes_cb);
* request calling sendpay.
*/

struct sendpay_request {
struct payment *payment;
struct route *route;
};

static struct command_result *sendpay_done(struct command *cmd, const char *buf,
const jsmntok_t *result,
struct sendpay_request *data)
{
struct payment *payment = data->payment;
tal_free(data);

assert(payment->pending_rpcs > 0);
payment->pending_rpcs--;
return payment_continue(payment);
}

static struct command_result *sendpay_fail(struct command *cmd, const char *buf,
const jsmntok_t *result,
struct sendpay_request *data)
{
data->route->sendpay_failed = true;
return sendpay_done(cmd, buf, result, data);
}

static struct command_result *send_routes_cb(struct payment *payment)
{
assert(payment);
Expand All @@ -645,8 +670,59 @@ static struct command_result *send_routes_cb(struct payment *payment)
assert(cmd);
for (size_t i = 0; i < tal_count(routetracker->computed_routes); i++) {
struct route *route = routetracker->computed_routes[i];

route_sendpay_request(cmd, take(route), payment);
route->sendpay_failed = false;

const struct payment_info *pinfo = &payment->payment_info;

struct sendpay_request *data = tal(cmd, struct sendpay_request);
data->payment = payment;
data->route = route;

struct out_req *req =
jsonrpc_request_start(cmd->plugin, cmd, "sendpay",
sendpay_done, sendpay_fail, data);

json_add_route_hops(req->js, "route", route->hops);
json_add_sha256(req->js, "payment_hash", &pinfo->payment_hash);
json_add_u64(req->js, "partid", route->key.partid);
json_add_u64(req->js, "groupid", route->key.groupid);

/* FIXME: sendpay has a check that we don't total more than
* the exact amount, if we're setting partid (i.e. MPP).
* However, we always set partid, and we add a shadow amount if
* we've only have one part, so we have to use that amount
* here.
*
* The spec was loosened so you are actually allowed
* to overpay, so this check is now overzealous. */
const size_t pathlen = tal_count(route->hops);
if (pathlen > 0 &&
amount_msat_greater(route_delivers(route), pinfo->amount))
json_add_amount_msat(req->js, "amount_msat",
route_delivers(route));
else
json_add_amount_msat(req->js, "amount_msat",
pinfo->amount);

if (pinfo->payment_secret)
json_add_secret(req->js, "payment_secret",
pinfo->payment_secret);

/* FIXME: some of these fields might not be required for all
* payment parts. */
json_add_string(req->js, "bolt11", pinfo->invstr);

if (pinfo->payment_metadata)
json_add_hex_talarr(req->js, "payment_metadata",
pinfo->payment_metadata);
if (pinfo->label)
json_add_string(req->js, "label", pinfo->label);
if (pinfo->description)
json_add_string(req->js, "description",
pinfo->description);

send_outreq(cmd->plugin, req);
payment->pending_rpcs++;

payment_note(payment, LOG_INFORM,
"Sent route request: partid=%" PRIu64
Expand Down
3 changes: 3 additions & 0 deletions plugins/renepay/route.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ struct payment_result {

/* Describes a payment route. It points to a unique sendpay and payment. */
struct route {
/* Flag this route was not sent due to an error in sendpay. */
bool sendpay_failed;

enum jsonrpc_errcode final_error;
const char *final_msg;

Expand Down

0 comments on commit fa243fe

Please sign in to comment.