diff --git a/Cargo.lock b/Cargo.lock index e18e2095ed..4a4c2f5394 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1986,6 +1986,11 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbfc4744c1b8f2a09adc0e55242f60b1af195d88596bd8700be74418c056c555" +[[package]] +name = "dyn-clone" +version = "1.0.14" +source = "git+https://github.com/dtolnay/dyn-clone?tag=1.0.14#cee99471c46f9f512640aa03c680a547ac72c22c" + [[package]] name = "ed25519" version = "2.2.2" @@ -2588,6 +2593,7 @@ dependencies = [ "custom_debug", "dashmap", "derivative", + "dyn-clone 1.0.14", "either", "embed-doc-image", "espresso-systems-common 0.4.1", @@ -2816,6 +2822,7 @@ dependencies = [ "derivative", "digest 0.10.7", "displaydoc", + "dyn-clone 1.0.14", "either", "espresso-systems-common 0.4.1", "ethereum-types", @@ -3329,7 +3336,7 @@ dependencies = [ "derivative", "displaydoc", "downcast-rs", - "dyn-clone", + "dyn-clone 1.0.13", "hashbrown 0.13.2", "itertools 0.10.5", "jf-utils", diff --git a/crates/hotshot/Cargo.toml b/crates/hotshot/Cargo.toml index 671519ec84..34f9a9b63e 100644 --- a/crates/hotshot/Cargo.toml +++ b/crates/hotshot/Cargo.toml @@ -112,6 +112,7 @@ sha3 = "^0.10" snafu = { workspace = true } surf-disco = { workspace = true } time = { workspace = true } +dyn-clone = { git = "https://github.com/dtolnay/dyn-clone", tag = "1.0.14" } tracing = { workspace = true } typenum = { workspace = true } diff --git a/crates/hotshot/src/traits/networking.rs b/crates/hotshot/src/traits/networking.rs index e48e71d29b..3f197d6f94 100644 --- a/crates/hotshot/src/traits/networking.rs +++ b/crates/hotshot/src/traits/networking.rs @@ -9,15 +9,15 @@ pub mod libp2p_network; pub mod memory_network; pub mod web_server_libp2p_fallback; pub mod web_server_network; - +use custom_debug::Debug; +use hotshot_types::traits::metrics::{Counter, Gauge, Metrics}; pub use hotshot_types::traits::network::{ ChannelSendSnafu, CouldNotDeliverSnafu, FailedToDeserializeSnafu, FailedToSerializeSnafu, NetworkError, NetworkReliability, NoSuchNodeSnafu, ShutDownSnafu, }; -use hotshot_types::traits::metrics::{Counter, Gauge, Metrics}; - /// Contains the metrics that we're interested in from the networking interfaces +#[derive(Clone, Debug)] pub struct NetworkingMetrics { #[allow(dead_code)] /// A [`Gauge`] which tracks how many peers are connected diff --git a/crates/hotshot/src/traits/networking/libp2p_network.rs b/crates/hotshot/src/traits/networking/libp2p_network.rs index da8784e0a7..c010f8a8da 100644 --- a/crates/hotshot/src/traits/networking/libp2p_network.rs +++ b/crates/hotshot/src/traits/networking/libp2p_network.rs @@ -1,7 +1,6 @@ //! Libp2p based/production networking implementation //! This module provides a libp2p based networking implementation where each node in the //! network forms a tcp or udp connection to a subset of other nodes in the network - use super::NetworkingMetrics; use crate::NodeImplementation; use async_compatibility_layer::{ @@ -78,6 +77,7 @@ impl Debug for Libp2pNetwork { pub type PeerInfoVec = Arc, Multiaddr)>>>; /// The underlying state of the libp2p network +#[derive(Debug)] struct Libp2pNetworkInner { /// this node's public key pk: K, @@ -320,7 +320,7 @@ impl Libp2pNetwork { let (node_lookup_send, node_lookup_recv) = unbounded(); let (cache_gc_shutdown_send, cache_gc_shutdown_recv) = unbounded::<()>(); - let result = Libp2pNetwork { + let mut result = Libp2pNetwork { inner: Arc::new(Libp2pNetworkInner { handle: network_handle, broadcast_recv, @@ -404,13 +404,14 @@ impl Libp2pNetwork { } /// Initiates connection to the outside world - fn spawn_connect(&self, id: usize) { + fn spawn_connect(&mut self, id: usize) { let pk = self.inner.pk.clone(); let bootstrap_ref = self.inner.bootstrap_addrs.clone(); let num_bootstrap = self.inner.bootstrap_addrs_len; let handle = self.inner.handle.clone(); let is_bootstrapped = self.inner.is_bootstrapped.clone(); let node_type = self.inner.handle.config().node_type; + let metrics_connected_peers = self.inner.clone(); async_spawn({ let is_ready = self.inner.is_ready.clone(); async move { @@ -439,6 +440,11 @@ impl Libp2pNetwork { .await .unwrap(); + let connected_num = handle.num_connected().await?; + metrics_connected_peers + .metrics + .connected_peers + .set(connected_num); while !is_bootstrapped.load(Ordering::Relaxed) { async_sleep(Duration::from_secs(1)).await; } diff --git a/crates/hotshot/src/traits/networking/memory_network.rs b/crates/hotshot/src/traits/networking/memory_network.rs index 51f7bbbdf7..4add5dd222 100644 --- a/crates/hotshot/src/traits/networking/memory_network.rs +++ b/crates/hotshot/src/traits/networking/memory_network.rs @@ -87,6 +87,7 @@ enum Combo { } /// Internal state for a `MemoryNetwork` instance +#[derive(Debug)] struct MemoryNetworkInner { /// Input for broadcast messages broadcast_input: RwLock>>>, diff --git a/crates/types/Cargo.toml b/crates/types/Cargo.toml index 742882cce2..9f65d06657 100644 --- a/crates/types/Cargo.toml +++ b/crates/types/Cargo.toml @@ -49,6 +49,7 @@ time = { workspace = true } tracing = { workspace = true } ethereum-types = { workspace = true } typenum = { workspace = true } +dyn-clone = { git = "https://github.com/dtolnay/dyn-clone", tag = "1.0.14" } [dev-dependencies] serde_json = "1.0.107" diff --git a/crates/types/src/traits/metrics.rs b/crates/types/src/traits/metrics.rs index 4d73e5f67a..e326fca9bc 100644 --- a/crates/types/src/traits/metrics.rs +++ b/crates/types/src/traits/metrics.rs @@ -6,6 +6,7 @@ //! - [`Histogram`]: stores multiple float values based for a graph (example usage: CPU %) //! - [`Label`]: Stores the last string (example usage: current version, network online/offline) +use dyn_clone::DynClone; use std::fmt::Debug; /// The metrics type. @@ -78,12 +79,12 @@ impl Label for NoMetrics { } /// An ever-incrementing counter -pub trait Counter: Send + Sync + Debug { +pub trait Counter: Send + Sync + Debug + DynClone { /// Add a value to the counter fn add(&self, amount: usize); } /// A gauge that stores the latest value. -pub trait Gauge: Send + Sync + Debug { +pub trait Gauge: Send + Sync + Debug + DynClone { /// Set the gauge value fn set(&self, amount: usize); @@ -92,16 +93,20 @@ pub trait Gauge: Send + Sync + Debug { } /// A histogram which will record a series of points. -pub trait Histogram: Send + Sync + Debug { +pub trait Histogram: Send + Sync + Debug + DynClone { /// Add a point to this histogram. fn add_point(&self, point: f64); } /// A label that stores the last string value. -pub trait Label: Send + Sync { +pub trait Label: Send + Sync + DynClone { /// Set the label value fn set(&self, value: String); } +dyn_clone::clone_trait_object!(Gauge); +dyn_clone::clone_trait_object!(Counter); +dyn_clone::clone_trait_object!(Histogram); +dyn_clone::clone_trait_object!(Label); #[cfg(test)] mod test { @@ -111,7 +116,7 @@ mod test { sync::{Arc, Mutex}, }; - #[derive(Debug)] + #[derive(Debug, Clone)] struct TestMetrics { prefix: String, values: Arc>,