Skip to content

Commit

Permalink
WIP: Success probability for ProbabilisticScorer
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Jan 25, 2024
1 parent 9e60823 commit 707cc82
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ impl<G: Deref<Target = NetworkGraph<L>> + 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;
Expand Down
35 changes: 34 additions & 1 deletion lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1390,6 +1412,17 @@ impl<G: Deref<Target = NetworkGraph<L>>, 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)
Expand Down

0 comments on commit 707cc82

Please sign in to comment.