From e6cf8c0a2447aeec4174307c2cb5cd371b3794a7 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 7 Mar 2024 13:26:36 +1030 Subject: [PATCH] plugins/pay: fix capacity bias. We attempted to introduce a "capacity bias" so we would penalize channels where we were using a large amount of their total capacity. Unfortunately, this was both naive, and applied wrongly: -log((capmsat + 1 - amtmsat) / (capmsat + 1)); As an integer gives 0 up to about 65% capacity. We then applied this by multiplying: (cmsat * rmsat * bias) / (cmsat + rmsat + bias + 1); Giving a total score of 0 in these cases! If we're using the arithmetic mean we really need to add 1 to bias. We might as well use a double the whole way through, for a slightly more fine-grained approach. Signed-off-by: Rusty Russell --- plugins/libplugin-pay.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index ebf779c07..da5926d47 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -747,10 +747,10 @@ static bool payment_route_can_carry_even_disabled(const struct gossmap *map, * \mu*fee(amt) which is the linearized version which for small amounts and * suitable value of \mu should be good enough) */ -static u64 capacity_bias(const struct gossmap *map, - const struct gossmap_chan *c, - int dir, - struct amount_msat amount) +static double capacity_bias(const struct gossmap *map, + const struct gossmap_chan *c, + int dir, + struct amount_msat amount) { struct amount_sat capacity; u64 amtmsat = amount.millisatoshis; /* Raw: lengthy math */ @@ -773,14 +773,15 @@ static u64 route_score(u32 distance, { u64 cmsat = cost.millisatoshis; /* Raw: lengthy math */ u64 rmsat = risk.millisatoshis; /* Raw: lengthy math */ - u64 bias = capacity_bias(global_gossmap, c, dir, cost); + /* 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 */ - u64 costs = (cmsat * rmsat * bias) / (cmsat + rmsat + bias + 1); + double costs = (cmsat * rmsat * bias) / (cmsat + rmsat + bias + 1); if (costs > 0xFFFFFFFF) - costs = 0xFFFFFFFF; - return costs; + return 0xFFFFFFFF; + return (u64)costs; } static struct route_hop *route(const tal_t *ctx,