Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Finishing touches.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman S. Borschel committed Apr 3, 2020
1 parent e28f642 commit 31cdf4f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 101 deletions.
30 changes: 18 additions & 12 deletions client/network/src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,27 @@ struct NodeInfo {
/// When we will remove the entry about this node from the list, or `None` if we're connected
/// to the node.
info_expire: Option<Instant>,
/// How we're connected to the node.
endpoints: SmallVec<[ConnectedPoint; 2]>,
/// Non-empty list of connected endpoints, one per connection.
endpoints: SmallVec<[ConnectedPoint; crate::MAX_CONNECTIONS_PER_PEER]>,
/// Version reported by the remote, or `None` if unknown.
client_version: Option<String>,
/// Latest ping time with this node.
latest_ping: Option<Duration>,
}

impl NodeInfo {
fn new(endpoint: ConnectedPoint) -> Self {
let mut endpoints = SmallVec::new();
endpoints.push(endpoint);
NodeInfo {
info_expire: None,
endpoints,
client_version: None,
latest_ping: None,
}
}
}

impl DebugInfoBehaviour {
/// Builds a new `DebugInfoBehaviour`.
pub fn new(
Expand Down Expand Up @@ -122,9 +135,9 @@ impl DebugInfoBehaviour {
pub struct Node<'a>(&'a NodeInfo);

impl<'a> Node<'a> {
/// Returns the endpoint we are connected to or were last connected to.
/// Returns the endpoint of an established connection to the peer.
pub fn endpoint(&self) -> &'a ConnectedPoint {
&self.0.endpoints[0] // TODO: Multiple?
&self.0.endpoints[0] // `endpoints` are non-empty by definition
}

/// Returns the latest version information we know of.
Expand Down Expand Up @@ -179,14 +192,7 @@ impl NetworkBehaviour for DebugInfoBehaviour {
self.identify.inject_connection_established(peer_id, conn, endpoint);
match self.nodes_info.entry(peer_id.clone()) {
Entry::Vacant(e) => {
let mut endpoints = SmallVec::new();
endpoints.push(endpoint.clone());
e.insert(NodeInfo {
info_expire: None,
endpoints,
client_version: None,
latest_ping: None,
});
e.insert(NodeInfo::new(endpoint.clone()));
}
Entry::Occupied(e) => {
let e = e.into_mut();
Expand Down
9 changes: 9 additions & 0 deletions client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,12 @@ pub use libp2p::{Multiaddr, PeerId};
pub use libp2p::multiaddr;

pub use sc_peerset::ReputationChange;

/// The maximum allowed number of established connections per peer.
///
/// Typically, and by design of the network behaviours in this crate,
/// there is a single established connection per peer. However, to
/// avoid unnecessary and nondeterministic connection closure in
/// case of (possibly repeated) simultaneous dialing attempts between
/// two peers, the per-peer connection limit is not set to 1 but 2.
const MAX_CONNECTIONS_PER_PEER: usize = 2;
6 changes: 3 additions & 3 deletions client/network/src/protocol/generic_proto/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ enum PeerState {
/// We may still have ongoing traffic with that peer, but it should cease shortly.
Disabled {
/// The connections that are currently open for custom protocol traffic.
open: SmallVec<[ConnectionId; 2]>,
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
/// If `Some`, any dial attempts to this peer are delayed until the given `Instant`.
banned_until: Option<Instant>,
},
Expand All @@ -177,7 +177,7 @@ enum PeerState {
/// but should get disconnected in a few seconds.
DisabledPendingEnable {
/// The connections that are currently open for custom protocol traffic.
open: SmallVec<[ConnectionId; 2]>,
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
/// When to enable this remote.
timer: futures_timer::Delay,
/// When the `timer` will trigger.
Expand All @@ -188,7 +188,7 @@ enum PeerState {
/// enabled state.
Enabled {
/// The connections that are currently open for custom protocol traffic.
open: SmallVec<[ConnectionId; 2]>,
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
},

/// We received an incoming connection from this peer and forwarded that
Expand Down
Loading

0 comments on commit 31cdf4f

Please sign in to comment.