Skip to content

Commit 0528924

Browse files
committed
Use enum for channel success probability
The channel success probability can be represented as a ratio, which allows for approximating the log10.
1 parent 5e86bbf commit 0528924

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

lightning/src/routing/scoring.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,13 @@ struct DirectedChannelLiquidity<L: Deref<Target = u64>, T: Time, U: Deref<Target
565565
half_life: Duration,
566566
}
567567

568+
/// The likelihood of an event occurring.
569+
enum Probability {
570+
Zero,
571+
One,
572+
Ratio { numerator: u64, denominator: u64 },
573+
}
574+
568575
impl<G: Deref<Target = NetworkGraph>, T: Time> ProbabilisticScorerUsingTime<G, T> {
569576
/// Creates a new scorer using the given scoring parameters for sending payments from a node
570577
/// through a network graph.
@@ -648,18 +655,18 @@ impl<T: Time> ChannelLiquidity<T> {
648655
impl<L: Deref<Target = u64>, T: Time, U: Deref<Target = T>> DirectedChannelLiquidity<L, T, U> {
649656
/// Returns the success probability of routing the given HTLC `amount_msat` through the channel
650657
/// in this direction.
651-
fn success_probability(&self, amount_msat: u64) -> f64 {
658+
fn success_probability(&self, amount_msat: u64) -> Probability {
652659
let max_liquidity_msat = self.max_liquidity_msat();
653660
let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat);
654661
if amount_msat > max_liquidity_msat {
655-
0.0
662+
Probability::Zero
656663
} else if amount_msat <= min_liquidity_msat {
657-
1.0
664+
Probability::One
658665
} else {
659666
let numerator = max_liquidity_msat + 1 - amount_msat;
660667
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+
}
663670
}
664671

665672
/// 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
738745
.unwrap_or(&ChannelLiquidity::new())
739746
.as_directed(source, target, capacity_msat, liquidity_offset_half_life)
740747
.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)
745758
}
746759

747760
fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {

0 commit comments

Comments
 (0)