Merge pull request #1338 from TheBlueMatt/2022-02-bindings-105
[rust-lightning] / lightning / src / routing / scoring.rs
index 9f3b7806a991681259deb4ae8655cc060a730382..6b399dec663860621c7039dfd64accf56cfff8c1 100644 (file)
@@ -189,6 +189,32 @@ impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
        }
 }
 
+/// [`Score`] implementation that uses a fixed penalty.
+pub struct FixedPenaltyScorer {
+       penalty_msat: u64,
+}
+
+impl_writeable_tlv_based!(FixedPenaltyScorer, {
+       (0, penalty_msat, required),
+});
+
+impl FixedPenaltyScorer {
+       /// Creates a new scorer using `penalty_msat`.
+       pub fn with_penalty(penalty_msat: u64) -> Self {
+               Self { penalty_msat }
+       }
+}
+
+impl Score for FixedPenaltyScorer {
+       fn channel_penalty_msat(&self, _: u64, _: u64, _: u64, _: &NodeId, _: &NodeId) -> u64 {
+               self.penalty_msat
+       }
+
+       fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
+
+       fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
+}
+
 /// [`Score`] implementation that provides reasonable default behavior.
 ///
 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
@@ -202,12 +228,14 @@ impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
 /// behavior.
 ///
 /// [module-level documentation]: crate::routing::scoring
-pub type Scorer = ScorerUsingTime::<ConfiguredTime>;
-
+#[deprecated(
+       since = "0.0.105",
+       note = "ProbabilisticScorer should be used instead of Scorer.",
+)]
 #[cfg(not(feature = "no-std"))]
-type ConfiguredTime = std::time::Instant;
+pub type Scorer = ScorerUsingTime::<std::time::Instant>;
 #[cfg(feature = "no-std")]
-type ConfiguredTime = time::Eternity;
+pub type Scorer = ScorerUsingTime::<time::Eternity>;
 
 // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
 // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
@@ -216,7 +244,6 @@ type ConfiguredTime = time::Eternity;
 /// [`Score`] implementation.
 ///
 /// (C-not exported) generally all users should use the [`Scorer`] type alias.
-#[doc(hidden)]
 pub struct ScorerUsingTime<T: Time> {
        params: ScoringParameters,
        // TODO: Remove entries of closed channels.
@@ -301,18 +328,6 @@ impl<T: Time> ScorerUsingTime<T> {
                        channel_failures: HashMap::new(),
                }
        }
-
-       /// Creates a new scorer using `penalty_msat` as a fixed channel penalty.
-       #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
-       pub fn with_fixed_penalty(penalty_msat: u64) -> Self {
-               Self::new(ScoringParameters {
-                       base_penalty_msat: penalty_msat,
-                       failure_penalty_msat: 0,
-                       failure_penalty_half_life: Duration::from_secs(0),
-                       overuse_penalty_start_1024th: 1024,
-                       overuse_penalty_msat_per_1024th: 0,
-               })
-       }
 }
 
 impl<T: Time> ChannelFailure<T> {
@@ -470,12 +485,14 @@ impl<T: Time> Readable for ChannelFailure<T> {
 /// behavior.
 ///
 /// [1]: https://arxiv.org/abs/2107.05322
-pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, ConfiguredTime>;
+#[cfg(not(feature = "no-std"))]
+pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, std::time::Instant>;
+#[cfg(feature = "no-std")]
+pub type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, time::Eternity>;
 
 /// Probabilistic [`Score`] implementation.
 ///
 /// (C-not exported) generally all users should use the [`ProbabilisticScorer`] type alias.
-#[doc(hidden)]
 pub struct ProbabilisticScorerUsingTime<G: Deref<Target = NetworkGraph>, T: Time> {
        params: ProbabilisticScoringParameters,
        network_graph: G,