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

Commit af8748a

Browse files
author
Roman S. Borschel
committed
Some cleanup.
1 parent b8f20bb commit af8748a

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

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

+42-30
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@ use wasm_timer::Instant;
7474
/// manager. In other words, the peerset manager doesn't differentiate whether we are dialing a
7575
/// node or connected to it.
7676
///
77-
/// For each peer, one connection is designated as the "control connection". If there are
78-
/// multiple connections to a peer, which may currently be at most two and only as a result
79-
/// of "simultaneous" dialing, the enabled/disabled status of all secondary connections
80-
/// follows the status of the control connection and hence maintain a unified status that
77+
/// For each peer, one connection is designated as the "primary connection". If there are
78+
/// multiple connections to a peer, which may currently practically be at most two and only
79+
/// as a result of "simultaneous" dialing, the enabled/disabled status of all secondary connections
80+
/// follows the status of the primary connection and hence maintain a unified status that
8181
/// is in sync with the peerset manager. Furtheremore, if a secondary connection fails
8282
/// due to an error, it has no impact on the current state of the peer on the external
83-
/// API.
83+
/// API. Secondary connections can thus serve traffic but do not affect the status of
84+
/// a peer w.r.t. this behaviour and the associated peerset manager.
8485
///
8586
/// Additionally, there also exists a "banning" system. If we fail to dial a node, we "ban" it for
8687
/// a few seconds. If the PSM requests a node that is in the "banned" state, then we delay the
@@ -144,7 +145,7 @@ enum PeerState {
144145
///
145146
/// We may still have ongoing requests to that peer, but it should cease shortly.
146147
Disabled {
147-
/// The designated control connection.
148+
/// The designated primary (usually the only) connection.
148149
connection: Option<ConnectionId>,
149150
/// How we are connected to this peer.
150151
connected_point: ConnectedPoint,
@@ -160,7 +161,7 @@ enum PeerState {
160161
/// will be enabled when `timer` fires. This peer can still perform Kademlia queries and such,
161162
/// but should get disconnected in a few seconds.
162163
DisabledPendingEnable {
163-
/// The designated control connection.
164+
/// The designated primary (usually the only) connection.
164165
connection: Option<ConnectionId>,
165166
/// How we are connected to this peer.
166167
connected_point: ConnectedPoint,
@@ -177,7 +178,7 @@ enum PeerState {
177178
/// We are connected to this peer and the peerset has accepted it. The handler is in the
178179
/// enabled state.
179180
Enabled {
180-
/// The designated control connection.
181+
/// The designated primary (usually the only) connection.
181182
connection: Option<ConnectionId>,
182183
/// How we are connected to this peer.
183184
connected_point: ConnectedPoint,
@@ -189,7 +190,7 @@ enum PeerState {
189190
/// is in initialization mode. We are waiting for the Accept or Reject from the peerset. There
190191
/// is a corresponding entry in `incoming`.
191192
Incoming {
192-
/// The designated control connection.
193+
/// The designated primary (usually the only) connection.
193194
connection: Option<ConnectionId>,
194195
/// How we are connected to this peer.
195196
connected_point: ConnectedPoint,
@@ -211,6 +212,8 @@ impl PeerState {
211212
}
212213
}
213214

215+
/// The ID designated primary connection, if one is assigned
216+
/// w.r.t. the current state of the peer.
214217
fn connection(&self) -> &Option<ConnectionId> {
215218
match self {
216219
PeerState::Incoming { ref connection, .. } |
@@ -988,7 +991,7 @@ impl NetworkBehaviour for GenericProto {
988991
fn inject_event(
989992
&mut self,
990993
source: PeerId,
991-
_connection: ConnectionId,
994+
connection: ConnectionId,
992995
event: NotifsHandlerOut,
993996
) {
994997
match event {
@@ -1003,16 +1006,15 @@ impl NetworkBehaviour for GenericProto {
10031006
};
10041007

10051008
match entry.get_mut() {
1006-
PeerState::Enabled { connected_point, connection, .. } |
1007-
PeerState::Disabled { connected_point, connection, .. } |
1008-
PeerState::DisabledPendingEnable { connected_point, connection, .. } |
1009-
PeerState::Incoming { connected_point, connection, .. }
1009+
PeerState::Enabled { connected_point, connection: primary, .. } |
1010+
PeerState::Disabled { connected_point, connection: primary, .. } |
1011+
PeerState::DisabledPendingEnable { connected_point, connection: primary, .. } |
1012+
PeerState::Incoming { connected_point, connection: primary, .. }
10101013
if connected_point == &endpoint =>
10111014
{
1012-
// We take that handler as the handler of our control connection
1015+
// We take that handler as the one of our primary connection
10131016
// for this peer tracked in the `PeerState`.
1014-
*connection = connection.or(Some(_connection));
1015-
debug!(target: "sub-libp2p", "Control connection for {:?} set.", source);
1017+
*primary = primary.or(Some(connection));
10161018
}
10171019
_ => {}
10181020
}
@@ -1028,7 +1030,7 @@ impl NetworkBehaviour for GenericProto {
10281030
if let Some(event) = event {
10291031
self.events.push(NetworkBehaviourAction::NotifyHandler {
10301032
peer_id: source,
1031-
handler: NotifyHandler::One(_connection),
1033+
handler: NotifyHandler::One(connection),
10321034
event
10331035
});
10341036
}
@@ -1043,8 +1045,9 @@ impl NetworkBehaviour for GenericProto {
10431045
return
10441046
};
10451047

1046-
// Return early if this is not the connection we are tracking.
1047-
if entry.get().connection() != &Some(_connection) {
1048+
// Return early if this is not the primary connection w.r.t. the
1049+
// `PeerState` and PSM.
1050+
if entry.get().connection() != &Some(connection) {
10481051
debug!(target: "sub-libp2p", "Secondary connection closed by {:?}", reason);
10491052
return
10501053
}
@@ -1110,16 +1113,23 @@ impl NetworkBehaviour for GenericProto {
11101113
NotifsHandlerOut::Open => {
11111114
debug!(target: "sub-libp2p", "Handler({:?}) => Open", source);
11121115
let endpoint = match self.peers.get_mut(&source) {
1113-
Some(PeerState::Enabled { ref mut open, ref connected_point, connection }) |
1114-
Some(PeerState::DisabledPendingEnable { ref mut open, ref connected_point, connection, .. }) |
1115-
Some(PeerState::Disabled { ref mut open, ref connected_point, connection, .. }) => {
1116-
if connection != &Some(_connection) {
1117-
// It is a secondary connection.
1118-
debug!(target: "sub-libp2p", "Secondary connection opened custom protocol.");
1116+
Some(PeerState::Enabled {
1117+
ref mut open, connected_point, connection: primary
1118+
}) |
1119+
Some(PeerState::DisabledPendingEnable {
1120+
ref mut open, connected_point, connection: primary, ..
1121+
}) |
1122+
Some(PeerState::Disabled {
1123+
ref mut open, connected_point, connection: primary, ..
1124+
}) => {
1125+
if primary != &Some(connection) {
1126+
debug!(target: "sub-libp2p",
1127+
"Secondary connection opened custom protocol.");
11191128
return
11201129
}
11211130
if *open {
1122-
error!(target: "sub-libp2p", "Open: State mismatch in the custom protos handler");
1131+
error!(target: "sub-libp2p",
1132+
"Open: State mismatch in the custom protos handler");
11231133
return
11241134
}
11251135
*open = true;
@@ -1198,13 +1208,15 @@ impl NetworkBehaviour for GenericProto {
11981208
}
11991209

12001210
NotifsHandlerOut::ProtocolError { error, .. } => {
1201-
if self.peers.get(&source).and_then(|p| *p.connection()) != Some(_connection) {
1202-
debug!(target: "sub-libp2p", "Handler({:?}) => Secondary connection error: {:?}",
1211+
if self.peers.get(&source).and_then(|p| *p.connection()) != Some(connection) {
1212+
debug!(target: "sub-libp2p",
1213+
"Handler({:?}) => Secondary connection severe protocol error: {:?}",
12031214
source, error);
12041215
return
12051216
}
12061217

1207-
debug!(target: "sub-libp2p", "Handler({:?}) => Severe protocol error: {:?}",
1218+
debug!(target: "sub-libp2p",
1219+
"Handler({:?}) => Severe protocol error: {:?}",
12081220
source, error);
12091221
// A severe protocol error happens when we detect a "bad" node, such as a node on
12101222
// a different chain, or a node that doesn't speak the same protocol(s). We

0 commit comments

Comments
 (0)