node: Don't panic on initialization error

This commit is contained in:
cloudhead 2024-01-09 14:55:39 +01:00
parent 14b90ffbf9
commit 073c55fa3b
No known key found for this signature in database
3 changed files with 27 additions and 14 deletions

View File

@ -53,6 +53,9 @@ pub enum Error {
/// An address database error. /// An address database error.
#[error("address database error: {0}")] #[error("address database error: {0}")]
Address(#[from] address::Error), Address(#[from] address::Error),
/// A service error.
#[error("service error: {0}")]
Service(#[from] service::Error),
/// An I/O error. /// An I/O error.
#[error("i/o error: {0}")] #[error("i/o error: {0}")]
Io(#[from] io::Error), Io(#[from] io::Error),
@ -209,7 +212,7 @@ impl Runtime {
} }
let emitter: Emitter<Event> = Default::default(); let emitter: Emitter<Event> = Default::default();
let service = service::Service::new( let mut service = service::Service::new(
config, config,
clock, clock,
db, db,
@ -220,9 +223,10 @@ impl Runtime {
announcement, announcement,
emitter.clone(), emitter.clone(),
); );
service.initialize(clock)?;
let (worker_send, worker_recv) = chan::unbounded::<worker::Task>(); 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(); let mut local_addrs = Vec::new();
for addr in listen { for addr in listen {

View File

@ -362,8 +362,8 @@ pub struct Service<D, S, G> {
last_prune: LocalTime, last_prune: LocalTime,
/// Last time the service announced its inventory. /// Last time the service announced its inventory.
last_announce: LocalTime, last_announce: LocalTime,
/// Time when the service was initialized. /// Time when the service was initialized, or `None` if it wasn't initialized.
start_time: LocalTime, started_at: Option<LocalTime>,
/// Publishes events to subscribers. /// Publishes events to subscribers.
emitter: Emitter<Event>, emitter: Emitter<Event>,
} }
@ -421,11 +421,16 @@ where
last_sync: LocalTime::default(), last_sync: LocalTime::default(),
last_prune: LocalTime::default(), last_prune: LocalTime::default(),
last_announce: LocalTime::default(), last_announce: LocalTime::default(),
start_time: LocalTime::default(), started_at: None,
emitter, 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. /// Return the next i/o action to execute.
#[allow(clippy::should_implement_trait)] #[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Option<io::Io> { pub fn next(&mut self) -> Option<io::Io> {
@ -522,7 +527,7 @@ where
debug!(target: "service", "Init @{}", time.as_millis()); debug!(target: "service", "Init @{}", time.as_millis());
let nid = self.node_id(); let nid = self.node_id();
self.start_time = time; self.started_at = Some(time);
// Ensure that our local node is in our address database. // Ensure that our local node is in our address database.
self.db self.db
@ -609,15 +614,22 @@ where
} }
pub fn tick(&mut self, now: LocalTime) { 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; self.clock = now;
} }
pub fn wake(&mut self) { pub fn wake(&mut self) {
let now = self.clock; 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 { if now - self.last_idle >= IDLE_INTERVAL {
trace!(target: "service", "Running 'idle' task..."); trace!(target: "service", "Running 'idle' task...");

View File

@ -338,15 +338,12 @@ where
G: Signer + Ecdh<Pk = NodeId>, G: Signer + Ecdh<Pk = NodeId>,
{ {
pub fn new( pub fn new(
mut service: Service<D, S, G>, service: Service<D, S, G>,
worker: chan::Sender<Task>, worker: chan::Sender<Task>,
signer: G, signer: G,
proxy: net::SocketAddr, proxy: net::SocketAddr,
clock: LocalTime,
) -> Self { ) -> Self {
service assert!(service.started().is_some(), "Service must be initialized");
.initialize(clock)
.expect("Wire::new: error initializing service");
Self { Self {
service, service,