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

Commit fd9134a

Browse files
author
Roman S. Borschel
committed
Finishing touches.
1 parent 309c75c commit fd9134a

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
@@ -163,7 +163,7 @@ enum PeerState {
163163
/// We may still have ongoing traffic with that peer, but it should cease shortly.
164164
Disabled {
165165
/// The connections that are currently open for custom protocol traffic.
166-
open: SmallVec<[ConnectionId; 2]>,
166+
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
167167
/// If `Some`, any dial attempts to this peer are delayed until the given `Instant`.
168168
banned_until: Option<Instant>,
169169
},
@@ -173,7 +173,7 @@ enum PeerState {
173173
/// but should get disconnected in a few seconds.
174174
DisabledPendingEnable {
175175
/// The connections that are currently open for custom protocol traffic.
176-
open: SmallVec<[ConnectionId; 2]>,
176+
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
177177
/// When to enable this remote.
178178
timer: futures_timer::Delay,
179179
/// When the `timer` will trigger.
@@ -184,7 +184,7 @@ enum PeerState {
184184
/// enabled state.
185185
Enabled {
186186
/// The connections that are currently open for custom protocol traffic.
187-
open: SmallVec<[ConnectionId; 2]>,
187+
open: SmallVec<[ConnectionId; crate::MAX_CONNECTIONS_PER_PEER]>,
188188
},
189189

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

0 commit comments

Comments
 (0)