core-lightning/common/dijkstra.h
Rusty Russell ffb324f283 common: fix dijkstra scoring.
The "path_score" callback was supposed to evaluate the *entire path*,
but that was counter-intuitive and opened the door to a cost function
bug which caused this path cost to be less than the closer path.

In particular, the capacity bias code didn't understand this at all.

1. Rename the function to `channel_score` and remove the "distance"
   parameter (always "1" since you're supposed to be evaluating a
   single hop).
2. Rename "cost" to the more specific "fee": "score" is our
   actual cost function result (we avoid the word "cost" as it
   may get confused with satoshi amounts).
3. For capacity biassing, we do want to know the amount, but
   explicitly hand that as a separate parameter "total".
4. Fix a minor bug where total handed to scoring function previously
   included channel fee (this is wrong: fee is paid before sending into
   channel).
5. Remove the now-unused total_delay member from the dijkstra
   struct.

Here are the results of our test now (routing 4194303 msat, which
didn't crash the old code, so we could compare).  In both cases
we could find routes to 615 nodes:

Linear success probability (when found): min-max(mean +/- stddev)
	Before: 0.484764-0.999750(0.9781+/-0.049)
	After:  0.487040-0.999543(0.952548+/-0.075)

Hops:
	Before: 1-5(2.13821+/-0.66)
	After:  1-5(2.98374+/-0.77)

Fees:
	Before: 0-50041(2173.75+/-5.3e+03)
	After:  0-50848(922.457+/-2.7e+03)

Delay (blocks):
	Before: 0-294(83.1642+/-68)
	After:  0-196(65.8081+/-60)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Fixes: https://github.com/ElementsProject/lightning/issues/7092
Changelog-Fixed: Plugins: `pay` would occasionally crash on routing.
Changelog-Fixed: Plugins: `pay` route algorithm fixed and refined to balance fees and capacity far better.
2024-03-07 14:09:14 +01:00

48 lines
1.4 KiB
C

#ifndef LIGHTNING_COMMON_DIJKSTRA_H
#define LIGHTNING_COMMON_DIJKSTRA_H
#include "config.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),
u64 (*channel_score)(struct amount_msat fee,
struct amount_msat risk,
/* We want to know this to compare with channel capacity */
struct amount_msat total,
int dir,
const struct gossmap_chan *c),
void *arg);
#define dijkstra(ctx, map, start, amount, riskfactor, channel_ok, \
channel_score, 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), \
(channel_score), \
(arg))
/* Returns UINT_MAX if unreachable. */
u32 dijkstra_distance(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 */