Skip to content

Commit f5a6ac3

Browse files
AgeManningpawanjay176
authored andcommitted
Track multiaddr in connection status (sigp#5308)
* Record the multiaddr for connected peers
1 parent 6dd7547 commit f5a6ac3

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

beacon_node/lighthouse_network/src/peer_manager/peerdb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,8 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
756756

757757
// Update the connection state
758758
match direction {
759-
ConnectionDirection::Incoming => info.connect_ingoing(Some(seen_address)),
760-
ConnectionDirection::Outgoing => info.connect_outgoing(Some(seen_address)),
759+
ConnectionDirection::Incoming => info.connect_ingoing(seen_address),
760+
ConnectionDirection::Outgoing => info.connect_outgoing(seen_address),
761761
}
762762
}
763763

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@ impl<T: EthSpec> PeerInfo<T> {
307307

308308
/// Checks if the peer is outbound-only
309309
pub fn is_outbound_only(&self) -> bool {
310-
matches!(self.connection_status, Connected {n_in, n_out} if n_in == 0 && n_out > 0)
310+
matches!(self.connection_status, Connected {n_in, n_out, ..} if n_in == 0 && n_out > 0)
311311
}
312312

313313
/// Returns the number of connections with this peer.
314314
pub fn connections(&self) -> (u8, u8) {
315315
match self.connection_status {
316-
Connected { n_in, n_out } => (n_in, n_out),
316+
Connected { n_in, n_out, .. } => (n_in, n_out),
317317
_ => (0, 0),
318318
}
319319
}
@@ -421,41 +421,45 @@ impl<T: EthSpec> PeerInfo<T> {
421421

422422
/// Modifies the status to Connected and increases the number of ingoing
423423
/// connections by one
424-
pub(super) fn connect_ingoing(&mut self, seen_multiaddr: Option<Multiaddr>) {
424+
pub(super) fn connect_ingoing(&mut self, multiaddr: Multiaddr) {
425+
self.seen_multiaddrs.insert(multiaddr.clone());
426+
425427
match &mut self.connection_status {
426428
Connected { n_in, .. } => *n_in += 1,
427429
Disconnected { .. }
428430
| Banned { .. }
429431
| Dialing { .. }
430432
| Disconnecting { .. }
431433
| Unknown => {
432-
self.connection_status = Connected { n_in: 1, n_out: 0 };
434+
self.connection_status = Connected {
435+
n_in: 1,
436+
n_out: 0,
437+
multiaddr,
438+
};
433439
self.connection_direction = Some(ConnectionDirection::Incoming);
434440
}
435441
}
436-
437-
if let Some(multiaddr) = seen_multiaddr {
438-
self.seen_multiaddrs.insert(multiaddr);
439-
}
440442
}
441443

442444
/// Modifies the status to Connected and increases the number of outgoing
443445
/// connections by one
444-
pub(super) fn connect_outgoing(&mut self, seen_multiaddr: Option<Multiaddr>) {
446+
pub(super) fn connect_outgoing(&mut self, multiaddr: Multiaddr) {
447+
self.seen_multiaddrs.insert(multiaddr.clone());
445448
match &mut self.connection_status {
446449
Connected { n_out, .. } => *n_out += 1,
447450
Disconnected { .. }
448451
| Banned { .. }
449452
| Dialing { .. }
450453
| Disconnecting { .. }
451454
| Unknown => {
452-
self.connection_status = Connected { n_in: 0, n_out: 1 };
455+
self.connection_status = Connected {
456+
n_in: 0,
457+
n_out: 1,
458+
multiaddr,
459+
};
453460
self.connection_direction = Some(ConnectionDirection::Outgoing);
454461
}
455462
}
456-
if let Some(multiaddr) = seen_multiaddr {
457-
self.seen_multiaddrs.insert(multiaddr);
458-
}
459463
}
460464

461465
#[cfg(test)]
@@ -487,6 +491,8 @@ pub enum ConnectionDirection {
487491
pub enum PeerConnectionStatus {
488492
/// The peer is connected.
489493
Connected {
494+
/// The multiaddr that we are connected via.
495+
multiaddr: Multiaddr,
490496
/// number of ingoing connections.
491497
n_in: u8,
492498
/// number of outgoing connections.
@@ -522,7 +528,12 @@ impl Serialize for PeerConnectionStatus {
522528
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
523529
let mut s = serializer.serialize_struct("connection_status", 6)?;
524530
match self {
525-
Connected { n_in, n_out } => {
531+
Connected {
532+
n_in,
533+
n_out,
534+
multiaddr,
535+
} => {
536+
s.serialize_field("multiaddr", multiaddr)?;
526537
s.serialize_field("status", "connected")?;
527538
s.serialize_field("connections_in", n_in)?;
528539
s.serialize_field("connections_out", n_out)?;

0 commit comments

Comments
 (0)