17
17
use fnv:: FnvHashMap ;
18
18
use futures:: prelude:: * ;
19
19
use libp2p:: Multiaddr ;
20
- use libp2p:: core:: nodes :: listeners :: ListenerId ;
20
+ use libp2p:: core:: connection :: { ConnectionId , ListenerId } ;
21
21
use libp2p:: core:: { ConnectedPoint , either:: EitherOutput , PeerId , PublicKey } ;
22
22
use libp2p:: swarm:: { IntoProtocolsHandler , IntoProtocolsHandlerSelect , ProtocolsHandler } ;
23
23
use libp2p:: swarm:: { NetworkBehaviour , NetworkBehaviourAction , PollParameters } ;
24
24
use libp2p:: identify:: { Identify , IdentifyEvent , IdentifyInfo } ;
25
25
use libp2p:: ping:: { Ping , PingConfig , PingEvent , PingSuccess } ;
26
26
use log:: { debug, trace, error} ;
27
- use std:: error;
27
+ use smallvec:: SmallVec ;
28
+ use std:: { error, io} ;
28
29
use std:: collections:: hash_map:: Entry ;
29
30
use std:: pin:: Pin ;
30
31
use std:: task:: { Context , Poll } ;
@@ -56,14 +57,27 @@ struct NodeInfo {
56
57
/// When we will remove the entry about this node from the list, or `None` if we're connected
57
58
/// to the node.
58
59
info_expire : Option < Instant > ,
59
- /// How we're connected to the node .
60
- endpoint : ConnectedPoint ,
60
+ /// Non-empty list of connected endpoints, one per connection .
61
+ endpoints : SmallVec < [ ConnectedPoint ; crate :: MAX_CONNECTIONS_PER_PEER ] > ,
61
62
/// Version reported by the remote, or `None` if unknown.
62
63
client_version : Option < String > ,
63
64
/// Latest ping time with this node.
64
65
latest_ping : Option < Duration > ,
65
66
}
66
67
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
+
67
81
impl DebugInfoBehaviour {
68
82
/// Builds a new `DebugInfoBehaviour`.
69
83
pub fn new (
@@ -121,9 +135,9 @@ impl DebugInfoBehaviour {
121
135
pub struct Node < ' a > ( & ' a NodeInfo ) ;
122
136
123
137
impl < ' a > Node < ' a > {
124
- /// Returns the endpoint we are connected to or were last connected to .
138
+ /// Returns the endpoint of an established connection to the peer .
125
139
pub fn endpoint ( & self ) -> & ' a ConnectedPoint {
126
- & self . 0 . endpoint
140
+ & self . 0 . endpoints [ 0 ] // `endpoints` are non-empty by definition
127
141
}
128
142
129
143
/// Returns the latest version information we know of.
@@ -168,18 +182,17 @@ impl NetworkBehaviour for DebugInfoBehaviour {
168
182
list
169
183
}
170
184
171
- fn inject_connected ( & mut self , peer_id : PeerId , endpoint : ConnectedPoint ) {
172
- self . ping . inject_connected ( peer_id. clone ( ) , endpoint. clone ( ) ) ;
173
- self . identify . inject_connected ( peer_id. clone ( ) , endpoint. clone ( ) ) ;
185
+ fn inject_connected ( & mut self , peer_id : & PeerId ) {
186
+ self . ping . inject_connected ( peer_id) ;
187
+ self . identify . inject_connected ( peer_id) ;
188
+ }
174
189
175
- match self . nodes_info . entry ( peer_id) {
190
+ fn inject_connection_established ( & mut self , peer_id : & PeerId , conn : & ConnectionId , endpoint : & ConnectedPoint ) {
191
+ self . ping . inject_connection_established ( peer_id, conn, endpoint) ;
192
+ self . identify . inject_connection_established ( peer_id, conn, endpoint) ;
193
+ match self . nodes_info . entry ( peer_id. clone ( ) ) {
176
194
Entry :: Vacant ( e) => {
177
- e. insert ( NodeInfo {
178
- info_expire : None ,
179
- endpoint,
180
- client_version : None ,
181
- latest_ping : None ,
182
- } ) ;
195
+ e. insert ( NodeInfo :: new ( endpoint. clone ( ) ) ) ;
183
196
}
184
197
Entry :: Occupied ( e) => {
185
198
let e = e. into_mut ( ) ;
@@ -188,14 +201,26 @@ impl NetworkBehaviour for DebugInfoBehaviour {
188
201
e. latest_ping = None ;
189
202
}
190
203
e. info_expire = None ;
191
- e. endpoint = endpoint ;
204
+ e. endpoints . push ( endpoint. clone ( ) ) ;
192
205
}
193
206
}
194
207
}
195
208
196
- fn inject_disconnected ( & mut self , peer_id : & PeerId , endpoint : ConnectedPoint ) {
197
- self . ping . inject_disconnected ( peer_id, endpoint. clone ( ) ) ;
198
- self . identify . inject_disconnected ( peer_id, endpoint) ;
209
+ fn inject_connection_closed ( & mut self , peer_id : & PeerId , conn : & ConnectionId , endpoint : & ConnectedPoint ) {
210
+ self . ping . inject_connection_closed ( peer_id, conn, endpoint) ;
211
+ self . identify . inject_connection_closed ( peer_id, conn, endpoint) ;
212
+
213
+ if let Some ( entry) = self . nodes_info . get_mut ( peer_id) {
214
+ entry. endpoints . retain ( |ep| ep != endpoint)
215
+ } else {
216
+ error ! ( target: "sub-libp2p" ,
217
+ "Unknown connection to {:?} closed: {:?}" , peer_id, endpoint) ;
218
+ }
219
+ }
220
+
221
+ fn inject_disconnected ( & mut self , peer_id : & PeerId ) {
222
+ self . ping . inject_disconnected ( peer_id) ;
223
+ self . identify . inject_disconnected ( peer_id) ;
199
224
200
225
if let Some ( entry) = self . nodes_info . get_mut ( peer_id) {
201
226
entry. info_expire = Some ( Instant :: now ( ) + CACHE_EXPIRE ) ;
@@ -205,26 +230,15 @@ impl NetworkBehaviour for DebugInfoBehaviour {
205
230
}
206
231
}
207
232
208
- fn inject_node_event (
233
+ fn inject_event (
209
234
& mut self ,
210
235
peer_id : PeerId ,
236
+ connection : ConnectionId ,
211
237
event : <<Self :: ProtocolsHandler as IntoProtocolsHandler >:: Handler as ProtocolsHandler >:: OutEvent
212
238
) {
213
239
match event {
214
- EitherOutput :: First ( event) => self . ping . inject_node_event ( peer_id, event) ,
215
- EitherOutput :: Second ( event) => self . identify . inject_node_event ( peer_id, event) ,
216
- }
217
- }
218
-
219
- fn inject_replaced ( & mut self , peer_id : PeerId , closed_endpoint : ConnectedPoint , new_endpoint : ConnectedPoint ) {
220
- self . ping . inject_replaced ( peer_id. clone ( ) , closed_endpoint. clone ( ) , new_endpoint. clone ( ) ) ;
221
- self . identify . inject_replaced ( peer_id. clone ( ) , closed_endpoint, new_endpoint. clone ( ) ) ;
222
-
223
- if let Some ( entry) = self . nodes_info . get_mut ( & peer_id) {
224
- entry. endpoint = new_endpoint;
225
- } else {
226
- error ! ( target: "sub-libp2p" ,
227
- "Disconnected from node we were not connected to {:?}" , peer_id) ;
240
+ EitherOutput :: First ( event) => self . ping . inject_event ( peer_id, connection, event) ,
241
+ EitherOutput :: Second ( event) => self . identify . inject_event ( peer_id, connection, event) ,
228
242
}
229
243
}
230
244
@@ -258,9 +272,9 @@ impl NetworkBehaviour for DebugInfoBehaviour {
258
272
self . identify . inject_listener_error ( id, err) ;
259
273
}
260
274
261
- fn inject_listener_closed ( & mut self , id : ListenerId ) {
262
- self . ping . inject_listener_closed ( id) ;
263
- self . identify . inject_listener_closed ( id) ;
275
+ fn inject_listener_closed ( & mut self , id : ListenerId , reason : Result < ( ) , & io :: Error > ) {
276
+ self . ping . inject_listener_closed ( id, reason ) ;
277
+ self . identify . inject_listener_closed ( id, reason ) ;
264
278
}
265
279
266
280
fn poll (
@@ -283,11 +297,12 @@ impl NetworkBehaviour for DebugInfoBehaviour {
283
297
} ,
284
298
Poll :: Ready ( NetworkBehaviourAction :: DialAddress { address } ) =>
285
299
return Poll :: Ready ( NetworkBehaviourAction :: DialAddress { address } ) ,
286
- Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id } ) =>
287
- return Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id } ) ,
288
- Poll :: Ready ( NetworkBehaviourAction :: SendEvent { peer_id, event } ) =>
289
- return Poll :: Ready ( NetworkBehaviourAction :: SendEvent {
300
+ Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id, condition } ) =>
301
+ return Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id, condition } ) ,
302
+ Poll :: Ready ( NetworkBehaviourAction :: NotifyHandler { peer_id, handler , event } ) =>
303
+ return Poll :: Ready ( NetworkBehaviourAction :: NotifyHandler {
290
304
peer_id,
305
+ handler,
291
306
event : EitherOutput :: First ( event)
292
307
} ) ,
293
308
Poll :: Ready ( NetworkBehaviourAction :: ReportObservedAddr { address } ) =>
@@ -312,11 +327,12 @@ impl NetworkBehaviour for DebugInfoBehaviour {
312
327
} ,
313
328
Poll :: Ready ( NetworkBehaviourAction :: DialAddress { address } ) =>
314
329
return Poll :: Ready ( NetworkBehaviourAction :: DialAddress { address } ) ,
315
- Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id } ) =>
316
- return Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id } ) ,
317
- Poll :: Ready ( NetworkBehaviourAction :: SendEvent { peer_id, event } ) =>
318
- return Poll :: Ready ( NetworkBehaviourAction :: SendEvent {
330
+ Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id, condition } ) =>
331
+ return Poll :: Ready ( NetworkBehaviourAction :: DialPeer { peer_id, condition } ) ,
332
+ Poll :: Ready ( NetworkBehaviourAction :: NotifyHandler { peer_id, handler , event } ) =>
333
+ return Poll :: Ready ( NetworkBehaviourAction :: NotifyHandler {
319
334
peer_id,
335
+ handler,
320
336
event : EitherOutput :: Second ( event)
321
337
} ) ,
322
338
Poll :: Ready ( NetworkBehaviourAction :: ReportObservedAddr { address } ) =>
0 commit comments