From a34974eb8397a8f93c47d0509aaa77ed82bd7346 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 2 Nov 2021 22:06:21 +0000 Subject: [PATCH] (Bindings Only) Seal scorer::Time and only expose the Instant-based Scorer --- lightning/src/routing/scorer.rs | 66 ++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/lightning/src/routing/scorer.rs b/lightning/src/routing/scorer.rs index 8b5fae70a..9cd5dd7fe 100644 --- a/lightning/src/routing/scorer.rs +++ b/lightning/src/routing/scorer.rs @@ -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::; -/// 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 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 { params: ScoringParameters, // TODO: Remove entries of closed channels. channel_failures: HashMap>, } +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::; + /// Parameters for configuring [`Scorer`]. pub struct ScoringParameters { /// A fixed penalty in msats to apply to each channel. @@ -137,20 +155,6 @@ struct ChannelFailure { last_failed: T, } -/// A measurement of time. -pub trait Time: Sub 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 ScorerUsingTime { /// 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 { -- 2.39.5