2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2020-08-26 02:40:49 +02:00
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/opt/opt.h>
|
|
|
|
#include <ccan/time/time.h>
|
|
|
|
#include <common/dijkstra.h>
|
|
|
|
#include <common/gossmap.h>
|
|
|
|
#include <common/route.h>
|
2023-04-12 22:27:46 +02:00
|
|
|
#include <common/setup.h>
|
2020-08-26 02:40:49 +02:00
|
|
|
#include <devtools/clean_topo.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-05-22 09:10:01 +02:00
|
|
|
static struct route_hop *least_cost(struct gossmap *map,
|
|
|
|
struct gossmap_node *src,
|
|
|
|
struct gossmap_node *dst)
|
2020-08-26 02:40:49 +02:00
|
|
|
{
|
|
|
|
const struct dijkstra *dij;
|
|
|
|
u32 srcidx = gossmap_node_idx(map, src);
|
|
|
|
/* 10ksat, budget is 0.5% */
|
|
|
|
const struct amount_msat sent = AMOUNT_MSAT(10000000);
|
|
|
|
const struct amount_msat budget = amount_msat_div(sent, 200);
|
|
|
|
struct amount_msat fee;
|
|
|
|
const u32 riskfactor = 10;
|
|
|
|
/* Max distance is 20 */
|
|
|
|
const u32 distance_budget = ROUTING_MAX_HOPS;
|
|
|
|
struct amount_msat maxcost;
|
2021-05-22 09:10:01 +02:00
|
|
|
struct route_hop *path;
|
2020-08-26 02:40:49 +02:00
|
|
|
struct timemono tstart, tstop;
|
|
|
|
|
|
|
|
setup_locale();
|
|
|
|
setup_tmpctx();
|
|
|
|
|
|
|
|
tstart = time_mono();
|
|
|
|
dij = dijkstra(tmpctx, map, dst,
|
|
|
|
sent, riskfactor, route_can_carry,
|
2020-10-20 05:58:06 +02:00
|
|
|
route_score_cheaper, NULL);
|
2020-08-26 02:40:49 +02:00
|
|
|
tstop = time_mono();
|
|
|
|
|
|
|
|
printf("# Time to find route: %"PRIu64" usec\n",
|
|
|
|
time_to_usec(timemono_between(tstop, tstart)));
|
|
|
|
|
|
|
|
if (dijkstra_distance(dij, srcidx) > distance_budget) {
|
|
|
|
printf("failed (too far)\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!amount_msat_add(&maxcost, sent, budget))
|
|
|
|
abort();
|
2021-05-22 09:10:01 +02:00
|
|
|
path = route_from_dijkstra(map, map, dij, src, sent, 0);
|
|
|
|
if (amount_msat_greater(path[0].amount, maxcost)) {
|
2020-08-26 02:40:49 +02:00
|
|
|
printf("failed (too expensive)\n");
|
2021-05-22 09:10:01 +02:00
|
|
|
return tal_free(path);
|
2020-08-26 02:40:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("# path length %zu\n", tal_count(path));
|
|
|
|
/* We don't pay fee on first hop! */
|
2021-05-22 09:10:01 +02:00
|
|
|
if (!amount_msat_sub(&fee, path[0].amount, sent))
|
2020-08-26 02:40:49 +02:00
|
|
|
abort();
|
|
|
|
printf("# path fee %s\n",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_amount_msat(tmpctx, fee));
|
2020-08-26 02:40:49 +02:00
|
|
|
tal_free(dij);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct timemono tstart, tstop;
|
|
|
|
struct gossmap_node *n, *dst;
|
|
|
|
struct gossmap *map;
|
|
|
|
struct node_id dstid;
|
|
|
|
bool clean_topology = false;
|
2021-08-20 06:12:27 +02:00
|
|
|
size_t num_channel_updates_rejected;
|
2020-08-26 02:40:49 +02:00
|
|
|
|
2023-04-12 22:27:46 +02:00
|
|
|
common_setup(argv[0]);
|
|
|
|
|
2020-08-26 02:40:49 +02:00
|
|
|
opt_register_noarg("--clean-topology", opt_set_bool, &clean_topology,
|
|
|
|
"Clean up topology before run");
|
|
|
|
opt_register_noarg("-h|--help", opt_usage_and_exit,
|
|
|
|
"<gossipstore> <srcid>|all <dstid>\n"
|
|
|
|
"A routing test and benchmark program.",
|
|
|
|
"Get usage information");
|
|
|
|
opt_parse(&argc, argv, opt_log_stderr_exit);
|
|
|
|
if (argc != 4)
|
|
|
|
opt_usage_exit_fail("Expect 3 arguments");
|
|
|
|
|
|
|
|
tstart = time_mono();
|
2021-08-20 06:12:27 +02:00
|
|
|
map = gossmap_load(NULL, argv[1], &num_channel_updates_rejected);
|
2020-08-26 02:40:49 +02:00
|
|
|
if (!map)
|
|
|
|
err(1, "Loading gossip store %s", argv[1]);
|
|
|
|
tstop = time_mono();
|
|
|
|
|
2021-08-20 06:12:27 +02:00
|
|
|
printf("# Time to load: %"PRIu64" msec (%zu ignored)\n",
|
|
|
|
time_to_msec(timemono_between(tstop, tstart)),
|
|
|
|
num_channel_updates_rejected);
|
2020-08-26 02:40:49 +02:00
|
|
|
|
|
|
|
if (clean_topology)
|
|
|
|
clean_topo(map, false);
|
|
|
|
|
|
|
|
if (!node_id_from_hexstr(argv[3], strlen(argv[3]), &dstid))
|
|
|
|
errx(1, "Bad dstid");
|
|
|
|
dst = gossmap_find_node(map, &dstid);
|
|
|
|
if (!dst)
|
|
|
|
errx(1, "Unknown destination node '%s'", argv[3]);
|
|
|
|
|
|
|
|
if (streq(argv[2], "all")) {
|
|
|
|
for (n = gossmap_first_node(map);
|
|
|
|
n;
|
|
|
|
n = gossmap_next_node(map, n)) {
|
2022-07-25 09:00:09 +02:00
|
|
|
struct node_id srcid;
|
2020-08-26 02:40:49 +02:00
|
|
|
|
|
|
|
gossmap_node_get_id(map, n, &srcid);
|
|
|
|
printf("# %s->%s\n",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_node_id(tmpctx, &srcid),
|
|
|
|
fmt_node_id(tmpctx, &dstid));
|
2020-08-26 02:40:49 +02:00
|
|
|
tal_free(least_cost(map, n, dst));
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-22 09:10:01 +02:00
|
|
|
struct route_hop *path;
|
2020-08-26 02:40:49 +02:00
|
|
|
struct node_id srcid;
|
|
|
|
|
|
|
|
if (!node_id_from_hexstr(argv[2], strlen(argv[2]), &srcid))
|
|
|
|
errx(1, "Bad srcid");
|
|
|
|
n = gossmap_find_node(map, &srcid);
|
|
|
|
if (!n)
|
|
|
|
errx(1, "Unknown source node '%s'", argv[2]);
|
|
|
|
path = least_cost(map, n, dst);
|
|
|
|
if (!path)
|
|
|
|
exit(1);
|
|
|
|
for (size_t i = 0; i < tal_count(path); i++) {
|
|
|
|
printf("%s->%s via %s\n",
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_node_id(tmpctx, &srcid),
|
|
|
|
fmt_node_id(tmpctx, &path[i].node_id),
|
|
|
|
fmt_short_channel_id(tmpctx, path[i].scid));
|
2021-05-22 09:10:01 +02:00
|
|
|
srcid = path[i].node_id;
|
2020-08-26 02:40:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_free(map);
|
|
|
|
}
|