Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: Don't panic on initialization error
cloudhead committed 2 years ago
commit 073c55fa3b4657bd2d60ea7fbf0d30cb8f9b0aea
parent 14b90ffbf91595c347376bf2f7f6a772aa19fdb5
3 files changed +27 -14
modified radicle-node/src/runtime.rs
@@ -53,6 +53,9 @@ pub enum Error {
    /// An address database error.
    #[error("address database error: {0}")]
    Address(#[from] address::Error),
+
    /// A service error.
+
    #[error("service error: {0}")]
+
    Service(#[from] service::Error),
    /// An I/O error.
    #[error("i/o error: {0}")]
    Io(#[from] io::Error),
@@ -209,7 +212,7 @@ impl Runtime {
        }

        let emitter: Emitter<Event> = Default::default();
-
        let service = service::Service::new(
+
        let mut service = service::Service::new(
            config,
            clock,
            db,
@@ -220,9 +223,10 @@ impl Runtime {
            announcement,
            emitter.clone(),
        );
+
        service.initialize(clock)?;

        let (worker_send, worker_recv) = chan::unbounded::<worker::Task>();
-
        let mut wire = Wire::new(service, worker_send, signer.clone(), proxy, clock);
+
        let mut wire = Wire::new(service, worker_send, signer.clone(), proxy);
        let mut local_addrs = Vec::new();

        for addr in listen {
modified radicle-node/src/service.rs
@@ -362,8 +362,8 @@ pub struct Service<D, S, G> {
    last_prune: LocalTime,
    /// Last time the service announced its inventory.
    last_announce: LocalTime,
-
    /// Time when the service was initialized.
-
    start_time: LocalTime,
+
    /// Time when the service was initialized, or `None` if it wasn't initialized.
+
    started_at: Option<LocalTime>,
    /// Publishes events to subscribers.
    emitter: Emitter<Event>,
}
@@ -421,11 +421,16 @@ where
            last_sync: LocalTime::default(),
            last_prune: LocalTime::default(),
            last_announce: LocalTime::default(),
-
            start_time: LocalTime::default(),
+
            started_at: None,
            emitter,
        }
    }

+
    /// Whether the service was started (initialized) and if so, at what time.
+
    pub fn started(&self) -> Option<LocalTime> {
+
        self.started_at
+
    }
+

    /// Return the next i/o action to execute.
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> Option<io::Io> {
@@ -522,7 +527,7 @@ where
        debug!(target: "service", "Init @{}", time.as_millis());

        let nid = self.node_id();
-
        self.start_time = time;
+
        self.started_at = Some(time);

        // Ensure that our local node is in our address database.
        self.db
@@ -609,15 +614,22 @@ where
    }

    pub fn tick(&mut self, now: LocalTime) {
-
        trace!(target: "service", "Tick +{}", now - self.start_time);
-

+
        trace!(
+
            target: "service",
+
            "Tick +{}",
+
            now - self.started_at.expect("Service::tick: service must be initialized")
+
        );
        self.clock = now;
    }

    pub fn wake(&mut self) {
        let now = self.clock;

-
        trace!(target: "service", "Wake +{}", now - self.start_time);
+
        trace!(
+
            target: "service",
+
            "Wake +{}",
+
            now - self.started_at.expect("Service::wake: service must be initialized")
+
        );

        if now - self.last_idle >= IDLE_INTERVAL {
            trace!(target: "service", "Running 'idle' task...");
modified radicle-node/src/wire/protocol.rs
@@ -338,15 +338,12 @@ where
    G: Signer + Ecdh<Pk = NodeId>,
{
    pub fn new(
-
        mut service: Service<D, S, G>,
+
        service: Service<D, S, G>,
        worker: chan::Sender<Task>,
        signer: G,
        proxy: net::SocketAddr,
-
        clock: LocalTime,
    ) -> Self {
-
        service
-
            .initialize(clock)
-
            .expect("Wire::new: error initializing service");
+
        assert!(service.started().is_some(), "Service must be initialized");

        Self {
            service,