@@ -565,6 +565,13 @@ struct DirectedChannelLiquidity<L: Deref<Target = u64>, T: Time, U: Deref<Target
565
565
half_life : Duration ,
566
566
}
567
567
568
+ /// The likelihood of an event occurring.
569
+ enum Probability {
570
+ Zero ,
571
+ One ,
572
+ Ratio { numerator : u64 , denominator : u64 } ,
573
+ }
574
+
568
575
impl < G : Deref < Target = NetworkGraph > , T : Time > ProbabilisticScorerUsingTime < G , T > {
569
576
/// Creates a new scorer using the given scoring parameters for sending payments from a node
570
577
/// through a network graph.
@@ -648,18 +655,18 @@ impl<T: Time> ChannelLiquidity<T> {
648
655
impl < L : Deref < Target = u64 > , T : Time , U : Deref < Target = T > > DirectedChannelLiquidity < L , T , U > {
649
656
/// Returns the success probability of routing the given HTLC `amount_msat` through the channel
650
657
/// in this direction.
651
- fn success_probability ( & self , amount_msat : u64 ) -> f64 {
658
+ fn success_probability ( & self , amount_msat : u64 ) -> Probability {
652
659
let max_liquidity_msat = self . max_liquidity_msat ( ) ;
653
660
let min_liquidity_msat = core:: cmp:: min ( self . min_liquidity_msat ( ) , max_liquidity_msat) ;
654
661
if amount_msat > max_liquidity_msat {
655
- 0.0
662
+ Probability :: Zero
656
663
} else if amount_msat <= min_liquidity_msat {
657
- 1.0
664
+ Probability :: One
658
665
} else {
659
666
let numerator = max_liquidity_msat + 1 - amount_msat;
660
667
let denominator = max_liquidity_msat + 1 - min_liquidity_msat;
661
- numerator as f64 / denominator as f64
662
- } . max ( 0.01 ) // Lower bound the success probability to ensure some channel is selected.
668
+ Probability :: Ratio { numerator , denominator }
669
+ }
663
670
}
664
671
665
672
/// Returns the lower bound of the channel liquidity balance in this direction.
@@ -738,10 +745,16 @@ impl<G: Deref<Target = NetworkGraph>, T: Time> Score for ProbabilisticScorerUsin
738
745
. unwrap_or ( & ChannelLiquidity :: new ( ) )
739
746
. as_directed ( source, target, capacity_msat, liquidity_offset_half_life)
740
747
. success_probability ( amount_msat) ;
741
- // NOTE: If success_probability is ever changed to return 0.0, log10 is undefined so return
742
- // u64::max_value instead.
743
- debug_assert ! ( success_probability > core:: f64 :: EPSILON ) ;
744
- ( -( success_probability. log10 ( ) ) * liquidity_penalty_multiplier_msat as f64 ) as u64
748
+ let channel_penalty_msat = match success_probability {
749
+ Probability :: Zero => u64:: max_value ( ) ,
750
+ Probability :: One => 0 ,
751
+ Probability :: Ratio { numerator, denominator } => {
752
+ let success_probability = numerator as f64 / denominator as f64 ;
753
+ ( -( success_probability. log10 ( ) ) * liquidity_penalty_multiplier_msat as f64 ) as u64
754
+ }
755
+ } ;
756
+ // Upper bound the penalty to ensure some channel is selected.
757
+ channel_penalty_msat. min ( 2 * liquidity_penalty_multiplier_msat)
745
758
}
746
759
747
760
fn payment_path_failed ( & mut self , path : & [ & RouteHop ] , short_channel_id : u64 ) {
0 commit comments