1
1
import type { PeerId } from "@libp2p/interface-peer-id" ;
2
2
import type { PeerInfo } from "@libp2p/interface-peer-info" ;
3
- import type { ConnectionManagerOptions , IRelay } from "@waku/interfaces" ;
3
+ import type { Peer } from "@libp2p/interface-peer-store" ;
4
+ import { CustomEvent , EventEmitter } from "@libp2p/interfaces/events" ;
5
+ import {
6
+ ConnectionManagerOptions ,
7
+ EPeersByDiscoveryEvents ,
8
+ IPeersByDiscoveryEvents ,
9
+ IRelay ,
10
+ PeersByDiscoveryResult ,
11
+ } from "@waku/interfaces" ;
4
12
import { Libp2p , Tags } from "@waku/interfaces" ;
5
13
import debug from "debug" ;
6
14
@@ -12,7 +20,7 @@ export const DEFAULT_MAX_BOOTSTRAP_PEERS_ALLOWED = 1;
12
20
export const DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER = 3 ;
13
21
export const DEFAULT_MAX_PARALLEL_DIALS = 3 ;
14
22
15
- export class ConnectionManager {
23
+ export class ConnectionManager extends EventEmitter < IPeersByDiscoveryEvents > {
16
24
private static instances = new Map < string , ConnectionManager > ( ) ;
17
25
private keepAliveManager : KeepAliveManager ;
18
26
private options : ConnectionManagerOptions ;
@@ -44,12 +52,57 @@ export class ConnectionManager {
44
52
return instance ;
45
53
}
46
54
55
+ public async getPeersByDiscovery ( ) : Promise < PeersByDiscoveryResult > {
56
+ const peersDiscovered = await this . libp2p . peerStore . all ( ) ;
57
+ const peersConnected = this . libp2p
58
+ . getConnections ( )
59
+ . map ( ( conn ) => conn . remotePeer ) ;
60
+
61
+ const peersDiscoveredByBootstrap : Peer [ ] = [ ] ;
62
+ const peersDiscoveredByPeerExchange : Peer [ ] = [ ] ;
63
+ const peersConnectedByBootstrap : Peer [ ] = [ ] ;
64
+ const peersConnectedByPeerExchange : Peer [ ] = [ ] ;
65
+
66
+ for ( const peer of peersDiscovered ) {
67
+ const tags = await this . getTagNamesForPeer ( peer . id ) ;
68
+
69
+ if ( tags . includes ( Tags . BOOTSTRAP ) ) {
70
+ peersDiscoveredByBootstrap . push ( peer ) ;
71
+ } else if ( tags . includes ( Tags . PEER_EXCHANGE ) ) {
72
+ peersDiscoveredByPeerExchange . push ( peer ) ;
73
+ }
74
+ }
75
+
76
+ for ( const peerId of peersConnected ) {
77
+ const peer = await this . libp2p . peerStore . get ( peerId ) ;
78
+ const tags = await this . getTagNamesForPeer ( peerId ) ;
79
+
80
+ if ( tags . includes ( Tags . BOOTSTRAP ) ) {
81
+ peersConnectedByBootstrap . push ( peer ) ;
82
+ } else if ( tags . includes ( Tags . PEER_EXCHANGE ) ) {
83
+ peersConnectedByPeerExchange . push ( peer ) ;
84
+ }
85
+ }
86
+
87
+ return {
88
+ DISCOVERED : {
89
+ [ Tags . BOOTSTRAP ] : peersDiscoveredByBootstrap ,
90
+ [ Tags . PEER_EXCHANGE ] : peersDiscoveredByPeerExchange ,
91
+ } ,
92
+ CONNECTED : {
93
+ [ Tags . BOOTSTRAP ] : peersConnectedByBootstrap ,
94
+ [ Tags . PEER_EXCHANGE ] : peersConnectedByPeerExchange ,
95
+ } ,
96
+ } ;
97
+ }
98
+
47
99
private constructor (
48
100
libp2p : Libp2p ,
49
101
keepAliveOptions : KeepAliveOptions ,
50
102
relay ?: IRelay ,
51
103
options ?: Partial < ConnectionManagerOptions >
52
104
) {
105
+ super ( ) ;
53
106
this . libp2p = libp2p ;
54
107
this . options = {
55
108
maxDialAttemptsForPeer : DEFAULT_MAX_DIAL_ATTEMPTS_FOR_PEER ,
@@ -240,6 +293,30 @@ export class ConnectionManager {
240
293
void ( async ( ) => {
241
294
const { id : peerId } = evt . detail ;
242
295
296
+ const isBootstrap = ( await this . getTagNamesForPeer ( peerId ) ) . includes (
297
+ Tags . BOOTSTRAP
298
+ ) ;
299
+
300
+ if ( isBootstrap ) {
301
+ this . dispatchEvent (
302
+ new CustomEvent < PeerId > (
303
+ EPeersByDiscoveryEvents . PEER_DISCOVERY_BOOTSTRAP ,
304
+ {
305
+ detail : peerId ,
306
+ }
307
+ )
308
+ ) ;
309
+ } else {
310
+ this . dispatchEvent (
311
+ new CustomEvent < PeerId > (
312
+ EPeersByDiscoveryEvents . PEER_DISCOVERY_PEER_EXCHANGE ,
313
+ {
314
+ detail : peerId ,
315
+ }
316
+ )
317
+ ) ;
318
+ }
319
+
243
320
try {
244
321
await this . attemptDial ( peerId ) ;
245
322
} catch ( error ) {
@@ -267,7 +344,25 @@ export class ConnectionManager {
267
344
bootstrapConnections . length > this . options . maxBootstrapPeersAllowed
268
345
) {
269
346
await this . dropConnection ( peerId ) ;
347
+ } else {
348
+ this . dispatchEvent (
349
+ new CustomEvent < PeerId > (
350
+ EPeersByDiscoveryEvents . PEER_CONNECT_BOOTSTRAP ,
351
+ {
352
+ detail : peerId ,
353
+ }
354
+ )
355
+ ) ;
270
356
}
357
+ } else {
358
+ this . dispatchEvent (
359
+ new CustomEvent < PeerId > (
360
+ EPeersByDiscoveryEvents . PEER_CONNECT_PEER_EXCHANGE ,
361
+ {
362
+ detail : peerId ,
363
+ }
364
+ )
365
+ ) ;
271
366
}
272
367
} ) ( ) ;
273
368
} ,
0 commit comments