Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/unstable' into decouple-subnets
Browse files Browse the repository at this point in the history
# Conflicts:
#	beacon_node/lighthouse_network/src/peer_manager/mod.rs
  • Loading branch information
jimmygchen committed Jan 15, 2025
2 parents 283f940 + 4fd8e52 commit 2d874d8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
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 @@ -66,6 +66,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 @@ -1345,7 +1347,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_group_count: HashMap<u64, i64> = HashMap::new();

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

Expand All @@ -1383,6 +1387,29 @@ impl<E: EthSpec> PeerManager<E> {
.entry(meta_data.custody_group_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 @@ -1413,7 +1440,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 @@ -124,6 +124,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

0 comments on commit 2d874d8

Please sign in to comment.