Skip to content

Use existing peer count metrics loop to check for open_nat toggle #6800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 15, 2025
33 changes: 30 additions & 3 deletions beacon_node/lighthouse_network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub const MIN_OUTBOUND_ONLY_FACTOR: f32 = 0.2;
/// limit is 55, and we are at 55 peers, the following parameter provisions a few more slots of
/// dialing priority peers we need for validator duties.
pub const PRIORITY_PEER_EXCESS: f32 = 0.2;
/// The numbre of inbound libp2p peers we have seen before we consider our NAT to be open.
pub const LIBP2P_NAT_OPEN_THRESHOLD: usize = 3;

/// The main struct that handles peer's reputation and connection status.
pub struct PeerManager<E: EthSpec> {
Expand Down Expand Up @@ -1307,7 +1309,9 @@ impl<E: EthSpec> PeerManager<E> {
fn update_peer_count_metrics(&self) {
let mut peers_connected = 0;
let mut clients_per_peer = HashMap::new();
let mut peers_connected_mutli: HashMap<(&str, &str), i32> = HashMap::new();
let mut inbound_ipv4_peers_connected: usize = 0;
let mut inbound_ipv6_peers_connected: usize = 0;
let mut peers_connected_multi: HashMap<(&str, &str), i32> = HashMap::new();
let mut peers_per_custody_subnet_count: HashMap<u64, i64> = HashMap::new();

for (_, peer_info) in self.network_globals.peers.read().connected_peers() {
Expand Down Expand Up @@ -1336,7 +1340,7 @@ impl<E: EthSpec> PeerManager<E> {
})
})
.unwrap_or("unknown");
*peers_connected_mutli
*peers_connected_multi
.entry((direction, transport))
.or_default() += 1;

Expand All @@ -1345,6 +1349,29 @@ impl<E: EthSpec> PeerManager<E> {
.entry(meta_data.custody_subnet_count)
.or_default() += 1;
}
// Check if incoming peer is ipv4
if peer_info.is_incoming_ipv4_connection() {
inbound_ipv4_peers_connected += 1;
}

// Check if incoming peer is ipv6
if peer_info.is_incoming_ipv6_connection() {
inbound_ipv6_peers_connected += 1;
}
}

// 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);
} else {
metrics::set_gauge_vec(&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);
} else {
metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv6"], 0);
}

// PEERS_CONNECTED
Expand Down Expand Up @@ -1375,7 +1402,7 @@ impl<E: EthSpec> PeerManager<E> {
metrics::set_gauge_vec(
&metrics::PEERS_CONNECTED_MULTI,
&[direction, transport],
*peers_connected_mutli
*peers_connected_multi
.get(&(direction, transport))
.unwrap_or(&0) as i64,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ impl<E: EthSpec> PeerInfo<E> {
self.connection_direction.as_ref()
}

/// Returns true if this is an incoming ipv4 connection.
pub fn is_incoming_ipv4_connection(&self) -> bool {
self.seen_multiaddrs.iter().any(|multiaddr| {
multiaddr
.iter()
.any(|protocol| matches!(protocol, libp2p::core::multiaddr::Protocol::Ip4(_)))
})
}

/// Returns true if this is an incoming ipv6 connection.
pub fn is_incoming_ipv6_connection(&self) -> bool {
self.seen_multiaddrs.iter().any(|multiaddr| {
multiaddr
.iter()
.any(|protocol| matches!(protocol, libp2p::core::multiaddr::Protocol::Ip6(_)))
})
}

/// Returns the sync status of the peer.
pub fn sync_status(&self) -> &SyncStatus {
&self.sync_status
Expand Down
Loading