diff --git a/crates/topos-p2p/src/behaviour.rs b/crates/topos-p2p/src/behaviour.rs index 75b5e6c35..6d666d969 100644 --- a/crates/topos-p2p/src/behaviour.rs +++ b/crates/topos-p2p/src/behaviour.rs @@ -9,7 +9,7 @@ pub(crate) mod peer_info; pub(crate) mod topos; /// Represents the health status of a behaviour inside the p2p layer -#[derive(Default, PartialEq, Eq)] +#[derive(Debug, Default, PartialEq, Eq)] pub(crate) enum HealthStatus { #[default] Initializing, diff --git a/crates/topos-p2p/src/behaviour/discovery.rs b/crates/topos-p2p/src/behaviour/discovery.rs index 67a18aee4..cd1962890 100644 --- a/crates/topos-p2p/src/behaviour/discovery.rs +++ b/crates/topos-p2p/src/behaviour/discovery.rs @@ -80,7 +80,10 @@ impl DiscoveryBehaviour { next_bootstrap_query: if known_peers.is_empty() { None } else { - Some(Box::pin(tokio::time::interval(config.bootstrap_interval))) + let mut interval = tokio::time::interval(config.bootstrap_interval); + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + + Some(Box::pin(interval)) }, health_status: if known_peers.is_empty() { HealthStatus::Healthy @@ -105,9 +108,14 @@ impl DiscoveryBehaviour { } /// Change the interval of the next bootstrap queries - pub fn change_interval(&mut self, duration: Duration) -> Result<(), P2PError> { + pub async fn change_interval(&mut self, duration: Duration) -> Result<(), P2PError> { if let Some(interval) = self.next_bootstrap_query.as_mut() { - interval.set(tokio::time::interval(duration)); + let mut new_interval = tokio::time::interval(duration); + // Delay the next tick + new_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + // ignore first tick + _ = new_interval.tick().await; + interval.set(new_interval); } Ok(()) diff --git a/crates/topos-p2p/src/runtime/handle_event.rs b/crates/topos-p2p/src/runtime/handle_event.rs index 0f5dff1f8..596859cd1 100644 --- a/crates/topos-p2p/src/runtime/handle_event.rs +++ b/crates/topos-p2p/src/runtime/handle_event.rs @@ -197,6 +197,7 @@ impl EventHandler<SwarmEvent<ComposedEvent>> for Runtime { _ = self.event_sender.send(event).await; } + info!("Healthystatus: {:?}", self.health_status); Ok(()) } } diff --git a/crates/topos-p2p/src/runtime/handle_event/discovery.rs b/crates/topos-p2p/src/runtime/handle_event/discovery.rs index c08195829..c82863046 100644 --- a/crates/topos-p2p/src/runtime/handle_event/discovery.rs +++ b/crates/topos-p2p/src/runtime/handle_event/discovery.rs @@ -43,17 +43,34 @@ impl EventHandler<Box<Event>> for Runtime { } if num_remaining == 0 && self.swarm.behaviour().discovery.health_status == HealthStatus::Initializing => { - warn!( - "Bootstrap query finished but unable to connect to bootnode during \ - initialization, switching to unhealthy and fast bootstrap mode", - ); - - let behaviour = self.swarm.behaviour_mut(); - - behaviour.discovery.health_status = HealthStatus::Unhealthy; - _ = behaviour - .discovery - .change_interval(self.config.discovery.fast_bootstrap_interval); + if self + .health_state + .successfully_connected_to_bootpeer + .is_none() + { + warn!( + "Bootstrap query finished but unable to connect to bootnode during \ + initialization, switching from discovery(initializing) -> \ + discover(unhealthy) and fast bootstrap mode", + ); + + let behaviour = self.swarm.behaviour_mut(); + + behaviour.discovery.health_status = HealthStatus::Unhealthy; + _ = behaviour + .discovery + .change_interval(self.config.discovery.fast_bootstrap_interval) + .await; + } else { + warn!( + "Bootstrap query finished with bootnode, switching from \ + discovery(initializing) -> discovery(healthy)", + ); + + let behaviour = self.swarm.behaviour_mut(); + + behaviour.discovery.health_status = HealthStatus::Healthy; + } } Event::OutboundQueryProgressed { @@ -107,15 +124,16 @@ impl EventHandler<Box<Event>> for Runtime { && self.swarm.behaviour().discovery.health_status == HealthStatus::Unhealthy => { info!( - "Bootstrap query finished with bootnode, switching to healthy and normal \ - bootstrap mode", + "Bootstrap query finished with bootnode, switching discover(unhealthy) -> \ + discover(healthy) and normal bootstrap mode", ); let behaviour = self.swarm.behaviour_mut(); behaviour.discovery.health_status = HealthStatus::Healthy; _ = behaviour .discovery - .change_interval(self.config.discovery.bootstrap_interval); + .change_interval(self.config.discovery.bootstrap_interval) + .await; } Event::OutboundQueryProgressed { diff --git a/crates/topos-tce-synchronizer/src/checkpoints_collector/tests/integration.rs b/crates/topos-tce-synchronizer/src/checkpoints_collector/tests/integration.rs index c6ebb71cb..3ea5a07c9 100644 --- a/crates/topos-tce-synchronizer/src/checkpoints_collector/tests/integration.rs +++ b/crates/topos-tce-synchronizer/src/checkpoints_collector/tests/integration.rs @@ -44,7 +44,6 @@ async fn network_test() { .bootstrap(&[cfg.clone(), boot_node.clone()], None) .await .unwrap(); - use topos_core::api::grpc::shared::v1::Uuid as APIUuid; let peer = boot_node.keypair.public().to_peer_id();