diff --git a/Cargo.lock b/Cargo.lock index af5d63e97f4..2cdfb093e1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -955,6 +955,7 @@ dependencies = [ "hyper 1.6.0", "lighthouse_network", "monitoring_api", + "network_utils", "node_test_rig", "sensitive_url", "serde_json", @@ -964,7 +965,6 @@ dependencies = [ "task_executor", "tracing", "types", - "unused_port", ] [[package]] @@ -1224,6 +1224,7 @@ dependencies = [ "lighthouse_network", "log", "logging", + "network_utils", "serde", "tokio", "tracing", @@ -3287,6 +3288,7 @@ dependencies = [ "futures", "hex", "logging", + "network_utils", "reqwest", "sensitive_url", "serde_json", @@ -3294,7 +3296,6 @@ dependencies = [ "tempfile", "tokio", "types", - "unused_port", ] [[package]] @@ -4292,6 +4293,7 @@ dependencies = [ "lru", "metrics", "network", + "network_utils", "operation_pool", "parking_lot 0.12.3", "proto_array", @@ -4326,6 +4328,7 @@ dependencies = [ "logging", "malloc_utils", "metrics", + "network_utils", "reqwest", "serde", "slot_clock", @@ -5071,6 +5074,7 @@ dependencies = [ "lighthouse_version", "log", "malloc_utils", + "network_utils", "rayon", "serde", "serde_json", @@ -5637,6 +5641,7 @@ dependencies = [ "logging", "malloc_utils", "metrics", + "network_utils", "sensitive_url", "serde", "serde_json", @@ -5649,7 +5654,6 @@ dependencies = [ "tracing", "tracing-subscriber", "types", - "unused_port", "validator_client", "validator_dir", "validator_manager", @@ -5685,6 +5689,7 @@ dependencies = [ "lru", "lru_cache", "metrics", + "network_utils", "parking_lot 0.12.3", "prometheus-client", "quickcheck", @@ -5700,7 +5705,6 @@ dependencies = [ "superstruct", "task_executor", "tempfile", - "tiny-keccak", "tokio", "tokio-io-timeout", "tokio-util", @@ -5708,7 +5712,6 @@ dependencies = [ "tracing-subscriber", "types", "unsigned-varint 0.8.0", - "unused_port", ] [[package]] @@ -6400,6 +6403,21 @@ dependencies = [ "types", ] +[[package]] +name = "network_utils" +version = "0.1.0" +dependencies = [ + "discv5", + "hex", + "libp2p-identity", + "lru_cache", + "metrics", + "multiaddr", + "parking_lot 0.12.3", + "serde", + "tiny-keccak", +] + [[package]] name = "nix" version = "0.24.3" @@ -9077,6 +9095,8 @@ name = "system_health" version = "0.1.0" dependencies = [ "lighthouse_network", + "metrics", + "network_utils", "parking_lot 0.12.3", "serde", "sysinfo", @@ -9885,14 +9905,6 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" -[[package]] -name = "unused_port" -version = "0.1.0" -dependencies = [ - "lru_cache", - "parking_lot 0.12.3", -] - [[package]] name = "url" version = "2.5.4" diff --git a/Cargo.toml b/Cargo.toml index 817c2f2d802..2de5cff8486 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ members = [ "common/malloc_utils", "common/metrics", "common/monitoring_api", + "common/network_utils", "common/oneshot_broadcast", "common/pretty_reqwest_error", "common/sensitive_url", @@ -44,7 +45,6 @@ members = [ "common/target_check", "common/task_executor", "common/test_random_derive", - "common/unused_port", "common/validator_dir", "common/warp_utils", "common/workspace_members", @@ -190,6 +190,7 @@ mockall_double = "0.3" mockito = "1.5.0" monitoring_api = { path = "common/monitoring_api" } network = { path = "beacon_node/network" } +network_utils = { path = "common/network_utils" } node_test_rig = { path = "testing/node_test_rig" } num_cpus = "1" once_cell = "1.17.1" @@ -257,7 +258,6 @@ tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] } tree_hash = "0.10.0" tree_hash_derive = "0.10.0" types = { path = "consensus/types" } -unused_port = { path = "common/unused_port" } url = "2" uuid = { version = "0.8", features = ["serde", "v4"] } validator_client = { path = "validator_client" } diff --git a/beacon_node/Cargo.toml b/beacon_node/Cargo.toml index 456376e79bd..dd7416af540 100644 --- a/beacon_node/Cargo.toml +++ b/beacon_node/Cargo.toml @@ -33,6 +33,7 @@ http_api = { workspace = true } hyper = { workspace = true } lighthouse_network = { workspace = true } monitoring_api = { workspace = true } +network_utils = { workspace = true } sensitive_url = { workspace = true } serde_json = { workspace = true } slasher = { workspace = true } @@ -41,7 +42,6 @@ strum = { workspace = true } task_executor = { workspace = true } tracing = { workspace = true } types = { workspace = true } -unused_port = { workspace = true } [dev-dependencies] node_test_rig = { path = "../testing/node_test_rig" } diff --git a/beacon_node/http_api/Cargo.toml b/beacon_node/http_api/Cargo.toml index 781a4cfa44e..3ff47ff7c67 100644 --- a/beacon_node/http_api/Cargo.toml +++ b/beacon_node/http_api/Cargo.toml @@ -25,6 +25,7 @@ logging = { workspace = true } lru = { workspace = true } metrics = { workspace = true } network = { workspace = true } +network_utils = { workspace = true } operation_pool = { workspace = true } parking_lot = { workspace = true } proto_array = { workspace = true } diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index cacdd4a44c5..d2e5d16df0a 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -54,10 +54,11 @@ use eth2::types::{ use eth2::{CONSENSUS_VERSION_HEADER, CONTENT_TYPE_HEADER, SSZ_CONTENT_TYPE_HEADER}; use health_metrics::observe::Observe; use lighthouse_network::rpc::methods::MetaData; -use lighthouse_network::{types::SyncState, Enr, EnrExt, NetworkGlobals, PeerId, PubsubMessage}; +use lighthouse_network::{types::SyncState, Enr, NetworkGlobals, PeerId, PubsubMessage}; use lighthouse_version::version_with_platform; use logging::{crit, SSELoggingComponents}; use network::{NetworkMessage, NetworkSenders, ValidatorSubscriptionMessage}; +use network_utils::enr_ext::EnrExt; use operation_pool::ReceivedPreCapella; use parking_lot::RwLock; pub use publish_blocks::{ diff --git a/beacon_node/http_api/tests/tests.rs b/beacon_node/http_api/tests/tests.rs index ecd20f3f79c..e88d96095de 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -24,8 +24,9 @@ use http_api::{ test_utils::{create_api_server, ApiServer}, BlockId, StateId, }; -use lighthouse_network::{types::SyncState, Enr, EnrExt, PeerId}; +use lighthouse_network::{types::SyncState, Enr, PeerId}; use network::NetworkReceivers; +use network_utils::enr_ext::EnrExt; use operation_pool::attestation_storage::CheckpointKey; use proto_array::ExecutionStatus; use sensitive_url::SensitiveUrl; diff --git a/beacon_node/http_metrics/Cargo.toml b/beacon_node/http_metrics/Cargo.toml index e12053ac43b..b74c04a4cb6 100644 --- a/beacon_node/http_metrics/Cargo.toml +++ b/beacon_node/http_metrics/Cargo.toml @@ -13,6 +13,7 @@ lighthouse_version = { workspace = true } logging = { workspace = true } malloc_utils = { workspace = true } metrics = { workspace = true } +network_utils = { workspace = true } serde = { workspace = true } slot_clock = { workspace = true } store = { workspace = true } diff --git a/beacon_node/http_metrics/src/metrics.rs b/beacon_node/http_metrics/src/metrics.rs index bcfb8e4c9cf..786e14971fa 100644 --- a/beacon_node/http_metrics/src/metrics.rs +++ b/beacon_node/http_metrics/src/metrics.rs @@ -37,7 +37,7 @@ pub fn gather_prometheus_metrics( store::scrape_for_metrics(db_path, freezer_db_path); } - lighthouse_network::scrape_discovery_metrics(); + network_utils::discovery_metrics::scrape_discovery_metrics(); health_metrics::metrics::scrape_health_metrics(); diff --git a/beacon_node/lighthouse_network/Cargo.toml b/beacon_node/lighthouse_network/Cargo.toml index 3ee967eeee2..5f0ac3707bb 100644 --- a/beacon_node/lighthouse_network/Cargo.toml +++ b/beacon_node/lighthouse_network/Cargo.toml @@ -31,6 +31,7 @@ logging = { workspace = true } lru = { workspace = true } lru_cache = { workspace = true } metrics = { workspace = true } +network_utils = { workspace = true } parking_lot = { workspace = true } prometheus-client = "0.22.0" rand = { workspace = true } @@ -43,7 +44,6 @@ ssz_types = { workspace = true } strum = { workspace = true } superstruct = { workspace = true } task_executor = { workspace = true } -tiny-keccak = "2" tokio = { workspace = true } tokio-io-timeout = "1" tokio-util = { workspace = true } @@ -51,7 +51,6 @@ tracing = { workspace = true } tracing-subscriber = { workspace = true } types = { workspace = true } unsigned-varint = { version = "0.8", features = ["codec"] } -unused_port = { workspace = true } [dependencies.libp2p] version = "0.55" diff --git a/beacon_node/lighthouse_network/src/config.rs b/beacon_node/lighthouse_network/src/config.rs index aee53a469c4..7b8dc4ccf4f 100644 --- a/beacon_node/lighthouse_network/src/config.rs +++ b/beacon_node/lighthouse_network/src/config.rs @@ -1,4 +1,3 @@ -use crate::listen_addr::{ListenAddr, ListenAddress}; use crate::rpc::config::{InboundRateLimiterConfig, OutboundRateLimiterConfig}; use crate::types::GossipKind; use crate::{Enr, PeerIdSerialized}; @@ -7,6 +6,7 @@ use directory::{ }; use libp2p::Multiaddr; use local_ip_address::local_ipv6; +use network_utils::listen_addr::{ListenAddr, ListenAddress}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::net::{Ipv4Addr, Ipv6Addr}; diff --git a/beacon_node/lighthouse_network/src/discovery/enr.rs b/beacon_node/lighthouse_network/src/discovery/enr.rs index 053527f1199..f36b501bf18 100644 --- a/beacon_node/lighthouse_network/src/discovery/enr.rs +++ b/beacon_node/lighthouse_network/src/discovery/enr.rs @@ -2,14 +2,14 @@ pub use discv5::enr::CombinedKey; -use super::enr_ext::CombinedKeyExt; -use super::enr_ext::{EnrExt, QUIC6_ENR_KEY, QUIC_ENR_KEY}; use super::ENR_FILENAME; use crate::types::{Enr, EnrAttestationBitfield, EnrSyncCommitteeBitfield}; use crate::NetworkConfig; use alloy_rlp::bytes::Bytes; use libp2p::identity::Keypair; use lighthouse_version::{client_name, version}; +use network_utils::enr_ext::CombinedKeyExt; +use network_utils::enr_ext::{EnrExt, QUIC6_ENR_KEY, QUIC_ENR_KEY}; use ssz::{Decode, Encode}; use ssz_types::BitVector; use std::fs::File; diff --git a/beacon_node/lighthouse_network/src/discovery/mod.rs b/beacon_node/lighthouse_network/src/discovery/mod.rs index bc7802ce9aa..949b1cbc777 100644 --- a/beacon_node/lighthouse_network/src/discovery/mod.rs +++ b/beacon_node/lighthouse_network/src/discovery/mod.rs @@ -4,7 +4,6 @@ //! queries and manages access to the discovery routing table. pub(crate) mod enr; -pub mod enr_ext; // Allow external use of the lighthouse ENR builder use crate::service::TARGET_SUBNET_PEERS; @@ -12,8 +11,8 @@ use crate::{metrics, ClearDialError}; use crate::{Enr, NetworkConfig, NetworkGlobals, Subnet, SubnetDiscovery}; use discv5::{enr::NodeId, Discv5}; pub use enr::{build_enr, load_enr_from_disk, use_or_load_enr, CombinedKey, Eth2Enr}; -pub use enr_ext::{peer_id_to_node_id, CombinedKeyExt, EnrExt}; pub use libp2p::identity::{Keypair, PublicKey}; +use network_utils::enr_ext::{peer_id_to_node_id, CombinedKeyExt, EnrExt}; use alloy_rlp::bytes::Bytes; use enr::{ATTESTATION_BITFIELD_ENR_KEY, ETH2_ENR_KEY, SYNC_COMMITTEE_BITFIELD_ENR_KEY}; @@ -33,6 +32,7 @@ pub use libp2p::{ }; use logging::crit; use lru::LruCache; +use network_utils::discovery_metrics; use ssz::Encode; use std::num::NonZeroUsize; use std::{ @@ -687,7 +687,10 @@ impl Discovery { min_ttl, retries, }); - metrics::set_gauge(&metrics::DISCOVERY_QUEUE, self.queued_queries.len() as i64); + metrics::set_gauge( + &discovery_metrics::DISCOVERY_QUEUE, + self.queued_queries.len() as i64, + ); } } @@ -722,7 +725,10 @@ impl Discovery { } } // Update the queue metric - metrics::set_gauge(&metrics::DISCOVERY_QUEUE, self.queued_queries.len() as i64); + metrics::set_gauge( + &discovery_metrics::DISCOVERY_QUEUE, + self.queued_queries.len() as i64, + ); processed } @@ -1227,7 +1233,7 @@ mod tests { let spec = Arc::new(ChainSpec::default()); let keypair = secp256k1::Keypair::generate(); let mut config = NetworkConfig::default(); - config.set_listening_addr(crate::ListenAddress::unused_v4_ports()); + config.set_listening_addr(network_utils::listen_addr::ListenAddress::unused_v4_ports()); let config = Arc::new(config); let enr_key: CombinedKey = CombinedKey::from_secp256k1(&keypair); let next_fork_digest = [0; 4]; diff --git a/beacon_node/lighthouse_network/src/lib.rs b/beacon_node/lighthouse_network/src/lib.rs index 40fdd71b383..b07352f8d67 100644 --- a/beacon_node/lighthouse_network/src/lib.rs +++ b/beacon_node/lighthouse_network/src/lib.rs @@ -6,14 +6,12 @@ mod config; pub mod service; pub mod discovery; -pub mod listen_addr; pub mod metrics; pub mod peer_manager; pub mod rpc; pub mod types; use libp2p::swarm::DialError; -pub use listen_addr::*; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::str::FromStr; @@ -107,13 +105,12 @@ pub use crate::types::{ pub use prometheus_client; pub use config::Config as NetworkConfig; -pub use discovery::{CombinedKeyExt, EnrExt, Eth2Enr}; +pub use discovery::Eth2Enr; pub use discv5; pub use gossipsub::{IdentTopic, MessageAcceptance, MessageId, Topic, TopicHash}; pub use libp2p; pub use libp2p::{core::ConnectedPoint, PeerId, Swarm}; pub use libp2p::{multiaddr, Multiaddr}; -pub use metrics::scrape_discovery_metrics; pub use peer_manager::{ peerdb::client::Client, peerdb::score::{PeerAction, ReportSource}, diff --git a/beacon_node/lighthouse_network/src/metrics.rs b/beacon_node/lighthouse_network/src/metrics.rs index da986f28841..623d43a7270 100644 --- a/beacon_node/lighthouse_network/src/metrics.rs +++ b/beacon_node/lighthouse_network/src/metrics.rs @@ -1,14 +1,6 @@ pub use metrics::*; use std::sync::LazyLock; -pub static NAT_OPEN: LazyLock> = LazyLock::new(|| { - try_create_int_gauge_vec( - "nat_open", - "An estimate indicating if the local node is reachable from external nodes", - &["protocol"], - ) -}); - pub static ADDRESS_UPDATE_COUNT: LazyLock> = LazyLock::new(|| { try_create_int_counter( "libp2p_address_update_total", @@ -53,31 +45,6 @@ pub static PEER_DISCONNECT_EVENT_COUNT: LazyLock> = LazyLock: "Count of libp2p peer disconnect events", ) }); -pub static DISCOVERY_BYTES: LazyLock> = LazyLock::new(|| { - try_create_int_gauge_vec( - "discovery_bytes", - "The number of bytes sent and received in discovery", - &["direction"], - ) -}); -pub static DISCOVERY_QUEUE: LazyLock> = LazyLock::new(|| { - try_create_int_gauge( - "discovery_queue_size", - "The number of discovery queries awaiting execution", - ) -}); -pub static DISCOVERY_REQS: LazyLock> = LazyLock::new(|| { - try_create_float_gauge( - "discovery_requests", - "The number of unsolicited discovery requests per second", - ) -}); -pub static DISCOVERY_SESSIONS: LazyLock> = LazyLock::new(|| { - try_create_int_gauge( - "discovery_sessions", - "The number of active discovery sessions with peers", - ) -}); pub static DISCOVERY_NO_USEFUL_ENRS: LazyLock> = LazyLock::new(|| { try_create_int_counter( "discovery_no_useful_enrs_found", @@ -219,14 +186,3 @@ pub static RESPONSE_IDLING: LazyLock> = LazyLock::new(|| { "The time our response remained idle in the response limiter", ) }); - -pub fn scrape_discovery_metrics() { - let metrics = - discv5::metrics::Metrics::from(discv5::Discv5::::raw_metrics()); - set_float_gauge(&DISCOVERY_REQS, metrics.unsolicited_requests_per_second); - set_gauge(&DISCOVERY_SESSIONS, metrics.active_sessions as i64); - set_gauge_vec(&DISCOVERY_BYTES, &["inbound"], metrics.bytes_recv as i64); - set_gauge_vec(&DISCOVERY_BYTES, &["outbound"], metrics.bytes_sent as i64); - set_gauge_vec(&NAT_OPEN, &["discv5_ipv4"], metrics.ipv4_contactable as i64); - set_gauge_vec(&NAT_OPEN, &["discv5_ipv6"], metrics.ipv6_contactable as i64); -} diff --git a/beacon_node/lighthouse_network/src/peer_manager/mod.rs b/beacon_node/lighthouse_network/src/peer_manager/mod.rs index 01cc1611058..dd9fc25d482 100644 --- a/beacon_node/lighthouse_network/src/peer_manager/mod.rs +++ b/beacon_node/lighthouse_network/src/peer_manager/mod.rs @@ -1,7 +1,5 @@ //! Implementation of Lighthouse's peer management system. -use crate::discovery::enr_ext::EnrExt; -use crate::discovery::peer_id_to_node_id; use crate::rpc::{GoodbyeReason, MetaData, Protocol, RPCError, RpcErrorResponse}; use crate::service::TARGET_SUBNET_PEERS; use crate::{metrics, Gossipsub, NetworkGlobals, PeerId, Subnet, SubnetDiscovery}; @@ -26,6 +24,8 @@ pub mod peerdb; use crate::peer_manager::peerdb::client::ClientKind; use libp2p::multiaddr; +use network_utils::discovery_metrics; +use network_utils::enr_ext::{peer_id_to_node_id, EnrExt}; pub use peerdb::peer_info::{ ConnectionDirection, PeerConnectionStatus, PeerConnectionStatus::*, PeerInfo, }; @@ -1418,16 +1418,16 @@ impl PeerManager { // Set ipv4 nat_open metric flag if threshold of peercount is met, unset if below threshold if inbound_ipv4_peers_connected >= LIBP2P_NAT_OPEN_THRESHOLD { - metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv4"], 1); + metrics::set_gauge_vec(&discovery_metrics::NAT_OPEN, &["libp2p_ipv4"], 1); } else { - metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv4"], 0); + metrics::set_gauge_vec(&discovery_metrics::NAT_OPEN, &["libp2p_ipv4"], 0); } // Set ipv6 nat_open metric flag if threshold of peercount is met, unset if below threshold if inbound_ipv6_peers_connected >= LIBP2P_NAT_OPEN_THRESHOLD { - metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv6"], 1); + metrics::set_gauge_vec(&discovery_metrics::NAT_OPEN, &["libp2p_ipv6"], 1); } else { - metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv6"], 0); + metrics::set_gauge_vec(&discovery_metrics::NAT_OPEN, &["libp2p_ipv6"], 0); } // PEERS_CONNECTED diff --git a/beacon_node/lighthouse_network/src/peer_manager/network_behaviour.rs b/beacon_node/lighthouse_network/src/peer_manager/network_behaviour.rs index a45b941e58d..ebf69a892e5 100644 --- a/beacon_node/lighthouse_network/src/peer_manager/network_behaviour.rs +++ b/beacon_node/lighthouse_network/src/peer_manager/network_behaviour.rs @@ -12,11 +12,12 @@ use libp2p::swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFail use libp2p::swarm::dial_opts::{DialOpts, PeerCondition}; use libp2p::swarm::dummy::ConnectionHandler; use libp2p::swarm::{ConnectionDenied, ConnectionId, NetworkBehaviour, ToSwarm}; -pub use metrics::{set_gauge_vec, NAT_OPEN}; +use metrics::set_gauge_vec; +use network_utils::discovery_metrics::NAT_OPEN; +use network_utils::enr_ext::EnrExt; use tracing::{debug, error, trace}; use types::EthSpec; -use crate::discovery::enr_ext::EnrExt; use crate::types::SyncState; use crate::{metrics, ClearDialError}; diff --git a/beacon_node/lighthouse_network/src/peer_manager/peerdb.rs b/beacon_node/lighthouse_network/src/peer_manager/peerdb.rs index 6559b247241..9889c2ae736 100644 --- a/beacon_node/lighthouse_network/src/peer_manager/peerdb.rs +++ b/beacon_node/lighthouse_network/src/peer_manager/peerdb.rs @@ -1,10 +1,9 @@ use crate::discovery::enr::PEERDAS_CUSTODY_GROUP_COUNT_ENR_KEY; -use crate::discovery::{peer_id_to_node_id, CombinedKey}; -use crate::{ - metrics, multiaddr::Multiaddr, types::Subnet, Enr, EnrExt, Gossipsub, PeerId, SyncInfo, -}; +use crate::discovery::CombinedKey; +use crate::{metrics, multiaddr::Multiaddr, types::Subnet, Enr, Gossipsub, PeerId, SyncInfo}; use itertools::Itertools; use logging::crit; +use network_utils::enr_ext::{peer_id_to_node_id, EnrExt}; use peer_info::{ConnectionDirection, PeerConnectionStatus, PeerInfo}; use score::{PeerAction, ReportSource, Score, ScoreState}; use std::net::IpAddr; diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index 4a6f34c76d3..52b99f12c95 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -17,7 +17,6 @@ use crate::types::{ all_topics_at_fork, core_topics_to_subscribe, is_fork_non_core_topic, subnet_from_topic_hash, GossipEncoding, GossipKind, GossipTopic, SnappyTransform, Subnet, SubnetDiscovery, }; -use crate::EnrExt; use crate::Eth2Enr; use crate::{metrics, Enr, NetworkGlobals, PubsubMessage, TopicHash}; use api_types::{AppRequestId, Response}; @@ -33,6 +32,7 @@ use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent}; use libp2p::upnp::tokio::Behaviour as Upnp; use libp2p::{identify, PeerId, SwarmBuilder}; use logging::crit; +use network_utils::enr_ext::EnrExt; use std::num::{NonZeroU8, NonZeroUsize}; use std::path::PathBuf; use std::pin::Pin; diff --git a/beacon_node/lighthouse_network/src/types/globals.rs b/beacon_node/lighthouse_network/src/types/globals.rs index cc4d758b4ae..b8b5466a43d 100644 --- a/beacon_node/lighthouse_network/src/types/globals.rs +++ b/beacon_node/lighthouse_network/src/types/globals.rs @@ -3,7 +3,8 @@ use super::TopicConfig; use crate::peer_manager::peerdb::PeerDB; use crate::rpc::{MetaData, MetaDataV3}; use crate::types::{BackFillState, SyncState}; -use crate::{Client, Enr, EnrExt, GossipTopic, Multiaddr, NetworkConfig, PeerId}; +use crate::{Client, Enr, GossipTopic, Multiaddr, NetworkConfig, PeerId}; +use network_utils::enr_ext::EnrExt; use parking_lot::RwLock; use std::collections::HashSet; use std::sync::Arc; @@ -277,7 +278,7 @@ impl NetworkGlobals { config: Arc, spec: Arc, ) -> NetworkGlobals { - use crate::CombinedKeyExt; + use network_utils::enr_ext::CombinedKeyExt; let keypair = libp2p::identity::secp256k1::Keypair::generate(); let enr_key: discv5::enr::CombinedKey = discv5::enr::CombinedKey::from_secp256k1(&keypair); let enr = discv5::enr::Enr::builder().build(&enr_key).unwrap(); diff --git a/beacon_node/lighthouse_network/tests/common.rs b/beacon_node/lighthouse_network/tests/common.rs index 61f48a9a6fd..a5c13a0d3a5 100644 --- a/beacon_node/lighthouse_network/tests/common.rs +++ b/beacon_node/lighthouse_network/tests/common.rs @@ -1,9 +1,9 @@ #![cfg(test)] use lighthouse_network::service::Network as LibP2PService; use lighthouse_network::Enr; -use lighthouse_network::EnrExt; use lighthouse_network::Multiaddr; use lighthouse_network::{NetworkConfig, NetworkEvent}; +use network_utils::enr_ext::EnrExt; use std::sync::Arc; use std::sync::Weak; use tokio::runtime::Runtime; diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index f55b91d58c3..9caafc39fb0 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -14,8 +14,8 @@ use directory::{DEFAULT_BEACON_NODE_DIR, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR}; use environment::RuntimeContext; use execution_layer::DEFAULT_JWT_FILE; use http_api::TlsConfig; -use lighthouse_network::ListenAddress; use lighthouse_network::{multiaddr::Protocol, Enr, Multiaddr, NetworkConfig, PeerIdSerialized}; +use network_utils::listen_addr::ListenAddress; use sensitive_url::SensitiveUrl; use std::collections::HashSet; use std::fmt::Debug; @@ -1021,7 +1021,7 @@ pub fn parse_listening_addresses(cli_args: &ArgMatches) -> Result Result Result Result Result Result> = LazyLock::new(|| { + try_create_int_gauge_vec( + "nat_open", + "An estimate indicating if the local node is reachable from external nodes", + &["protocol"], + ) +}); +pub static DISCOVERY_BYTES: LazyLock> = LazyLock::new(|| { + try_create_int_gauge_vec( + "discovery_bytes", + "The number of bytes sent and received in discovery", + &["direction"], + ) +}); +pub static DISCOVERY_QUEUE: LazyLock> = LazyLock::new(|| { + try_create_int_gauge( + "discovery_queue_size", + "The number of discovery queries awaiting execution", + ) +}); +pub static DISCOVERY_REQS: LazyLock> = LazyLock::new(|| { + try_create_float_gauge( + "discovery_requests", + "The number of unsolicited discovery requests per second", + ) +}); +pub static DISCOVERY_SESSIONS: LazyLock> = LazyLock::new(|| { + try_create_int_gauge( + "discovery_sessions", + "The number of active discovery sessions with peers", + ) +}); + +pub fn scrape_discovery_metrics() { + let metrics = + discv5::metrics::Metrics::from(discv5::Discv5::::raw_metrics()); + set_float_gauge(&DISCOVERY_REQS, metrics.unsolicited_requests_per_second); + set_gauge(&DISCOVERY_SESSIONS, metrics.active_sessions as i64); + set_gauge_vec(&DISCOVERY_BYTES, &["inbound"], metrics.bytes_recv as i64); + set_gauge_vec(&DISCOVERY_BYTES, &["outbound"], metrics.bytes_sent as i64); + set_gauge_vec(&NAT_OPEN, &["discv5_ipv4"], metrics.ipv4_contactable as i64); + set_gauge_vec(&NAT_OPEN, &["discv5_ipv6"], metrics.ipv6_contactable as i64); +} diff --git a/beacon_node/lighthouse_network/src/discovery/enr_ext.rs b/common/network_utils/src/enr_ext.rs similarity index 98% rename from beacon_node/lighthouse_network/src/discovery/enr_ext.rs rename to common/network_utils/src/enr_ext.rs index bae72356044..c104a173a45 100644 --- a/beacon_node/lighthouse_network/src/discovery/enr_ext.rs +++ b/common/network_utils/src/enr_ext.rs @@ -1,11 +1,12 @@ //! ENR extension trait to support libp2p integration. -use crate::{Enr, Multiaddr, PeerId}; use discv5::enr::{CombinedKey, CombinedPublicKey}; -use libp2p::core::multiaddr::Protocol; -use libp2p::identity::{ed25519, secp256k1, KeyType, Keypair, PublicKey}; +use libp2p_identity::{ed25519, secp256k1, KeyType, Keypair, PublicKey}; +use multiaddr::{Multiaddr, PeerId, Protocol}; use tiny_keccak::{Hasher, Keccak}; +type Enr = discv5::enr::Enr; + pub const QUIC_ENR_KEY: &str = "quic"; pub const QUIC6_ENR_KEY: &str = "quic6"; diff --git a/common/network_utils/src/lib.rs b/common/network_utils/src/lib.rs new file mode 100644 index 00000000000..c3d6ee1e0c0 --- /dev/null +++ b/common/network_utils/src/lib.rs @@ -0,0 +1,4 @@ +pub mod discovery_metrics; +pub mod enr_ext; +pub mod listen_addr; +pub mod unused_port; diff --git a/beacon_node/lighthouse_network/src/listen_addr.rs b/common/network_utils/src/listen_addr.rs similarity index 86% rename from beacon_node/lighthouse_network/src/listen_addr.rs rename to common/network_utils/src/listen_addr.rs index 3b0ff98b34f..bdd94b3414c 100644 --- a/beacon_node/lighthouse_network/src/listen_addr.rs +++ b/common/network_utils/src/listen_addr.rs @@ -1,6 +1,6 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; -use libp2p::{multiaddr::Protocol, Multiaddr}; +use multiaddr::{Multiaddr, Protocol}; use serde::{Deserialize, Serialize}; /// A listening address composed by an Ip, an UDP port and a TCP port. @@ -84,23 +84,21 @@ impl ListenAddress { .chain(v6_tcp_multiaddr) } - #[cfg(test)] pub fn unused_v4_ports() -> Self { ListenAddress::V4(ListenAddr { addr: Ipv4Addr::UNSPECIFIED, - disc_port: unused_port::unused_udp4_port().unwrap(), - quic_port: unused_port::unused_udp4_port().unwrap(), - tcp_port: unused_port::unused_tcp4_port().unwrap(), + disc_port: crate::unused_port::unused_udp4_port().unwrap(), + quic_port: crate::unused_port::unused_udp4_port().unwrap(), + tcp_port: crate::unused_port::unused_tcp4_port().unwrap(), }) } - #[cfg(test)] pub fn unused_v6_ports() -> Self { ListenAddress::V6(ListenAddr { addr: Ipv6Addr::UNSPECIFIED, - disc_port: unused_port::unused_udp6_port().unwrap(), - quic_port: unused_port::unused_udp6_port().unwrap(), - tcp_port: unused_port::unused_tcp6_port().unwrap(), + disc_port: crate::unused_port::unused_udp6_port().unwrap(), + quic_port: crate::unused_port::unused_udp6_port().unwrap(), + tcp_port: crate::unused_port::unused_tcp6_port().unwrap(), }) } } diff --git a/common/unused_port/src/lib.rs b/common/network_utils/src/unused_port.rs similarity index 100% rename from common/unused_port/src/lib.rs rename to common/network_utils/src/unused_port.rs diff --git a/common/system_health/Cargo.toml b/common/system_health/Cargo.toml index 034683f72e0..2cafc42d6e4 100644 --- a/common/system_health/Cargo.toml +++ b/common/system_health/Cargo.toml @@ -5,6 +5,8 @@ edition = { workspace = true } [dependencies] lighthouse_network = { workspace = true } +metrics = { workspace = true } +network_utils = { workspace = true } parking_lot = { workspace = true } serde = { workspace = true } sysinfo = { workspace = true } diff --git a/common/system_health/src/lib.rs b/common/system_health/src/lib.rs index 9f351e943bb..703c6e03498 100644 --- a/common/system_health/src/lib.rs +++ b/common/system_health/src/lib.rs @@ -1,4 +1,5 @@ use lighthouse_network::{types::SyncState, NetworkGlobals}; +use network_utils::discovery_metrics; use parking_lot::RwLock; use serde::{Deserialize, Serialize}; use std::path::{Path, PathBuf}; @@ -219,33 +220,21 @@ impl NatState { /// Observes if NAT traversal is possible. pub fn observe_nat() -> NatState { - let discv5_ipv4 = lighthouse_network::metrics::get_int_gauge( - &lighthouse_network::metrics::NAT_OPEN, - &["discv5_ipv4"], - ) - .map(|g| g.get() == 1) - .unwrap_or_default(); - - let discv5_ipv6 = lighthouse_network::metrics::get_int_gauge( - &lighthouse_network::metrics::NAT_OPEN, - &["discv5_ipv6"], - ) - .map(|g| g.get() == 1) - .unwrap_or_default(); - - let libp2p_ipv4 = lighthouse_network::metrics::get_int_gauge( - &lighthouse_network::metrics::NAT_OPEN, - &["libp2p_ipv4"], - ) - .map(|g| g.get() == 1) - .unwrap_or_default(); - - let libp2p_ipv6 = lighthouse_network::metrics::get_int_gauge( - &lighthouse_network::metrics::NAT_OPEN, - &["libp2p_ipv6"], - ) - .map(|g| g.get() == 1) - .unwrap_or_default(); + let discv5_ipv4 = metrics::get_int_gauge(&discovery_metrics::NAT_OPEN, &["discv5_ipv4"]) + .map(|g| g.get() == 1) + .unwrap_or_default(); + + let discv5_ipv6 = metrics::get_int_gauge(&discovery_metrics::NAT_OPEN, &["discv5_ipv6"]) + .map(|g| g.get() == 1) + .unwrap_or_default(); + + let libp2p_ipv4 = metrics::get_int_gauge(&discovery_metrics::NAT_OPEN, &["libp2p_ipv4"]) + .map(|g| g.get() == 1) + .unwrap_or_default(); + + let libp2p_ipv6 = metrics::get_int_gauge(&discovery_metrics::NAT_OPEN, &["libp2p_ipv6"]) + .map(|g| g.get() == 1) + .unwrap_or_default(); NatState { discv5_ipv4, diff --git a/common/unused_port/Cargo.toml b/common/unused_port/Cargo.toml deleted file mode 100644 index 2d771cd6008..00000000000 --- a/common/unused_port/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "unused_port" -version = "0.1.0" -edition = { workspace = true } -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -lru_cache = { workspace = true } -parking_lot = { workspace = true } diff --git a/lcli/Cargo.toml b/lcli/Cargo.toml index a54c10dc680..567f4d5e7f8 100644 --- a/lcli/Cargo.toml +++ b/lcli/Cargo.toml @@ -33,6 +33,7 @@ lighthouse_network = { workspace = true } lighthouse_version = { workspace = true } log = { workspace = true } malloc_utils = { workspace = true } +network_utils = { workspace = true } rayon = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/lcli/src/generate_bootnode_enr.rs b/lcli/src/generate_bootnode_enr.rs index b2fd7e7ec71..420d6eff2a5 100644 --- a/lcli/src/generate_bootnode_enr.rs +++ b/lcli/src/generate_bootnode_enr.rs @@ -1,9 +1,10 @@ use clap::ArgMatches; use lighthouse_network::{ - discovery::{build_enr, CombinedKey, CombinedKeyExt, ENR_FILENAME}, + discovery::{build_enr, CombinedKey, ENR_FILENAME}, libp2p::identity::secp256k1, NetworkConfig, NETWORK_KEY_FILENAME, }; +use network_utils::enr_ext::CombinedKeyExt; use std::io::Write; use std::path::PathBuf; use std::{fs, net::Ipv4Addr}; diff --git a/lighthouse/Cargo.toml b/lighthouse/Cargo.toml index 6a8fa00c1e0..0d79026b955 100644 --- a/lighthouse/Cargo.toml +++ b/lighthouse/Cargo.toml @@ -58,6 +58,7 @@ lighthouse_version = { workspace = true } logging = { workspace = true } malloc_utils = { workspace = true } metrics = { workspace = true } +network_utils = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } serde_yaml = { workspace = true } @@ -67,7 +68,6 @@ task_executor = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true } types = { workspace = true } -unused_port = { workspace = true } validator_client = { workspace = true } validator_manager = { path = "../validator_manager" } diff --git a/lighthouse/tests/beacon_node.rs b/lighthouse/tests/beacon_node.rs index 884e5eddeba..46dfdc7f52c 100644 --- a/lighthouse/tests/beacon_node.rs +++ b/lighthouse/tests/beacon_node.rs @@ -9,6 +9,9 @@ use beacon_node::{ }; use beacon_processor::BeaconProcessorConfig; use lighthouse_network::PeerId; +use network_utils::unused_port::{ + unused_tcp4_port, unused_tcp6_port, unused_udp4_port, unused_udp6_port, +}; use std::fs::File; use std::io::{Read, Write}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; @@ -21,7 +24,6 @@ use std::time::Duration; use tempfile::TempDir; use types::non_zero_usize::new_non_zero_usize; use types::{Address, Checkpoint, Epoch, Hash256, MainnetEthSpec}; -use unused_port::{unused_tcp4_port, unused_tcp6_port, unused_udp4_port, unused_udp6_port}; const DEFAULT_EXECUTION_ENDPOINT: &str = "http://localhost:8551/"; const DEFAULT_EXECUTION_JWT_SECRET_KEY: &str = diff --git a/lighthouse/tests/boot_node.rs b/lighthouse/tests/boot_node.rs index b243cd6001e..bb4b2b15d7e 100644 --- a/lighthouse/tests/boot_node.rs +++ b/lighthouse/tests/boot_node.rs @@ -5,6 +5,7 @@ use clap::ArgMatches; use clap_utils::get_eth2_network_config; use lighthouse_network::discovery::ENR_FILENAME; use lighthouse_network::Enr; +use network_utils::unused_port::unused_udp4_port; use std::fs::File; use std::io::Write; use std::net::Ipv4Addr; @@ -12,7 +13,6 @@ use std::path::{Path, PathBuf}; use std::process::Command; use std::str::FromStr; use tempfile::TempDir; -use unused_port::unused_udp4_port; const IP_ADDRESS: &str = "192.168.2.108"; diff --git a/testing/execution_engine_integration/Cargo.toml b/testing/execution_engine_integration/Cargo.toml index 07d8d98f1d5..eef13cfc738 100644 --- a/testing/execution_engine_integration/Cargo.toml +++ b/testing/execution_engine_integration/Cargo.toml @@ -18,6 +18,7 @@ fork_choice = { workspace = true } futures = { workspace = true } hex = { workspace = true } logging = { workspace = true } +network_utils = { workspace = true } reqwest = { workspace = true } sensitive_url = { workspace = true } serde_json = { workspace = true } @@ -25,4 +26,3 @@ task_executor = { workspace = true } tempfile = { workspace = true } tokio = { workspace = true } types = { workspace = true } -unused_port = { workspace = true } diff --git a/testing/execution_engine_integration/src/execution_engine.rs b/testing/execution_engine_integration/src/execution_engine.rs index 61a50b0405e..ed4ee4682f4 100644 --- a/testing/execution_engine_integration/src/execution_engine.rs +++ b/testing/execution_engine_integration/src/execution_engine.rs @@ -1,10 +1,10 @@ use ethers_providers::{Http, Provider}; use execution_layer::DEFAULT_JWT_FILE; +use network_utils::unused_port::unused_tcp4_port; use sensitive_url::SensitiveUrl; use std::path::PathBuf; use std::process::Child; use tempfile::TempDir; -use unused_port::unused_tcp4_port; pub const KEYSTORE_PASSWORD: &str = "testpwd"; pub const ACCOUNT1: &str = "7b8C3a386C0eea54693fFB0DA17373ffC9228139"; diff --git a/testing/execution_engine_integration/src/geth.rs b/testing/execution_engine_integration/src/geth.rs index 91d6c7fd57c..4b62e68e94f 100644 --- a/testing/execution_engine_integration/src/geth.rs +++ b/testing/execution_engine_integration/src/geth.rs @@ -1,11 +1,11 @@ use crate::build_utils; use crate::execution_engine::GenericExecutionEngine; use crate::genesis_json::geth_genesis_json; +use network_utils::unused_port::unused_tcp4_port; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Output}; use std::{env, fs}; use tempfile::TempDir; -use unused_port::unused_tcp4_port; const GETH_BRANCH: &str = "master"; const GETH_REPO_URL: &str = "https://github.com/ethereum/go-ethereum"; diff --git a/testing/execution_engine_integration/src/nethermind.rs b/testing/execution_engine_integration/src/nethermind.rs index c3b8651789f..6a336161bd8 100644 --- a/testing/execution_engine_integration/src/nethermind.rs +++ b/testing/execution_engine_integration/src/nethermind.rs @@ -1,12 +1,12 @@ use crate::build_utils; use crate::execution_engine::GenericExecutionEngine; use crate::genesis_json::nethermind_genesis_json; +use network_utils::unused_port::unused_tcp4_port; use std::env; use std::fs; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Output}; use tempfile::TempDir; -use unused_port::unused_tcp4_port; /// We've pinned the Nethermind version since our method of using the `master` branch to /// find the latest tag isn't working. It appears Nethermind don't always tag on `master`.