Skip to content

Commit 313aecf

Browse files
committed
renepay: use rpcActions to unreserve routes
Calling askrene-unreserve using rpcActions. Either: - after geting a fail notification, - or success notification, - or if the route destructor is called. Changelog-None Signed-off-by: Lagrang3 <[email protected]>
1 parent 6908fb4 commit 313aecf

File tree

7 files changed

+81
-33
lines changed

7 files changed

+81
-33
lines changed

plugins/renepay/json.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct route *tal_route_from_json(const tal_t *ctx, const char *buf,
6767
route->final_msg = NULL;
6868
route->final_error = LIGHTNINGD;
6969
route->shared_secrets = NULL;
70+
route->unreserve_action = NULL;
7071

7172
return route;
7273
fail:

plugins/renepay/mods.c

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -594,42 +594,55 @@ REGISTER_PAYMENT_MODIFIER(getroutes, getroutes_cb);
594594
* Use askrene API to reserve liquidity on the computed routes.
595595
*/
596596

597-
static struct command_result *unreserve_done(struct command *aux_cmd,
598-
const char *method UNUSED,
599-
const char *buf UNUSED,
600-
const jsmntok_t *result UNUSED,
601-
struct route *route UNUSED)
597+
static struct command_result *
598+
unreserve_done(struct command *cmd, const char *method, const char *buf,
599+
const jsmntok_t *result, struct route *route UNUSED)
602600
{
603-
return aux_command_done(aux_cmd);
601+
route->unreserve_action = tal_free(route->unreserve_action);
602+
return command_still_pending(cmd);
604603
}
605604

606605
static struct command_result *
607-
unreserve_fail(struct command *aux_cmd, const char *method, const char *buf,
606+
unreserve_fail(struct command *cmd, const char *method, const char *buf,
608607
const jsmntok_t *result, struct route *route UNUSED)
609608
{
610-
plugin_log(aux_cmd->plugin, LOG_UNUSUAL, "%s failed: %.*s", method,
609+
plugin_log(cmd->plugin, LOG_UNUSUAL, "%s failed: %.*s", method,
611610
json_tok_full_len(result), json_tok_full(buf, result));
612-
return aux_command_done(aux_cmd);
611+
return command_still_pending(cmd);
613612
}
614613

615-
static void unreserve_route(struct route *route, struct command *aux_cmd)
614+
static void json_unreserve(struct json_stream *js, struct route *route)
616615
{
617-
struct out_req *req =
618-
jsonrpc_request_start(aux_cmd, "askrene-unreserve", unreserve_done,
619-
unreserve_fail, route);
620-
json_array_start(req->js, "path");
616+
json_array_start(js, "path");
621617
for (size_t i = 0; i < tal_count(route->hops); i++) {
622618
const struct route_hop *hop = &route->hops[i];
623619
struct short_channel_id_dir scidd = {.scid = hop->scid,
624620
.dir = hop->direction};
625-
json_object_start(req->js, NULL);
626-
json_add_short_channel_id_dir(req->js, "short_channel_id_dir",
621+
json_object_start(js, NULL);
622+
json_add_short_channel_id_dir(js, "short_channel_id_dir",
627623
scidd);
628-
json_add_amount_msat(req->js, "amount_msat", hop->amount);
629-
json_object_end(req->js);
624+
json_add_amount_msat(js, "amount_msat", hop->amount);
625+
json_object_end(js);
630626
}
631-
json_array_end(req->js);
632-
send_outreq(req);
627+
json_array_end(js);
628+
}
629+
630+
static struct command_result *unreserve_aux_final(struct command *aux_cmd,
631+
struct route *route)
632+
{
633+
return aux_command_done(aux_cmd);
634+
}
635+
636+
static void unreserve_route(struct route *route, struct command *aux_cmd)
637+
{
638+
struct rpcbatch *batch =
639+
rpcbatch_new(aux_cmd, unreserve_aux_final, route);
640+
if (route->unreserve_action) {
641+
struct out_req *req =
642+
rpcbatch_append_action(batch, route->unreserve_action);
643+
send_outreq(req);
644+
}
645+
rpcbatch_done(batch);
633646
}
634647

635648
static struct command_result *reserve_done(struct command *cmd,
@@ -639,8 +652,15 @@ static struct command_result *reserve_done(struct command *cmd,
639652
struct route *route)
640653
{
641654
/* A new command is issued to handle the destruction of this route.
642-
* I hope aux_cmd outlives the current payment session cmd. */
655+
* I hope aux_cmd outlives the current payment session cmd.
656+
* We encode this action as an entity of its own "struct rpcaction".
657+
* We can attach the unreserve action to the route destructor but also
658+
* if we need to unreserve after a notification we can do that as well
659+
* without repeating the same code again, just re-use the action. */
643660
struct command *aux_cmd = aux_command(cmd);
661+
route->unreserve_action =
662+
rpcaction_new(aux_cmd, "askrene-unreserve", unreserve_done,
663+
unreserve_fail, json_unreserve, route);
644664
tal_add_destructor2(route, unreserve_route, aux_cmd);
645665
return command_still_pending(cmd);
646666
}
@@ -664,6 +684,7 @@ static struct command_result *reserve_routes_done(struct command *cmd,
664684

665685
static void add_reserve_request(struct rpcbatch *batch, struct route *route)
666686
{
687+
assert(route->unreserve_action == NULL);
667688
struct out_req *req = add_to_rpcbatch(
668689
batch, "askrene-reserve", reserve_done, reserve_fail, route);
669690
json_array_start(req->js, "path");

plugins/renepay/route.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct route *new_route(const tal_t *ctx, u64 groupid,
2121
route->amount_sent = amount_sent;
2222
route->path_num = -1;
2323
route->shared_secrets = NULL;
24+
route->unreserve_action = NULL;
2425
return route;
2526
}
2627

plugins/renepay/route.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ struct route {
7575

7676
/* blinded path index if any */
7777
int path_num;
78+
79+
/* An rpcaction that calls askrene-unreserve. */
80+
struct rpcaction *unreserve_action;
7881
};
7982

8083
static inline struct routekey routekey(const struct sha256 *hash, u64 groupid,

plugins/renepay/routefail.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,24 @@ static struct command_result *routefail_done(struct command *cmd,
6060
return notification_handled(cmd);
6161
}
6262

63-
struct command_result *routefail_start(struct command *cmd,
64-
struct payment *payment,
65-
struct route *route)
63+
struct command_result *routesuccess_start(struct command *cmd,
64+
struct route *route)
6665
{
66+
// FIXME: call askrene-inform-channel with inform=succeeded for this
67+
// route
68+
struct renepay *renepay = get_renepay(cmd->plugin);
69+
struct payment *payment = route_get_payment_verify(renepay, route);
70+
routetracker_add_to_final(payment, payment->routetracker, route);
71+
return notification_handled(cmd);
72+
}
73+
74+
struct command_result *routefail_start(struct command *cmd, struct route *route)
75+
{
76+
struct renepay *renepay = get_renepay(cmd->plugin);
6777
struct routefail *r = tal(cmd, struct routefail);
6878
r->batch = rpcbatch_new(cmd, routefail_done, r);
6979
r->route = route;
70-
r->payment = payment;
80+
r->payment = route_get_payment_verify(renepay, route);
7181
update_gossip(r);
7282
handle_failure(r);
7383
return rpcbatch_done(r->batch);

plugins/renepay/routefail.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <plugins/renepay/route.h>
88

99
struct command_result *routefail_start(struct command *cmd,
10-
struct payment *payment,
1110
struct route *route);
1211

12+
struct command_result *routesuccess_start(struct command *cmd,
13+
struct route *route);
14+
1315
#endif /* LIGHTNING_PLUGINS_RENEPAY_ROUTEFAIL_H */

plugins/renepay/routetracker.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,15 @@ struct command_result *notification_sendpay_failure(struct command *cmd,
291291
route->result->status = SENDPAY_FAILED;
292292
}
293293
get_erring_scidd_from_index(route);
294-
/* we do some error processing steps before calling
295-
* route_failure_register. */
296-
return routefail_start(cmd, payment, route);
294+
295+
/* Unreserve first and then call routefail_start */
296+
struct rpcbatch *batch = rpcbatch_new(cmd, routefail_start, route);
297+
if (route->unreserve_action) {
298+
struct out_req *req =
299+
rpcbatch_append_action(batch, route->unreserve_action);
300+
send_outreq(req);
301+
}
302+
return rpcbatch_done(batch);
297303
}
298304

299305
struct command_result *notification_sendpay_success(struct command *cmd,
@@ -345,8 +351,12 @@ struct command_result *notification_sendpay_success(struct command *cmd,
345351
json_tok_full_len(sub), json_tok_full(buf, sub));
346352

347353
assert(route->result->status == SENDPAY_COMPLETE);
348-
// FIXME: call askrene-inform-channel with inform=succeeded for this
349-
// route
350-
routetracker_add_to_final(payment, payment->routetracker, route);
351-
return notification_handled(cmd);
354+
/* Unreserve first and then call routesuccess_start */
355+
struct rpcbatch *batch = rpcbatch_new(cmd, routesuccess_start, route);
356+
if (route->unreserve_action) {
357+
struct out_req *req =
358+
rpcbatch_append_action(batch, route->unreserve_action);
359+
send_outreq(req);
360+
}
361+
return rpcbatch_done(batch);
352362
}

0 commit comments

Comments
 (0)