Skip to content

Commit 72cc777

Browse files
committed
renepay: split send routes into two steps
Splig send routes into two steps: 1. reserve liquidity, 2. call sendpay Changelog-None Signed-off-by: Lagrang3 <[email protected]>
1 parent 45342a2 commit 72cc777

File tree

1 file changed

+67
-31
lines changed

1 file changed

+67
-31
lines changed

plugins/renepay/mods.c

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -589,18 +589,11 @@ static struct command_result *getroutes_cb(struct payment *payment)
589589
REGISTER_PAYMENT_MODIFIER(getroutes, getroutes_cb);
590590

591591
/*****************************************************************************
592-
* send_routes
592+
* reserve_routes
593593
*
594-
* This payment modifier takes the payment routes and starts the payment
595-
* request calling sendpay.
594+
* Use askrene API to reserve liquidity on the computed routes.
596595
*/
597596

598-
static struct command_result *sendroutes_done(struct command *cmd,
599-
struct payment *payment)
600-
{
601-
return payment_continue(payment);
602-
}
603-
604597
static struct command_result *unreserve_done(struct command *aux_cmd,
605598
const char *method UNUSED,
606599
const char *buf UNUSED,
@@ -663,6 +656,62 @@ static struct command_result *reserve_fail(struct command *cmd,
663656
return command_still_pending(cmd);
664657
}
665658

659+
static struct command_result *reserve_routes_done(struct command *cmd,
660+
struct payment *payment)
661+
{
662+
return payment_continue(payment);
663+
}
664+
665+
static void add_reserve_request(struct rpcbatch *batch, struct route *route)
666+
{
667+
struct out_req *req = add_to_rpcbatch(
668+
batch, "askrene-reserve", reserve_done, reserve_fail, route);
669+
json_array_start(req->js, "path");
670+
for (int i = 0; i < tal_count(route->hops); i++) {
671+
const struct route_hop *hop = &route->hops[i];
672+
struct short_channel_id_dir scidd = {.scid = hop->scid,
673+
.dir = hop->direction};
674+
json_object_start(req->js, NULL);
675+
json_add_short_channel_id_dir(req->js, "short_channel_id_dir",
676+
scidd);
677+
json_add_amount_msat(req->js, "amount_msat", hop->amount);
678+
json_object_end(req->js);
679+
}
680+
json_array_end(req->js);
681+
send_outreq(req);
682+
}
683+
684+
static struct command_result *reserve_routes_cb(struct payment *payment)
685+
{
686+
assert(payment);
687+
struct command *cmd = payment_command(payment);
688+
assert(cmd);
689+
struct rpcbatch *batch =
690+
rpcbatch_new(cmd, reserve_routes_done, payment);
691+
692+
for (size_t i = 0;
693+
i < tal_count(payment->routetracker->computed_routes); i++) {
694+
struct route *route = payment->routetracker->computed_routes[i];
695+
add_reserve_request(batch, route);
696+
}
697+
return rpcbatch_done(batch);
698+
}
699+
700+
REGISTER_PAYMENT_MODIFIER(reserve_routes, reserve_routes_cb);
701+
702+
/*****************************************************************************
703+
* send_routes
704+
*
705+
* This payment modifier takes the payment routes and starts the payment
706+
* request calling sendpay.
707+
*/
708+
709+
static struct command_result *sendroutes_done(struct command *cmd,
710+
struct payment *payment)
711+
{
712+
return payment_continue(payment);
713+
}
714+
666715
/* Callback function for sendpay request success. */
667716
static struct command_result *
668717
renesendpay_done(struct command *cmd, const char *method UNUSED,
@@ -693,21 +742,7 @@ renesendpay_done(struct command *cmd, const char *method UNUSED,
693742
} else
694743
route->shared_secrets = NULL;
695744

696-
struct out_req *req = jsonrpc_request_start(
697-
cmd, "askrene-reserve", reserve_done, reserve_fail, route);
698-
json_array_start(req->js, "path");
699-
for (i = 0; i < tal_count(route->hops); i++) {
700-
const struct route_hop *hop = &route->hops[i];
701-
struct short_channel_id_dir scidd = {.scid = hop->scid,
702-
.dir = hop->direction};
703-
json_object_start(req->js, NULL);
704-
json_add_short_channel_id_dir(req->js, "short_channel_id_dir",
705-
scidd);
706-
json_add_amount_msat(req->js, "amount_msat", hop->amount);
707-
json_object_end(req->js);
708-
}
709-
json_array_end(req->js);
710-
return send_outreq(req);
745+
return command_still_pending(cmd);
711746
}
712747

713748
/* FIXME: check when will renesendpay fail */
@@ -1427,13 +1462,14 @@ void *payment_virtual_program[] = {
14271462
/*16*/ OP_CALL, &checktimeout_pay_mod,
14281463
/*18*/ OP_CALL, &refreshgossmap_pay_mod,
14291464
/*20*/ OP_CALL, &getroutes_pay_mod,
1430-
/*22*/ OP_CALL, &send_routes_pay_mod,
1465+
/*22*/ OP_CALL, &reserve_routes_pay_mod,
1466+
/*24*/ OP_CALL, &send_routes_pay_mod,
14311467
/*do*/
1432-
/*24*/ OP_CALL, &sleep_pay_mod,
1433-
/*26*/ OP_CALL, &collect_results_pay_mod,
1468+
/*26*/ OP_CALL, &sleep_pay_mod,
1469+
/*28*/ OP_CALL, &collect_results_pay_mod,
14341470
/*while*/
1435-
/*28*/ OP_IF, &nothaveresults_pay_cond, (void *)24,
1471+
/*30*/ OP_IF, &nothaveresults_pay_cond, (void *)26,
14361472
/* while */
1437-
/*31*/ OP_IF, &retry_pay_cond, (void *)14,
1438-
/*34*/ OP_CALL, &end_pay_mod, /* safety net, default failure if reached */
1439-
/*36*/ NULL};
1473+
/*33*/ OP_IF, &retry_pay_cond, (void *)14,
1474+
/*36*/ OP_CALL, &end_pay_mod, /* safety net, default failure if reached */
1475+
/*38*/ NULL};

0 commit comments

Comments
 (0)