Skip to content

Commit 1970522

Browse files
authoredApr 23, 2020
Merge pull request #70 from ChainSafe/chore/remove-peer-info-usage
chore: remove peer-info usage
2 parents cf62125 + 5177489 commit 1970522

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
@@ -39,10 +39,9 @@
3939
"err-code": "^2.0.0",
4040
"it-length-prefixed": "^3.0.0",
4141
"it-pipe": "^1.0.1",
42-
"libp2p-pubsub": "~0.4.2",
42+
"libp2p-pubsub": "^0.5.0",
4343
"p-map": "^4.0.0",
4444
"peer-id": "~0.13.3",
45-
"peer-info": "~0.17.0",
4645
"protons": "^1.0.1",
4746
"time-cache": "^0.3.0"
4847
},
@@ -56,7 +55,7 @@
5655
"detect-node": "^2.0.4",
5756
"dirty-chai": "^2.0.1",
5857
"it-pair": "^1.0.0",
59-
"libp2p-floodsub": "^0.20.3",
58+
"libp2p-floodsub": "^0.21.0",
6059
"lodash": "^4.17.15",
6160
"mocha": "^7.1.1",
6261
"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
})

‎test/2-nodes.spec.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ describe('2 nodes', () => {
6868

6969
// Notice peers of connection
7070
const [d0, d1] = ConnectionPair()
71-
onConnect0(nodes[1].peerInfo, d0)
72-
onConnect1(nodes[0].peerInfo, d1)
71+
onConnect0(nodes[1].peerId, d0)
72+
onConnect1(nodes[0].peerId, d1)
7373

7474
expect(nodes[0].peers.size).to.be.eql(1)
7575
expect(nodes[1].peers.size).to.be.eql(1)
@@ -93,7 +93,7 @@ describe('2 nodes', () => {
9393
nodes[1].subscribe(topic)
9494

9595
// await subscription change
96-
const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => {
96+
const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => {
9797
nodes[0].once('pubsub:subscription-change', (...args) => resolve(args))
9898
})
9999

@@ -104,7 +104,7 @@ describe('2 nodes', () => {
104104
expectSet(first(nodes[0].peers).topics, [topic])
105105
expectSet(first(nodes[1].peers).topics, [topic])
106106

107-
expect(changedPeerInfo.id.toB58String()).to.equal(first(nodes[0].peers).info.id.toB58String())
107+
expect(changedPeerId.toB58String()).to.equal(first(nodes[0].peers).id.toB58String())
108108
expectSet(changedTopics, [topic])
109109
expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: true }])
110110

@@ -114,8 +114,8 @@ describe('2 nodes', () => {
114114
new Promise((resolve) => nodes[1].once('gossipsub:heartbeat', resolve))
115115
])
116116

117-
expect(first(nodes[0].mesh.get(topic)).info.id.toB58String()).to.equal(first(nodes[0].peers).info.id.toB58String())
118-
expect(first(nodes[1].mesh.get(topic)).info.id.toB58String()).to.equal(first(nodes[1].peers).info.id.toB58String())
117+
expect(first(nodes[0].mesh.get(topic)).id.toB58String()).to.equal(first(nodes[0].peers).id.toB58String())
118+
expect(first(nodes[1].mesh.get(topic)).id.toB58String()).to.equal(first(nodes[1].peers).id.toB58String())
119119
})
120120
})
121121

