@@ -629,6 +629,31 @@ REGISTER_PAYMENT_MODIFIER(compute_routes, compute_routes_cb);
629629 * request calling sendpay.
630630 */
631631
632+ struct sendpay_request {
633+ struct payment * payment ;
634+ struct route * route ;
635+ };
636+
637+ static struct command_result * sendpay_done (struct command * cmd , const char * buf ,
638+ const jsmntok_t * result ,
639+ struct sendpay_request * data )
640+ {
641+ struct payment * payment = data -> payment ;
642+ tal_free (data );
643+
644+ assert (payment -> pending_rpcs > 0 );
645+ payment -> pending_rpcs -- ;
646+ return payment_continue (payment );
647+ }
648+
649+ static struct command_result * sendpay_fail (struct command * cmd , const char * buf ,
650+ const jsmntok_t * result ,
651+ struct sendpay_request * data )
652+ {
653+ data -> route -> sendpay_failed = true;
654+ return sendpay_done (cmd , buf , result , data );
655+ }
656+
632657static struct command_result * send_routes_cb (struct payment * payment )
633658{
634659 assert (payment );
@@ -645,8 +670,59 @@ static struct command_result *send_routes_cb(struct payment *payment)
645670 assert (cmd );
646671 for (size_t i = 0 ; i < tal_count (routetracker -> computed_routes ); i ++ ) {
647672 struct route * route = routetracker -> computed_routes [i ];
648-
649- route_sendpay_request (cmd , take (route ), payment );
673+ route -> sendpay_failed = false;
674+
675+ const struct payment_info * pinfo = & payment -> payment_info ;
676+
677+ struct sendpay_request * data = tal (cmd , struct sendpay_request );
678+ data -> payment = payment ;
679+ data -> route = route ;
680+
681+ struct out_req * req =
682+ jsonrpc_request_start (cmd -> plugin , cmd , "sendpay" ,
683+ sendpay_done , sendpay_fail , data );
684+
685+ json_add_route_hops (req -> js , "route" , route -> hops );
686+ json_add_sha256 (req -> js , "payment_hash" , & pinfo -> payment_hash );
687+ json_add_u64 (req -> js , "partid" , route -> key .partid );
688+ json_add_u64 (req -> js , "groupid" , route -> key .groupid );
689+
690+ /* FIXME: sendpay has a check that we don't total more than
691+ * the exact amount, if we're setting partid (i.e. MPP).
692+ * However, we always set partid, and we add a shadow amount if
693+ * we've only have one part, so we have to use that amount
694+ * here.
695+ *
696+ * The spec was loosened so you are actually allowed
697+ * to overpay, so this check is now overzealous. */
698+ const size_t pathlen = tal_count (route -> hops );
699+ if (pathlen > 0 &&
700+ amount_msat_greater (route_delivers (route ), pinfo -> amount ))
701+ json_add_amount_msat (req -> js , "amount_msat" ,
702+ route_delivers (route ));
703+ else
704+ json_add_amount_msat (req -> js , "amount_msat" ,
705+ pinfo -> amount );
706+
707+ if (pinfo -> payment_secret )
708+ json_add_secret (req -> js , "payment_secret" ,
709+ pinfo -> payment_secret );
710+
711+ /* FIXME: some of these fields might not be required for all
712+ * payment parts. */
713+ json_add_string (req -> js , "bolt11" , pinfo -> invstr );
714+
715+ if (pinfo -> payment_metadata )
716+ json_add_hex_talarr (req -> js , "payment_metadata" ,
717+ pinfo -> payment_metadata );
718+ if (pinfo -> label )
719+ json_add_string (req -> js , "label" , pinfo -> label );
720+ if (pinfo -> description )
721+ json_add_string (req -> js , "description" ,
722+ pinfo -> description );
723+
724+ send_outreq (cmd -> plugin , req );
725+ payment -> pending_rpcs ++ ;
650726
651727 payment_note (payment , LOG_INFORM ,
652728 "Sent route request: partid=%" PRIu64
0 commit comments