mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
pay: Use log probability based bias in channel selection
Changelog-Changed: pay: The route selection will now use the log-propability-based channel selection to increase success rate and reduce time to completion
This commit is contained in:
committed by
Christian Decker
parent
233d339061
commit
0ba1bc30fe
@@ -8,6 +8,7 @@
|
|||||||
#include <common/random_select.h>
|
#include <common/random_select.h>
|
||||||
#include <common/type_to_string.h>
|
#include <common/type_to_string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <math.h>
|
||||||
#include <plugins/libplugin-pay.h>
|
#include <plugins/libplugin-pay.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <wire/peer_wire.h>
|
#include <wire/peer_wire.h>
|
||||||
@@ -707,22 +708,15 @@ static u64 capacity_bias(const struct gossmap *map,
|
|||||||
int dir,
|
int dir,
|
||||||
struct amount_msat amount)
|
struct amount_msat amount)
|
||||||
{
|
{
|
||||||
struct amount_msat fee;
|
|
||||||
struct amount_sat capacity;
|
struct amount_sat capacity;
|
||||||
|
u64 capmsat, amtmsat = amount.millisatoshis; /* Raw: lengthy math */
|
||||||
/* Median fees are 1000 base, 10 ppm, so scale capacity bias to that */
|
|
||||||
/* Overflow is pretty-much impossible, so ignore. */
|
|
||||||
if (!amount_msat_fee(&fee, amount, 1000, 10))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
/* Can fail in theory if gossmap changed underneath. */
|
/* Can fail in theory if gossmap changed underneath. */
|
||||||
if (!gossmap_chan_get_capacity(map, c, &capacity))
|
if (!gossmap_chan_get_capacity(map, c, &capacity))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* bias = fee * (amt / (c + 1)) */
|
capmsat = capacity.satoshis * 1000; /* Raw: lengthy math */
|
||||||
return fee.millisatoshis /* Raw: complex math & laziness */
|
return -log((capmsat + 1 - amtmsat) / (capmsat + 1));
|
||||||
* amount.millisatoshis /* Raw: complex math & laziness */
|
|
||||||
/ (capacity.satoshis*1000 + 1); /* Raw: complex math & laziness */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prioritize costs over distance, but bias to larger channels. */
|
/* Prioritize costs over distance, but bias to larger channels. */
|
||||||
@@ -732,10 +726,13 @@ static u64 route_score(u32 distance,
|
|||||||
int dir,
|
int dir,
|
||||||
const struct gossmap_chan *c)
|
const struct gossmap_chan *c)
|
||||||
{
|
{
|
||||||
u64 costs = cost.millisatoshis + risk.millisatoshis /* Raw: score */
|
u64 cmsat = cost.millisatoshis; /* Raw: lengthy math */
|
||||||
/* We use global_gossmap (can't still be NULL)
|
u64 rmsat = risk.millisatoshis; /* Raw: lengthy math */
|
||||||
* *without* get_gossmap() which might change topology. */
|
u64 bias = capacity_bias(global_gossmap, c, dir, cost);
|
||||||
+ capacity_bias(global_gossmap, c, dir, cost);
|
|
||||||
|
/* Smoothed harmonic mean to avoid division by 0 */
|
||||||
|
u64 costs = (cmsat * rmsat * bias) / (cmsat + rmsat + bias + 1);
|
||||||
|
|
||||||
if (costs > 0xFFFFFFFF)
|
if (costs > 0xFFFFFFFF)
|
||||||
costs = 0xFFFFFFFF;
|
costs = 0xFFFFFFFF;
|
||||||
return costs;
|
return costs;
|
||||||
|
|||||||
Reference in New Issue
Block a user