@@ -459,110 +459,6 @@ static void json_add_getroutes(struct json_stream *js,
459459 json_array_end (js );
460460}
461461
462- /* Returns an error message, or sets *routes */
463- static const char * get_routes (const tal_t * ctx ,
464- struct command * cmd ,
465- const struct node_id * source ,
466- const struct node_id * dest ,
467- struct amount_msat amount ,
468- struct amount_msat maxfee ,
469- u32 finalcltv ,
470- u32 maxdelay ,
471- const char * * layers ,
472- struct gossmap_localmods * localmods ,
473- const struct layer * local_layer ,
474- bool single_path ,
475- struct route * * * routes ,
476- struct amount_msat * * amounts ,
477- const struct additional_cost_htable * additional_costs ,
478- double * probability )
479- {
480- struct askrene * askrene = get_askrene (cmd -> plugin );
481- struct route_query * rq = tal (ctx , struct route_query );
482- struct flow * * flows ;
483- const struct gossmap_node * srcnode , * dstnode ;
484- const char * ret ;
485- struct timerel time_delta ;
486- struct timemono time_start = time_mono ();
487-
488- if (gossmap_refresh (askrene -> gossmap )) {
489- /* FIXME: gossmap_refresh callbacks to we can update in place */
490- tal_free (askrene -> capacities );
491- askrene -> capacities = get_capacities (askrene , askrene -> plugin , askrene -> gossmap );
492- }
493-
494- rq -> cmd = cmd ;
495- rq -> plugin = cmd -> plugin ;
496- rq -> gossmap = askrene -> gossmap ;
497- rq -> reserved = askrene -> reserved ;
498- rq -> layers = tal_arr (rq , const struct layer * , 0 );
499- rq -> capacities = tal_dup_talarr (rq , fp16_t , askrene -> capacities );
500- rq -> additional_costs = additional_costs ;
501-
502- apply_layers (askrene , rq , source , amount , localmods , layers , local_layer );
503-
504- /* Clear scids with reservations, too, so we don't have to look up
505- * all the time! */
506- reserves_clear_capacities (askrene -> reserved , askrene -> gossmap , rq -> capacities );
507-
508- gossmap_apply_localmods (askrene -> gossmap , localmods );
509-
510- /* localmods can add channels, so we need to allocate biases array *afterwards* */
511- rq -> biases = tal_arrz (rq , s8 , gossmap_max_chan_idx (askrene -> gossmap ) * 2 );
512-
513- /* Note any channel biases */
514- for (size_t i = 0 ; i < tal_count (rq -> layers ); i ++ )
515- layer_apply_biases (rq -> layers [i ], askrene -> gossmap , rq -> biases );
516-
517- srcnode = gossmap_find_node (askrene -> gossmap , source );
518- if (!srcnode ) {
519- ret = rq_log (ctx , rq , LOG_INFORM ,
520- "Unknown source node %s" ,
521- fmt_node_id (tmpctx , source ));
522- goto fail ;
523- }
524-
525- dstnode = gossmap_find_node (askrene -> gossmap , dest );
526- if (!dstnode ) {
527- ret = rq_log (ctx , rq , LOG_INFORM ,
528- "Unknown destination node %s" ,
529- fmt_node_id (tmpctx , dest ));
530- goto fail ;
531- }
532-
533- /* FIXME: single_path should signal a change in algorithm. */
534- ret = default_routes (rq , rq , srcnode , dstnode , amount , single_path ,
535- maxfee , finalcltv , maxdelay , & flows , probability );
536- if (ret ) {
537- goto fail ;
538- }
539- assert (tal_count (flows ) > 0 );
540- rq_log (tmpctx , rq , LOG_DBG , "Final answer has %zu flows" ,
541- tal_count (flows ));
542-
543- /* convert flows to routes */
544- * routes = convert_flows_to_routes (rq , rq , finalcltv , flows , amounts );
545- assert (tal_count (* routes ) == tal_count (flows ));
546- assert (tal_count (* amounts ) == tal_count (flows ));
547-
548- /* At last we remove the localmods from the gossmap. */
549- gossmap_remove_localmods (askrene -> gossmap , localmods );
550- time_delta = timemono_between (time_mono (), time_start );
551- rq_log (tmpctx , rq , LOG_DBG , "get_routes completed in %" PRIu64 " ms" ,
552- time_to_msec (time_delta ));
553- return NULL ;
554-
555- /* Explicit failure path keeps the compiler (gcc version 12.3.0 -O3) from
556- * warning about uninitialized variables in the caller */
557- fail :
558- assert (ret != NULL );
559- gossmap_remove_localmods (askrene -> gossmap , localmods );
560- time_delta = timemono_between (time_mono (), time_start );
561- rq_log (tmpctx , rq , LOG_DBG , "get_routes failed after %" PRIu64 " ms" ,
562- time_to_msec (time_delta ));
563- return ret ;
564- }
565-
566462void get_constraints (const struct route_query * rq ,
567463 const struct gossmap_chan * chan ,
568464 int dir ,
@@ -603,26 +499,115 @@ static struct command_result *do_getroutes(struct command *cmd,
603499 struct gossmap_localmods * localmods ,
604500 const struct getroutes_info * info )
605501{
502+ struct askrene * askrene = get_askrene (cmd -> plugin );
503+ struct route_query * rq = tal (cmd , struct route_query );
606504 const char * err ;
607505 double probability ;
608506 struct amount_msat * amounts ;
609507 struct route * * routes ;
508+ struct flow * * flows ;
610509 struct json_stream * response ;
611510
612- err = get_routes (cmd , cmd ,
613- info -> source , info -> dest ,
614- * info -> amount , * info -> maxfee , * info -> finalcltv ,
615- * info -> maxdelay , info -> layers , localmods , info -> local_layer ,
616- have_layer (info -> layers , "auto.no_mpp_support" ),
617- & routes , & amounts , info -> additional_costs , & probability );
511+ /* update the gossmap */
512+ if (gossmap_refresh (askrene -> gossmap )) {
513+ /* FIXME: gossmap_refresh callbacks to we can update in place */
514+ tal_free (askrene -> capacities );
515+ askrene -> capacities =
516+ get_capacities (askrene , askrene -> plugin , askrene -> gossmap );
517+ }
518+
519+ /* build this request structure */
520+ rq -> cmd = cmd ;
521+ rq -> plugin = cmd -> plugin ;
522+ rq -> gossmap = askrene -> gossmap ;
523+ rq -> reserved = askrene -> reserved ;
524+ rq -> layers = tal_arr (rq , const struct layer * , 0 );
525+ rq -> capacities = tal_dup_talarr (rq , fp16_t , askrene -> capacities );
526+ /* FIXME: we still need to do something useful with these */
527+ rq -> additional_costs = info -> additional_costs ;
528+
529+ /* apply selected layers to the localmods */
530+ apply_layers (askrene , rq , info -> source , * info -> amount , localmods ,
531+ info -> layers , info -> local_layer );
532+
533+ /* Clear scids with reservations, too, so we don't have to look up
534+ * all the time! */
535+ reserves_clear_capacities (askrene -> reserved , askrene -> gossmap ,
536+ rq -> capacities );
537+
538+ /* we temporarily apply localmods */
539+ gossmap_apply_localmods (askrene -> gossmap , localmods );
540+
541+ /* localmods can add channels, so we need to allocate biases array
542+ * *afterwards* */
543+ rq -> biases =
544+ tal_arrz (rq , s8 , gossmap_max_chan_idx (askrene -> gossmap ) * 2 );
545+
546+ /* Note any channel biases */
547+ for (size_t i = 0 ; i < tal_count (rq -> layers ); i ++ )
548+ layer_apply_biases (rq -> layers [i ], askrene -> gossmap , rq -> biases );
549+
550+ /* checkout the source */
551+ const struct gossmap_node * srcnode =
552+ gossmap_find_node (askrene -> gossmap , info -> source );
553+ if (!srcnode ) {
554+ err = rq_log (tmpctx , rq , LOG_INFORM , "Unknown source node %s" ,
555+ fmt_node_id (tmpctx , info -> source ));
556+ goto fail ;
557+ }
558+
559+ /* checkout the destination */
560+ const struct gossmap_node * dstnode =
561+ gossmap_find_node (askrene -> gossmap , info -> dest );
562+ if (!dstnode ) {
563+ err = rq_log (tmpctx , rq , LOG_INFORM ,
564+ "Unknown destination node %s" ,
565+ fmt_node_id (tmpctx , info -> dest ));
566+ goto fail ;
567+ }
568+
569+ /* Compute the routes. At this point we might select between multiple
570+ * algorithms. */
571+ struct timemono time_start = time_mono ();
572+ err = default_routes (rq , rq , srcnode , dstnode , * info -> amount ,
573+ /* only one path? = */
574+ have_layer (info -> layers , "auto.no_mpp_support" ),
575+ * info -> maxfee , * info -> finalcltv , * info -> maxdelay ,
576+ & flows , & probability );
577+ struct timerel time_delta = timemono_between (time_mono (), time_start );
578+
579+ /* log the time of computation */
580+ rq_log (tmpctx , rq , LOG_DBG , "get_routes %s %" PRIu64 " ms" ,
581+ err ? "failed after" : "completed in" ,
582+ time_to_msec (time_delta ));
618583 if (err )
619- return command_fail ( cmd , PAY_ROUTE_NOT_FOUND , "%s" , err ) ;
584+ goto fail ;
620585
586+ /* otherwise we continue */
587+ assert (tal_count (flows ) > 0 );
588+ rq_log (tmpctx , rq , LOG_DBG , "Final answer has %zu flows" ,
589+ tal_count (flows ));
590+
591+ /* convert flows to routes */
592+ routes = convert_flows_to_routes (rq , rq , * info -> finalcltv , flows ,
593+ & amounts );
594+ assert (tal_count (routes ) == tal_count (flows ));
595+ assert (tal_count (amounts ) == tal_count (flows ));
596+
597+ /* At last we remove the localmods from the gossmap. */
598+ gossmap_remove_localmods (askrene -> gossmap , localmods );
599+
600+ /* output the results */
621601 response = jsonrpc_stream_success (cmd );
622602 json_add_getroutes (response , routes , amounts , probability ,
623603 * info -> finalcltv );
624604 return command_finished (cmd , response );
625- }
605+
606+ fail :
607+ assert (err );
608+ gossmap_remove_localmods (askrene -> gossmap , localmods );
609+ return command_fail (cmd , PAY_ROUTE_NOT_FOUND , "%s" , err );
610+ }
626611
627612static void add_localchan (struct gossmap_localmods * mods ,
628613 const struct node_id * self ,
0 commit comments