2
2
3
3
const { utils } = require ( 'libp2p-pubsub' )
4
4
5
- const PeerInfo = require ( 'peer-info ' )
5
+ const PeerId = require ( 'peer-id ' )
6
6
7
7
const BasicPubsub = require ( './pubsub' )
8
8
const { MessageCache } = require ( './messageCache' )
@@ -13,7 +13,7 @@ const Heartbeat = require('./heartbeat')
13
13
14
14
class GossipSub extends BasicPubsub {
15
15
/**
16
- * @param {PeerInfo } peerInfo instance of the peer's PeerInfo
16
+ * @param {PeerId } peerId instance of the peer's PeerId
17
17
* @param {Object } registrar
18
18
* @param {function } registrar.handle
19
19
* @param {function } registrar.register
@@ -26,15 +26,15 @@ class GossipSub extends BasicPubsub {
26
26
* @param {Object } [options.messageCache] override the default MessageCache
27
27
* @constructor
28
28
*/
29
- constructor ( peerInfo , registrar , options = { } ) {
30
- if ( ! PeerInfo . isPeerInfo ( peerInfo ) ) {
31
- throw new Error ( 'peer info must be an instance of `peer-info `' )
29
+ constructor ( peerId , registrar , options = { } ) {
30
+ if ( ! PeerId . isPeerId ( peerId ) ) {
31
+ throw new Error ( 'peerId must be an instance of `peer-id `' )
32
32
}
33
33
34
34
super ( {
35
35
debugName : 'libp2p:gossipsub' ,
36
36
multicodec : constants . GossipSubID ,
37
- peerInfo ,
37
+ peerId ,
38
38
registrar,
39
39
options
40
40
} )
@@ -95,7 +95,7 @@ class GossipSub extends BasicPubsub {
95
95
* Removes a peer from the router
96
96
* @override
97
97
* @param {Peer } peer
98
- * @returns {PeerInfo }
98
+ * @returns {Peer }
99
99
*/
100
100
_removePeer ( peer ) {
101
101
super . _removePeer ( peer )
@@ -162,13 +162,13 @@ class GossipSub extends BasicPubsub {
162
162
163
163
// Emit to floodsub peers
164
164
this . peers . forEach ( ( peer ) => {
165
- if ( peer . info . protocols . has ( constants . FloodSubID ) &&
166
- peer . info . id . toB58String ( ) !== msg . from &&
165
+ if ( peer . protocols . includes ( constants . FloodSubID ) &&
166
+ peer . id . toB58String ( ) !== msg . from &&
167
167
utils . anyMatch ( peer . topics , topics ) &&
168
168
peer . isWritable
169
169
) {
170
170
peer . sendMessages ( utils . normalizeOutRpcMessages ( [ msg ] ) )
171
- this . log ( 'publish msg on topics - floodsub' , topics , peer . info . id . toB58String ( ) )
171
+ this . log ( 'publish msg on topics - floodsub' , topics , peer . id . toB58String ( ) )
172
172
}
173
173
} )
174
174
@@ -178,11 +178,11 @@ class GossipSub extends BasicPubsub {
178
178
return
179
179
}
180
180
this . mesh . get ( topic ) . forEach ( ( peer ) => {
181
- if ( ! peer . isWritable || peer . info . id . toB58String ( ) === msg . from ) {
181
+ if ( ! peer . isWritable || peer . id . toB58String ( ) === msg . from ) {
182
182
return
183
183
}
184
184
peer . sendMessages ( utils . normalizeOutRpcMessages ( [ msg ] ) )
185
- this . log ( 'publish msg on topic - meshsub' , topic , peer . info . id . toB58String ( ) )
185
+ this . log ( 'publish msg on topic - meshsub' , topic , peer . id . toB58String ( ) )
186
186
} )
187
187
} )
188
188
}
@@ -213,7 +213,7 @@ class GossipSub extends BasicPubsub {
213
213
return
214
214
}
215
215
216
- this . log ( 'IHAVE: Asking for %d messages from %s' , iwant . size , peer . info . id . toB58String ( ) )
216
+ this . log ( 'IHAVE: Asking for %d messages from %s' , iwant . size , peer . id . toB58String ( ) )
217
217
218
218
return {
219
219
messageIDs : Array . from ( iwant )
@@ -244,7 +244,7 @@ class GossipSub extends BasicPubsub {
244
244
return
245
245
}
246
246
247
- this . log ( 'IWANT: Sending %d messages to %s' , ihave . size , peer . info . id . toB58String ( ) )
247
+ this . log ( 'IWANT: Sending %d messages to %s' , ihave . size , peer . id . toB58String ( ) )
248
248
249
249
return Array . from ( ihave . values ( ) )
250
250
}
@@ -263,7 +263,7 @@ class GossipSub extends BasicPubsub {
263
263
if ( ! peers ) {
264
264
prune . push ( topicID )
265
265
} else {
266
- this . log ( 'GRAFT: Add mesh link from %s in %s' , peer . info . id . toB58String ( ) , topicID )
266
+ this . log ( 'GRAFT: Add mesh link from %s in %s' , peer . id . toB58String ( ) , topicID )
267
267
peers . add ( peer )
268
268
peer . topics . add ( topicID )
269
269
this . mesh . set ( topicID , peers )
@@ -293,7 +293,7 @@ class GossipSub extends BasicPubsub {
293
293
prune . forEach ( ( { topicID } ) => {
294
294
const peers = this . mesh . get ( topicID )
295
295
if ( peers ) {
296
- this . log ( 'PRUNE: Remove mesh link to %s in %s' , peer . info . id . toB58String ( ) , topicID )
296
+ this . log ( 'PRUNE: Remove mesh link to %s in %s' , peer . id . toB58String ( ) , topicID )
297
297
peers . delete ( peer )
298
298
peer . topics . delete ( topicID )
299
299
}
@@ -352,7 +352,7 @@ class GossipSub extends BasicPubsub {
352
352
this . mesh . set ( topic , peers )
353
353
}
354
354
this . mesh . get ( topic ) . forEach ( ( peer ) => {
355
- this . log ( 'JOIN: Add mesh link to %s in %s' , peer . info . id . toB58String ( ) , topic )
355
+ this . log ( 'JOIN: Add mesh link to %s in %s' , peer . id . toB58String ( ) , topic )
356
356
this . _sendGraft ( peer , topic )
357
357
} )
358
358
} )
@@ -373,7 +373,7 @@ class GossipSub extends BasicPubsub {
373
373
const meshPeers = this . mesh . get ( topic )
374
374
if ( meshPeers ) {
375
375
meshPeers . forEach ( ( peer ) => {
376
- this . log ( 'LEAVE: Remove mesh link to %s in %s' , peer . info . id . toB58String ( ) , topic )
376
+ this . log ( 'LEAVE: Remove mesh link to %s in %s' , peer . id . toB58String ( ) , topic )
377
377
this . _sendPrune ( peer , topic )
378
378
} )
379
379
this . mesh . delete ( topic )
@@ -405,7 +405,7 @@ class GossipSub extends BasicPubsub {
405
405
406
406
// floodsub peers
407
407
peersInTopic . forEach ( ( peer ) => {
408
- if ( peer . info . protocols . has ( constants . FloodSubID ) ) {
408
+ if ( peer . protocols . includes ( constants . FloodSubID ) ) {
409
409
tosend . add ( peer )
410
410
}
411
411
} )
@@ -436,7 +436,7 @@ class GossipSub extends BasicPubsub {
436
436
} )
437
437
// Publish messages to peers
438
438
tosend . forEach ( ( peer ) => {
439
- if ( peer . info . id . toB58String ( ) === msgObj . from ) {
439
+ if ( peer . id . toB58String ( ) === msgObj . from ) {
440
440
return
441
441
}
442
442
this . _sendRpc ( peer , { msgs : [ msgObj ] } )
@@ -591,7 +591,7 @@ class GossipSub extends BasicPubsub {
591
591
* @returns {void }
592
592
*/
593
593
_pushGossip ( peer , controlIHaveMsgs ) {
594
- this . log ( 'Add gossip to %s' , peer . info . id . toB58String ( ) )
594
+ this . log ( 'Add gossip to %s' , peer . id . toB58String ( ) )
595
595
const gossip = this . gossip . get ( peer ) || [ ]
596
596
this . gossip . set ( peer , gossip . concat ( controlIHaveMsgs ) )
597
597
}
0 commit comments