Add SinceEpoch time to test Scorer hermetically
[rust-lightning] / lightning / src / routing / scorer.rs
index 956b09a54211e4dd90b9d6585c5c0aadb43edc47..9df3fbec81104810f9a894d315d730077ccaa568 100644 (file)
@@ -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<T: Time> Writeable for ScorerUsingTime<T> {
        #[inline]
        fn write<W: Writer>(&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<T: Time> Readable for ScorerUsingTime<T> {
        #[inline]
        fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
-               Ok(Self {
+               let res = Ok(Self {
                        params: Readable::read(r)?,
                        channel_failures: Readable::read(r)?,
-               })
+               });
+               read_tlv_fields!(r, {});
+               res
        }
 }
 
 impl<T: Time> Writeable for ChannelFailure<T> {
        #[inline]
        fn write<W: Writer>(&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<T: Time> Readable for ChannelFailure<T> {
        #[inline]
        fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               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<Duration> = 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<Duration> 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);
+       }
+}