@@ -595,6 +595,146 @@ REGISTER_PAYMENT_MODIFIER(getroutes, getroutes_cb);
595595 * request calling sendpay.
596596 */
597597
598+ static struct command_result * sendroutes_done (struct command * cmd ,
599+ struct payment * payment )
600+ {
601+ return payment_continue (payment );
602+ }
603+
604+ /* Callback function for sendpay request success. */
605+ static struct command_result *
606+ renesendpay_done (struct command * cmd , const char * method UNUSED ,
607+ const char * buf , const jsmntok_t * result , struct route * route )
608+ {
609+ assert (route );
610+ struct renepay * renepay = get_renepay (cmd -> plugin );
611+ struct payment * payment = route_get_payment_verify (renepay , route );
612+ route_pending_register (payment , payment -> routetracker , route );
613+
614+ const jsmntok_t * t ;
615+ size_t i ;
616+ bool ret ;
617+
618+ const jsmntok_t * secretstok =
619+ json_get_member (buf , result , "shared_secrets" );
620+
621+ if (secretstok ) {
622+ assert (secretstok -> type == JSMN_ARRAY );
623+
624+ route -> shared_secrets =
625+ tal_arr (route , struct secret , secretstok -> size );
626+ json_for_each_arr (i , t , secretstok )
627+ {
628+ ret = json_to_secret (buf , t , & route -> shared_secrets [i ]);
629+ assert (ret );
630+ }
631+ } else
632+ route -> shared_secrets = NULL ;
633+ return command_still_pending (cmd );
634+ }
635+
636+ /* FIXME: check when will renesendpay fail */
637+ static struct command_result *
638+ renesendpay_fail (struct command * cmd , const char * method UNUSED ,
639+ const char * buf , const jsmntok_t * tok , struct route * route )
640+ {
641+ assert (route );
642+ struct renepay * renepay = get_renepay (cmd -> plugin );
643+ struct payment * payment = route_get_payment_verify (renepay , route );
644+ struct routetracker * routetracker = payment -> routetracker ;
645+ assert (routetracker );
646+
647+ enum jsonrpc_errcode errcode ;
648+ const char * msg ;
649+ const char * err ;
650+
651+ err = json_scan (tmpctx , buf , tok , "{code:%,message:%}" ,
652+ JSON_SCAN (json_to_jsonrpc_errcode , & errcode ),
653+ JSON_SCAN_TAL (tmpctx , json_strdup , & msg ));
654+ if (err )
655+ plugin_err (cmd -> plugin ,
656+ "Unable to parse sendpay error: %s, json: %.*s" , err ,
657+ json_tok_full_len (tok ), json_tok_full (buf , tok ));
658+
659+ payment_note (payment , LOG_INFORM ,
660+ "Sendpay failed: partid=%" PRIu64
661+ " errorcode:%d message=%s" ,
662+ route -> key .partid , errcode , msg );
663+
664+ if (errcode != PAY_TRY_OTHER_ROUTE ) {
665+ plugin_log (cmd -> plugin , LOG_UNUSUAL ,
666+ "Strange error from sendpay: %.*s" ,
667+ json_tok_full_len (tok ), json_tok_full (buf , tok ));
668+ }
669+
670+ /* There is no new knowledge from this kind of failure.
671+ * We just disable this scid. */
672+ // FIXME: askrene disable this channel
673+ struct short_channel_id_dir scidd_disable = {
674+ .scid = route -> hops [0 ].scid , .dir = route -> hops [0 ].direction };
675+ payment_disable_chan (payment , scidd_disable , LOG_INFORM ,
676+ "sendpay didn't like first hop: %s" , msg );
677+
678+ if (!route_map_del (routetracker -> sent_routes , route ))
679+ plugin_err (cmd -> plugin , "%s: route (%s) is not marked as sent" ,
680+ __func__ , fmt_routekey (tmpctx , & route -> key ));
681+ tal_free (route );
682+ return command_still_pending (cmd );
683+ }
684+
685+ static void add_sendpay_request (struct rpcbatch * batch , struct route * route ,
686+ struct payment * payment )
687+ {
688+ struct payment_info * pinfo = & payment -> payment_info ;
689+ struct out_req * req = add_to_rpcbatch (
690+ batch , "renesendpay" , renesendpay_done , renesendpay_fail , route );
691+ const size_t pathlen = tal_count (route -> hops );
692+ json_add_sha256 (req -> js , "payment_hash" , & route -> key .payment_hash );
693+ json_add_u64 (req -> js , "partid" , route -> key .partid );
694+ json_add_u64 (req -> js , "groupid" , route -> key .groupid );
695+ json_add_string (req -> js , "invoice" , pinfo -> invstr );
696+ json_add_node_id (req -> js , "destination" , & pinfo -> destination );
697+ json_add_amount_msat (req -> js , "amount_msat" , route -> amount_deliver );
698+ json_add_amount_msat (req -> js , "total_amount_msat" , pinfo -> amount );
699+ json_add_u32 (req -> js , "final_cltv" , pinfo -> final_cltv );
700+
701+ if (pinfo -> label )
702+ json_add_string (req -> js , "label" , pinfo -> label );
703+ if (pinfo -> description )
704+ json_add_string (req -> js , "description" , pinfo -> description );
705+
706+ json_array_start (req -> js , "route" );
707+ /* An empty route means a payment to oneself, pathlen=0 */
708+ for (size_t j = 0 ; j < pathlen ; j ++ ) {
709+ const struct route_hop * hop = & route -> hops [j ];
710+ json_object_start (req -> js , NULL );
711+ json_add_node_id (req -> js , "id" , & hop -> node_id );
712+ json_add_short_channel_id (req -> js , "channel" , hop -> scid );
713+ json_add_amount_msat (req -> js , "amount_msat" , hop -> amount );
714+ json_add_num (req -> js , "direction" , hop -> direction );
715+ json_add_u32 (req -> js , "delay" , hop -> delay );
716+ json_add_string (req -> js , "style" , "tlv" );
717+ json_object_end (req -> js );
718+ }
719+ json_array_end (req -> js );
720+
721+ /* Either we have a payment_secret for BOLT11 or blinded_paths for
722+ * BOLT12 */
723+ if (pinfo -> payment_secret )
724+ json_add_secret (req -> js , "payment_secret" ,
725+ pinfo -> payment_secret );
726+ else {
727+ assert (pinfo -> blinded_paths );
728+ const struct blinded_path * bpath =
729+ pinfo -> blinded_paths [route -> path_num ];
730+ json_myadd_blinded_path (req -> js , "blinded_path" , bpath );
731+ }
732+ send_outreq (req );
733+ route_map_add (payment -> routetracker -> sent_routes , route );
734+ if (taken (route ))
735+ tal_steal (payment -> routetracker -> sent_routes , route );
736+ }
737+
598738static struct command_result * send_routes_cb (struct payment * payment )
599739{
600740 assert (payment );
@@ -609,11 +749,11 @@ static struct command_result *send_routes_cb(struct payment *payment)
609749 }
610750 struct command * cmd = payment_command (payment );
611751 assert (cmd );
752+ struct rpcbatch * batch = rpcbatch_new (cmd , sendroutes_done , payment );
753+
612754 for (size_t i = 0 ; i < tal_count (routetracker -> computed_routes ); i ++ ) {
613755 struct route * route = routetracker -> computed_routes [i ];
614-
615- route_sendpay_request (cmd , take (route ), payment );
616-
756+ add_sendpay_request (batch , take (route ), payment );
617757 payment_note (payment , LOG_INFORM ,
618758 "Sent route request: partid=%" PRIu64
619759 " amount=%s prob=%.3lf fees=%s delay=%u path=%s" ,
@@ -624,7 +764,7 @@ static struct command_result *send_routes_cb(struct payment *payment)
624764 route_delay (route ), fmt_route_path (tmpctx , route ));
625765 }
626766 tal_resize (& routetracker -> computed_routes , 0 );
627- return payment_continue ( payment );
767+ return rpcbatch_done ( batch );
628768}
629769
630770REGISTER_PAYMENT_MODIFIER (send_routes , send_routes_cb );
0 commit comments