From 417ab81e2e4522485c3944ddef9f68ab0441029e Mon Sep 17 00:00:00 2001 From: Sishan Long Date: Fri, 22 Sep 2023 15:01:52 -0700 Subject: [PATCH 1/5] Add clone() to NetworkingMetrics --- Cargo.lock | 9 ++++++++- crates/hotshot/Cargo.toml | 1 + crates/hotshot/src/traits/networking.rs | 2 +- .../src/traits/networking/libp2p_network.rs | 9 ++++++--- crates/types/Cargo.toml | 1 + crates/types/src/traits/metrics.rs | 15 ++++++++++----- 6 files changed, 27 insertions(+), 10 deletions(-) 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..017fae77ec 100644 --- a/crates/hotshot/src/traits/networking.rs +++ b/crates/hotshot/src/traits/networking.rs @@ -14,10 +14,10 @@ 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)] 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..1cea7633c1 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::{ @@ -320,7 +319,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 +403,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.metrics.connected_peers.clone(); async_spawn({ let is_ready = self.inner.is_ready.clone(); async move { @@ -439,6 +439,9 @@ impl Libp2pNetwork { .await .unwrap(); + let connected_num = handle.num_connected().await?; + metrics_connected_peers.set(connected_num); + while !is_bootstrapped.load(Ordering::Relaxed) { async_sleep(Duration::from_secs(1)).await; } 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..78aaef7817 100644 --- a/crates/types/src/traits/metrics.rs +++ b/crates/types/src/traits/metrics.rs @@ -7,6 +7,7 @@ //! - [`Label`]: Stores the last string (example usage: current version, network online/offline) use std::fmt::Debug; +use dyn_clone::DynClone; /// The metrics type. pub trait Metrics: Send + Sync { @@ -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>, From d5c07866631c2983b9bbde1c949aea8ef091a675 Mon Sep 17 00:00:00 2001 From: Sishan Long Date: Fri, 22 Sep 2023 16:48:54 -0700 Subject: [PATCH 2/5] fix lint --- crates/hotshot/src/traits/networking.rs | 3 +-- crates/hotshot/src/traits/networking/libp2p_network.rs | 1 - crates/types/src/traits/metrics.rs | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/hotshot/src/traits/networking.rs b/crates/hotshot/src/traits/networking.rs index 017fae77ec..e1ac026546 100644 --- a/crates/hotshot/src/traits/networking.rs +++ b/crates/hotshot/src/traits/networking.rs @@ -9,12 +9,11 @@ pub mod libp2p_network; pub mod memory_network; pub mod web_server_libp2p_fallback; pub mod web_server_network; - +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)] diff --git a/crates/hotshot/src/traits/networking/libp2p_network.rs b/crates/hotshot/src/traits/networking/libp2p_network.rs index 1cea7633c1..ac949a331f 100644 --- a/crates/hotshot/src/traits/networking/libp2p_network.rs +++ b/crates/hotshot/src/traits/networking/libp2p_network.rs @@ -441,7 +441,6 @@ impl Libp2pNetwork { let connected_num = handle.num_connected().await?; metrics_connected_peers.set(connected_num); - while !is_bootstrapped.load(Ordering::Relaxed) { async_sleep(Duration::from_secs(1)).await; } diff --git a/crates/types/src/traits/metrics.rs b/crates/types/src/traits/metrics.rs index 78aaef7817..e326fca9bc 100644 --- a/crates/types/src/traits/metrics.rs +++ b/crates/types/src/traits/metrics.rs @@ -6,8 +6,8 @@ //! - [`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 std::fmt::Debug; use dyn_clone::DynClone; +use std::fmt::Debug; /// The metrics type. pub trait Metrics: Send + Sync { From c1fd4ddda8e5fc7cf0a0e0e2ca9d5384e754410e Mon Sep 17 00:00:00 2001 From: Sishan Long Date: Mon, 25 Sep 2023 15:07:57 -0700 Subject: [PATCH 3/5] add Debug to NetworkingMetrics --- crates/hotshot/src/traits/networking.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/hotshot/src/traits/networking.rs b/crates/hotshot/src/traits/networking.rs index e1ac026546..3f197d6f94 100644 --- a/crates/hotshot/src/traits/networking.rs +++ b/crates/hotshot/src/traits/networking.rs @@ -9,6 +9,7 @@ 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, @@ -16,7 +17,7 @@ pub use hotshot_types::traits::network::{ }; /// Contains the metrics that we're interested in from the networking interfaces -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct NetworkingMetrics { #[allow(dead_code)] /// A [`Gauge`] which tracks how many peers are connected From 5fb921802e4274518febe540ad6c73292c781709 Mon Sep 17 00:00:00 2001 From: Sishan Long Date: Mon, 25 Sep 2023 17:35:17 -0700 Subject: [PATCH 4/5] Add Debug for MemoryNetowkrInner --- crates/hotshot/src/traits/networking/libp2p_network.rs | 5 +++-- crates/hotshot/src/traits/networking/memory_network.rs | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/hotshot/src/traits/networking/libp2p_network.rs b/crates/hotshot/src/traits/networking/libp2p_network.rs index ac949a331f..7654490e2a 100644 --- a/crates/hotshot/src/traits/networking/libp2p_network.rs +++ b/crates/hotshot/src/traits/networking/libp2p_network.rs @@ -77,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, @@ -410,7 +411,7 @@ impl Libp2pNetwork { 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.metrics.connected_peers.clone(); + let metrics_connected_peers = self.inner.clone(); async_spawn({ let is_ready = self.inner.is_ready.clone(); async move { @@ -440,7 +441,7 @@ impl Libp2pNetwork { .unwrap(); let connected_num = handle.num_connected().await?; - metrics_connected_peers.set(connected_num); + 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>>>, From b44750336a9c9784a5ae4468084944996701f5bb Mon Sep 17 00:00:00 2001 From: Sishan Long Date: Mon, 25 Sep 2023 17:36:52 -0700 Subject: [PATCH 5/5] fmt check --- crates/hotshot/src/traits/networking/libp2p_network.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/hotshot/src/traits/networking/libp2p_network.rs b/crates/hotshot/src/traits/networking/libp2p_network.rs index 7654490e2a..c010f8a8da 100644 --- a/crates/hotshot/src/traits/networking/libp2p_network.rs +++ b/crates/hotshot/src/traits/networking/libp2p_network.rs @@ -441,7 +441,10 @@ impl Libp2pNetwork { .unwrap(); let connected_num = handle.num_connected().await?; - metrics_connected_peers.metrics.connected_peers.set(connected_num); + metrics_connected_peers + .metrics + .connected_peers + .set(connected_num); while !is_bootstrapped.load(Ordering::Relaxed) { async_sleep(Duration::from_secs(1)).await; }