From ee1b6bc81bb4b7dafb95a95eabcfcaf6b76cea04 Mon Sep 17 00:00:00 2001 From: Daniel Knopik <107140945+dknopik@users.noreply.github.com> Date: Wed, 10 Sep 2025 14:59:24 +0200 Subject: [PATCH] Create `network_utils` crate (#7761) Anchor currently depends on `lighthouse_network` for a few types and utilities that live within. As we use our own libp2p behaviours, we actually do not use the core logic in that crate. This makes us transitively depend on a bunch of unneeded crates (even a whole separate libp2p if the versions mismatch!) Move things we require into it's own lightweight crate. Co-Authored-By: Daniel Knopik --- Cargo.lock | 38 +++++++++------ Cargo.toml | 4 +- beacon_node/Cargo.toml | 2 +- beacon_node/http_api/Cargo.toml | 1 + beacon_node/http_api/src/lib.rs | 3 +- beacon_node/http_api/tests/tests.rs | 3 +- beacon_node/http_metrics/Cargo.toml | 1 + beacon_node/http_metrics/src/metrics.rs | 2 +- beacon_node/lighthouse_network/Cargo.toml | 3 +- beacon_node/lighthouse_network/src/config.rs | 2 +- .../lighthouse_network/src/discovery/enr.rs | 4 +- .../lighthouse_network/src/discovery/mod.rs | 16 +++++-- beacon_node/lighthouse_network/src/lib.rs | 5 +- beacon_node/lighthouse_network/src/metrics.rs | 44 ------------------ .../src/peer_manager/mod.rs | 16 +++---- .../src/peer_manager/network_behaviour.rs | 5 +- .../src/peer_manager/peerdb.rs | 7 ++- .../lighthouse_network/src/service/mod.rs | 2 +- .../lighthouse_network/src/types/globals.rs | 5 +- .../lighthouse_network/tests/common.rs | 2 +- beacon_node/src/config.rs | 34 +++++++------- boot_node/Cargo.toml | 1 + boot_node/src/config.rs | 3 +- boot_node/src/server.rs | 3 +- common/network_utils/Cargo.toml | 17 +++++++ common/network_utils/src/discovery_metrics.rs | 46 +++++++++++++++++++ .../network_utils/src}/enr_ext.rs | 7 +-- common/network_utils/src/lib.rs | 4 ++ .../network_utils}/src/listen_addr.rs | 16 +++---- .../src/unused_port.rs} | 0 common/system_health/Cargo.toml | 2 + common/system_health/src/lib.rs | 37 ++++++--------- common/unused_port/Cargo.toml | 9 ---- lcli/Cargo.toml | 1 + lcli/src/generate_bootnode_enr.rs | 3 +- lighthouse/Cargo.toml | 2 +- lighthouse/tests/beacon_node.rs | 4 +- lighthouse/tests/boot_node.rs | 5 +- .../execution_engine_integration/Cargo.toml | 2 +- .../src/execution_engine.rs | 2 +- .../execution_engine_integration/src/geth.rs | 2 +- .../src/nethermind.rs | 2 +- 42 files changed, 198 insertions(+), 169 deletions(-) create mode 100644 common/network_utils/Cargo.toml create mode 100644 common/network_utils/src/discovery_metrics.rs rename {beacon_node/lighthouse_network/src/discovery => common/network_utils/src}/enr_ext.rs (98%) create mode 100644 common/network_utils/src/lib.rs rename {beacon_node/lighthouse_network => common/network_utils}/src/listen_addr.rs (86%) rename common/{unused_port/src/lib.rs => network_utils/src/unused_port.rs} (100%) delete mode 100644 common/unused_port/Cargo.toml diff --git a/Cargo.lock b/Cargo.lock index 96768211eb..88b5b7b57d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -936,6 +936,7 @@ dependencies = [ "hyper 1.6.0", "lighthouse_network", "monitoring_api", + "network_utils", "node_test_rig", "sensitive_url", "serde_json", @@ -945,7 +946,6 @@ dependencies = [ "task_executor", "tracing", "types", - "unused_port", ] [[package]] @@ -1205,6 +1205,7 @@ dependencies = [ "lighthouse_network", "log", "logging", + "network_utils", "serde", "tokio", "tracing", @@ -3314,6 +3315,7 @@ dependencies = [ "futures", "hex", "logging", + "network_utils", "reqwest 0.11.27", "sensitive_url", "serde_json", @@ -3321,7 +3323,6 @@ dependencies = [ "tempfile", "tokio", "types", - "unused_port", ] [[package]] @@ -4300,6 +4301,7 @@ dependencies = [ "lru", "metrics", "network", + "network_utils", "operation_pool", "parking_lot 0.12.3", "proto_array", @@ -4334,6 +4336,7 @@ dependencies = [ "logging", "malloc_utils", "metrics", + "network_utils", "reqwest 0.11.27", "serde", "slot_clock", @@ -5068,6 +5071,7 @@ dependencies = [ "lighthouse_version", "log", "malloc_utils", + "network_utils", "rayon", "serde", "serde_json", @@ -5583,6 +5587,7 @@ dependencies = [ "logging", "malloc_utils", "metrics", + "network_utils", "opentelemetry", "opentelemetry-otlp", "opentelemetry_sdk", @@ -5599,7 +5604,6 @@ dependencies = [ "tracing-opentelemetry", "tracing-subscriber", "types", - "unused_port", "validator_client", "validator_dir", "validator_manager", @@ -5635,6 +5639,7 @@ dependencies = [ "lru", "lru_cache", "metrics", + "network_utils", "parking_lot 0.12.3", "prometheus-client", "quickcheck", @@ -5650,14 +5655,12 @@ dependencies = [ "superstruct", "task_executor", "tempfile", - "tiny-keccak", "tokio", "tokio-util", "tracing", "tracing-subscriber", "types", "unsigned-varint 0.8.0", - "unused_port", ] [[package]] @@ -6355,6 +6358,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" @@ -9179,6 +9197,8 @@ name = "system_health" version = "0.1.0" dependencies = [ "lighthouse_network", + "metrics", + "network_utils", "parking_lot 0.12.3", "serde", "sysinfo", @@ -10022,14 +10042,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 ca5f7bc153..0b930b605d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ members = [ "common/malloc_utils", "common/metrics", "common/monitoring_api", + "common/network_utils", "common/oneshot_broadcast", "common/pretty_reqwest_error", "common/sensitive_url", @@ -45,7 +46,6 @@ members = [ "common/target_check", "common/task_executor", "common/test_random_derive", - "common/unused_port", "common/validator_dir", "common/warp_utils", "common/workspace_members", @@ -194,6 +194,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" @@ -265,7 +266,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 456376e79b..dd7416af54 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 2061df3762..7dd0d0223f 100644 --- a/beacon_node/http_api/Cargo.toml +++ b/beacon_node/http_api/Cargo.toml @@ -26,6 +26,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 bfe0bd4d38..5c6a9df739 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::{Enr, EnrExt, NetworkGlobals, PeerId, PubsubMessage, types::SyncState}; +use lighthouse_network::{Enr, NetworkGlobals, PeerId, PubsubMessage, types::SyncState}; use lighthouse_version::version_with_platform; use logging::{SSELoggingComponents, crit}; 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 91f8666381..2072fb9932 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -26,8 +26,9 @@ use http_api::{ BlockId, StateId, test_utils::{ApiServer, create_api_server}, }; -use lighthouse_network::{Enr, EnrExt, PeerId, types::SyncState}; +use lighthouse_network::{Enr, PeerId, types::SyncState}; 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 e12053ac43..b74c04a4cb 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 dbb0707a90..c19fa8fd3b 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 9963cc0bc4..7e69f6770b 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.23.0" rand = { workspace = true } @@ -43,14 +44,12 @@ ssz_types = { workspace = true } strum = { workspace = true } superstruct = { workspace = true } task_executor = { workspace = true } -tiny-keccak = "2" tokio = { workspace = true } tokio-util = { workspace = true } 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.56" diff --git a/beacon_node/lighthouse_network/src/config.rs b/beacon_node/lighthouse_network/src/config.rs index 23d545798f..89c6c58d4f 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::peer_manager::config::DEFAULT_TARGET_PEERS; use crate::rpc::config::{InboundRateLimiterConfig, OutboundRateLimiterConfig}; use crate::types::GossipKind; @@ -8,6 +7,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 bb3a32daf2..bb9ff299c5 100644 --- a/beacon_node/lighthouse_network/src/discovery/enr.rs +++ b/beacon_node/lighthouse_network/src/discovery/enr.rs @@ -3,13 +3,13 @@ pub use discv5::enr::CombinedKey; use super::ENR_FILENAME; -use super::enr_ext::CombinedKeyExt; -use super::enr_ext::{EnrExt, QUIC_ENR_KEY, QUIC6_ENR_KEY}; use crate::NetworkConfig; use crate::types::{Enr, EnrAttestationBitfield, EnrSyncCommitteeBitfield}; 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, QUIC_ENR_KEY, QUIC6_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 a245e830b9..49de62546d 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::{ClearDialError, metrics}; use crate::{Enr, NetworkConfig, NetworkGlobals, Subnet, SubnetDiscovery}; use discv5::{Discv5, enr::NodeId}; pub use enr::{CombinedKey, Eth2Enr, build_enr, load_enr_from_disk, use_or_load_enr}; -pub use enr_ext::{CombinedKeyExt, EnrExt, peer_id_to_node_id}; pub use libp2p::identity::{Keypair, PublicKey}; +use network_utils::enr_ext::{CombinedKeyExt, EnrExt, peer_id_to_node_id}; 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 } @@ -1233,7 +1239,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 5c4a458650..b6be9b5222 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::{Deserialize, Deserializer, Serialize, Serializer, de}; 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::{Multiaddr, multiaddr}; pub use libp2p::{PeerId, Swarm, core::ConnectedPoint}; -pub use metrics::scrape_discovery_metrics; pub use peer_manager::{ ConnectionDirection, PeerConnectionStatus, PeerInfo, PeerManager, SyncInfo, SyncStatus, peerdb::PeerDB, diff --git a/beacon_node/lighthouse_network/src/metrics.rs b/beacon_node/lighthouse_network/src/metrics.rs index da986f2884..623d43a727 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 e7c6f69242..592fccdc74 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::{Gossipsub, NetworkGlobals, PeerId, Subnet, SubnetDiscovery, metrics}; @@ -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::{EnrExt, peer_id_to_node_id}; pub use peerdb::peer_info::{ConnectionDirection, PeerConnectionStatus, PeerInfo}; use peerdb::score::{PeerAction, ReportSource}; pub use peerdb::sync_status::{SyncInfo, SyncStatus}; @@ -1111,7 +1111,7 @@ impl PeerManager { /// /// Protection criteria: /// - Outbound peers: don't prune if it would drop below target outbound peer count - /// - Data column sampling: ≤ MIN_SAMPLING_COLUMN_SUBNET_PEERS (2) peers per subnet + /// - Data column sampling: ≤ MIN_SAMPLING_COLUMN_SUBNET_PEERS (2) peers per subnet /// - Sync committees: ≤ MIN_SYNC_COMMITTEE_PEERS (2) peers per committee /// - Attestation subnets: protect peers on the scarcest attestation subnets /// @@ -1586,16 +1586,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 @@ -2780,7 +2780,7 @@ mod tests { /// /// Create 6 peers with different sync statuses: /// Peer0: Behind - /// Peer1: Unknown + /// Peer1: Unknown /// Peer2: Synced /// Peer3: Advanced /// Peer4: Synced 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 43d9b90d8d..729dbd193b 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::{NAT_OPEN, set_gauge_vec}; +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::{ClearDialError, metrics}; diff --git a/beacon_node/lighthouse_network/src/peer_manager/peerdb.rs b/beacon_node/lighthouse_network/src/peer_manager/peerdb.rs index 083c3f00c2..0ccad8d042 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::CombinedKey; use crate::discovery::enr::PEERDAS_CUSTODY_GROUP_COUNT_ENR_KEY; -use crate::discovery::{CombinedKey, peer_id_to_node_id}; -use crate::{ - Enr, EnrExt, Gossipsub, PeerId, SyncInfo, metrics, multiaddr::Multiaddr, types::Subnet, -}; +use crate::{Enr, Gossipsub, PeerId, SyncInfo, metrics, multiaddr::Multiaddr, types::Subnet}; use itertools::Itertools; use logging::crit; +use network_utils::enr_ext::{EnrExt, peer_id_to_node_id}; 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 9edb70555d..ea2c53a07f 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -1,5 +1,4 @@ use self::gossip_cache::GossipCache; -use crate::EnrExt; use crate::Eth2Enr; use crate::config::{GossipsubConfigParams, NetworkLoad, gossipsub_config}; use crate::discovery::{ @@ -33,6 +32,7 @@ use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent}; use libp2p::upnp::tokio::Behaviour as Upnp; use libp2p::{PeerId, SwarmBuilder, identify}; 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 bcb4758386..b8c34f8392 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; @@ -250,7 +251,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 6b111cfdc1..8a3047692f 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::Enr; -use lighthouse_network::EnrExt; use lighthouse_network::Multiaddr; use lighthouse_network::service::Network as LibP2PService; 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 7e4b77e9aa..1b5f25b317 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -13,8 +13,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::{Enr, Multiaddr, NetworkConfig, PeerIdSerialized, multiaddr::Protocol}; +use network_utils::listen_addr::ListenAddress; use sensitive_url::SensitiveUrl; use std::collections::HashSet; use std::fmt::Debug; @@ -1011,7 +1011,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 1d065ebf4a..627dd15559 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::{KeyType, Keypair, PublicKey, ed25519, secp256k1}; +use libp2p_identity::{KeyType, Keypair, PublicKey, ed25519, secp256k1}; +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 0000000000..c3d6ee1e0c --- /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 85232c0b35..bdd94b3414 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, multiaddr::Protocol}; +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 034683f72e..2cafc42d6e 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 31b222c540..b61bdec486 100644 --- a/common/system_health/src/lib.rs +++ b/common/system_health/src/lib.rs @@ -1,4 +1,5 @@ use lighthouse_network::{NetworkGlobals, types::SyncState}; +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_ipv4 = metrics::get_int_gauge(&discovery_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 discv5_ipv6 = metrics::get_int_gauge(&discovery_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_ipv4 = metrics::get_int_gauge(&discovery_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 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 2d771cd600..0000000000 --- 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 b962fa3b81..2eed9da4c0 100644 --- a/lcli/Cargo.toml +++ b/lcli/Cargo.toml @@ -30,6 +30,7 @@ hex = { workspace = true } lighthouse_network = { workspace = true } lighthouse_version = { workspace = true } log = { 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 6fe13d17c3..ddd36e7e7a 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::{ NETWORK_KEY_FILENAME, NetworkConfig, - discovery::{CombinedKey, CombinedKeyExt, ENR_FILENAME, build_enr}, + discovery::{CombinedKey, ENR_FILENAME, build_enr}, libp2p::identity::secp256k1, }; +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 849d30bcf2..bf8241f8a2 100644 --- a/lighthouse/Cargo.toml +++ b/lighthouse/Cargo.toml @@ -57,6 +57,7 @@ lighthouse_tracing = { workspace = true } lighthouse_version = { workspace = true } logging = { workspace = true } metrics = { workspace = true } +network_utils = { workspace = true } opentelemetry = { workspace = true } opentelemetry-otlp = { workspace = true } opentelemetry_sdk = { workspace = true } @@ -70,7 +71,6 @@ tracing = { workspace = true } tracing-opentelemetry = { 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 38fd54d29d..1fd3cc1b79 100644 --- a/lighthouse/tests/beacon_node.rs +++ b/lighthouse/tests/beacon_node.rs @@ -10,6 +10,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}; @@ -22,7 +25,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 bd1cd7574e..38111ca0ef 100644 --- a/lighthouse/tests/boot_node.rs +++ b/lighthouse/tests/boot_node.rs @@ -3,8 +3,8 @@ use boot_node::config::BootNodeConfigSerialization; use crate::exec::{CommandLineTestExec, CompletedTest}; use clap::ArgMatches; use clap_utils::get_eth2_network_config; -use lighthouse_network::Enr; -use lighthouse_network::discovery::ENR_FILENAME; +use lighthouse_network::{Enr, discovery::ENR_FILENAME}; +use network_utils::unused_port::unused_udp4_port; use std::fs::File; use std::io::Write; use std::net::Ipv4Addr; @@ -12,7 +12,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 07d8d98f1d..eef13cfc73 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 61a50b0405..ed4ee4682f 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 91d6c7fd57..4b62e68e94 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 c3b8651789..6a336161bd 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`.