Skip to content

Commit b1770ba

Browse files
committed
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.
1 parent 8da30df commit b1770ba

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lightning/src/routing/scoring.rs

+34
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> whe
956956
/// with `scid` towards the given `target` node, based on the historical estimated liquidity
957957
/// bounds.
958958
///
959+
/// Returns `None` if there is no (or insufficient) historical data for the given channel (or
960+
/// the provided `target` is not a party to the channel).
961+
///
959962
/// These are the same bounds as returned by
960963
/// [`Self::historical_estimated_channel_liquidity_probabilities`] (but not those returned by
961964
/// [`Self::estimated_channel_liquidity_range`]).
@@ -978,6 +981,37 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ProbabilisticScorer<G, L> whe
978981
}
979982
None
980983
}
984+
985+
/// Query the probability of payment success sending the given `amount_msat` over the channel
986+
/// with `scid` towards the given `target` node, based on the live estimated liquidity bounds.
987+
///
988+
/// This will return `Some` for any channel which is present in the [`NetworkGraph`], including
989+
/// if we have no bound information beside the channel's capacity.
990+
pub fn live_estimated_payment_success_probability(
991+
&self, scid: u64, target: &NodeId, amount_msat: u64, params: &ProbabilisticScoringFeeParameters,
992+
) -> Option<f64> {
993+
let graph = self.network_graph.read_only();
994+
995+
if let Some(chan) = graph.channels().get(&scid) {
996+
if let Some((directed_info, source)) = chan.as_directed_to(target) {
997+
let capacity_msat = directed_info.effective_capacity().as_msat();
998+
let dummy_liq = ChannelLiquidity::new(Duration::ZERO);
999+
let liq = self.channel_liquidities.get(&scid)
1000+
.unwrap_or(&dummy_liq)
1001+
.as_directed(&source, &target, capacity_msat);
1002+
let min_liq = liq.min_liquidity_msat();
1003+
let max_liq = liq.max_liquidity_msat();
1004+
if amount_msat <= liq.min_liquidity_msat() {
1005+
return Some(1.0);
1006+
} else if amount_msat > liq.max_liquidity_msat() {
1007+
return Some(0.0);
1008+
}
1009+
let (num, den) = success_probability(amount_msat, min_liq, max_liq, capacity_msat, &params, false);
1010+
return Some(num as f64 / den as f64);
1011+
}
1012+
}
1013+
None
1014+
}
9811015
}
9821016

9831017
impl ChannelLiquidity {

0 commit comments

Comments
 (0)