]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add the ability to fetch a probability from live liquidity bounds 2024-11-live-success-prob-125
authorMatt Corallo <git@bluematt.me>
Thu, 21 Nov 2024 22:24:16 +0000 (22:24 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 22 Nov 2024 14:11:03 +0000 (14:11 +0000)
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

index 61b0086ebc63cb2e71a5d16e3af7588b67b838e9..616801e37ee056afb8f29f6321b85971ca0fc223 100644 (file)
@@ -958,6 +958,9 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> 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`]).
@@ -980,6 +983,37 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> 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<f64> {
+               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, &params, false);
+                               return Some(num as f64 / den as f64);
+                       }
+               }
+               None
+       }
 }
 
 impl ChannelLiquidity {