diff --git a/beacon_node/eth2_libp2p/src/peer_manager/peer_info.rs b/beacon_node/eth2_libp2p/src/peer_manager/peer_info.rs index f0872968e..71025ee52 100644 --- a/beacon_node/eth2_libp2p/src/peer_manager/peer_info.rs +++ b/beacon_node/eth2_libp2p/src/peer_manager/peer_info.rs @@ -43,6 +43,9 @@ pub struct PeerInfo { pub min_ttl: Option, /// Is the peer a trusted peer. pub is_trusted: bool, + /// Direction of the first connection of the last (or current) connected session with this peer. + /// None if this peer was never connected. + pub connection_direction: Option, } impl Default for PeerInfo { @@ -58,6 +61,7 @@ impl Default for PeerInfo { meta_data: None, min_ttl: None, is_trusted: false, + connection_direction: None, } } } @@ -112,6 +116,30 @@ impl PeerInfo { } } + /// Modifies the status to Connected and increases the number of ingoing + /// connections by one + pub(crate) fn connect_ingoing(&mut self) { + match &mut self.connection_status { + Connected { n_in, .. } => *n_in += 1, + Disconnected { .. } | Banned { .. } | Dialing { .. } | Unknown => { + self.connection_status = Connected { n_in: 1, n_out: 0 }; + self.connection_direction = Some(ConnectionDirection::Incoming); + } + } + } + + /// Modifies the status to Connected and increases the number of outgoing + /// connections by one + pub(crate) fn connect_outgoing(&mut self) { + match &mut self.connection_status { + Connected { n_out, .. } => *n_out += 1, + Disconnected { .. } | Banned { .. } | Dialing { .. } | Unknown => { + self.connection_status = Connected { n_in: 0, n_out: 1 }; + self.connection_direction = Some(ConnectionDirection::Outgoing); + } + } + } + #[cfg(test)] /// Add an f64 to a non-trusted peer's score abiding by the limits. pub fn add_to_score(&mut self, score: f64) { @@ -136,6 +164,13 @@ impl Default for PeerStatus { } } +/// Connection Direction of connection. +#[derive(Debug, Clone, Serialize)] +pub enum ConnectionDirection { + Incoming, + Outgoing, +} + /// Connection Status of the peer. #[derive(Debug, Clone)] pub enum PeerConnectionStatus { @@ -251,28 +286,6 @@ impl PeerConnectionStatus { matches!(self, Disconnected { .. }) } - /// Modifies the status to Connected and increases the number of ingoing - /// connections by one - pub fn connect_ingoing(&mut self) { - match self { - Connected { n_in, .. } => *n_in += 1, - Disconnected { .. } | Banned { .. } | Dialing { .. } | Unknown => { - *self = Connected { n_in: 1, n_out: 0 } - } - } - } - - /// Modifies the status to Connected and increases the number of outgoing - /// connections by one - pub fn connect_outgoing(&mut self) { - match self { - Connected { n_out, .. } => *n_out += 1, - Disconnected { .. } | Banned { .. } | Dialing { .. } | Unknown => { - *self = Connected { n_in: 0, n_out: 1 } - } - } - } - /// Modifies the status to Disconnected and sets the last seen instant to now pub fn disconnect(&mut self) { *self = Disconnected { diff --git a/beacon_node/eth2_libp2p/src/peer_manager/peerdb.rs b/beacon_node/eth2_libp2p/src/peer_manager/peerdb.rs index 9a7914750..9f9a4296c 100644 --- a/beacon_node/eth2_libp2p/src/peer_manager/peerdb.rs +++ b/beacon_node/eth2_libp2p/src/peer_manager/peerdb.rs @@ -365,7 +365,7 @@ impl PeerDB { } self.banned_peers_count .remove_banned_peer(&info.connection_status); - info.connection_status.connect_ingoing(); + info.connect_ingoing(); // Add the seen ip address to the peer's info if let Some(ip_addr) = multiaddr.iter().find_map(|p| match p { @@ -386,7 +386,7 @@ impl PeerDB { } self.banned_peers_count .remove_banned_peer(&info.connection_status); - info.connection_status.connect_outgoing(); + info.connect_outgoing(); // Add the seen ip address to the peer's info if let Some(ip_addr) = multiaddr.iter().find_map(|p| match p {