X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Frouting%2Fscoring.rs;h=96f90ad4e466e8dbb2f2a46ee3c1d728aa20b78b;hb=1f6b334da9a2ec4af0adaafe19ed5281031c7681;hp=bfcefc96f5b21b56a3d205d501948a7c9651a686;hpb=7adf2c7f5f1e1c4a72108dcc30899a6ed6964c76;p=rust-lightning diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index bfcefc96..96f90ad4 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -63,7 +63,9 @@ use util::time::Time; use prelude::*; use core::fmt; -use core::cell::{RefCell, RefMut}; +#[cfg(not(c_bindings))] +use core::cell::RefCell; +use core::cell::RefMut; use core::ops::{Deref, DerefMut}; use core::time::Duration; use io::{self, Read}; @@ -146,11 +148,13 @@ pub trait LockableScore<'a> { /// /// We need this trait to be able to pass in a scorer to `lightning-background-processor` that will enable us to /// use the Persister to persist it. +#[cfg(not(c_bindings))] // This doesn't make sense in bindings as all `Score`s are `Writeable`. pub trait WriteableScore<'a>: LockableScore<'a> + Writeable {} +#[cfg(not(c_bindings))] // This doesn't make sense in bindings as all `Score`s are `Writeable`. impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {} -/// (C-not exported) +#[cfg(not(c_bindings))] impl<'a, T: 'a + Score> LockableScore<'a> for Mutex { type Locked = MutexGuard<'a, T>; @@ -159,6 +163,7 @@ impl<'a, T: 'a + Score> LockableScore<'a> for Mutex { } } +#[cfg(not(c_bindings))] impl<'a, T: 'a + Score> LockableScore<'a> for RefCell { type Locked = RefMut<'a, T>; @@ -174,14 +179,21 @@ pub struct MultiThreadedLockableScore { } #[cfg(c_bindings)] /// (C-not exported) -impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore { - type Locked = MutexGuard<'a, T>; +impl<'a, S: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore { + type Locked = MutexGuard<'a, S>; - fn lock(&'a self) -> MutexGuard<'a, T> { + fn lock(&'a self) -> MutexGuard<'a, S> { Mutex::lock(&self.score).unwrap() } } +#[cfg(c_bindings)] +impl Writeable for MultiThreadedLockableScore { + fn write(&self, writer: &mut W) -> Result<(), io::Error> { + S::write(&*self.lock(), writer) + } +} + #[cfg(c_bindings)] impl MultiThreadedLockableScore { /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`]. @@ -260,12 +272,30 @@ impl ReadableArgs for FixedPenaltyScorer { } #[cfg(not(feature = "no-std"))] -type ConfiguredTime = std::time::Instant; -#[cfg(feature = "no-std")] -use util::time::Eternity; +/// [`Score`] implementation using channel success probability distributions. +/// +/// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt +/// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability +/// distributions are defined based on knowledge learned from successful and unsuccessful attempts. +/// Then the negative `log10` of the success probability is used to determine the cost of routing a +/// specific HTLC amount through a channel. +/// +/// Knowledge about channel liquidity balances takes the form of upper and lower bounds on the +/// possible liquidity. Certainty of the bounds is decreased over time using a decay function. See +/// [`ProbabilisticScoringParameters`] for details. +/// +/// Since the scorer aims to learn the current channel liquidity balances, it works best for nodes +/// with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment +/// volume are more likely to experience failed payment paths, which would need to be retried. +/// +/// # Note +/// +/// Mixing the `no-std` feature between serialization and deserialization results in undefined +/// behavior. +/// +/// [1]: https://arxiv.org/abs/2107.05322 +pub type ProbabilisticScorer = ProbabilisticScorerUsingTime::; #[cfg(feature = "no-std")] -type ConfiguredTime = Eternity; - /// [`Score`] implementation using channel success probability distributions. /// /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt @@ -288,7 +318,7 @@ type ConfiguredTime = Eternity; /// behavior. /// /// [1]: https://arxiv.org/abs/2107.05322 -pub type ProbabilisticScorer = ProbabilisticScorerUsingTime::; +pub type ProbabilisticScorer = ProbabilisticScorerUsingTime::; /// Probabilistic [`Score`] implementation. ///