@@ -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
606605static 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
635648static 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
665685static 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" );
0 commit comments