@@ -691,6 +691,52 @@ static bool payment_route_can_carry_even_disabled(const struct gossmap *map,
691
691
return payment_route_check (map , c , dir , amount , p );
692
692
}
693
693
694
+ /* Rene Pickhardt:
695
+ *
696
+ * Btw the linear term of the Taylor series of -log((c+1-x)/(c+1)) is 1/(c+1)
697
+ * meaning that another suitable Weight for Dijkstra would be amt/(c+1) +
698
+ * \mu*fee(amt) which is the linearized version which for small amounts and
699
+ * suitable value of \mu should be good enough)
700
+ */
701
+ static u64 capacity_bias (const struct gossmap * map ,
702
+ const struct gossmap_chan * c ,
703
+ int dir ,
704
+ struct amount_msat amount )
705
+ {
706
+ struct amount_msat fee ;
707
+ struct amount_sat capacity ;
708
+
709
+ /* Median fees are 1000 base, 10 ppm, so scale capacity bias to that */
710
+ /* Overflow is pretty-much impossible, so ignore. */
711
+ if (!amount_msat_fee (& fee , amount , 1000 , 10 ))
712
+ return 0 ;
713
+
714
+ /* Can fail in theory if gossmap changed underneath. */
715
+ if (!gossmap_chan_get_capacity (map , c , & capacity ))
716
+ return 0 ;
717
+
718
+ /* 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 */
722
+ }
723
+
724
+ /* Prioritize costs over distance, but bias to larger channels. */
725
+ static u64 route_score (u32 distance ,
726
+ struct amount_msat cost ,
727
+ struct amount_msat risk ,
728
+ int dir ,
729
+ const struct gossmap_chan * c )
730
+ {
731
+ u64 costs = cost .millisatoshis + risk .millisatoshis /* Raw: score */
732
+ /* We use global_gossmap (can't still be NULL)
733
+ * *without* get_gossmap() which might change topology. */
734
+ + capacity_bias (global_gossmap , c , dir , cost );
735
+ if (costs > 0xFFFFFFFF )
736
+ costs = 0xFFFFFFFF ;
737
+ return costs ;
738
+ }
739
+
694
740
static struct route_hop * route (const tal_t * ctx ,
695
741
struct gossmap * gossmap ,
696
742
const struct gossmap_node * src ,
@@ -712,14 +758,14 @@ static struct route_hop *route(const tal_t *ctx,
712
758
713
759
can_carry = payment_route_can_carry ;
714
760
dij = dijkstra (tmpctx , gossmap , dst , amount , riskfactor ,
715
- can_carry , route_score_cheaper , p );
761
+ can_carry , route_score , p );
716
762
r = route_from_dijkstra (ctx , gossmap , dij , src , amount , final_delay );
717
763
if (!r ) {
718
764
/* Try using disabled channels too */
719
765
/* FIXME: is there somewhere we can annotate this for paystatus? */
720
766
can_carry = payment_route_can_carry_even_disabled ;
721
767
dij = dijkstra (ctx , gossmap , dst , amount , riskfactor ,
722
- can_carry , route_score_cheaper , p );
768
+ can_carry , route_score , p );
723
769
r = route_from_dijkstra (ctx , gossmap , dij , src ,
724
770
amount , final_delay );
725
771
if (!r ) {
0 commit comments