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,30 @@ 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 ( {
35
+ debugName : 'libp2p:gossipsub' ,
36
+ multicodec : constants . GossipSubID ,
37
+ peerInfo,
38
+ registrar,
39
+ options
40
+ } )
24
41
25
42
/**
26
43
* Map of topic meshes
@@ -71,37 +88,35 @@ class GossipSub extends BasicPubsub {
71
88
72
89
/**
73
90
* Removes a peer from the router
74
- *
75
91
* @override
76
92
* @param {Peer } peer
77
93
* @returns {PeerInfo }
78
94
*/
79
95
_removePeer ( peer ) {
80
96
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
97
94
- // Remove from gossip mapping
95
- this . gossip . delete ( peer )
96
- // Remove from control mapping
97
- this . control . delete ( peer )
98
+ // Remove this peer from the mesh
99
+ // eslint-disable-next-line no-unused-vars
100
+ for ( const [ _ , peers ] of this . mesh . entries ( ) ) {
101
+ peers . delete ( peer )
102
+ }
103
+
104
+ // Remove this peer from the fanout
105
+ // eslint-disable-next-line no-unused-vars
106
+ for ( const [ _ , peers ] of this . fanout . entries ( ) ) {
107
+ peers . delete ( peer )
98
108
}
109
+
110
+ // Remove from gossip mapping
111
+ this . gossip . delete ( peer )
112
+ // Remove from control mapping
113
+ this . control . delete ( peer )
114
+
99
115
return peer
100
116
}
101
117
102
118
/**
103
119
* Handles an rpc control message from a peer
104
- *
105
120
* @param {Peer } peer
106
121
* @param {rpc.RPC } rpc
107
122
* @returns {void }
@@ -139,6 +154,7 @@ class GossipSub extends BasicPubsub {
139
154
if ( ! this . _options . gossipIncoming ) {
140
155
return
141
156
}
157
+
142
158
// Emit to floodsub peers
143
159
this . peers . forEach ( ( peer ) => {
144
160
if ( peer . info . protocols . has ( constants . FloodSubID ) &&
@@ -168,10 +184,8 @@ class GossipSub extends BasicPubsub {
168
184
169
185
/**
170
186
* Handles IHAVE messages
171
- *
172
187
* @param {Peer } peer
173
188
* @param {Array<rpc.RPC.ControlIHave> } ihave
174
- *
175
189
* @returns {rpc.RPC.ControlIWant }
176
190
*/
177
191
_handleIHave ( peer , ihave ) {
@@ -204,10 +218,8 @@ class GossipSub extends BasicPubsub {
204
218
/**
205
219
* Handles IWANT messages
206
220
* Returns messages to send back to peer
207
- *
208
221
* @param {Peer } peer
209
222
* @param {Array<rpc.RPC.ControlIWant> } iwant
210
- *
211
223
* @returns {Array<rpc.RPC.Message> }
212
224
*/
213
225
_handleIWant ( peer , iwant ) {
@@ -234,12 +246,9 @@ class GossipSub extends BasicPubsub {
234
246
235
247
/**
236
248
* Handles Graft messages
237
- *
238
249
* @param {Peer } peer
239
250
* @param {Array<rpc.RPC.ControlGraft> } graft
240
- *
241
251
* @return {Array<rpc.RPC.ControlPrune> }
242
- *
243
252
*/
244
253
_handleGraft ( peer , graft ) {
245
254
const prune = [ ]
@@ -271,12 +280,9 @@ class GossipSub extends BasicPubsub {
271
280
272
281
/**
273
282
* Handles Prune messages
274
- *
275
283
* @param {Peer } peer
276
284
* @param {Array<rpc.RPC.ControlPrune> } prune
277
- *
278
285
* @returns {void }
279
- *
280
286
*/
281
287
_handlePrune ( peer , prune ) {
282
288
prune . forEach ( ( { topicID } ) => {
@@ -292,36 +298,28 @@ class GossipSub extends BasicPubsub {
292
298
/**
293
299
* Mounts the gossipsub protocol onto the libp2p node and sends our
294
300
* our subscriptions to every peer connected
295
- *
296
301
* @override
297
- * @param {Function } callback
298
- * @returns {void }
299
- *
302
+ * @returns {Promise }
300
303
*/
301
- start ( callback ) {
302
- super . start ( ( err ) => {
303
- if ( err ) return callback ( err )
304
- this . heartbeat . start ( callback )
305
- } )
304
+ async start ( ) {
305
+ await super . start ( )
306
+ this . heartbeat . start ( )
306
307
}
307
308
308
309
/**
309
310
* Unmounts the gossipsub protocol and shuts down every connection
310
- *
311
311
* @override
312
- * @param {Function } callback
313
- * @returns {void }
312
+ * @returns {Promise }
314
313
*/
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
- } )
314
+ async stop ( ) {
315
+ await super . stop ( )
316
+ this . heartbeat . stop ( )
317
+
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 ( )
325
323
}
326
324
327
325
/**
@@ -355,7 +353,6 @@ class GossipSub extends BasicPubsub {
355
353
356
354
/**
357
355
* Leave topics
358
- *
359
356
* @param {Array<string>|string } topics
360
357
* @returns {void }
361
358
*/
@@ -431,7 +428,6 @@ class GossipSub extends BasicPubsub {
431
428
432
429
/**
433
430
* Sends a GRAFT message to a peer
434
- *
435
431
* @param {Peer } peer
436
432
* @param {String } topic
437
433
* @returns {void }
@@ -447,7 +443,6 @@ class GossipSub extends BasicPubsub {
447
443
448
444
/**
449
445
* Sends a PRUNE message to a peer
450
- *
451
446
* @param {Peer } peer
452
447
* @param {String } topic
453
448
* @returns {void }
@@ -505,7 +500,6 @@ class GossipSub extends BasicPubsub {
505
500
506
501
/**
507
502
* Send graft and prune messages
508
- *
509
503
* @param {Map<Peer, Array<String>> } tograft
510
504
* @param {Map<Peer, Array<String>> } toprune
511
505
*/
@@ -532,7 +526,6 @@ class GossipSub extends BasicPubsub {
532
526
533
527
/**
534
528
* Emits gossip to peers in a particular topic
535
- *
536
529
* @param {String } topic
537
530
* @param {Set<Peer> } peers - peers to exclude
538
531
* @returns {void }
@@ -575,7 +568,6 @@ class GossipSub extends BasicPubsub {
575
568
576
569
/**
577
570
* Adds new IHAVE messages to pending gossip
578
- *
579
571
* @param {Peer } peer
580
572
* @param {Array<rpc.RPC.ControlIHave> } controlIHaveMsgs
581
573
* @returns {void }
@@ -588,7 +580,6 @@ class GossipSub extends BasicPubsub {
588
580
589
581
/**
590
582
* Returns the current time in milliseconds
591
- *
592
583
* @returns {number }
593
584
*/
594
585
_now ( ) {
0 commit comments