Skip to content

Commit 326e893

Browse files
authored
Merge of #6800
2 parents 587c3e2 + a3c5ce9 commit 326e893

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

beacon_node/lighthouse_network/src/peer_manager/mod.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pub const MIN_OUTBOUND_ONLY_FACTOR: f32 = 0.2;
6363
/// limit is 55, and we are at 55 peers, the following parameter provisions a few more slots of
6464
/// dialing priority peers we need for validator duties.
6565
pub const PRIORITY_PEER_EXCESS: f32 = 0.2;
66+
/// The numbre of inbound libp2p peers we have seen before we consider our NAT to be open.
67+
pub const LIBP2P_NAT_OPEN_THRESHOLD: usize = 3;
6668

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

13131317
for (_, peer_info) in self.network_globals.peers.read().connected_peers() {
@@ -1336,7 +1340,7 @@ impl<E: EthSpec> PeerManager<E> {
13361340
})
13371341
})
13381342
.unwrap_or("unknown");
1339-
*peers_connected_mutli
1343+
*peers_connected_multi
13401344
.entry((direction, transport))
13411345
.or_default() += 1;
13421346

@@ -1345,6 +1349,29 @@ impl<E: EthSpec> PeerManager<E> {
13451349
.entry(meta_data.custody_subnet_count)
13461350
.or_default() += 1;
13471351
}
1352+
// Check if incoming peer is ipv4
1353+
if peer_info.is_incoming_ipv4_connection() {
1354+
inbound_ipv4_peers_connected += 1;
1355+
}
1356+
1357+
// Check if incoming peer is ipv6
1358+
if peer_info.is_incoming_ipv6_connection() {
1359+
inbound_ipv6_peers_connected += 1;
1360+
}
1361+
}
1362+
1363+
// Set ipv4 nat_open metric flag if threshold of peercount is met, unset if below threshold
1364+
if inbound_ipv4_peers_connected >= LIBP2P_NAT_OPEN_THRESHOLD {
1365+
metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv4"], 1);
1366+
} else {
1367+
metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv4"], 0);
1368+
}
1369+
1370+
// Set ipv6 nat_open metric flag if threshold of peercount is met, unset if below threshold
1371+
if inbound_ipv6_peers_connected >= LIBP2P_NAT_OPEN_THRESHOLD {
1372+
metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv6"], 1);
1373+
} else {
1374+
metrics::set_gauge_vec(&metrics::NAT_OPEN, &["libp2p_ipv6"], 0);
13481375
}
13491376

13501377
// PEERS_CONNECTED
@@ -1375,7 +1402,7 @@ impl<E: EthSpec> PeerManager<E> {
13751402
metrics::set_gauge_vec(
13761403
&metrics::PEERS_CONNECTED_MULTI,
13771404
&[direction, transport],
1378-
*peers_connected_mutli
1405+
*peers_connected_multi
13791406
.get(&(direction, transport))
13801407
.unwrap_or(&0) as i64,
13811408
);

beacon_node/lighthouse_network/src/peer_manager/peerdb/peer_info.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ impl<E: EthSpec> PeerInfo<E> {
122122
self.connection_direction.as_ref()
123123
}
124124

125+
/// Returns true if this is an incoming ipv4 connection.
126+
pub fn is_incoming_ipv4_connection(&self) -> bool {
127+
self.seen_multiaddrs.iter().any(|multiaddr| {
128+
multiaddr
129+
.iter()
130+
.any(|protocol| matches!(protocol, libp2p::core::multiaddr::Protocol::Ip4(_)))
131+
})
132+
}
133+
134+
/// Returns true if this is an incoming ipv6 connection.
135+
pub fn is_incoming_ipv6_connection(&self) -> bool {
136+
self.seen_multiaddrs.iter().any(|multiaddr| {
137+
multiaddr
138+
.iter()
139+
.any(|protocol| matches!(protocol, libp2p::core::multiaddr::Protocol::Ip6(_)))
140+
})
141+
}
142+
125143
/// Returns the sync status of the peer.
126144
pub fn sync_status(&self) -> &SyncStatus {
127145
&self.sync_status

0 commit comments

Comments
 (0)