Skip to content

Commit 4092a9d

Browse files
committed
askrene: extracet convert_flows_to_routes into its own function.
Signed-off-by: Rusty Russell <[email protected]>
1 parent f8ba1a1 commit 4092a9d

File tree

1 file changed

+55
-37
lines changed

1 file changed

+55
-37
lines changed

plugins/askrene/askrene.c

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,56 @@ static void apply_layers(struct askrene *askrene, struct route_query *rq,
375375
layer_clear_overridden_capacities(l, askrene->gossmap, rq->capacities);
376376
}
377377
}
378+
379+
/* Convert back into routes, with delay and other information fixed */
380+
static struct route **convert_flows_to_routes(const tal_t *ctx,
381+
struct route_query *rq,
382+
u32 finalcltv,
383+
struct flow **flows,
384+
struct amount_msat **amounts)
385+
{
386+
struct route **routes;
387+
routes = tal_arr(ctx, struct route *, tal_count(flows));
388+
*amounts = tal_arr(ctx, struct amount_msat, tal_count(flows));
389+
390+
for (size_t i = 0; i < tal_count(flows); i++) {
391+
struct route *r;
392+
struct amount_msat msat;
393+
u32 delay;
394+
395+
routes[i] = r = tal(routes, struct route);
396+
r->success_prob = flow_probability(flows[i], rq);
397+
r->hops = tal_arr(r, struct route_hop, tal_count(flows[i]->path));
398+
399+
/* Fill in backwards to calc amount and delay */
400+
msat = flows[i]->delivers;
401+
delay = finalcltv;
402+
403+
for (int j = tal_count(flows[i]->path) - 1; j >= 0; j--) {
404+
struct route_hop *rh = &r->hops[j];
405+
struct gossmap_node *far_end;
406+
const struct half_chan *h = flow_edge(flows[i], j);
407+
408+
if (!amount_msat_add_fee(&msat, h->base_fee, h->proportional_fee))
409+
plugin_err(rq->plugin, "Adding fee to amount");
410+
delay += h->delay;
411+
412+
rh->scid = gossmap_chan_scid(rq->gossmap, flows[i]->path[j]);
413+
rh->direction = flows[i]->dirs[j];
414+
far_end = gossmap_nth_node(rq->gossmap, flows[i]->path[j], !flows[i]->dirs[j]);
415+
gossmap_node_get_id(rq->gossmap, far_end, &rh->node_id);
416+
rh->amount = msat;
417+
rh->delay = delay;
418+
}
419+
(*amounts)[i] = flows[i]->delivers;
420+
rq_log(tmpctx, rq, LOG_INFORM, "Flow %zu/%zu: %s",
421+
i, tal_count(flows),
422+
fmt_route(tmpctx, r, (*amounts)[i], finalcltv));
423+
}
424+
425+
return routes;
426+
}
427+
378428
/* Returns an error message, or sets *routes */
379429
static const char *get_routes(const tal_t *ctx,
380430
struct command *cmd,
@@ -456,44 +506,12 @@ static const char *get_routes(const tal_t *ctx,
456506
rq_log(tmpctx, rq, LOG_DBG, "Final answer has %zu flows",
457507
tal_count(flows));
458508

459-
/* Convert back into routes, with delay and other information fixed */
460-
*routes = tal_arr(ctx, struct route *, tal_count(flows));
461-
*amounts = tal_arr(ctx, struct amount_msat, tal_count(flows));
462-
for (size_t i = 0; i < tal_count(flows); i++) {
463-
struct route *r;
464-
struct amount_msat msat;
465-
u32 delay;
466-
467-
(*routes)[i] = r = tal(*routes, struct route);
468-
r->success_prob = flow_probability(flows[i], rq);
469-
r->hops = tal_arr(r, struct route_hop, tal_count(flows[i]->path));
470-
471-
/* Fill in backwards to calc amount and delay */
472-
msat = flows[i]->delivers;
473-
delay = finalcltv;
474-
475-
for (int j = tal_count(flows[i]->path) - 1; j >= 0; j--) {
476-
struct route_hop *rh = &r->hops[j];
477-
struct gossmap_node *far_end;
478-
const struct half_chan *h = flow_edge(flows[i], j);
479-
480-
if (!amount_msat_add_fee(&msat, h->base_fee, h->proportional_fee))
481-
plugin_err(rq->plugin, "Adding fee to amount");
482-
delay += h->delay;
483-
484-
rh->scid = gossmap_chan_scid(rq->gossmap, flows[i]->path[j]);
485-
rh->direction = flows[i]->dirs[j];
486-
far_end = gossmap_nth_node(rq->gossmap, flows[i]->path[j], !flows[i]->dirs[j]);
487-
gossmap_node_get_id(rq->gossmap, far_end, &rh->node_id);
488-
rh->amount = msat;
489-
rh->delay = delay;
490-
}
491-
(*amounts)[i] = flows[i]->delivers;
492-
rq_log(tmpctx, rq, LOG_INFORM, "Flow %zu/%zu: %s",
493-
i, tal_count(flows),
494-
fmt_route(tmpctx, r, (*amounts)[i], finalcltv));
495-
}
509+
/* convert flows to routes */
510+
*routes = convert_flows_to_routes(rq, rq, finalcltv, flows, amounts);
511+
assert(tal_count(*routes) == tal_count(flows));
512+
assert(tal_count(*amounts) == tal_count(flows));
496513

514+
/* At last we remove the localmods from the gossmap. */
497515
gossmap_remove_localmods(askrene->gossmap, localmods);
498516
time_delta = timemono_between(time_mono(), time_start);
499517
rq_log(tmpctx, rq, LOG_DBG, "get_routes completed in %" PRIu64 " ms",

0 commit comments

Comments
 (0)