@@ -314,13 +314,13 @@ export class ConnectionManager
314
314
}
315
315
316
316
private async attemptDial ( peerId : PeerId ) : Promise < void > {
317
+ if ( ! ( await this . shouldDialPeer ( peerId ) ) ) return ;
318
+
317
319
if ( this . currentActiveDialCount >= this . options . maxParallelDials ) {
318
320
this . pendingPeerDialQueue . push ( peerId ) ;
319
321
return ;
320
322
}
321
323
322
- if ( ! ( await this . shouldDialPeer ( peerId ) ) ) return ;
323
-
324
324
this . dialPeer ( peerId ) . catch ( ( err ) => {
325
325
log ( `Error dialing peer ${ peerId . toString ( ) } : ${ err } ` ) ;
326
326
} ) ;
@@ -331,34 +331,7 @@ export class ConnectionManager
331
331
void ( async ( ) => {
332
332
const { id : peerId } = evt . detail ;
333
333
334
- if ( ! ( await this . isPeerTopicConfigured ( peerId ) ) ) {
335
- const shardInfo = await this . getPeerShardInfo (
336
- peerId ,
337
- this . libp2p . peerStore
338
- ) ;
339
- log (
340
- `Discovered peer ${ peerId . toString ( ) } with ShardInfo ${ shardInfo } is not part of any of the configured pubsub topics (${
341
- this . configuredPubSubTopics
342
- } ).
343
- Not dialing.`
344
- ) ;
345
- return ;
346
- }
347
-
348
- const isBootstrap = ( await this . getTagNamesForPeer ( peerId ) ) . includes (
349
- Tags . BOOTSTRAP
350
- ) ;
351
-
352
- this . dispatchEvent (
353
- new CustomEvent < PeerId > (
354
- isBootstrap
355
- ? EPeersByDiscoveryEvents . PEER_DISCOVERY_BOOTSTRAP
356
- : EPeersByDiscoveryEvents . PEER_DISCOVERY_PEER_EXCHANGE ,
357
- {
358
- detail : peerId
359
- }
360
- )
361
- ) ;
334
+ await this . dispatchDiscoveryEvent ( peerId ) ;
362
335
363
336
try {
364
337
await this . attemptDial ( peerId ) ;
@@ -421,15 +394,54 @@ export class ConnectionManager
421
394
} ;
422
395
423
396
/**
424
- * Checks if the peer is dialable based on the following conditions:
425
- * 1. If the peer is a bootstrap peer, it is only dialable if the number of current bootstrap connections is less than the max allowed.
426
- * 2. If the peer is not a bootstrap peer
397
+ * Checks if the peer should be dialed based on the following conditions:
398
+ * 1. If the peer is already connected, don't dial
399
+ * 2. If the peer is not part of any of the configured pubsub topics, don't dial
400
+ * 3. If the peer is not dialable based on bootstrap status, don't dial
401
+ * @returns true if the peer should be dialed, false otherwise
427
402
*/
428
403
private async shouldDialPeer ( peerId : PeerId ) : Promise < boolean > {
404
+ // if we're already connected to the peer, don't dial
429
405
const isConnected = this . libp2p . getConnections ( peerId ) . length > 0 ;
406
+ if ( isConnected ) {
407
+ log ( `Already connected to peer ${ peerId . toString ( ) } . Not dialing.` ) ;
408
+ return false ;
409
+ }
430
410
431
- if ( isConnected ) return false ;
411
+ // if the peer is not part of any of the configured pubsub topics, don't dial
412
+ if ( ! ( await this . isPeerTopicConfigured ( peerId ) ) ) {
413
+ const shardInfo = await this . getPeerShardInfo (
414
+ peerId ,
415
+ this . libp2p . peerStore
416
+ ) ;
417
+ log (
418
+ `Discovered peer ${ peerId . toString ( ) } with ShardInfo ${ shardInfo } is not part of any of the configured pubsub topics (${
419
+ this . configuredPubSubTopics
420
+ } ).
421
+ Not dialing.`
422
+ ) ;
423
+ return false ;
424
+ }
432
425
426
+ // if the peer is not dialable based on bootstrap status, don't dial
427
+ if ( ! ( await this . isPeerDialableBasedOnBootstrapStatus ( peerId ) ) ) {
428
+ log (
429
+ `Peer ${ peerId . toString ( ) } is not dialable based on bootstrap status. Not dialing.`
430
+ ) ;
431
+ return false ;
432
+ }
433
+
434
+ return true ;
435
+ }
436
+
437
+ /**
438
+ * Checks if the peer is dialable based on the following conditions:
439
+ * 1. If the peer is a bootstrap peer, it is only dialable if the number of current bootstrap connections is less than the max allowed.
440
+ * 2. If the peer is not a bootstrap peer
441
+ */
442
+ private async isPeerDialableBasedOnBootstrapStatus (
443
+ peerId : PeerId
444
+ ) : Promise < boolean > {
433
445
const tagNames = await this . getTagNamesForPeer ( peerId ) ;
434
446
435
447
const isBootstrap = tagNames . some ( ( tagName ) => tagName === Tags . BOOTSTRAP ) ;
@@ -449,6 +461,23 @@ export class ConnectionManager
449
461
return false ;
450
462
}
451
463
464
+ private async dispatchDiscoveryEvent ( peerId : PeerId ) : Promise < void > {
465
+ const isBootstrap = ( await this . getTagNamesForPeer ( peerId ) ) . includes (
466
+ Tags . BOOTSTRAP
467
+ ) ;
468
+
469
+ this . dispatchEvent (
470
+ new CustomEvent < PeerId > (
471
+ isBootstrap
472
+ ? EPeersByDiscoveryEvents . PEER_DISCOVERY_BOOTSTRAP
473
+ : EPeersByDiscoveryEvents . PEER_DISCOVERY_PEER_EXCHANGE ,
474
+ {
475
+ detail : peerId
476
+ }
477
+ )
478
+ ) ;
479
+ }
480
+
452
481
/**
453
482
* Fetches the tag names for a given peer
454
483
*/
0 commit comments