(Bindings Only) Seal scorer::Time and only expose the Instant-based Scorer
authorMatt Corallo <git@bluematt.me>
Tue, 2 Nov 2021 22:06:21 +0000 (22:06 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 3 Nov 2021 17:12:02 +0000 (17:12 +0000)
lightning/src/routing/scorer.rs

index 8b5fae70abdbffa34e6a365f312eb654f5165d84..9cd5dd7fe784d4aa2a3b1023d6b3cac912556d7e 100644 (file)
@@ -64,23 +64,27 @@ use core::ops::Sub;
 use core::time::Duration;
 use io::{self, Read};
 
-/// [`routing::Score`] implementation that provides reasonable default behavior.
-///
-/// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
-/// slightly higher fees are available. Will further penalize channels that fail to relay payments.
-///
-/// See [module-level documentation] for usage.
-///
-/// [module-level documentation]: crate::routing::scorer
-pub type Scorer = ScorerUsingTime::<DefaultTime>;
 
-/// Time used by [`Scorer`].
-#[cfg(not(feature = "no-std"))]
-pub type DefaultTime = std::time::Instant;
-
-/// Time used by [`Scorer`].
-#[cfg(feature = "no-std")]
-pub type DefaultTime = Eternity;
+pub(crate) mod sealed {
+       use core::ops::Sub;
+       use core::time::Duration;
+       /// A measurement of time.
+       ///
+       /// Sealed due to C bindings limitations in understanding the `Sub` supertrait.
+       pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
+               /// Returns an instance corresponding to the current moment.
+               fn now() -> Self;
+
+               /// Returns the amount of time elapsed since `self` was created.
+               fn elapsed(&self) -> Duration;
+
+               /// Returns the amount of time passed since the beginning of [`Time`].
+               ///
+               /// Used during (de-)serialization.
+               fn duration_since_epoch() -> Duration;
+       }
+}
+pub(crate) use self::sealed::Time;
 
 /// [`routing::Score`] implementation parameterized by [`Time`].
 ///
@@ -89,12 +93,26 @@ pub type DefaultTime = Eternity;
 /// # Note
 ///
 /// Mixing [`Time`] types between serialization and deserialization results in undefined behavior.
+///
+/// (C-not exported) we export [`Scorer`] below instead.
 pub struct ScorerUsingTime<T: Time> {
        params: ScoringParameters,
        // TODO: Remove entries of closed channels.
        channel_failures: HashMap<u64, ChannelFailure<T>>,
 }
 
+use std::time::Instant;
+
+/// [`routing::Score`] implementation that provides reasonable default behavior.
+///
+/// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
+/// slightly higher fees are available. Will further penalize channels that fail to relay payments.
+///
+/// See [module-level documentation] for usage.
+///
+/// [module-level documentation]: crate::routing::scorer
+pub type Scorer = ScorerUsingTime::<Instant>;
+
 /// Parameters for configuring [`Scorer`].
 pub struct ScoringParameters {
        /// A fixed penalty in msats to apply to each channel.
@@ -137,20 +155,6 @@ struct ChannelFailure<T: Time> {
        last_failed: T,
 }
 
-/// A measurement of time.
-pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
-       /// Returns an instance corresponding to the current moment.
-       fn now() -> Self;
-
-       /// Returns the amount of time elapsed since `self` was created.
-       fn elapsed(&self) -> Duration;
-
-       /// Returns the amount of time passed since the beginning of [`Time`].
-       ///
-       /// Used during (de-)serialization.
-       fn duration_since_epoch() -> Duration;
-}
-
 impl<T: Time> ScorerUsingTime<T> {
        /// Creates a new scorer using the given scoring parameters.
        pub fn new(params: ScoringParameters) -> Self {
@@ -247,6 +251,8 @@ impl Time for std::time::Instant {
 }
 
 /// A state in which time has no meaning.
+///
+/// (C-not exported)
 pub struct Eternity;
 
 impl Time for Eternity {