19
19
// DEALINGS IN THE SOFTWARE.
20
20
21
21
use crate :: handler:: { self , Handler , InEvent } ;
22
- use crate :: protocol:: { Info , Protocol , UpgradeError } ;
22
+ use crate :: protocol:: { Info , UpgradeError } ;
23
23
use libp2p_core:: { multiaddr, ConnectedPoint , Endpoint , Multiaddr } ;
24
24
use libp2p_identity:: PeerId ;
25
25
use libp2p_identity:: PublicKey ;
26
26
use libp2p_swarm:: behaviour:: { ConnectionClosed , ConnectionEstablished , DialFailure , FromSwarm } ;
27
27
use libp2p_swarm:: {
28
28
AddressScore , ConnectionDenied , DialError , ExternalAddresses , ListenAddresses ,
29
- NetworkBehaviour , NotifyHandler , PollParameters , StreamProtocol , StreamUpgradeError ,
30
- THandlerInEvent , ToSwarm ,
29
+ NetworkBehaviour , NotifyHandler , PollParameters , StreamUpgradeError , THandlerInEvent , ToSwarm ,
31
30
} ;
32
31
use libp2p_swarm:: { ConnectionId , THandler , THandlerOutEvent } ;
33
32
use lru:: LruCache ;
@@ -50,10 +49,6 @@ pub struct Behaviour {
50
49
config : Config ,
51
50
/// For each peer we're connected to, the observed address to send back to it.
52
51
connected : HashMap < PeerId , HashMap < ConnectionId , Multiaddr > > ,
53
- /// Pending requests to be fulfilled, either `Handler` requests for `Behaviour` info
54
- /// to address identification requests, or push requests to peers
55
- /// with current information about the local peer.
56
- requests : Vec < Request > ,
57
52
/// Pending events to be emitted when polled.
58
53
events : VecDeque < ToSwarm < Event , InEvent > > ,
59
54
/// The addresses of all peers that we have discovered.
@@ -63,15 +58,6 @@ pub struct Behaviour {
63
58
external_addresses : ExternalAddresses ,
64
59
}
65
60
66
- /// A `Behaviour` request to be fulfilled, either `Handler` requests for `Behaviour` info
67
- /// to address identification requests, or push requests to peers
68
- /// with current information about the local peer.
69
- #[ derive( Debug , PartialEq , Eq ) ]
70
- struct Request {
71
- peer_id : PeerId ,
72
- protocol : Protocol ,
73
- }
74
-
75
61
/// Configuration for the [`identify::Behaviour`](Behaviour).
76
62
#[ non_exhaustive]
77
63
#[ derive( Debug , Clone ) ]
@@ -184,7 +170,6 @@ impl Behaviour {
184
170
Self {
185
171
config,
186
172
connected : HashMap :: new ( ) ,
187
- requests : Vec :: new ( ) ,
188
173
events : VecDeque :: new ( ) ,
189
174
discovered_peers,
190
175
listen_addresses : Default :: default ( ) ,
@@ -203,13 +188,11 @@ impl Behaviour {
203
188
continue ;
204
189
}
205
190
206
- let request = Request {
191
+ self . events . push_back ( ToSwarm :: NotifyHandler {
207
192
peer_id : p,
208
- protocol : Protocol :: Push ,
209
- } ;
210
- if !self . requests . contains ( & request) {
211
- self . requests . push ( request) ;
212
- }
193
+ handler : NotifyHandler :: Any ,
194
+ event : InEvent :: Push ,
195
+ } ) ;
213
196
}
214
197
}
215
198
@@ -239,6 +222,14 @@ impl Behaviour {
239
222
}
240
223
}
241
224
}
225
+
226
+ fn all_addresses ( & self ) -> HashSet < Multiaddr > {
227
+ self . listen_addresses
228
+ . iter ( )
229
+ . chain ( self . external_addresses . iter ( ) )
230
+ . cloned ( )
231
+ . collect ( )
232
+ }
242
233
}
243
234
244
235
impl NetworkBehaviour for Behaviour {
@@ -261,6 +252,7 @@ impl NetworkBehaviour for Behaviour {
261
252
self . config . protocol_version . clone ( ) ,
262
253
self . config . agent_version . clone ( ) ,
263
254
remote_addr. clone ( ) ,
255
+ self . all_addresses ( ) ,
264
256
) )
265
257
}
266
258
@@ -280,13 +272,14 @@ impl NetworkBehaviour for Behaviour {
280
272
self . config . protocol_version . clone ( ) ,
281
273
self . config . agent_version . clone ( ) ,
282
274
addr. clone ( ) , // TODO: This is weird? That is the public address we dialed, shouldn't need to tell the other party?
275
+ self . all_addresses ( ) ,
283
276
) )
284
277
}
285
278
286
279
fn on_connection_handler_event (
287
280
& mut self ,
288
281
peer_id : PeerId ,
289
- connection_id : ConnectionId ,
282
+ _ : ConnectionId ,
290
283
event : THandlerOutEvent < Self > ,
291
284
) {
292
285
match event {
@@ -315,12 +308,6 @@ impl NetworkBehaviour for Behaviour {
315
308
self . events
316
309
. push_back ( ToSwarm :: GenerateEvent ( Event :: Pushed { peer_id } ) ) ;
317
310
}
318
- handler:: Event :: Identify => {
319
- self . requests . push ( Request {
320
- peer_id,
321
- protocol : Protocol :: Identify ( connection_id) ,
322
- } ) ;
323
- }
324
311
handler:: Event :: IdentificationError ( error) => {
325
312
self . events
326
313
. push_back ( ToSwarm :: GenerateEvent ( Event :: Error { peer_id, error } ) ) ;
@@ -331,50 +318,13 @@ impl NetworkBehaviour for Behaviour {
331
318
fn poll (
332
319
& mut self ,
333
320
_cx : & mut Context < ' _ > ,
334
- params : & mut impl PollParameters ,
321
+ _ : & mut impl PollParameters ,
335
322
) -> Poll < ToSwarm < Self :: OutEvent , THandlerInEvent < Self > > > {
336
323
if let Some ( event) = self . events . pop_front ( ) {
337
324
return Poll :: Ready ( event) ;
338
325
}
339
326
340
- // Check for pending requests.
341
- match self . requests . pop ( ) {
342
- Some ( Request {
343
- peer_id,
344
- protocol : Protocol :: Push ,
345
- } ) => Poll :: Ready ( ToSwarm :: NotifyHandler {
346
- peer_id,
347
- handler : NotifyHandler :: Any ,
348
- event : InEvent {
349
- listen_addrs : self
350
- . listen_addresses
351
- . iter ( )
352
- . chain ( self . external_addresses . iter ( ) )
353
- . cloned ( )
354
- . collect ( ) ,
355
- supported_protocols : supported_protocols ( params) ,
356
- protocol : Protocol :: Push ,
357
- } ,
358
- } ) ,
359
- Some ( Request {
360
- peer_id,
361
- protocol : Protocol :: Identify ( connection_id) ,
362
- } ) => Poll :: Ready ( ToSwarm :: NotifyHandler {
363
- peer_id,
364
- handler : NotifyHandler :: One ( connection_id) ,
365
- event : InEvent {
366
- listen_addrs : self
367
- . listen_addresses
368
- . iter ( )
369
- . chain ( self . external_addresses . iter ( ) )
370
- . cloned ( )
371
- . collect ( ) ,
372
- supported_protocols : supported_protocols ( params) ,
373
- protocol : Protocol :: Identify ( connection_id) ,
374
- } ,
375
- } ) ,
376
- None => Poll :: Pending ,
377
- }
327
+ Poll :: Pending
378
328
}
379
329
380
330
fn handle_pending_outbound_connection (
@@ -393,8 +343,35 @@ impl NetworkBehaviour for Behaviour {
393
343
}
394
344
395
345
fn on_swarm_event ( & mut self , event : FromSwarm < Self :: ConnectionHandler > ) {
396
- self . listen_addresses . on_swarm_event ( & event) ;
397
- self . external_addresses . on_swarm_event ( & event) ;
346
+ let listen_addr_changed = self . listen_addresses . on_swarm_event ( & event) ;
347
+ let external_addr_changed = self . external_addresses . on_swarm_event ( & event) ;
348
+
349
+ if listen_addr_changed || external_addr_changed {
350
+ // notify all connected handlers about our changed addresses
351
+ let change_events = self
352
+ . connected
353
+ . iter ( )
354
+ . flat_map ( |( peer, map) | map. keys ( ) . map ( |id| ( * peer, id) ) )
355
+ . map ( |( peer_id, connection_id) | ToSwarm :: NotifyHandler {
356
+ peer_id,
357
+ handler : NotifyHandler :: One ( * connection_id) ,
358
+ event : InEvent :: AddressesChanged ( self . all_addresses ( ) ) ,
359
+ } )
360
+ . collect :: < Vec < _ > > ( ) ;
361
+
362
+ self . events . extend ( change_events)
363
+ }
364
+
365
+ if listen_addr_changed && self . config . push_listen_addr_updates {
366
+ // trigger an identify push for all connected peers
367
+ let push_events = self . connected . keys ( ) . map ( |peer| ToSwarm :: NotifyHandler {
368
+ peer_id : * peer,
369
+ handler : NotifyHandler :: Any ,
370
+ event : InEvent :: Push ,
371
+ } ) ;
372
+
373
+ self . events . extend ( push_events) ;
374
+ }
398
375
399
376
match event {
400
377
FromSwarm :: ConnectionEstablished ( connection_established) => {
@@ -408,30 +385,11 @@ impl NetworkBehaviour for Behaviour {
408
385
} ) => {
409
386
if remaining_established == 0 {
410
387
self . connected . remove ( & peer_id) ;
411
- self . requests . retain ( |request| {
412
- request
413
- != & Request {
414
- peer_id,
415
- protocol : Protocol :: Push ,
416
- }
417
- } ) ;
418
388
} else if let Some ( addrs) = self . connected . get_mut ( & peer_id) {
419
389
addrs. remove ( & connection_id) ;
420
390
}
421
391
}
422
392
FromSwarm :: DialFailure ( DialFailure { peer_id, error, .. } ) => {
423
- if let Some ( peer_id) = peer_id {
424
- if !self . connected . contains_key ( & peer_id) {
425
- self . requests . retain ( |request| {
426
- request
427
- != & Request {
428
- peer_id,
429
- protocol : Protocol :: Push ,
430
- }
431
- } ) ;
432
- }
433
- }
434
-
435
393
if let Some ( entry) = peer_id. and_then ( |id| self . discovered_peers . get_mut ( & id) ) {
436
394
if let DialError :: Transport ( errors) = error {
437
395
for ( addr, _error) in errors {
@@ -440,20 +398,9 @@ impl NetworkBehaviour for Behaviour {
440
398
}
441
399
}
442
400
}
443
- FromSwarm :: NewListenAddr ( _) | FromSwarm :: ExpiredListenAddr ( _) => {
444
- if self . config . push_listen_addr_updates {
445
- for p in self . connected . keys ( ) {
446
- let request = Request {
447
- peer_id : * p,
448
- protocol : Protocol :: Push ,
449
- } ;
450
- if !self . requests . contains ( & request) {
451
- self . requests . push ( request) ;
452
- }
453
- }
454
- }
455
- }
456
- FromSwarm :: AddressChange ( _)
401
+ FromSwarm :: NewListenAddr ( _)
402
+ | FromSwarm :: ExpiredListenAddr ( _)
403
+ | FromSwarm :: AddressChange ( _)
457
404
| FromSwarm :: ListenFailure ( _)
458
405
| FromSwarm :: NewListener ( _)
459
406
| FromSwarm :: ListenerError ( _)
@@ -496,17 +443,6 @@ pub enum Event {
496
443
} ,
497
444
}
498
445
499
- fn supported_protocols ( params : & impl PollParameters ) -> Vec < StreamProtocol > {
500
- // The protocol names can be bytes, but the identify protocol except UTF-8 strings.
501
- // There's not much we can do to solve this conflict except strip non-UTF-8 characters.
502
- params
503
- . supported_protocols ( )
504
- . filter_map ( |p| {
505
- StreamProtocol :: try_from_owned ( String :: from_utf8_lossy ( & p) . to_string ( ) ) . ok ( )
506
- } )
507
- . collect ( )
508
- }
509
-
510
446
/// If there is a given peer_id in the multiaddr, make sure it is the same as
511
447
/// the given peer_id. If there is no peer_id for the peer in the mutiaddr, this returns true.
512
448
fn multiaddr_matches_peer_id ( addr : & Multiaddr , peer_id : & PeerId ) -> bool {
0 commit comments