X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Frouting%2Fscorer.rs;h=9df3fbec81104810f9a894d315d730077ccaa568;hb=2a7d9c8ddd08bcc205eec223a0f35730271de072;hp=956b09a54211e4dd90b9d6585c5c0aadb43edc47;hpb=55fc0a1c10b5c412cb83bac509f7072d239c62c9;p=rust-lightning diff --git a/lightning/src/routing/scorer.rs b/lightning/src/routing/scorer.rs index 956b09a5..9df3fbec 100644 --- a/lightning/src/routing/scorer.rs +++ b/lightning/src/routing/scorer.rs @@ -247,6 +247,7 @@ impl Time for std::time::Instant { } /// A state in which time has no meaning. +#[derive(Debug, PartialEq, Eq)] pub struct Eternity; impl Time for Eternity { @@ -275,34 +276,122 @@ impl Writeable for ScorerUsingTime { #[inline] fn write(&self, w: &mut W) -> Result<(), io::Error> { self.params.write(w)?; - self.channel_failures.write(w) + self.channel_failures.write(w)?; + write_tlv_fields!(w, {}); + Ok(()) } } impl Readable for ScorerUsingTime { #[inline] fn read(r: &mut R) -> Result { - Ok(Self { + let res = Ok(Self { params: Readable::read(r)?, channel_failures: Readable::read(r)?, - }) + }); + read_tlv_fields!(r, {}); + res } } impl Writeable for ChannelFailure { #[inline] fn write(&self, w: &mut W) -> Result<(), io::Error> { - self.undecayed_penalty_msat.write(w)?; - (T::duration_since_epoch() - self.last_failed.elapsed()).write(w) + let duration_since_epoch = T::duration_since_epoch() - self.last_failed.elapsed(); + write_tlv_fields!(w, { + (0, self.undecayed_penalty_msat, required), + (2, duration_since_epoch, required), + }); + Ok(()) } } impl Readable for ChannelFailure { #[inline] fn read(r: &mut R) -> Result { + let mut undecayed_penalty_msat = 0; + let mut duration_since_epoch = Duration::from_secs(0); + read_tlv_fields!(r, { + (0, undecayed_penalty_msat, required), + (2, duration_since_epoch, required), + }); Ok(Self { - undecayed_penalty_msat: Readable::read(r)?, - last_failed: T::now() - (T::duration_since_epoch() - Readable::read(r)?), + undecayed_penalty_msat, + last_failed: T::now() - (T::duration_since_epoch() - duration_since_epoch), }) } } + +#[cfg(test)] +mod tests { + use super::{Eternity, ScoringParameters, ScorerUsingTime, Time}; + + use routing::Score; + use routing::network_graph::NodeId; + + use bitcoin::secp256k1::PublicKey; + use core::cell::Cell; + use core::ops::Sub; + use core::time::Duration; + + /// Time that can be advanced manually in tests. + #[derive(Debug, PartialEq, Eq)] + struct SinceEpoch(Duration); + + impl SinceEpoch { + thread_local! { + static ELAPSED: Cell = core::cell::Cell::new(Duration::from_secs(0)); + } + + fn advance(duration: Duration) { + Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration)) + } + } + + impl Time for SinceEpoch { + fn now() -> Self { + Self(Self::duration_since_epoch()) + } + + fn duration_since_epoch() -> Duration { + Self::ELAPSED.with(|elapsed| elapsed.get()) + } + + fn elapsed(&self) -> Duration { + Self::duration_since_epoch() - self.0 + } + } + + impl Sub for SinceEpoch { + type Output = Self; + + fn sub(self, other: Duration) -> Self { + Self(self.0 - other) + } + } + + #[test] + fn time_passes_when_advanced() { + let now = SinceEpoch::now(); + assert_eq!(now.elapsed(), Duration::from_secs(0)); + + SinceEpoch::advance(Duration::from_secs(1)); + SinceEpoch::advance(Duration::from_secs(1)); + + let elapsed = now.elapsed(); + let later = SinceEpoch::now(); + + assert_eq!(elapsed, Duration::from_secs(2)); + assert_eq!(later - elapsed, now); + } + + #[test] + fn time_never_passes_in_an_eternity() { + let now = Eternity::now(); + let elapsed = now.elapsed(); + let later = Eternity::now(); + + assert_eq!(now.elapsed(), Duration::from_secs(0)); + assert_eq!(later - elapsed, now); + } +}