Track multiaddr in connection status (#5308)
* Record the multiaddr for connected peers
This commit is contained in:
parent
744c598b1c
commit
d36241b4a1
@ -756,8 +756,8 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
|
|||||||
|
|
||||||
// Update the connection state
|
// Update the connection state
|
||||||
match direction {
|
match direction {
|
||||||
ConnectionDirection::Incoming => info.connect_ingoing(Some(seen_address)),
|
ConnectionDirection::Incoming => info.connect_ingoing(seen_address),
|
||||||
ConnectionDirection::Outgoing => info.connect_outgoing(Some(seen_address)),
|
ConnectionDirection::Outgoing => info.connect_outgoing(seen_address),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,13 +307,13 @@ impl<T: EthSpec> PeerInfo<T> {
|
|||||||
|
|
||||||
/// Checks if the peer is outbound-only
|
/// Checks if the peer is outbound-only
|
||||||
pub fn is_outbound_only(&self) -> bool {
|
pub fn is_outbound_only(&self) -> bool {
|
||||||
matches!(self.connection_status, Connected {n_in, n_out} if n_in == 0 && n_out > 0)
|
matches!(self.connection_status, Connected {n_in, n_out, ..} if n_in == 0 && n_out > 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of connections with this peer.
|
/// Returns the number of connections with this peer.
|
||||||
pub fn connections(&self) -> (u8, u8) {
|
pub fn connections(&self) -> (u8, u8) {
|
||||||
match self.connection_status {
|
match self.connection_status {
|
||||||
Connected { n_in, n_out } => (n_in, n_out),
|
Connected { n_in, n_out, .. } => (n_in, n_out),
|
||||||
_ => (0, 0),
|
_ => (0, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -421,7 +421,9 @@ impl<T: EthSpec> PeerInfo<T> {
|
|||||||
|
|
||||||
/// Modifies the status to Connected and increases the number of ingoing
|
/// Modifies the status to Connected and increases the number of ingoing
|
||||||
/// connections by one
|
/// connections by one
|
||||||
pub(super) fn connect_ingoing(&mut self, seen_multiaddr: Option<Multiaddr>) {
|
pub(super) fn connect_ingoing(&mut self, multiaddr: Multiaddr) {
|
||||||
|
self.seen_multiaddrs.insert(multiaddr.clone());
|
||||||
|
|
||||||
match &mut self.connection_status {
|
match &mut self.connection_status {
|
||||||
Connected { n_in, .. } => *n_in += 1,
|
Connected { n_in, .. } => *n_in += 1,
|
||||||
Disconnected { .. }
|
Disconnected { .. }
|
||||||
@ -429,19 +431,20 @@ impl<T: EthSpec> PeerInfo<T> {
|
|||||||
| Dialing { .. }
|
| Dialing { .. }
|
||||||
| Disconnecting { .. }
|
| Disconnecting { .. }
|
||||||
| Unknown => {
|
| Unknown => {
|
||||||
self.connection_status = Connected { n_in: 1, n_out: 0 };
|
self.connection_status = Connected {
|
||||||
|
n_in: 1,
|
||||||
|
n_out: 0,
|
||||||
|
multiaddr,
|
||||||
|
};
|
||||||
self.connection_direction = Some(ConnectionDirection::Incoming);
|
self.connection_direction = Some(ConnectionDirection::Incoming);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(multiaddr) = seen_multiaddr {
|
|
||||||
self.seen_multiaddrs.insert(multiaddr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Modifies the status to Connected and increases the number of outgoing
|
/// Modifies the status to Connected and increases the number of outgoing
|
||||||
/// connections by one
|
/// connections by one
|
||||||
pub(super) fn connect_outgoing(&mut self, seen_multiaddr: Option<Multiaddr>) {
|
pub(super) fn connect_outgoing(&mut self, multiaddr: Multiaddr) {
|
||||||
|
self.seen_multiaddrs.insert(multiaddr.clone());
|
||||||
match &mut self.connection_status {
|
match &mut self.connection_status {
|
||||||
Connected { n_out, .. } => *n_out += 1,
|
Connected { n_out, .. } => *n_out += 1,
|
||||||
Disconnected { .. }
|
Disconnected { .. }
|
||||||
@ -449,13 +452,14 @@ impl<T: EthSpec> PeerInfo<T> {
|
|||||||
| Dialing { .. }
|
| Dialing { .. }
|
||||||
| Disconnecting { .. }
|
| Disconnecting { .. }
|
||||||
| Unknown => {
|
| Unknown => {
|
||||||
self.connection_status = Connected { n_in: 0, n_out: 1 };
|
self.connection_status = Connected {
|
||||||
|
n_in: 0,
|
||||||
|
n_out: 1,
|
||||||
|
multiaddr,
|
||||||
|
};
|
||||||
self.connection_direction = Some(ConnectionDirection::Outgoing);
|
self.connection_direction = Some(ConnectionDirection::Outgoing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(multiaddr) = seen_multiaddr {
|
|
||||||
self.seen_multiaddrs.insert(multiaddr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -487,6 +491,8 @@ pub enum ConnectionDirection {
|
|||||||
pub enum PeerConnectionStatus {
|
pub enum PeerConnectionStatus {
|
||||||
/// The peer is connected.
|
/// The peer is connected.
|
||||||
Connected {
|
Connected {
|
||||||
|
/// The multiaddr that we are connected via.
|
||||||
|
multiaddr: Multiaddr,
|
||||||
/// number of ingoing connections.
|
/// number of ingoing connections.
|
||||||
n_in: u8,
|
n_in: u8,
|
||||||
/// number of outgoing connections.
|
/// number of outgoing connections.
|
||||||
@ -522,7 +528,12 @@ impl Serialize for PeerConnectionStatus {
|
|||||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
let mut s = serializer.serialize_struct("connection_status", 6)?;
|
let mut s = serializer.serialize_struct("connection_status", 6)?;
|
||||||
match self {
|
match self {
|
||||||
Connected { n_in, n_out } => {
|
Connected {
|
||||||
|
n_in,
|
||||||
|
n_out,
|
||||||
|
multiaddr,
|
||||||
|
} => {
|
||||||
|
s.serialize_field("multiaddr", multiaddr)?;
|
||||||
s.serialize_field("status", "connected")?;
|
s.serialize_field("status", "connected")?;
|
||||||
s.serialize_field("connections_in", n_in)?;
|
s.serialize_field("connections_in", n_in)?;
|
||||||
s.serialize_field("connections_out", n_out)?;
|
s.serialize_field("connections_out", n_out)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user