add direction information to PeerInfo (#1768)

## Issue Addressed

NA

## Proposed Changes

Adds a direction field to `PeerConnectionStatus` that can be accessed by calling `is_outgoing` which will return `true` iff the peer is connected and the first connection was an outgoing one.
This commit is contained in:
blacktemplar 2020-10-16 05:24:21 +00:00
parent d7b9d0dd9f
commit 6ba997b88e
2 changed files with 37 additions and 24 deletions

View File

@ -43,6 +43,9 @@ pub struct PeerInfo<T: EthSpec> {
pub min_ttl: Option<Instant>, pub min_ttl: Option<Instant>,
/// Is the peer a trusted peer. /// Is the peer a trusted peer.
pub is_trusted: bool, 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<ConnectionDirection>,
} }
impl<TSpec: EthSpec> Default for PeerInfo<TSpec> { impl<TSpec: EthSpec> Default for PeerInfo<TSpec> {
@ -58,6 +61,7 @@ impl<TSpec: EthSpec> Default for PeerInfo<TSpec> {
meta_data: None, meta_data: None,
min_ttl: None, min_ttl: None,
is_trusted: false, is_trusted: false,
connection_direction: None,
} }
} }
} }
@ -112,6 +116,30 @@ impl<T: EthSpec> PeerInfo<T> {
} }
} }
/// 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)] #[cfg(test)]
/// Add an f64 to a non-trusted peer's score abiding by the limits. /// Add an f64 to a non-trusted peer's score abiding by the limits.
pub fn add_to_score(&mut self, score: f64) { 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. /// Connection Status of the peer.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum PeerConnectionStatus { pub enum PeerConnectionStatus {
@ -251,28 +286,6 @@ impl PeerConnectionStatus {
matches!(self, Disconnected { .. }) 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 /// Modifies the status to Disconnected and sets the last seen instant to now
pub fn disconnect(&mut self) { pub fn disconnect(&mut self) {
*self = Disconnected { *self = Disconnected {

View File

@ -365,7 +365,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
} }
self.banned_peers_count self.banned_peers_count
.remove_banned_peer(&info.connection_status); .remove_banned_peer(&info.connection_status);
info.connection_status.connect_ingoing(); info.connect_ingoing();
// Add the seen ip address to the peer's info // Add the seen ip address to the peer's info
if let Some(ip_addr) = multiaddr.iter().find_map(|p| match p { if let Some(ip_addr) = multiaddr.iter().find_map(|p| match p {
@ -386,7 +386,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
} }
self.banned_peers_count self.banned_peers_count
.remove_banned_peer(&info.connection_status); .remove_banned_peer(&info.connection_status);
info.connection_status.connect_outgoing(); info.connect_outgoing();
// Add the seen ip address to the peer's info // Add the seen ip address to the peer's info
if let Some(ip_addr) = multiaddr.iter().find_map(|p| match p { if let Some(ip_addr) = multiaddr.iter().find_map(|p| match p {