From 534b4506503c1ab6955a3a8f4d46b83eee02a3a7 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 21 Nov 2024 22:24:16 +0000 Subject: [PATCH] Add the ability to fetch a probability from live liquidity bounds We already expose the estimated success probability from the historical liquidity bounds from `historical_estimated_payment_success_probability`, but we don't do that for the live liquidity bounds. Here we add a `live_estimated_payment_success_probability` which exposes the probability result from the live liquidity bounds as well. --- lightning/src/routing/scoring.rs | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 3dfc06944..6cb65e3b0 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -956,6 +956,9 @@ impl>, L: Deref> ProbabilisticScorer whe /// with `scid` towards the given `target` node, based on the historical estimated liquidity /// bounds. /// + /// Returns `None` if there is no (or insufficient) historical data for the given channel (or + /// the provided `target` is not a party to the channel). + /// /// These are the same bounds as returned by /// [`Self::historical_estimated_channel_liquidity_probabilities`] (but not those returned by /// [`Self::estimated_channel_liquidity_range`]). @@ -978,6 +981,37 @@ impl>, L: Deref> ProbabilisticScorer whe } None } + + /// Query the probability of payment success sending the given `amount_msat` over the channel + /// with `scid` towards the given `target` node, based on the live estimated liquidity bounds. + /// + /// This will return `Some` for any channel which is present in the [`NetworkGraph`], including + /// if we have no bound information beside the channel's capacity. + pub fn live_estimated_payment_success_probability( + &self, scid: u64, target: &NodeId, amount_msat: u64, params: &ProbabilisticScoringFeeParameters, + ) -> Option { + let graph = self.network_graph.read_only(); + + if let Some(chan) = graph.channels().get(&scid) { + if let Some((directed_info, source)) = chan.as_directed_to(target) { + let capacity_msat = directed_info.effective_capacity().as_msat(); + let dummy_liq = ChannelLiquidity::new(Duration::ZERO); + let liq = self.channel_liquidities.get(&scid) + .unwrap_or(&dummy_liq) + .as_directed(&source, &target, capacity_msat); + let min_liq = liq.min_liquidity_msat(); + let max_liq = liq.max_liquidity_msat(); + if amount_msat < liq.min_liquidity_msat() { + return Some(1.0); + } else if amount_msat > liq.max_liquidity_msat() { + return Some(0.0); + } + let (num, den) = success_probability(amount_msat, min_liq, max_liq, capacity_msat, ¶ms, false); + return Some(num as f64 / den as f64); + } + } + None + } } impl ChannelLiquidity { -- 2.39.5