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