@@ -63,6 +63,8 @@ pub const MIN_OUTBOUND_ONLY_FACTOR: f32 = 0.2;
63
63
/// limit is 55, and we are at 55 peers, the following parameter provisions a few more slots of
64
64
/// dialing priority peers we need for validator duties.
65
65
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 ;
66
68
67
69
/// The main struct that handles peer's reputation and connection status.
68
70
pub struct PeerManager < E : EthSpec > {
@@ -1307,7 +1309,9 @@ impl<E: EthSpec> PeerManager<E> {
1307
1309
fn update_peer_count_metrics ( & self ) {
1308
1310
let mut peers_connected = 0 ;
1309
1311
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 ( ) ;
1311
1315
let mut peers_per_custody_subnet_count: HashMap < u64 , i64 > = HashMap :: new ( ) ;
1312
1316
1313
1317
for ( _, peer_info) in self . network_globals . peers . read ( ) . connected_peers ( ) {
@@ -1336,7 +1340,7 @@ impl<E: EthSpec> PeerManager<E> {
1336
1340
} )
1337
1341
} )
1338
1342
. unwrap_or ( "unknown" ) ;
1339
- * peers_connected_mutli
1343
+ * peers_connected_multi
1340
1344
. entry ( ( direction, transport) )
1341
1345
. or_default ( ) += 1 ;
1342
1346
@@ -1345,6 +1349,29 @@ impl<E: EthSpec> PeerManager<E> {
1345
1349
. entry ( meta_data. custody_subnet_count )
1346
1350
. or_default ( ) += 1 ;
1347
1351
}
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 ) ;
1348
1375
}
1349
1376
1350
1377
// PEERS_CONNECTED
@@ -1375,7 +1402,7 @@ impl<E: EthSpec> PeerManager<E> {
1375
1402
metrics:: set_gauge_vec (
1376
1403
& metrics:: PEERS_CONNECTED_MULTI ,
1377
1404
& [ direction, transport] ,
1378
- * peers_connected_mutli
1405
+ * peers_connected_multi
1379
1406
. get ( & ( direction, transport) )
1380
1407
. unwrap_or ( & 0 ) as i64 ,
1381
1408
) ;
0 commit comments