@@ -74,13 +74,14 @@ use wasm_timer::Instant;
74
74
/// manager. In other words, the peerset manager doesn't differentiate whether we are dialing a
75
75
/// node or connected to it.
76
76
///
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
81
81
/// is in sync with the peerset manager. Furtheremore, if a secondary connection fails
82
82
/// 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.
84
85
///
85
86
/// Additionally, there also exists a "banning" system. If we fail to dial a node, we "ban" it for
86
87
/// 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 {
144
145
///
145
146
/// We may still have ongoing requests to that peer, but it should cease shortly.
146
147
Disabled {
147
- /// The designated control connection.
148
+ /// The designated primary (usually the only) connection.
148
149
connection : Option < ConnectionId > ,
149
150
/// How we are connected to this peer.
150
151
connected_point : ConnectedPoint ,
@@ -160,7 +161,7 @@ enum PeerState {
160
161
/// will be enabled when `timer` fires. This peer can still perform Kademlia queries and such,
161
162
/// but should get disconnected in a few seconds.
162
163
DisabledPendingEnable {
163
- /// The designated control connection.
164
+ /// The designated primary (usually the only) connection.
164
165
connection : Option < ConnectionId > ,
165
166
/// How we are connected to this peer.
166
167
connected_point : ConnectedPoint ,
@@ -177,7 +178,7 @@ enum PeerState {
177
178
/// We are connected to this peer and the peerset has accepted it. The handler is in the
178
179
/// enabled state.
179
180
Enabled {
180
- /// The designated control connection.
181
+ /// The designated primary (usually the only) connection.
181
182
connection : Option < ConnectionId > ,
182
183
/// How we are connected to this peer.
183
184
connected_point : ConnectedPoint ,
@@ -189,7 +190,7 @@ enum PeerState {
189
190
/// is in initialization mode. We are waiting for the Accept or Reject from the peerset. There
190
191
/// is a corresponding entry in `incoming`.
191
192
Incoming {
192
- /// The designated control connection.
193
+ /// The designated primary (usually the only) connection.
193
194
connection : Option < ConnectionId > ,
194
195
/// How we are connected to this peer.
195
196
connected_point : ConnectedPoint ,
@@ -211,6 +212,8 @@ impl PeerState {
211
212
}
212
213
}
213
214
215
+ /// The ID designated primary connection, if one is assigned
216
+ /// w.r.t. the current state of the peer.
214
217
fn connection ( & self ) -> & Option < ConnectionId > {
215
218
match self {
216
219
PeerState :: Incoming { ref connection, .. } |
@@ -988,7 +991,7 @@ impl NetworkBehaviour for GenericProto {
988
991
fn inject_event (
989
992
& mut self ,
990
993
source : PeerId ,
991
- _connection : ConnectionId ,
994
+ connection : ConnectionId ,
992
995
event : NotifsHandlerOut ,
993
996
) {
994
997
match event {
@@ -1003,16 +1006,15 @@ impl NetworkBehaviour for GenericProto {
1003
1006
} ;
1004
1007
1005
1008
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 , .. }
1010
1013
if connected_point == & endpoint =>
1011
1014
{
1012
- // We take that handler as the handler of our control connection
1015
+ // We take that handler as the one of our primary connection
1013
1016
// 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) ) ;
1016
1018
}
1017
1019
_ => { }
1018
1020
}
@@ -1028,7 +1030,7 @@ impl NetworkBehaviour for GenericProto {
1028
1030
if let Some ( event) = event {
1029
1031
self . events . push ( NetworkBehaviourAction :: NotifyHandler {
1030
1032
peer_id : source,
1031
- handler : NotifyHandler :: One ( _connection ) ,
1033
+ handler : NotifyHandler :: One ( connection ) ,
1032
1034
event
1033
1035
} ) ;
1034
1036
}
@@ -1043,8 +1045,9 @@ impl NetworkBehaviour for GenericProto {
1043
1045
return
1044
1046
} ;
1045
1047
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) {
1048
1051
debug ! ( target: "sub-libp2p" , "Secondary connection closed by {:?}" , reason) ;
1049
1052
return
1050
1053
}
@@ -1110,16 +1113,23 @@ impl NetworkBehaviour for GenericProto {
1110
1113
NotifsHandlerOut :: Open => {
1111
1114
debug ! ( target: "sub-libp2p" , "Handler({:?}) => Open" , source) ;
1112
1115
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." ) ;
1119
1128
return
1120
1129
}
1121
1130
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" ) ;
1123
1133
return
1124
1134
}
1125
1135
* open = true ;
@@ -1198,13 +1208,15 @@ impl NetworkBehaviour for GenericProto {
1198
1208
}
1199
1209
1200
1210
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: {:?}" ,
1203
1214
source, error) ;
1204
1215
return
1205
1216
}
1206
1217
1207
- debug ! ( target: "sub-libp2p" , "Handler({:?}) => Severe protocol error: {:?}" ,
1218
+ debug ! ( target: "sub-libp2p" ,
1219
+ "Handler({:?}) => Severe protocol error: {:?}" ,
1208
1220
source, error) ;
1209
1221
// A severe protocol error happens when we detect a "bad" node, such as a node on
1210
1222
// a different chain, or a node that doesn't speak the same protocol(s). We
0 commit comments