Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: Epoch type
◌ CI pending Fintan Halpenny committed 6 months ago
commit 2ec0e245e2d830d6772a5bbbcfcf5c5ebd5973c3
parent bc6dbfa989e452297d7d8c132f9b7365bc31ca69
1 pending (1 total) View logs
1 file changed +32 -3
modified crates/radicle-node/src/wire.rs
@@ -291,6 +291,35 @@ impl Peers {
    }
}

+
/// The epoch time of when the node started.
+
struct Epoch {
+
    /// The system time when the node started.
+
    started_time: SystemTime,
+
    /// The instant when the node started.
+
    started_at: Instant,
+
}
+

+
impl Epoch {
+
    /// Construct a new [`Epoch`].
+
    fn new(started_time: SystemTime, started_at: Instant) -> Self {
+
        Self {
+
            started_time,
+
            started_at,
+
        }
+
    }
+

+
    /// Construct an [`Epoch`] where both values are recorded using their
+
    /// equivalent `now` constructors.
+
    fn now() -> Self {
+
        Self::new(SystemTime::now(), Instant::now())
+
    }
+

+
    /// Get the elapsed [`SystemTime`] given a later [`Instant`].
+
    fn elapsed_time(&self, later: Instant) -> SystemTime {
+
        self.started_time + (later - self.started_at)
+
    }
+
}
+

/// Wire protocol implementation for a set of peers.
pub(crate) struct Wire<D, S, G: crypto::signature::Signer<crypto::Signature> + Ecdh> {
    /// Backing service instance.
@@ -314,7 +343,7 @@ pub(crate) struct Wire<D, S, G: crypto::signature::Signer<crypto::Signature> + E
    /// A (practically) infinite source of tokens to identify transports and listeners.
    tokens: Tokens,
    /// Record of system time and instant when the node started.
-
    epoch: (SystemTime, Instant),
+
    epoch: Epoch,
}

impl<D, S, G> Wire<D, S, G>
@@ -337,12 +366,12 @@ where
            listening: RandomMap::default(),
            peers: Peers(RandomMap::default()),
            tokens: Tokens::default(),
-
            epoch: (SystemTime::now(), Instant::now()),
+
            epoch: Epoch::now(),
        }
    }

    fn time(&self, instant: Instant) -> SystemTime {
-
        self.epoch.0 + (instant - self.epoch.1)
+
        self.epoch.elapsed_time(instant)
    }

    pub fn listen(&mut self, socket: Listener) {