@@ -152,7 +152,7 @@ describe('2 nodes', () => {
152152
const msg = await promise
153153

154154
expect(msg.data.toString()).to.equal('hey')
155-
expect(msg.from).to.be.eql(nodes[0].peerInfo.id.toB58String())
155+
expect(msg.from).to.be.eql(nodes[0].peerId.toB58String())
156156

157157
nodes[0].removeListener(topic, shouldNotHappen)
158158
})
@@ -166,7 +166,7 @@ describe('2 nodes', () => {
166166
const msg = await promise
167167

168168
expect(msg.data.toString()).to.equal('banana')
169-
expect(msg.from).to.be.eql(nodes[1].peerInfo.id.toB58String())
169+
expect(msg.from).to.be.eql(nodes[1].peerId.toB58String())
170170

171171
nodes[1].removeListener(topic, shouldNotHappen)
172172
})
@@ -180,7 +180,7 @@ describe('2 nodes', () => {
180180

181181
function receivedMsg (msg) {
182182
expect(msg.data.toString()).to.equal('banana')
183-
expect(msg.from).to.be.eql(nodes[1].peerInfo.id.toB58String())
183+
expect(msg.from).to.be.eql(nodes[1].peerId.toB58String())
184184
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
185185
expect(msg.topicIDs).to.be.eql([topic])
186186

@@ -205,7 +205,7 @@ describe('2 nodes', () => {
205205

206206
function receivedMsg (msg) {
207207
expect(msg.data.toString()).to.equal('banana')
208-
expect(msg.from).to.be.eql(nodes[1].peerInfo.id.toB58String())
208+
expect(msg.from).to.be.eql(nodes[1].peerId.toB58String())
209209
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
210210
expect(msg.topicIDs).to.be.eql([topic])
211211

@@ -252,14 +252,14 @@ describe('2 nodes', () => {
252252
nodes[0].unsubscribe(topic)
253253
expect(nodes[0].subscriptions.size).to.equal(0)
254254

255-
const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => {
255+
const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => {
256256
nodes[1].once('pubsub:subscription-change', (...args) => resolve(args))
257257
})
258258
await new Promise((resolve) => nodes[1].once('gossipsub:heartbeat', resolve))
259259

260260
expect(nodes[1].peers.size).to.equal(1)
261261
expectSet(first(nodes[1].peers).topics, [])
262-
expect(changedPeerInfo.id.toB58String()).to.equal(first(nodes[1].peers).info.id.toB58String())
262+
expect(changedPeerId.toB58String()).to.equal(first(nodes[1].peers).id.toB58String())
263263
expectSet(changedTopics, [])
264264
expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: false }])
265265
})
@@ -317,8 +317,8 @@ describe('2 nodes', () => {
317317

318318
// Notice peers of connection
319319
const [d0, d1] = ConnectionPair()
320-
onConnect0(nodes[1].peerInfo, d0)
321-
onConnect1(nodes[0].peerInfo, d1)
320+
onConnect0(nodes[1].peerId, d0)
321+
onConnect1(nodes[0].peerId, d1)
322322

323323
await Promise.all([
324324
new Promise((resolve) => nodes[0].once('pubsub:subscription-change', resolve)),

‎test/floodsub.spec.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe('gossipsub fallbacks to floodsub', () => {
5151

5252
// Notice peers of connection
5353
const [d0, d1] = ConnectionPair()
54-
onConnectGs(nodeFs.peerInfo, d0)
55-
onConnectFs(nodeGs.peerInfo, d1)
54+
onConnectGs(nodeFs.peerId, d0)
55+
onConnectFs(nodeGs.peerId, d1)
5656

5757
expect(nodeGs.peers.size).to.equal(1)
5858
expect(nodeFs.peers.size).to.equal(1)
@@ -115,8 +115,8 @@ describe('gossipsub fallbacks to floodsub', () => {
115115

116116
// Notice peers of connection
117117
const [d0, d1] = ConnectionPair()
118-
onConnectGs(nodeFs.peerInfo, d0)
119-
onConnectFs(nodeGs.peerInfo, d1)
118+
onConnectGs(nodeFs.peerId, d0)
119+
onConnectFs(nodeGs.peerId, d1)
120120
})
121121

122122
after(async function () {
@@ -134,7 +134,7 @@ describe('gossipsub fallbacks to floodsub', () => {
134134
nodeFs.subscribe(topic)
135135

136136
// await subscription change
137-
const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => {
137+
const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => {
138138
nodeGs.once('pubsub:subscription-change', (...args) => resolve(args))
139139
})
140140
await new Promise((resolve) => setTimeout(resolve, 1000))
@@ -146,7 +146,7 @@ describe('gossipsub fallbacks to floodsub', () => {
146146
expectSet(first(nodeGs.peers).topics, [topic])
147147
expectSet(first(nodeFs.peers).topics, [topic])
148148

149-
expect(changedPeerInfo.id.toB58String()).to.equal(first(nodeGs.peers).info.id.toB58String())
149+
expect(changedPeerId.toB58String()).to.equal(first(nodeGs.peers).id.toB58String())
150150
expectSet(changedTopics, [topic])
151151
expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: true }])
152152
})
@@ -170,8 +170,8 @@ describe('gossipsub fallbacks to floodsub', () => {
170170

171171
// Notice peers of connection
172172
const [d0, d1] = ConnectionPair()
173-
onConnectGs(nodeFs.peerInfo, d0)
174-
onConnectFs(nodeGs.peerInfo, d1)
173+
onConnectGs(nodeFs.peerId, d0)
174+
onConnectFs(nodeGs.peerId, d1)
175175

176176
nodeGs.subscribe(topic)
177177
nodeFs.subscribe(topic)
@@ -200,7 +200,7 @@ describe('gossipsub fallbacks to floodsub', () => {
200200

201201
promise.then((msg) => {
202202
expect(msg.data.toString()).to.equal('hey')
203-
expect(msg.from).to.be.eql(nodeGs.peerInfo.id.toB58String())
203+
expect(msg.from).to.be.eql(nodeGs.peerId.toB58String())
204204

205205
nodeGs.removeListener(topic, shouldNotHappen)
206206
done()
@@ -215,7 +215,7 @@ describe('gossipsub fallbacks to floodsub', () => {
215215
const msg = await promise
216216

217217
expect(msg.data.toString()).to.equal('banana')
218-
expect(msg.from).to.be.eql(nodeFs.peerInfo.id.toB58String())
218+
expect(msg.from).to.be.eql(nodeFs.peerId.toB58String())
219219
})
220220

221221
it('Publish 10 msg to a topic', (done) => {
@@ -230,7 +230,7 @@ describe('gossipsub fallbacks to floodsub', () => {
230230

231231
function receivedMsg (msg) {
232232
expect(msg.data.toString()).to.equal('banana ' + counter)
233-
expect(msg.from).to.be.eql(nodeGs.peerInfo.id.toB58String())
233+
expect(msg.from).to.be.eql(nodeGs.peerId.toB58String())
234234
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
235235
expect(msg.topicIDs).to.be.eql([topic])
236236

@@ -257,7 +257,7 @@ describe('gossipsub fallbacks to floodsub', () => {
257257

258258
function receivedMsg (msg) {
259259
expect(msg.data.toString()).to.equal('banana ' + counter)
260-
expect(msg.from).to.be.eql(nodeGs.peerInfo.id.toB58String())
260+
expect(msg.from).to.be.eql(nodeGs.peerId.toB58String())
261261
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
262262
expect(msg.topicIDs).to.be.eql([topic])
263263

@@ -291,8 +291,8 @@ describe('gossipsub fallbacks to floodsub', () => {
291291

292292
// Notice peers of connection
293293
const [d0, d1] = ConnectionPair()
294-
await onConnectGs(nodeFs.peerInfo, d0)
295-
await onConnectFs(nodeGs.peerInfo, d1)
294+
await onConnectGs(nodeFs.peerId, d0)
295+
await onConnectFs(nodeGs.peerId, d1)
296296

297297
nodeGs.subscribe(topic)
298298
nodeFs.subscribe(topic)
@@ -313,13 +313,13 @@ describe('gossipsub fallbacks to floodsub', () => {
313313
nodeGs.unsubscribe(topic)
314314
expect(nodeGs.subscriptions.size).to.equal(0)
315315

316-
const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => {
316+
const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => {
317317
nodeFs.once('floodsub:subscription-change', (...args) => resolve(args))
318318
})
319319

320320
expect(nodeFs.peers.size).to.equal(1)
321321
expectSet(first(nodeFs.peers).topics, [])
322-
expect(changedPeerInfo.id.toB58String()).to.equal(first(nodeFs.peers).info.id.toB58String())
322+
expect(changedPeerId.toB58String()).to.equal(first(nodeFs.peers).id.toB58String())
323323
expectSet(changedTopics, [])
324324
expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: false }])
325325
})

‎test/gossip-incoming.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('gossip incoming', () => {
4848
const msg = await promise
4949

5050
expect(msg.data.toString()).to.equal('hey')
51-
expect(msg.from).to.be.eql(nodes[0].peerInfo.id.toB58String())
51+
expect(msg.from).to.be.eql(nodes[0].peerId.toB58String())
5252

5353
nodes[0].removeListener(topic, shouldNotHappen)
5454
})

‎test/gossip.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('gossip', () => {
5252
.map((call) => call.args[1])
5353
.forEach((peerId) => {
5454
nodeA.mesh.get(topic).forEach((meshPeer) => {
55-
expect(meshPeer.info.id.toB58String()).to.not.equal(peerId)
55+
expect(meshPeer.id.toB58String()).to.not.equal(peerId)
5656
})
5757
})
5858

@@ -76,7 +76,7 @@ describe('gossip', () => {
7676
await new Promise((resolve) => setTimeout(resolve, 500))
7777

7878
const peerB = first(nodeA.mesh.get(topic))
79-
const nodeB = nodes.find((n) => n.peerInfo.id.toB58String() === peerB.info.id.toB58String())
79+
const nodeB = nodes.find((n) => n.peerId.toB58String() === peerB.id.toB58String())
8080

8181
// set spy
8282
sinon.spy(nodeB, 'log')
@@ -91,7 +91,7 @@ describe('gossip', () => {
9191
// expect control message to be sent alongside published message
9292
const call = nodeB.log.getCalls().find((call) => call.args[0] === 'GRAFT: Add mesh link from %s in %s')
9393
expect(call).to.not.equal(undefined)
94-
expect(call.args[1]).to.equal(nodeA.peerInfo.id.toB58String())
94+
expect(call.args[1]).to.equal(nodeA.peerId.toB58String())
9595

9696
// unset spy
9797
nodeB.log.restore()

‎test/heartbeat.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const { expect } = require('chai')
55

66
const Gossipsub = require('../src')
77
const { GossipSubHeartbeatInterval } = require('../src/constants')
8-
const { createPeerInfo, mockRegistrar } = require('./utils')
8+
const { createPeerId, mockRegistrar } = require('./utils')
99

1010
describe('heartbeat', () => {
1111
let gossipsub
1212

1313
before(async () => {
14-
const peerInfo = await createPeerInfo()
15-
gossipsub = new Gossipsub(peerInfo, mockRegistrar, { emitSelf: true })
14+
const peerId = await createPeerId()
15+
gossipsub = new Gossipsub(peerId, mockRegistrar, { emitSelf: true })
1616
await gossipsub.start()
1717
})
1818

‎test/mesh.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ describe('mesh overlay', () => {
4242

4343
// Notice peers of connection
4444
const [d0, d1] = ConnectionPair()
45-
onConnect0(nodes[n].peerInfo, d0)
46-
onConnectN(nodes[0].peerInfo, d1)
45+
onConnect0(nodes[n].peerId, d0)
46+
onConnectN(nodes[0].peerId, d1)
4747
}
4848

4949
// await mesh rebalancing
@@ -69,8 +69,8 @@ describe('mesh overlay', () => {
6969

7070
// Notice peers of connection
7171
const [d0, d1] = ConnectionPair()
72-
onConnect0(nodes[i].peerInfo, d0)
73-
onConnectN(nodes[0].peerInfo, d1)
72+
onConnect0(nodes[i].peerId, d0)
73+
onConnectN(nodes[0].peerId, d1)
7474
}
7575

7676
await new Promise((resolve) => setTimeout(resolve, 500))

‎test/multiple-nodes.spec.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ describe('multiple nodes (more than 2)', () => {
4141

4242
// Notice peers of connection
4343
const [d0, d1] = ConnectionPair()
44-
onConnectA(b.peerInfo, d0)
45-
onConnectB(a.peerInfo, d1)
44+
onConnectA(b.peerId, d0)
45+
onConnectB(a.peerId, d1)
4646

4747
const [d2, d3] = ConnectionPair()
48-
onConnectB(c.peerInfo, d2)
49-
onConnectC(b.peerInfo, d3)
48+
onConnectB(c.peerId, d2)
49+
onConnectC(b.peerId, d3)
5050
})
5151

5252
after(() => Promise.all(nodes.map((n) => n.stop())))
@@ -70,9 +70,9 @@ describe('multiple nodes (more than 2)', () => {
7070
expect(b.peers.size).to.equal(2)
7171
expect(c.peers.size).to.equal(1)
7272

73-
const aPeerId = a.peerInfo.id.toB58String()
74-
const bPeerId = b.peerInfo.id.toB58String()
75-
const cPeerId = c.peerInfo.id.toB58String()
73+
const aPeerId = a.peerId.toB58String()
74+
const bPeerId = b.peerId.toB58String()
75+
const cPeerId = c.peerId.toB58String()
7676

7777
expectSet(a.peers.get(bPeerId).topics, [topic])
7878
expectSet(b.peers.get(aPeerId).topics, [topic])
@@ -106,12 +106,12 @@ describe('multiple nodes (more than 2)', () => {
106106

107107
// Notice peers of connection
108108
const [d0, d1] = ConnectionPair()
109-
onConnectA(b.peerInfo, d0)
110-
onConnectB(a.peerInfo, d1)
109+
onConnectA(b.peerId, d0)
110+
onConnectB(a.peerId, d1)
111111

112112
const [d2, d3] = ConnectionPair()
113-
onConnectB(c.peerInfo, d2)
114-
onConnectC(b.peerInfo, d3)
113+
onConnectB(c.peerId, d2)
114+
onConnectC(b.peerId, d3)
115115

116116
a.subscribe(topic)
117117
b.subscribe(topic)
@@ -201,12 +201,12 @@ describe('multiple nodes (more than 2)', () => {
201201

202202
// Notice peers of connection
203203
const [d0, d1] = ConnectionPair()
204-
onConnectA(b.peerInfo, d0)
205-
onConnectB(a.peerInfo, d1)
204+
onConnectA(b.peerId, d0)
205+
onConnectB(a.peerId, d1)
206206

207207
const [d2, d3] = ConnectionPair()
208-
onConnectB(c.peerInfo, d2)
209-
onConnectC(b.peerInfo, d3)
208+
onConnectB(c.peerId, d2)
209+
onConnectC(b.peerId, d3)
210210

211211
a.subscribe(topic)
212212
b.subscribe(topic)
@@ -266,20 +266,20 @@ describe('multiple nodes (more than 2)', () => {
266266

267267
// Notice peers of connection
268268
const [d0, d1] = ConnectionPair()
269-
onConnectA(b.peerInfo, d0)
270-
onConnectB(a.peerInfo, d1)
269+
onConnectA(b.peerId, d0)
270+
onConnectB(a.peerId, d1)
271271

272272
const [d2, d3] = ConnectionPair()
273-
onConnectB(c.peerInfo, d2)
274-
onConnectC(b.peerInfo, d3)
273+
onConnectB(c.peerId, d2)
274+
onConnectC(b.peerId, d3)
275275

276276
const [d4, d5] = ConnectionPair()
277-
onConnectC(d.peerInfo, d4)
278-
onConnectD(c.peerInfo, d5)
277+
onConnectC(d.peerId, d4)
278+
onConnectD(c.peerId, d5)
279279

280280
const [d6, d7] = ConnectionPair()
281-
onConnectD(e.peerInfo, d6)
282-
onConnectE(d.peerInfo, d7)
281+
onConnectD(e.peerId, d6)
282+
onConnectE(d.peerId, d7)
283283

284284
a.subscribe(topic)
285285
b.subscribe(topic)

‎test/pubsub.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const pWaitFor = require('p-wait-for')
1111
const { utils } = require('libp2p-pubsub')
1212
const {
1313
createGossipsub,
14-
createPeerInfo,
14+
createPeerId,
1515
mockRegistrar
1616
} = require('./utils')
1717

@@ -127,7 +127,7 @@ describe('Pubsub', () => {
127127
it('should disconnect peer on stream error', async () => {
128128
sinon.spy(gossipsub, '_onPeerDisconnected')
129129

130-
const peerInfo = await createPeerInfo()
130+
const peerId = await createPeerId()
131131
const mockConn = {
132132
newStream () {
133133
return {
@@ -147,9 +147,9 @@ describe('Pubsub', () => {
147147
}
148148
}
149149

150-
gossipsub._onPeerConnected(peerInfo, mockConn)
150+
gossipsub._onPeerConnected(peerId, mockConn)
151151

152-
await pWaitFor(() => gossipsub._onPeerDisconnected.calledWith(peerInfo), { timeout: 1000 })
152+
await pWaitFor(() => gossipsub._onPeerDisconnected.calledWith(peerId), { timeout: 1000 })
153153
})
154154
})
155155
})

‎test/utils/index.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,27 @@ const DuplexPair = require('it-pair/duplex')
66
const pTimes = require('p-times')
77

88
const FloodSub = require('libp2p-floodsub')
9-
const { multicodec: floodsubMulticodec } = require('libp2p-floodsub')
109
const PeerId = require('peer-id')
11-
const PeerInfo = require('peer-info')
1210

1311
const GossipSub = require('../../src')
14-
const { GossipSubID } = require('../../src/constants')
1512

1613
exports.first = (map) => map.values().next().value
1714

1815
exports.expectSet = (set, subs) => {
1916
expect(Array.from(set.values())).to.eql(subs)
2017
}
2118

22-
const createPeerInfo = async (protocol = GossipSubID) => {
19+
const createPeerId = async () => {
2320
const peerId = await PeerId.create({ bits: 1024 })
24-
const peerInfo = await PeerInfo.create(peerId)
25-
peerInfo.protocols.add(protocol)
2621

27-
return peerInfo
22+
return peerId
2823
}
2924

30-
exports.createPeerInfo = createPeerInfo
25+
exports.createPeerId = createPeerId
3126

3227
const createGossipsub = async (registrar, shouldStart = false, options) => {
33-
const peerInfo = await createPeerInfo()
34-
const gs = new GossipSub(peerInfo, registrar, options)
28+
const peerId = await createPeerId()
29+
const gs = new GossipSub(peerId, registrar, options)
3530

3631
if (shouldStart) {
3732
await gs.start()
@@ -68,8 +63,8 @@ const connectGossipsubNodes = (nodes, registrarRecords, multicodec) => {
6863

6964
// Notice peers of connection
7065
const [d0, d1] = ConnectionPair()
71-
onConnectI(nodes[j].peerInfo, d0)
72-
onConnectJ(nodes[i].peerInfo, d1)
66+
onConnectI(nodes[j].peerId, d0)
67+
onConnectJ(nodes[i].peerId, d1)
7368
}
7469
}
7570

@@ -88,8 +83,8 @@ const createGossipsubConnectedNodes = async (n, multicodec, options) => {
8883
exports.createGossipsubConnectedNodes = createGossipsubConnectedNodes
8984

9085
const createFloodsubNode = async (registrar, shouldStart = false, options) => {
91-
const peerInfo = await createPeerInfo(floodsubMulticodec)
92-
const fs = new FloodSub(peerInfo, registrar, options)
86+
const peerId = await createPeerId()
87+
const fs = new FloodSub(peerId, registrar, options)
9388

9489
if (shouldStart) {
9590
await fs.start()

0 commit comments

Comments
 (0)
Please sign in to comment.