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

Commit 31cdf4f

Browse files
author
Roman S. Borschel
committed
Finishing touches.
1 parent e28f642 commit 31cdf4f

File tree

5 files changed

+160
-101
lines changed

5 files changed

+160
-101
lines changed

client/network/src/debug_info.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,27 @@ struct NodeInfo {
5757
/// When we will remove the entry about this node from the list, or `None` if we're connected
5858
/// to the node.
5959
info_expire: Option<Instant>,
60-
/// How we're connected to the node.
61-
endpoints: SmallVec<[ConnectedPoint; 2]>,
60+
/// Non-empty list of connected endpoints, one per connection.
61+
endpoints: SmallVec<[ConnectedPoint; crate::MAX_CONNECTIONS_PER_PEER]>,
6262
/// Version reported by the remote, or `None` if unknown.
6363
client_version: Option<String>,
6464
/// Latest ping time with this node.
6565
latest_ping: Option<Duration>,
6666
}
6767

68+
impl NodeInfo {
69+
fn new(endpoint: ConnectedPoint) -> Self {
70+
let mut endpoints = SmallVec::new();
71+
endpoints.push(endpoint);
72+
NodeInfo {
73+
info_expire: None,
74+
endpoints,
75+
client_version: None,
76+
latest_ping: None,
77+
}
78+
}
79+
}
80+
6881
impl DebugInfoBehaviour {
6982
/// Builds a new `DebugInfoBehaviour`.
7083
pub fn new(
@@ -122,9 +135,9 @@ impl DebugInfoBehaviour {
122135
pub struct Node<'a>(&'a NodeInfo);
123136

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

130143
/// Returns the latest version information we know of.
@@ -179,14 +192,7 @@ impl NetworkBehaviour for DebugInfoBehaviour {
179192
self.identify.inject_connection_established(peer_id, conn, endpoint);
180193
match self.nodes_info.entry(peer_id.clone()) {
181194
Entry::Vacant(e) => {
182-
let mut endpoints = SmallVec::new();
183-
endpoints.push(endpoint.clone());
184-
e.insert(NodeInfo {
185-
info_expire: None,
186-
endpoints,
187-
client_version: None,
188-
latest_ping: None,
189-
});
195+
e.insert(NodeInfo::new(endpoint.clone()));
190196
}
191197
Entry::Occupied(e) => {
192198
let e = e.into_mut();

client/network/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,12 @@ pub use libp2p::{Multiaddr, PeerId};
255255
pub use libp2p::multiaddr;
256256

257257
pub use sc_peerset::ReputationChange;
258+
259+
/// The maximum allowed number of established connections per peer.
260+
///
261+
/// Typically, and by design of the network behaviours in this crate,
262+
/// there is a single established connection per peer. However, to
263+
/// avoid unnecessary and nondeterministic connection closure in
264+
/// case of (possibly repeated) simultaneous dialing attempts between
265+
/// two peers, the per-peer connection limit is not set to 1 but 2.
266+
const MAX_CONNECTIONS_PER_PEER: usize = 2;

client/network/src/protocol/generic_proto/behaviour.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ enum PeerState {
167167
/// We may still have ongoing traffic with that peer, but it should cease shortly.
168168
Disabled {
169169
/// The connections that are currently open for custom protocol traffic.
170-
open: SmallVec<[ConnectionId; 2]>,
170+
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
171171
/// If `Some`, any dial attempts to this peer are delayed until the given `Instant`.
172172
banned_until: Option<Instant>,
173173
},
@@ -177,7 +177,7 @@ enum PeerState {
177177
/// but should get disconnected in a few seconds.
178178
DisabledPendingEnable {
179179
/// The connections that are currently open for custom protocol traffic.
180-
open: SmallVec<[ConnectionId; 2]>,
180+
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
181181
/// When to enable this remote.
182182
timer: futures_timer::Delay,
183183
/// When the `timer` will trigger.
@@ -188,7 +188,7 @@ enum PeerState {
188188
/// enabled state.
189189
Enabled {
190190
/// The connections that are currently open for custom protocol traffic.
191-
open: SmallVec<[ConnectionId; 2]>,
191+
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
192192
},
193193

194194
/// We received an incoming connection from this peer and forwarded that

0 commit comments

Comments
 (0)