3
3
const assert = require ( 'assert' )
4
4
const { utils } = require ( 'libp2p-pubsub' )
5
5
6
+ const PeerInfo = require ( 'peer-info' )
7
+
6
8
const BasicPubsub = require ( './pubsub' )
7
9
const { MessageCache } = require ( './messageCache' )
8
10
@@ -12,15 +14,24 @@ const Heartbeat = require('./heartbeat')
12
14
13
15
class GossipSub extends BasicPubsub {
14
16
/**
15
- * @param {Object } libp2p an instance of Libp2p
16
- * @param {Object } options
17
- * @param {bool } options.emitSelf if publish should emit to self, if subscribed, defaults to false
18
- * @param {bool } options.gossipIncoming if incoming messages on a subscribed topic should be automatically gossiped, defaults to true
19
- * @param {bool } options.fallbackToFloodsub if dial should fallback to floodsub, defaults to true
17
+ * @param {PeerInfo } peerInfo instance of the peer's PeerInfo
18
+ * @param {Object } registrar
19
+ * @param {function } registrar.register
20
+ * @param {function } registrar.unregister
21
+ * @param {Object } [options]
22
+ * @param {bool } [options.emitSelf] if publish should emit to self, if subscribed, defaults to false
23
+ * @param {bool } [options.gossipIncoming] if incoming messages on a subscribed topic should be automatically gossiped, defaults to true
24
+ * @param {bool } [options.fallbackToFloodsub] if dial should fallback to floodsub, defaults to true
20
25
* @constructor
21
26
*/
22
- constructor ( libp2p , options ) {
23
- super ( 'libp2p:gossipsub' , constants . GossipSubID , libp2p , options )
27
+ constructor ( peerInfo , registrar , options = { } ) {
28
+ assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peer info must be an instance of `peer-info`' )
29
+
30
+ // registrar handling
31
+ assert ( registrar && typeof registrar . register === 'function' , 'a register function must be provided in registrar' )
32
+ assert ( registrar && typeof registrar . unregister === 'function' , 'a unregister function must be provided in registrar' )
33
+
34
+ super ( 'libp2p:gossipsub' , constants . GossipSubID , peerInfo , registrar , options )
24
35
25
36
/**
26
37
* Map of topic meshes
@@ -71,37 +82,35 @@ class GossipSub extends BasicPubsub {
71
82
72
83
/**
73
84
* Removes a peer from the router
74
- *
75
85
* @override
76
86
* @param {Peer } peer
77
87
* @returns {PeerInfo }
78
88
*/
79
89
_removePeer ( peer ) {
80
90
super . _removePeer ( peer )
81
- // Only delete when no one else if referencing this peer.
82
- if ( peer . _references === 0 ) {
83
- // Remove this peer from the mesh
84
- // eslint-disable-next-line no-unused-vars
85
- for ( const [ _ , peers ] of this . mesh . entries ( ) ) {
86
- peers . delete ( peer )
87
- }
88
- // Remove this peer from the fanout
89
- // eslint-disable-next-line no-unused-vars
90
- for ( const [ _ , peers ] of this . fanout . entries ( ) ) {
91
- peers . delete ( peer )
92
- }
93
91
94
- // Remove from gossip mapping
95
- this . gossip . delete ( peer )
96
- // Remove from control mapping
97
- this . control . delete ( peer )
92
+ // Remove this peer from the mesh
93
+ // eslint-disable-next-line no-unused-vars
94
+ for ( const [ _ , peers ] of this . mesh . entries ( ) ) {
95
+ peers . delete ( peer )
98
96
}
97
+
98
+ // Remove this peer from the fanout
99
+ // eslint-disable-next-line no-unused-vars
100
+ for ( const [ _ , peers ] of this . fanout . entries ( ) ) {
101
+ peers . delete ( peer )
102
+ }
103
+
104
+ // Remove from gossip mapping
105
+ this . gossip . delete ( peer )
106
+ // Remove from control mapping
107
+ this . control . delete ( peer )
108
+
99
109
return peer
100
110
}
101
111
102
112
/**
103
113
* Handles an rpc control message from a peer
104
- *
105
114
* @param {Peer } peer
106
115
* @param {rpc.RPC } rpc
107
116
* @returns {void }
@@ -139,6 +148,7 @@ class GossipSub extends BasicPubsub {
139
148
if ( ! this . _options . gossipIncoming ) {
140
149
return
141
150
}
151
+
142
152
// Emit to floodsub peers
143
153
this . peers . forEach ( ( peer ) => {
144
154
if ( peer . info . protocols . has ( constants . FloodSubID ) &&
@@ -168,10 +178,8 @@ class GossipSub extends BasicPubsub {
168
178
169
179
/**
170
180
* Handles IHAVE messages
171
- *
172
181
* @param {Peer } peer
173
182
* @param {Array<rpc.RPC.ControlIHave> } ihave
174
- *
175
183
* @returns {rpc.RPC.ControlIWant }
176
184
*/
177
185
_handleIHave ( peer , ihave ) {
@@ -204,10 +212,8 @@ class GossipSub extends BasicPubsub {
204
212
/**
205
213
* Handles IWANT messages
206
214
* Returns messages to send back to peer
207
- *
208
215
* @param {Peer } peer
209
216
* @param {Array<rpc.RPC.ControlIWant> } iwant
210
- *
211
217
* @returns {Array<rpc.RPC.Message> }
212
218
*/
213
219
_handleIWant ( peer , iwant ) {
@@ -234,12 +240,9 @@ class GossipSub extends BasicPubsub {
234
240
235
241
/**
236
242
* Handles Graft messages
237
- *
238
243
* @param {Peer } peer
239
244
* @param {Array<rpc.RPC.ControlGraft> } graft
240
- *
241
245
* @return {Array<rpc.RPC.ControlPrune> }
242
- *
243
246
*/
244
247
_handleGraft ( peer , graft ) {
245
248
const prune = [ ]
@@ -271,12 +274,9 @@ class GossipSub extends BasicPubsub {
271
274
272
275
/**
273
276
* Handles Prune messages
274
- *
275
277
* @param {Peer } peer
276
278
* @param {Array<rpc.RPC.ControlPrune> } prune
277
- *
278
279
* @returns {void }
279
- *
280
280
*/
281
281
_handlePrune ( peer , prune ) {
282
282
prune . forEach ( ( { topicID } ) => {
@@ -292,36 +292,28 @@ class GossipSub extends BasicPubsub {
292
292
/**
293
293
* Mounts the gossipsub protocol onto the libp2p node and sends our
294
294
* our subscriptions to every peer connected
295
- *
296
295
* @override
297
- * @param {Function } callback
298
- * @returns {void }
299
- *
296
+ * @returns {Promise }
300
297
*/
301
- start ( callback ) {
302
- super . start ( ( err ) => {
303
- if ( err ) return callback ( err )
304
- this . heartbeat . start ( callback )
305
- } )
298
+ async start ( ) {
299
+ await super . start ( )
300
+ this . heartbeat . start ( )
306
301
}
307
302
308
303
/**
309
304
* Unmounts the gossipsub protocol and shuts down every connection
310
- *
311
305
* @override
312
- * @param {Function } callback
313
- * @returns {void }
306
+ * @returns {Promise }
314
307
*/
315
- stop ( callback ) {
316
- super . stop ( ( err ) => {
317
- if ( err ) return callback ( err )
318
- this . mesh = new Map ( )
319
- this . fanout = new Map ( )
320
- this . lastpub = new Map ( )
321
- this . gossip = new Map ( )
322
- this . control = new Map ( )
323
- this . heartbeat . stop ( callback )
324
- } )
308
+ async stop ( ) {
309
+ await super . stop ( )
310
+ this . heartbeat . stop ( )
311
+
312
+ this . mesh = new Map ( )
313
+ this . fanout = new Map ( )
314
+ this . lastpub = new Map ( )
315
+ this . gossip = new Map ( )
316
+ this . control = new Map ( )
325
317
}
326
318
327
319
/**
@@ -355,7 +347,6 @@ class GossipSub extends BasicPubsub {
355
347
356
348
/**
357
349
* Leave topics
358
- *
359
350
* @param {Array<string>|string } topics
360
351
* @returns {void }
361
352
*/
@@ -431,7 +422,6 @@ class GossipSub extends BasicPubsub {
431
422
432
423
/**
433
424
* Sends a GRAFT message to a peer
434
- *
435
425
* @param {Peer } peer
436
426
* @param {String } topic
437
427
* @returns {void }
@@ -447,7 +437,6 @@ class GossipSub extends BasicPubsub {
447
437
448
438
/**
449
439
* Sends a PRUNE message to a peer
450
- *
451
440
* @param {Peer } peer
452
441
* @param {String } topic
453
442
* @returns {void }
@@ -505,7 +494,6 @@ class GossipSub extends BasicPubsub {
505
494
506
495
/**
507
496
* Send graft and prune messages
508
- *
509
497
* @param {Map<Peer, Array<String>> } tograft
510
498
* @param {Map<Peer, Array<String>> } toprune
511
499
*/
@@ -532,7 +520,6 @@ class GossipSub extends BasicPubsub {
532
520
533
521
/**
534
522
* Emits gossip to peers in a particular topic
535
- *
536
523
* @param {String } topic
537
524
* @param {Set<Peer> } peers - peers to exclude
538
525
* @returns {void }
@@ -575,7 +562,6 @@ class GossipSub extends BasicPubsub {
575
562
576
563
/**
577
564
* Adds new IHAVE messages to pending gossip
578
- *
579
565
* @param {Peer } peer
580
566
* @param {Array<rpc.RPC.ControlIHave> } controlIHaveMsgs
581
567
* @returns {void }
@@ -588,7 +574,6 @@ class GossipSub extends BasicPubsub {
588
574
589
575
/**
590
576
* Returns the current time in milliseconds
591
- *
592
577
* @returns {number }
593
578
*/
594
579
_now ( ) {
0 commit comments