From 707cc821b11748d74297925739f7e13ee06252e4 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 25 Jan 2024 16:16:56 -0600 Subject: [PATCH] WIP: Success probability for ProbabilisticScorer --- lightning/src/routing/router.rs | 4 +++- lightning/src/routing/scoring.rs | 35 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 36ea344a21e..8368224df65 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -183,7 +183,9 @@ impl> + Clone, L: Deref, S: Deref, SP: Sized, let amount_msat = amount_msats; let effective_capacity = info.effective_capacity(); let usage = ChannelUsage { amount_msat, inflight_htlc_msat: 0, effective_capacity }; - let success_probability = scorer.channel_success_probability(scid, &info, usage); + let success_probability = scorer.channel_success_probability( + scid, &info, usage, &self.score_params + ); if !success_probability.is_finite() { return None; diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 342fd8c68ef..8f24539a222 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -118,7 +118,8 @@ pub trait ScoreLookUp { /// This is useful to determine whether a channel should be included in a blinded path and the /// preferred ordering of blinded paths. fn channel_success_probability( - &self, _short_channel_id: u64, _info: &DirectedChannelInfo, _usage: ChannelUsage + &self, _short_channel_id: u64, _info: &DirectedChannelInfo, _usage: ChannelUsage, + _score_params: &Self::ScoreParams ) -> f64 { 0.5 } /// Returns how certain any knowledge gained about the channel's liquidity balance is. @@ -1251,6 +1252,27 @@ DirectedChannelLiquidity< L, BRT, T> { liquidity_penalty_msat.saturating_add(amount_penalty_msat) } + fn success_probability( + &self, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters + ) -> f64 { + let amount_msat = usage.amount_msat; + let available_capacity = self.capacity_msat; + let max_liquidity_msat = self.max_liquidity_msat(); + let min_liquidity_msat = core::cmp::min(self.min_liquidity_msat(), max_liquidity_msat); + + if amount_msat <= min_liquidity_msat { + 1.0 + } else if amount_msat >= max_liquidity_msat { + 0.0 + } else { + let (numerator, denominator) = success_probability( + amount_msat, min_liquidity_msat, max_liquidity_msat, available_capacity, + score_params, false + ); + numerator as f64 / denominator as f64 + } + } + /// Returns the lower bound of the channel liquidity balance in this direction. #[inline(always)] fn min_liquidity_msat(&self) -> u64 { @@ -1390,6 +1412,17 @@ impl>, L: Deref> ScoreLookUp for Probabilistic .saturating_add(base_penalty_msat) } + fn channel_success_probability( + &self, short_channel_id: u64, info: &DirectedChannelInfo, usage: ChannelUsage, + score_params: &ProbabilisticScoringFeeParameters + ) -> f64 { + self.channel_liquidities + .get(&short_channel_id) + .unwrap_or(&ChannelLiquidity::new(Duration::ZERO)) + .as_directed(info.source(), info.target(), usage.effective_capacity.as_msat()) + .success_probability(usage, score_params) + } + fn channel_balance_certainty(&self, short_channel_id: u64, info: &DirectedChannelInfo) -> f64 { self.channel_liquidities .get(&short_channel_id)