Skip to content

Commit

Permalink
plugins/pay: fix route scoring calculation.
Browse files Browse the repository at this point in the history
It gave 0.  A lot.

Firstly, rmsat was often very small, because delays are often small.   Much
smaller than the actual fee.

We really just want to offset the bias and multiply it.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Mar 7, 2024
1 parent e6cf8c0 commit d5c576b
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions plugins/libplugin-pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,14 +771,29 @@ static u64 route_score(u32 distance,
int dir,
const struct gossmap_chan *c)
{
u64 cmsat = cost.millisatoshis; /* Raw: lengthy math */
u64 rmsat = risk.millisatoshis; /* Raw: lengthy math */
/* We multiply this, so 1 is neutral, higher is a penalty. */
double bias = capacity_bias(global_gossmap, c, dir, cost) + 1;

/* Smoothed harmonic mean to avoid division by 0 */
double costs = (cmsat * rmsat * bias) / (cmsat + rmsat + bias + 1);

double costs;
struct amount_msat msat;

/* These two are comparable, so simply sum them. */
if (!amount_msat_add(&msat, cost, risk))
msat = AMOUNT_MSAT(-1ULL);

/* Slight tiebreaker bias: 1 msat per distance */
if (!amount_msat_add(&msat, msat, amount_msat(distance)))
msat = AMOUNT_MSAT(-1ULL);

/* Percent penalty at different channel capacities:
* 1%: 1%
* 10%: 11%
* 25%: 29%
* 50%: 69%
* 75%: 138%
* 90%: 230%
* 95%: 300%
* 99%: 461%
*/
costs = (capacity_bias(global_gossmap, c, dir, cost) + 1)
* msat.millisatoshis; /* Raw: Weird math */
if (costs > 0xFFFFFFFF)
return 0xFFFFFFFF;
return (u64)costs;
Expand Down

0 comments on commit d5c576b

Please sign in to comment.