Skip to content

Commit a24f363

Browse files
committed
added probabilistic path finding and harmonic mean. not tested yet
1 parent 3a09397 commit a24f363

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

plugins/libplugin-pay.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <common/random_select.h>
99
#include <common/type_to_string.h>
1010
#include <errno.h>
11+
#include <math.h>
1112
#include <plugins/libplugin-pay.h>
1213
#include <sys/types.h>
1314
#include <wire/peer_wire.h>
@@ -700,25 +701,29 @@ static bool payment_route_can_carry_even_disabled(const struct gossmap *map,
700701
*/
701702
static u64 capacity_bias(const struct gossmap *map,
702703
const struct gossmap_chan *c,
703-
int dir,
704+
int dir,//TODO: seems not to be used
704705
struct amount_msat amount)
705706
{
706-
struct amount_msat fee;
707+
//struct amount_msat fee;
707708
struct amount_sat capacity;
708709

709710
/* Median fees are 1000 base, 10 ppm, so scale capacity bias to that */
710711
/* Overflow is pretty-much impossible, so ignore. */
711-
if (!amount_msat_fee(&fee, amount, 1000, 10))
712-
return 0;
712+
//TODO: don't have median results as fixed constants in the code
713+
//if (!amount_msat_fee(&fee, amount, 1000, 10))
714+
// return 0;
713715

714716
/* Can fail in theory if gossmap changed underneath. */
715717
if (!gossmap_chan_get_capacity(map, c, &capacity))
716718
return 0;
717719

720+
//negative log prob:
721+
return -log((capacity.satoshis*1000 + 1 - amount.millisatoshis)
722+
/ (capacity.satoshi*1000 + 1));
718723
/* bias = fee * (amt / (c + 1)) */
719-
return fee.millisatoshis /* Raw: complex math & laziness */
720-
* amount.millisatoshis /* Raw: complex math & laziness */
721-
/ (capacity.satoshis*1000 + 1); /* Raw: complex math & laziness */
724+
// return fee.millisatoshis /* Raw: complex math & laziness */
725+
// * amount.millisatoshis /* Raw: complex math & laziness */
726+
// / (capacity.satoshis*1000 + 1); /* Raw: complex math & laziness */
722727
}
723728

724729
/* Prioritize costs over distance, but bias to larger channels. */
@@ -728,10 +733,15 @@ static u64 route_score(u32 distance,
728733
int dir,
729734
const struct gossmap_chan *c)
730735
{
731-
u64 costs = cost.millisatoshis + risk.millisatoshis /* Raw: score */
736+
//u64 costs = cost.millisatoshis + risk.millisatoshis /* Raw: score */
732737
/* We use global_gossmap (can't still be NULL)
733738
* *without* get_gossmap() which might change topology. */
734-
+ capacity_bias(global_gossmap, c, dir, cost);
739+
// + capacity_bias(global_gossmap, c, dir, cost);
740+
//smoothed harmonic mean to avoid division by 1
741+
u64 costs = cost.millisatoshis*risk.millisatoshis
742+
*capacity_bias(global_gossmap,c,dir,cost)
743+
/(cost.millisatoshis+risk.millisatoshis
744+
+capacity_bias(global_gossmap,c,dir,cost)+1);
735745
if (costs > 0xFFFFFFFF)
736746
costs = 0xFFFFFFFF;
737747
return costs;

0 commit comments

Comments
 (0)