From d5c576bd698228187961cbb13dccd38d49c4f24e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 7 Mar 2024 13:26:36 +1030 Subject: [PATCH] plugins/pay: fix route scoring calculation. 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 --- plugins/libplugin-pay.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index da5926d47..9a3204006 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -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; + double costs; + struct amount_msat msat; - /* Smoothed harmonic mean to avoid division by 0 */ - double costs = (cmsat * rmsat * bias) / (cmsat + rmsat + bias + 1); + /* 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;