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

Function least_cost

devtools/route.c:13–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.85

Calls 15

gossmap_node_idxFunction · 0.85
amount_msat_divFunction · 0.85
setup_localeFunction · 0.85
setup_tmpctxFunction · 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

Tested by

no test coverage detected