core-lightning/common/dijkstra.h
Rusty Russell 59cc2f7559 common/dijkstra: routine to calculate shortest/cheapest path.
The user supplies callbacks to do channel selection and comparison.
Note that this continues to map the entire network; not just to the
source, for use with random routing.

Benchmarks: (using current mainnet gossip store)
	/devtools/route gossip-store-2020-07-27 all 03c981ed4ad15837f29a212dc8cf4b31f274105b7c95274a41449bf496ebd2fe10 | grep 'Time to find path'

With nothing (i.e. DEVELOPER build)
	Averages 17ms

With -Og (i.e. standard non-DEVELOPER build)
	Averages 14ms

With -O3 -flto:
	Averages 4ms

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-28 10:56:50 +09:30

62 lines
1.9 KiB
C

#ifndef LIGHTNING_COMMON_DIJKSTRA_H
#define LIGHTNING_COMMON_DIJKSTRA_H
#include "config.h"
#include <ccan/tal/tal.h>
#include <ccan/typesafe_cb/typesafe_cb.h>
#include <common/amount.h>
struct gossmap;
struct gossmap_chan;
struct gossmap_node;
/* Do Dijkstra: start in this case is the dst node. */
const struct dijkstra *
dijkstra_(const tal_t *ctx,
const struct gossmap *gossmap,
const struct gossmap_node *start,
struct amount_msat amount,
double riskfactor,
bool (*channel_ok)(const struct gossmap *map,
const struct gossmap_chan *c,
int dir,
struct amount_msat amount,
void *arg),
bool (*path_better)(u32 old_distance,
u32 new_distance,
struct amount_msat old_cost,
struct amount_msat new_cost,
struct amount_msat old_risk,
struct amount_msat new_risk,
void *arg),
void *arg);
#define dijkstra(ctx, map, start, amount, riskfactor, channel_ok, \
path_better, arg) \
dijkstra_((ctx), (map), (start), (amount), (riskfactor), \
typesafe_cb_preargs(bool, void *, (channel_ok), (arg), \
const struct gossmap *, \
const struct gossmap_chan *, \
int, struct amount_msat), \
typesafe_cb_preargs(bool, void *, (path_better), (arg), \
u32, u32, \
struct amount_msat, \
struct amount_msat, \
struct amount_msat, \
struct amount_msat), \
(arg))
/* Returns UINT_MAX if unreachable. */
u32 dijkstra_distance(const struct dijkstra *dij, u32 node_idx);
/* Total CLTV delay (0 if unreachable) */
u32 dijkstra_delay(const struct dijkstra *dij, u32 node_idx);
/* Total cost to get here (-1ULL if unreachable) */
struct amount_msat dijkstra_amount(const struct dijkstra *dij, u32 node_idx);
/* Best path we found to here */
struct gossmap_chan *dijkstra_best_chan(const struct dijkstra *dij,
u32 node_idx);
#endif /* LIGHTNING_COMMON_DIJKSTRA_H */