Skip to content

Commit 602ccaa

Browse files
committedApr 23, 2020
chore: remove peer-info usage
BREAKING CHANGE: using new topology api with peer-id instead of peer-info and new pubsub internal peer data structure
1 parent f2b67a2 commit 602ccaa

15 files changed

+121
-127
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const registrar = {
4444
}
4545
}
4646

47-
const gsub = new Gossipsub(peerInfo, registrar, options)
47+
const gsub = new Gossipsub(peerId, registrar, options)
4848

4949
await gsub.start()
5050

@@ -62,7 +62,7 @@ gsub.publish('fruit', new Buffer('banana'))
6262

6363
```js
6464
const options = {…}
65-
const gossipsub = new Gossipsub(peerInfo, registrar, options)
65+
const gossipsub = new Gossipsub(peerId, registrar, options)
6666
```
6767

6868
Options is an optional object with the following key-value pairs:

‎package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
"err-code": "^2.0.0",
3939
"it-length-prefixed": "^3.0.0",
4040
"it-pipe": "^1.0.1",
41-
"libp2p-pubsub": "~0.4.1",
41+
"libp2p-pubsub": "^0.5.0",
4242
"p-map": "^3.0.0",
4343
"peer-id": "~0.13.3",
44-
"peer-info": "~0.17.0",
4544
"protons": "^1.0.1",
4645
"time-cache": "^0.3.0"
4746
},
@@ -55,7 +54,7 @@
5554
"detect-node": "^2.0.4",
5655
"dirty-chai": "^2.0.1",
5756
"it-pair": "^1.0.0",
58-
"libp2p-floodsub": "^0.20.0",
57+
"libp2p-floodsub": "^0.21.0",
5958
"lodash": "^4.17.15",
6059
"mocha": "^6.2.1",
6160
"p-times": "^2.1.0",

‎src/heartbeat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Heartbeat {
8383
return
8484
}
8585

