MCPcopy Create free account
hub / github.com/ElementsProject/lightning / least_cost

Function least_cost

devtools/route.c:14–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12#include <stdio.h>
13
14static struct route_hop *least_cost(struct gossmap *map,
15 struct gossmap_node *src,
16 struct gossmap_node *dst)
17{
18 const struct dijkstra *dij;
19 u32 srcidx = gossmap_node_idx(map, src);
20 /* 10ksat, budget is 0.5% */
21 const struct amount_msat sent = AMOUNT_MSAT(10000000);
22 const struct amount_msat budget = amount_msat_div(sent, 200);
23 struct amount_msat fee;
24 const u32 riskfactor = 10;
25 /* Max distance is 20 */
26 const u32 distance_budget = ROUTING_MAX_HOPS;
27 struct amount_msat maxcost;
28 struct route_hop *path;
29 struct timemono tstart, tstop;
30
31 tstart = time_mono();
32 dij = dijkstra(tmpctx, map, dst,
33 sent, riskfactor, route_can_carry,
34 route_score_cheaper, NULL);
35 tstop = time_mono();
36
37 printf("# Time to find route: %"PRIu64" usec\n",
38 time_to_usec(timemono_between(tstop, tstart)));
39
40 if (dijkstra_distance(dij, srcidx) > distance_budget) {
41 printf("failed (too far)\n");
42 return NULL;
43 }
44 if (!amount_msat_add(&maxcost, sent, budget))
45 abort();
46 path = route_from_dijkstra(map, map, dij, src, sent, 0);
47 if (amount_msat_greater(path[0].amount, maxcost)) {
48 printf("failed (too expensive)\n");
49 return tal_free(path);
50 }
51
52 printf("# path length %zu\n", tal_count(path));
53 /* We don't pay fee on first hop! */
54 if (!amount_msat_sub(&fee, path[0].amount, sent))
55 abort();
56 printf("# path fee %s\n",
57 fmt_amount_msat(tmpctx, fee));
58 tal_free(dij);
59 return path;
60}
61
62int main(int argc, char *argv[])
63{

Callers 1

mainFunction · 0.85

Calls 14

gossmap_node_idxFunction · 0.85
amount_msat_divFunction · 0.85
time_monoFunction · 0.85
time_to_usecFunction · 0.85
timemono_betweenFunction · 0.85
dijkstra_distanceFunction · 0.85
amount_msat_addFunction · 0.85
abortFunction · 0.85
route_from_dijkstraFunction · 0.85
amount_msat_greaterFunction · 0.85
tal_freeFunction · 0.85
amount_msat_subFunction · 0.85

Tested by

no test coverage detected