86-
this.gossipsub.log('HEARTBEAT: Add mesh link to %s in %s', peer.info.id.toB58String(), topic)
86+
this.gossipsub.log('HEARTBEAT: Add mesh link to %s in %s', peer.id.toB58String(), topic)
8787
peers.add(peer)
8888
const peerGrafts = tograft.get(peer)
8989
if (!peerGrafts) {
@@ -102,7 +102,7 @@ class Heartbeat {
102102
peersArray = peersArray.slice(0, idontneed)
103103

104104
peersArray.forEach((peer) => {
105-
this.gossipsub.log('HEARTBEAT: Remove mesh link to %s in %s', peer.info.id.toB58String(), topic)
105+
this.gossipsub.log('HEARTBEAT: Remove mesh link to %s in %s', peer.id.toB58String(), topic)
106106
peers.delete(peer)
107107
const peerPrunes = toprune.get(peer)
108108
if (!peerPrunes) {

‎src/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/// <reference types="node"/>
55

6-
import PeerInfo = require('peer-info');
6+
import PeerId = require('peer-id');
77

88
export interface Registrar {
99
handle: Function;
@@ -29,7 +29,7 @@ import * as Events from "events";
2929
interface GossipSub extends Events.EventEmitter {}
3030

3131
declare class GossipSub {
32-
constructor(peerInfo: PeerInfo, registrar: Registrar, options: Options);
32+
constructor(peerId: PeerId, registrar: Registrar, options: Options);
3333
publish(topic: string, data: Buffer): Promise<void>;
3434
start(): Promise<void>;
3535
stop(): Promise<void>;

‎src/index.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { utils } = require('libp2p-pubsub')
44

5-
const PeerInfo = require('peer-info')
5+
const PeerId = require('peer-id')
66

77
const BasicPubsub = require('./pubsub')
88
const { MessageCache } = require('./messageCache')
@@ -13,7 +13,7 @@ const Heartbeat = require('./heartbeat')
1313

1414
class GossipSub extends BasicPubsub {
1515
/**
16-
* @param {PeerInfo} peerInfo instance of the peer's PeerInfo
16+
* @param {PeerId} peerId instance of the peer's PeerId
1717
* @param {Object} registrar
1818
* @param {function} registrar.handle
1919
* @param {function} registrar.register
@@ -26,15 +26,15 @@ class GossipSub extends BasicPubsub {
2626
* @param {Object} [options.messageCache] override the default MessageCache
2727
* @constructor
2828
*/
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`')
3232
}
3333

3434
super({
3535
debugName: 'libp2p:gossipsub',
3636
multicodec: constants.GossipSubID,
37-
peerInfo,
37+
peerId,
3838
registrar,
3939
options
4040
})
@@ -95,7 +95,7 @@ class GossipSub extends BasicPubsub {
9595
* Removes a peer from the router
9696
* @override
9797
* @param {Peer} peer
98-
* @returns {PeerInfo}
98+
* @returns {Peer}
9999
*/
100100
_removePeer (peer) {
101101
super._removePeer(peer)
@@ -162,13 +162,13 @@ class GossipSub extends BasicPubsub {
162162

163163
// Emit to floodsub peers
164164
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 &&
167167
utils.anyMatch(peer.topics, topics) &&
168168
peer.isWritable
169169
) {
170170
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())
172172
}
173173
})
174174

@@ -178,11 +178,11 @@ class GossipSub extends BasicPubsub {
178178
return
179179
}
180180
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) {
182182
return
183183
}
184184
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())
186186
})
187187
})
188188
}
@@ -213,7 +213,7 @@ class GossipSub extends BasicPubsub {
213213
return
214214
}
215215

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())
217217

218218
return {
219219
messageIDs: Array.from(iwant)
@@ -244,7 +244,7 @@ class GossipSub extends BasicPubsub {
244244
return
245245
}
246246

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())
248248

249249
return Array.from(ihave.values())
250250
}
@@ -263,7 +263,7 @@ class GossipSub extends BasicPubsub {
263263
if (!peers) {
264264
prune.push(topicID)
265265
} 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)
267267
peers.add(peer)
268268
peer.topics.add(topicID)
269269
this.mesh.set(topicID, peers)
@@ -293,7 +293,7 @@ class GossipSub extends BasicPubsub {
293293
prune.forEach(({ topicID }) => {
294294
const peers = this.mesh.get(topicID)
295295
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)
297297
peers.delete(peer)
298298
peer.topics.delete(topicID)
299299
}
@@ -352,7 +352,7 @@ class GossipSub extends BasicPubsub {
352352
this.mesh.set(topic, peers)
353353
}
354354
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)
356356
this._sendGraft(peer, topic)
357357
})
358358
})
@@ -373,7 +373,7 @@ class GossipSub extends BasicPubsub {
373373
const meshPeers = this.mesh.get(topic)
374374
if (meshPeers) {
375375
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)
377377
this._sendPrune(peer, topic)
378378
})
379379
this.mesh.delete(topic)
@@ -405,7 +405,7 @@ class GossipSub extends BasicPubsub {
405405

406406
// floodsub peers
407407
peersInTopic.forEach((peer) => {
408-
if (peer.info.protocols.has(constants.FloodSubID)) {
408+
if (peer.protocols.includes(constants.FloodSubID)) {
409409
tosend.add(peer)
410410
}
411411
})
@@ -436,7 +436,7 @@ class GossipSub extends BasicPubsub {
436436
})
437437
// Publish messages to peers
438438
tosend.forEach((peer) => {
439-
if (peer.info.id.toB58String() === msgObj.from) {
439+
if (peer.id.toB58String() === msgObj.from) {
440440
return
441441
}
442442
this._sendRpc(peer, { msgs: [msgObj] })
@@ -591,7 +591,7 @@ class GossipSub extends BasicPubsub {
591591
* @returns {void}
592592
*/
593593
_pushGossip (peer, controlIHaveMsgs) {
594-
this.log('Add gossip to %s', peer.info.id.toB58String())
594+
this.log('Add gossip to %s', peer.id.toB58String())
595595
const gossip = this.gossip.get(peer) || []
596596
this.gossip.set(peer, gossip.concat(controlIHaveMsgs))
597597
}

‎src/pubsub.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ class BasicPubSub extends Pubsub {
2020
* @param {Object} props
2121
* @param {String} props.debugName log namespace
2222
* @param {string} props.multicodec protocol identificer to connect
23-
* @param {PeerInfo} props.peerInfo peer's peerInfo
23+
* @param {PeerId} props.peerId peer's peerId
2424
* @param {Object} props.registrar registrar for libp2p protocols
2525
* @param {function} props.registrar.handle
2626
* @param {function} props.registrar.register
2727
* @param {function} props.registrar.unregister
2828
* @param {Object} [props.options]
29-
* @param {bool} [props.options.emitSelf] if publish should emit to self, if subscribed, defaults to false
30-
* @param {bool} [props.options.gossipIncoming] if incoming messages on a subscribed topic should be automatically gossiped, defaults to true
31-
* @param {bool} [props.options.fallbackToFloodsub] if dial should fallback to floodsub, defaults to true
29+
* @param {boolean} [props.options.emitSelf] if publish should emit to self, if subscribed, defaults to false
30+
* @param {boolean} [props.options.gossipIncoming] if incoming messages on a subscribed topic should be automatically gossiped, defaults to true
31+
* @param {boolean} [props.options.fallbackToFloodsub] if dial should fallback to floodsub, defaults to true
3232
* @constructor
3333
*/
34-
constructor ({ debugName, multicodec, peerInfo, registrar, options = {} }) {
34+
constructor ({ debugName, multicodec, peerId, registrar, options = {} }) {
3535
const multicodecs = [multicodec]
3636
const _options = {
3737
emitSelf: false,
@@ -48,7 +48,7 @@ class BasicPubSub extends Pubsub {
4848
super({
4949
debugName,
5050
multicodecs,
51-
peerInfo,
51+
peerId,
5252
registrar,
5353
..._options
5454
})
@@ -83,13 +83,13 @@ class BasicPubSub extends Pubsub {
8383
/**
8484
* Peer connected successfully with pubsub protocol.
8585
* @override
86-
* @param {PeerInfo} peerInfo peer info
86+
* @param {PeerId} peerId peer id
8787
* @param {Connection} conn connection to the peer
8888
* @returns {Promise<void>}
8989
*/
90-
async _onPeerConnected (peerInfo, conn) {
91-
await super._onPeerConnected(peerInfo, conn)
92-
const idB58Str = peerInfo.id.toB58String()
90+
async _onPeerConnected (peerId, conn) {
91+
await super._onPeerConnected(peerId, conn)
92+
const idB58Str = peerId.toB58String()
9393
const peer = this.peers.get(idB58Str)
9494

9595
if (peer && peer.isWritable) {
@@ -123,7 +123,7 @@ class BasicPubSub extends Pubsub {
123123
}
124124
)
125125
} catch (err) {
126-
this._onPeerDisconnected(peer.info, err)
126+
this._onPeerDisconnected(peer.id, err)
127127
}
128128
}
129129

@@ -167,7 +167,7 @@ class BasicPubSub extends Pubsub {
167167
topicSet.delete(peer)
168168
}
169169
})
170-
this.emit('pubsub:subscription-change', peer.info, peer.topics, subs)
170+
this.emit('pubsub:subscription-change', peer.id, peer.topics, subs)
171171
}
172172

173173
if (msgs.length) {
@@ -207,7 +207,7 @@ class BasicPubSub extends Pubsub {
207207
* @param {rpc.RPC.Message} msg
208208
*/
209209
_processRpcMessage (msg) {
210-
if (this.peerInfo.id.toB58String() === msg.from && !this._options.emitSelf) {
210+
if (this.peerId.toB58String() === msg.from && !this._options.emitSelf) {
211211
return
212212
}
213213

@@ -369,7 +369,7 @@ class BasicPubSub extends Pubsub {
369369
topics = utils.ensureArray(topics)
370370
messages = utils.ensureArray(messages)
371371

372-
const from = this.peerInfo.id.toB58String()
372+
const from = this.peerId.toB58String()
373373

374374
const buildMessage = (msg, cb) => {
375375
const seqno = utils.randomSeqno()
@@ -448,7 +448,7 @@ class BasicPubSub extends Pubsub {
448448
// Adds all peers using our protocol
449449
let peers = []
450450
peersInTopic.forEach((peer) => {
451-
if (peer.info.protocols.has(GossipSubID)) {
451+
if (peer.protocols.includes(GossipSubID)) {
452452
peers.push(peer)
453453
}
454454
})

0 commit comments

Comments
 (0)
Please sign in to comment.