From 602ccaaf77016323900b523cb19b3b438f7f6276 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 7 Apr 2020 14:11:04 +0200 Subject: [PATCH] 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 --- README.md | 4 ++-- package.json | 5 ++-- src/heartbeat.js | 4 ++-- src/index.d.ts | 4 ++-- src/index.js | 42 ++++++++++++++++---------------- src/pubsub.js | 30 +++++++++++------------ test/2-nodes.spec.js | 28 +++++++++++----------- test/floodsub.spec.js | 32 ++++++++++++------------- test/gossip-incoming.spec.js | 2 +- test/gossip.js | 6 ++--- test/heartbeat.spec.js | 6 ++--- test/mesh.spec.js | 8 +++---- test/multiple-nodes.spec.js | 46 ++++++++++++++++++------------------ test/pubsub.spec.js | 8 +++---- test/utils/index.js | 23 +++++++----------- 15 files changed, 121 insertions(+), 127 deletions(-) diff --git a/README.md b/README.md index 4cf64f08..9de14778 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ const registrar = { } } -const gsub = new Gossipsub(peerInfo, registrar, options) +const gsub = new Gossipsub(peerId, registrar, options) await gsub.start() @@ -62,7 +62,7 @@ gsub.publish('fruit', new Buffer('banana')) ```js const options = {…} -const gossipsub = new Gossipsub(peerInfo, registrar, options) +const gossipsub = new Gossipsub(peerId, registrar, options) ``` Options is an optional object with the following key-value pairs: diff --git a/package.json b/package.json index ec99fd60..cd9314c5 100644 --- a/package.json +++ b/package.json @@ -38,10 +38,9 @@ "err-code": "^2.0.0", "it-length-prefixed": "^3.0.0", "it-pipe": "^1.0.1", - "libp2p-pubsub": "~0.4.1", + "libp2p-pubsub": "^0.5.0", "p-map": "^3.0.0", "peer-id": "~0.13.3", - "peer-info": "~0.17.0", "protons": "^1.0.1", "time-cache": "^0.3.0" }, @@ -55,7 +54,7 @@ "detect-node": "^2.0.4", "dirty-chai": "^2.0.1", "it-pair": "^1.0.0", - "libp2p-floodsub": "^0.20.0", + "libp2p-floodsub": "^0.21.0", "lodash": "^4.17.15", "mocha": "^6.2.1", "p-times": "^2.1.0", diff --git a/src/heartbeat.js b/src/heartbeat.js index 276bcd76..b28f56e7 100644 --- a/src/heartbeat.js +++ b/src/heartbeat.js @@ -83,7 +83,7 @@ class Heartbeat { return } - this.gossipsub.log('HEARTBEAT: Add mesh link to %s in %s', peer.info.id.toB58String(), topic) + this.gossipsub.log('HEARTBEAT: Add mesh link to %s in %s', peer.id.toB58String(), topic) peers.add(peer) const peerGrafts = tograft.get(peer) if (!peerGrafts) { @@ -102,7 +102,7 @@ class Heartbeat { peersArray = peersArray.slice(0, idontneed) peersArray.forEach((peer) => { - this.gossipsub.log('HEARTBEAT: Remove mesh link to %s in %s', peer.info.id.toB58String(), topic) + this.gossipsub.log('HEARTBEAT: Remove mesh link to %s in %s', peer.id.toB58String(), topic) peers.delete(peer) const peerPrunes = toprune.get(peer) if (!peerPrunes) { diff --git a/src/index.d.ts b/src/index.d.ts index 5a359be1..8737c767 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -3,7 +3,7 @@ /// -import PeerInfo = require('peer-info'); +import PeerId = require('peer-id'); export interface Registrar { handle: Function; @@ -29,7 +29,7 @@ import * as Events from "events"; interface GossipSub extends Events.EventEmitter {} declare class GossipSub { - constructor(peerInfo: PeerInfo, registrar: Registrar, options: Options); + constructor(peerId: PeerId, registrar: Registrar, options: Options); publish(topic: string, data: Buffer): Promise; start(): Promise; stop(): Promise; diff --git a/src/index.js b/src/index.js index 9fa9b34a..252d68fc 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ const { utils } = require('libp2p-pubsub') -const PeerInfo = require('peer-info') +const PeerId = require('peer-id') const BasicPubsub = require('./pubsub') const { MessageCache } = require('./messageCache') @@ -13,7 +13,7 @@ const Heartbeat = require('./heartbeat') class GossipSub extends BasicPubsub { /** - * @param {PeerInfo} peerInfo instance of the peer's PeerInfo + * @param {PeerId} peerId instance of the peer's PeerId * @param {Object} registrar * @param {function} registrar.handle * @param {function} registrar.register @@ -26,15 +26,15 @@ class GossipSub extends BasicPubsub { * @param {Object} [options.messageCache] override the default MessageCache * @constructor */ - constructor (peerInfo, registrar, options = {}) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw new Error('peer info must be an instance of `peer-info`') + constructor (peerId, registrar, options = {}) { + if (!PeerId.isPeerId(peerId)) { + throw new Error('peerId must be an instance of `peer-id`') } super({ debugName: 'libp2p:gossipsub', multicodec: constants.GossipSubID, - peerInfo, + peerId, registrar, options }) @@ -95,7 +95,7 @@ class GossipSub extends BasicPubsub { * Removes a peer from the router * @override * @param {Peer} peer - * @returns {PeerInfo} + * @returns {Peer} */ _removePeer (peer) { super._removePeer(peer) @@ -162,13 +162,13 @@ class GossipSub extends BasicPubsub { // Emit to floodsub peers this.peers.forEach((peer) => { - if (peer.info.protocols.has(constants.FloodSubID) && - peer.info.id.toB58String() !== msg.from && + if (peer.protocols.includes(constants.FloodSubID) && + peer.id.toB58String() !== msg.from && utils.anyMatch(peer.topics, topics) && peer.isWritable ) { peer.sendMessages(utils.normalizeOutRpcMessages([msg])) - this.log('publish msg on topics - floodsub', topics, peer.info.id.toB58String()) + this.log('publish msg on topics - floodsub', topics, peer.id.toB58String()) } }) @@ -178,11 +178,11 @@ class GossipSub extends BasicPubsub { return } this.mesh.get(topic).forEach((peer) => { - if (!peer.isWritable || peer.info.id.toB58String() === msg.from) { + if (!peer.isWritable || peer.id.toB58String() === msg.from) { return } peer.sendMessages(utils.normalizeOutRpcMessages([msg])) - this.log('publish msg on topic - meshsub', topic, peer.info.id.toB58String()) + this.log('publish msg on topic - meshsub', topic, peer.id.toB58String()) }) }) } @@ -213,7 +213,7 @@ class GossipSub extends BasicPubsub { return } - this.log('IHAVE: Asking for %d messages from %s', iwant.size, peer.info.id.toB58String()) + this.log('IHAVE: Asking for %d messages from %s', iwant.size, peer.id.toB58String()) return { messageIDs: Array.from(iwant) @@ -244,7 +244,7 @@ class GossipSub extends BasicPubsub { return } - this.log('IWANT: Sending %d messages to %s', ihave.size, peer.info.id.toB58String()) + this.log('IWANT: Sending %d messages to %s', ihave.size, peer.id.toB58String()) return Array.from(ihave.values()) } @@ -263,7 +263,7 @@ class GossipSub extends BasicPubsub { if (!peers) { prune.push(topicID) } else { - this.log('GRAFT: Add mesh link from %s in %s', peer.info.id.toB58String(), topicID) + this.log('GRAFT: Add mesh link from %s in %s', peer.id.toB58String(), topicID) peers.add(peer) peer.topics.add(topicID) this.mesh.set(topicID, peers) @@ -293,7 +293,7 @@ class GossipSub extends BasicPubsub { prune.forEach(({ topicID }) => { const peers = this.mesh.get(topicID) if (peers) { - this.log('PRUNE: Remove mesh link to %s in %s', peer.info.id.toB58String(), topicID) + this.log('PRUNE: Remove mesh link to %s in %s', peer.id.toB58String(), topicID) peers.delete(peer) peer.topics.delete(topicID) } @@ -352,7 +352,7 @@ class GossipSub extends BasicPubsub { this.mesh.set(topic, peers) } this.mesh.get(topic).forEach((peer) => { - this.log('JOIN: Add mesh link to %s in %s', peer.info.id.toB58String(), topic) + this.log('JOIN: Add mesh link to %s in %s', peer.id.toB58String(), topic) this._sendGraft(peer, topic) }) }) @@ -373,7 +373,7 @@ class GossipSub extends BasicPubsub { const meshPeers = this.mesh.get(topic) if (meshPeers) { meshPeers.forEach((peer) => { - this.log('LEAVE: Remove mesh link to %s in %s', peer.info.id.toB58String(), topic) + this.log('LEAVE: Remove mesh link to %s in %s', peer.id.toB58String(), topic) this._sendPrune(peer, topic) }) this.mesh.delete(topic) @@ -405,7 +405,7 @@ class GossipSub extends BasicPubsub { // floodsub peers peersInTopic.forEach((peer) => { - if (peer.info.protocols.has(constants.FloodSubID)) { + if (peer.protocols.includes(constants.FloodSubID)) { tosend.add(peer) } }) @@ -436,7 +436,7 @@ class GossipSub extends BasicPubsub { }) // Publish messages to peers tosend.forEach((peer) => { - if (peer.info.id.toB58String() === msgObj.from) { + if (peer.id.toB58String() === msgObj.from) { return } this._sendRpc(peer, { msgs: [msgObj] }) @@ -591,7 +591,7 @@ class GossipSub extends BasicPubsub { * @returns {void} */ _pushGossip (peer, controlIHaveMsgs) { - this.log('Add gossip to %s', peer.info.id.toB58String()) + this.log('Add gossip to %s', peer.id.toB58String()) const gossip = this.gossip.get(peer) || [] this.gossip.set(peer, gossip.concat(controlIHaveMsgs)) } diff --git a/src/pubsub.js b/src/pubsub.js index 312ec67d..951fbbff 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -20,18 +20,18 @@ class BasicPubSub extends Pubsub { * @param {Object} props * @param {String} props.debugName log namespace * @param {string} props.multicodec protocol identificer to connect - * @param {PeerInfo} props.peerInfo peer's peerInfo + * @param {PeerId} props.peerId peer's peerId * @param {Object} props.registrar registrar for libp2p protocols * @param {function} props.registrar.handle * @param {function} props.registrar.register * @param {function} props.registrar.unregister * @param {Object} [props.options] - * @param {bool} [props.options.emitSelf] if publish should emit to self, if subscribed, defaults to false - * @param {bool} [props.options.gossipIncoming] if incoming messages on a subscribed topic should be automatically gossiped, defaults to true - * @param {bool} [props.options.fallbackToFloodsub] if dial should fallback to floodsub, defaults to true + * @param {boolean} [props.options.emitSelf] if publish should emit to self, if subscribed, defaults to false + * @param {boolean} [props.options.gossipIncoming] if incoming messages on a subscribed topic should be automatically gossiped, defaults to true + * @param {boolean} [props.options.fallbackToFloodsub] if dial should fallback to floodsub, defaults to true * @constructor */ - constructor ({ debugName, multicodec, peerInfo, registrar, options = {} }) { + constructor ({ debugName, multicodec, peerId, registrar, options = {} }) { const multicodecs = [multicodec] const _options = { emitSelf: false, @@ -48,7 +48,7 @@ class BasicPubSub extends Pubsub { super({ debugName, multicodecs, - peerInfo, + peerId, registrar, ..._options }) @@ -83,13 +83,13 @@ class BasicPubSub extends Pubsub { /** * Peer connected successfully with pubsub protocol. * @override - * @param {PeerInfo} peerInfo peer info + * @param {PeerId} peerId peer id * @param {Connection} conn connection to the peer * @returns {Promise} */ - async _onPeerConnected (peerInfo, conn) { - await super._onPeerConnected(peerInfo, conn) - const idB58Str = peerInfo.id.toB58String() + async _onPeerConnected (peerId, conn) { + await super._onPeerConnected(peerId, conn) + const idB58Str = peerId.toB58String() const peer = this.peers.get(idB58Str) if (peer && peer.isWritable) { @@ -123,7 +123,7 @@ class BasicPubSub extends Pubsub { } ) } catch (err) { - this._onPeerDisconnected(peer.info, err) + this._onPeerDisconnected(peer.id, err) } } @@ -167,7 +167,7 @@ class BasicPubSub extends Pubsub { topicSet.delete(peer) } }) - this.emit('pubsub:subscription-change', peer.info, peer.topics, subs) + this.emit('pubsub:subscription-change', peer.id, peer.topics, subs) } if (msgs.length) { @@ -207,7 +207,7 @@ class BasicPubSub extends Pubsub { * @param {rpc.RPC.Message} msg */ _processRpcMessage (msg) { - if (this.peerInfo.id.toB58String() === msg.from && !this._options.emitSelf) { + if (this.peerId.toB58String() === msg.from && !this._options.emitSelf) { return } @@ -369,7 +369,7 @@ class BasicPubSub extends Pubsub { topics = utils.ensureArray(topics) messages = utils.ensureArray(messages) - const from = this.peerInfo.id.toB58String() + const from = this.peerId.toB58String() const buildMessage = (msg, cb) => { const seqno = utils.randomSeqno() @@ -448,7 +448,7 @@ class BasicPubSub extends Pubsub { // Adds all peers using our protocol let peers = [] peersInTopic.forEach((peer) => { - if (peer.info.protocols.has(GossipSubID)) { + if (peer.protocols.includes(GossipSubID)) { peers.push(peer) } }) diff --git a/test/2-nodes.spec.js b/test/2-nodes.spec.js index 94310eb8..402b6de7 100644 --- a/test/2-nodes.spec.js +++ b/test/2-nodes.spec.js @@ -67,8 +67,8 @@ describe('2 nodes', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnect0(nodes[1].peerInfo, d0) - onConnect1(nodes[0].peerInfo, d1) + onConnect0(nodes[1].peerId, d0) + onConnect1(nodes[0].peerId, d1) expect(nodes[0].peers.size).to.be.eql(1) expect(nodes[1].peers.size).to.be.eql(1) @@ -92,7 +92,7 @@ describe('2 nodes', () => { nodes[1].subscribe(topic) // await subscription change - const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => { + const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => { nodes[0].once('pubsub:subscription-change', (...args) => resolve(args)) }) @@ -103,7 +103,7 @@ describe('2 nodes', () => { expectSet(first(nodes[0].peers).topics, [topic]) expectSet(first(nodes[1].peers).topics, [topic]) - expect(changedPeerInfo.id.toB58String()).to.equal(first(nodes[0].peers).info.id.toB58String()) + expect(changedPeerId.toB58String()).to.equal(first(nodes[0].peers).id.toB58String()) expectSet(changedTopics, [topic]) expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: true }]) @@ -113,8 +113,8 @@ describe('2 nodes', () => { new Promise((resolve) => nodes[1].once('gossipsub:heartbeat', resolve)) ]) - expect(first(nodes[0].mesh.get(topic)).info.id.toB58String()).to.equal(first(nodes[0].peers).info.id.toB58String()) - expect(first(nodes[1].mesh.get(topic)).info.id.toB58String()).to.equal(first(nodes[1].peers).info.id.toB58String()) + expect(first(nodes[0].mesh.get(topic)).id.toB58String()).to.equal(first(nodes[0].peers).id.toB58String()) + expect(first(nodes[1].mesh.get(topic)).id.toB58String()).to.equal(first(nodes[1].peers).id.toB58String()) }) }) @@ -151,7 +151,7 @@ describe('2 nodes', () => { const msg = await promise expect(msg.data.toString()).to.equal('hey') - expect(msg.from).to.be.eql(nodes[0].peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodes[0].peerId.toB58String()) nodes[0].removeListener(topic, shouldNotHappen) }) @@ -165,7 +165,7 @@ describe('2 nodes', () => { const msg = await promise expect(msg.data.toString()).to.equal('banana') - expect(msg.from).to.be.eql(nodes[1].peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodes[1].peerId.toB58String()) nodes[1].removeListener(topic, shouldNotHappen) }) @@ -179,7 +179,7 @@ describe('2 nodes', () => { function receivedMsg (msg) { expect(msg.data.toString()).to.equal('banana') - expect(msg.from).to.be.eql(nodes[1].peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodes[1].peerId.toB58String()) expect(Buffer.isBuffer(msg.seqno)).to.be.true() expect(msg.topicIDs).to.be.eql([topic]) @@ -204,7 +204,7 @@ describe('2 nodes', () => { function receivedMsg (msg) { expect(msg.data.toString()).to.equal('banana') - expect(msg.from).to.be.eql(nodes[1].peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodes[1].peerId.toB58String()) expect(Buffer.isBuffer(msg.seqno)).to.be.true() expect(msg.topicIDs).to.be.eql([topic]) @@ -251,14 +251,14 @@ describe('2 nodes', () => { nodes[0].unsubscribe(topic) expect(nodes[0].subscriptions.size).to.equal(0) - const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => { + const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => { nodes[1].once('pubsub:subscription-change', (...args) => resolve(args)) }) await new Promise((resolve) => nodes[1].once('gossipsub:heartbeat', resolve)) expect(nodes[1].peers.size).to.equal(1) expectSet(first(nodes[1].peers).topics, []) - expect(changedPeerInfo.id.toB58String()).to.equal(first(nodes[1].peers).info.id.toB58String()) + expect(changedPeerId.toB58String()).to.equal(first(nodes[1].peers).id.toB58String()) expectSet(changedTopics, []) expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: false }]) }) @@ -316,8 +316,8 @@ describe('2 nodes', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnect0(nodes[1].peerInfo, d0) - onConnect1(nodes[0].peerInfo, d1) + onConnect0(nodes[1].peerId, d0) + onConnect1(nodes[0].peerId, d1) await Promise.all([ new Promise((resolve) => nodes[0].once('pubsub:subscription-change', resolve)), diff --git a/test/floodsub.spec.js b/test/floodsub.spec.js index e9912e63..ce207c44 100644 --- a/test/floodsub.spec.js +++ b/test/floodsub.spec.js @@ -50,8 +50,8 @@ describe('gossipsub fallbacks to floodsub', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectGs(nodeFs.peerInfo, d0) - onConnectFs(nodeGs.peerInfo, d1) + onConnectGs(nodeFs.peerId, d0) + onConnectFs(nodeGs.peerId, d1) expect(nodeGs.peers.size).to.equal(1) expect(nodeFs.peers.size).to.equal(1) @@ -114,8 +114,8 @@ describe('gossipsub fallbacks to floodsub', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectGs(nodeFs.peerInfo, d0) - onConnectFs(nodeGs.peerInfo, d1) + onConnectGs(nodeFs.peerId, d0) + onConnectFs(nodeGs.peerId, d1) }) after(async function () { @@ -133,7 +133,7 @@ describe('gossipsub fallbacks to floodsub', () => { nodeFs.subscribe(topic) // await subscription change - const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => { + const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => { nodeGs.once('pubsub:subscription-change', (...args) => resolve(args)) }) await new Promise((resolve) => setTimeout(resolve, 1000)) @@ -145,7 +145,7 @@ describe('gossipsub fallbacks to floodsub', () => { expectSet(first(nodeGs.peers).topics, [topic]) expectSet(first(nodeFs.peers).topics, [topic]) - expect(changedPeerInfo.id.toB58String()).to.equal(first(nodeGs.peers).info.id.toB58String()) + expect(changedPeerId.toB58String()).to.equal(first(nodeGs.peers).id.toB58String()) expectSet(changedTopics, [topic]) expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: true }]) }) @@ -169,8 +169,8 @@ describe('gossipsub fallbacks to floodsub', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectGs(nodeFs.peerInfo, d0) - onConnectFs(nodeGs.peerInfo, d1) + onConnectGs(nodeFs.peerId, d0) + onConnectFs(nodeGs.peerId, d1) nodeGs.subscribe(topic) nodeFs.subscribe(topic) @@ -199,7 +199,7 @@ describe('gossipsub fallbacks to floodsub', () => { promise.then((msg) => { expect(msg.data.toString()).to.equal('hey') - expect(msg.from).to.be.eql(nodeGs.peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodeGs.peerId.toB58String()) nodeGs.removeListener(topic, shouldNotHappen) done() @@ -214,7 +214,7 @@ describe('gossipsub fallbacks to floodsub', () => { const msg = await promise expect(msg.data.toString()).to.equal('banana') - expect(msg.from).to.be.eql(nodeFs.peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodeFs.peerId.toB58String()) }) it('Publish 10 msg to a topic', (done) => { @@ -229,7 +229,7 @@ describe('gossipsub fallbacks to floodsub', () => { function receivedMsg (msg) { expect(msg.data.toString()).to.equal('banana ' + counter) - expect(msg.from).to.be.eql(nodeGs.peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodeGs.peerId.toB58String()) expect(Buffer.isBuffer(msg.seqno)).to.be.true() expect(msg.topicIDs).to.be.eql([topic]) @@ -256,7 +256,7 @@ describe('gossipsub fallbacks to floodsub', () => { function receivedMsg (msg) { expect(msg.data.toString()).to.equal('banana ' + counter) - expect(msg.from).to.be.eql(nodeGs.peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodeGs.peerId.toB58String()) expect(Buffer.isBuffer(msg.seqno)).to.be.true() expect(msg.topicIDs).to.be.eql([topic]) @@ -290,8 +290,8 @@ describe('gossipsub fallbacks to floodsub', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - await onConnectGs(nodeFs.peerInfo, d0) - await onConnectFs(nodeGs.peerInfo, d1) + await onConnectGs(nodeFs.peerId, d0) + await onConnectFs(nodeGs.peerId, d1) nodeGs.subscribe(topic) nodeFs.subscribe(topic) @@ -312,13 +312,13 @@ describe('gossipsub fallbacks to floodsub', () => { nodeGs.unsubscribe(topic) expect(nodeGs.subscriptions.size).to.equal(0) - const [changedPeerInfo, changedTopics, changedSubs] = await new Promise((resolve) => { + const [changedPeerId, changedTopics, changedSubs] = await new Promise((resolve) => { nodeFs.once('floodsub:subscription-change', (...args) => resolve(args)) }) expect(nodeFs.peers.size).to.equal(1) expectSet(first(nodeFs.peers).topics, []) - expect(changedPeerInfo.id.toB58String()).to.equal(first(nodeFs.peers).info.id.toB58String()) + expect(changedPeerId.toB58String()).to.equal(first(nodeFs.peers).id.toB58String()) expectSet(changedTopics, []) expect(changedSubs).to.be.eql([{ topicID: topic, subscribe: false }]) }) diff --git a/test/gossip-incoming.spec.js b/test/gossip-incoming.spec.js index f401c38f..44718d8a 100644 --- a/test/gossip-incoming.spec.js +++ b/test/gossip-incoming.spec.js @@ -47,7 +47,7 @@ describe('gossip incoming', () => { const msg = await promise expect(msg.data.toString()).to.equal('hey') - expect(msg.from).to.be.eql(nodes[0].peerInfo.id.toB58String()) + expect(msg.from).to.be.eql(nodes[0].peerId.toB58String()) nodes[0].removeListener(topic, shouldNotHappen) }) diff --git a/test/gossip.js b/test/gossip.js index a032a8a9..99f379cb 100644 --- a/test/gossip.js +++ b/test/gossip.js @@ -51,7 +51,7 @@ describe('gossip', () => { .map((call) => call.args[1]) .forEach((peerId) => { nodeA.mesh.get(topic).forEach((meshPeer) => { - expect(meshPeer.info.id.toB58String()).to.not.equal(peerId) + expect(meshPeer.id.toB58String()).to.not.equal(peerId) }) }) @@ -75,7 +75,7 @@ describe('gossip', () => { await new Promise((resolve) => setTimeout(resolve, 500)) const peerB = first(nodeA.mesh.get(topic)) - const nodeB = nodes.find((n) => n.peerInfo.id.toB58String() === peerB.info.id.toB58String()) + const nodeB = nodes.find((n) => n.peerId.toB58String() === peerB.id.toB58String()) // set spy sinon.spy(nodeB, 'log') @@ -90,7 +90,7 @@ describe('gossip', () => { // expect control message to be sent alongside published message const call = nodeB.log.getCalls().find((call) => call.args[0] === 'GRAFT: Add mesh link from %s in %s') expect(call).to.not.equal(undefined) - expect(call.args[1]).to.equal(nodeA.peerInfo.id.toB58String()) + expect(call.args[1]).to.equal(nodeA.peerId.toB58String()) // unset spy nodeB.log.restore() diff --git a/test/heartbeat.spec.js b/test/heartbeat.spec.js index 0d58602b..f89ffb51 100644 --- a/test/heartbeat.spec.js +++ b/test/heartbeat.spec.js @@ -5,14 +5,14 @@ const { expect } = require('chai') const Gossipsub = require('../src') const { GossipSubHeartbeatInterval } = require('../src/constants') -const { createPeerInfo, mockRegistrar } = require('./utils') +const { createPeerId, mockRegistrar } = require('./utils') describe('heartbeat', () => { let gossipsub before(async () => { - const peerInfo = await createPeerInfo() - gossipsub = new Gossipsub(peerInfo, mockRegistrar, { emitSelf: true }) + const peerId = await createPeerId() + gossipsub = new Gossipsub(peerId, mockRegistrar, { emitSelf: true }) await gossipsub.start() }) diff --git a/test/mesh.spec.js b/test/mesh.spec.js index f6c40440..4ac66da3 100644 --- a/test/mesh.spec.js +++ b/test/mesh.spec.js @@ -42,8 +42,8 @@ describe('mesh overlay', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnect0(nodes[n].peerInfo, d0) - onConnectN(nodes[0].peerInfo, d1) + onConnect0(nodes[n].peerId, d0) + onConnectN(nodes[0].peerId, d1) } // await mesh rebalancing @@ -69,8 +69,8 @@ describe('mesh overlay', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnect0(nodes[i].peerInfo, d0) - onConnectN(nodes[0].peerInfo, d1) + onConnect0(nodes[i].peerId, d0) + onConnectN(nodes[0].peerId, d1) } await new Promise((resolve) => setTimeout(resolve, 500)) diff --git a/test/multiple-nodes.spec.js b/test/multiple-nodes.spec.js index 50b4ffcb..531f08f9 100644 --- a/test/multiple-nodes.spec.js +++ b/test/multiple-nodes.spec.js @@ -41,12 +41,12 @@ describe('multiple nodes (more than 2)', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectA(b.peerInfo, d0) - onConnectB(a.peerInfo, d1) + onConnectA(b.peerId, d0) + onConnectB(a.peerId, d1) const [d2, d3] = ConnectionPair() - onConnectB(c.peerInfo, d2) - onConnectC(b.peerInfo, d3) + onConnectB(c.peerId, d2) + onConnectC(b.peerId, d3) }) after(() => Promise.all(nodes.map((n) => n.stop()))) @@ -70,9 +70,9 @@ describe('multiple nodes (more than 2)', () => { expect(b.peers.size).to.equal(2) expect(c.peers.size).to.equal(1) - const aPeerId = a.peerInfo.id.toB58String() - const bPeerId = b.peerInfo.id.toB58String() - const cPeerId = c.peerInfo.id.toB58String() + const aPeerId = a.peerId.toB58String() + const bPeerId = b.peerId.toB58String() + const cPeerId = c.peerId.toB58String() expectSet(a.peers.get(bPeerId).topics, [topic]) expectSet(b.peers.get(aPeerId).topics, [topic]) @@ -106,12 +106,12 @@ describe('multiple nodes (more than 2)', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectA(b.peerInfo, d0) - onConnectB(a.peerInfo, d1) + onConnectA(b.peerId, d0) + onConnectB(a.peerId, d1) const [d2, d3] = ConnectionPair() - onConnectB(c.peerInfo, d2) - onConnectC(b.peerInfo, d3) + onConnectB(c.peerId, d2) + onConnectC(b.peerId, d3) a.subscribe(topic) b.subscribe(topic) @@ -201,12 +201,12 @@ describe('multiple nodes (more than 2)', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectA(b.peerInfo, d0) - onConnectB(a.peerInfo, d1) + onConnectA(b.peerId, d0) + onConnectB(a.peerId, d1) const [d2, d3] = ConnectionPair() - onConnectB(c.peerInfo, d2) - onConnectC(b.peerInfo, d3) + onConnectB(c.peerId, d2) + onConnectC(b.peerId, d3) a.subscribe(topic) b.subscribe(topic) @@ -266,20 +266,20 @@ describe('multiple nodes (more than 2)', () => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectA(b.peerInfo, d0) - onConnectB(a.peerInfo, d1) + onConnectA(b.peerId, d0) + onConnectB(a.peerId, d1) const [d2, d3] = ConnectionPair() - onConnectB(c.peerInfo, d2) - onConnectC(b.peerInfo, d3) + onConnectB(c.peerId, d2) + onConnectC(b.peerId, d3) const [d4, d5] = ConnectionPair() - onConnectC(d.peerInfo, d4) - onConnectD(c.peerInfo, d5) + onConnectC(d.peerId, d4) + onConnectD(c.peerId, d5) const [d6, d7] = ConnectionPair() - onConnectD(e.peerInfo, d6) - onConnectE(d.peerInfo, d7) + onConnectD(e.peerId, d6) + onConnectE(d.peerId, d7) a.subscribe(topic) b.subscribe(topic) diff --git a/test/pubsub.spec.js b/test/pubsub.spec.js index 64a5f662..4b7cb420 100644 --- a/test/pubsub.spec.js +++ b/test/pubsub.spec.js @@ -11,7 +11,7 @@ const pWaitFor = require('p-wait-for') const { utils } = require('libp2p-pubsub') const { createGossipsub, - createPeerInfo, + createPeerId, mockRegistrar } = require('./utils') @@ -127,7 +127,7 @@ describe('Pubsub', () => { it('should disconnect peer on stream error', async () => { sinon.spy(gossipsub, '_onPeerDisconnected') - const peerInfo = await createPeerInfo() + const peerId = await createPeerId() const mockConn = { newStream () { return { @@ -147,9 +147,9 @@ describe('Pubsub', () => { } } - gossipsub._onPeerConnected(peerInfo, mockConn) + gossipsub._onPeerConnected(peerId, mockConn) - await pWaitFor(() => gossipsub._onPeerDisconnected.calledWith(peerInfo), { timeout: 1000 }) + await pWaitFor(() => gossipsub._onPeerDisconnected.calledWith(peerId), { timeout: 1000 }) }) }) }) diff --git a/test/utils/index.js b/test/utils/index.js index 489bdeb5..018ce970 100644 --- a/test/utils/index.js +++ b/test/utils/index.js @@ -6,12 +6,9 @@ const DuplexPair = require('it-pair/duplex') const pTimes = require('p-times') const FloodSub = require('libp2p-floodsub') -const { multicodec: floodsubMulticodec } = require('libp2p-floodsub') const PeerId = require('peer-id') -const PeerInfo = require('peer-info') const GossipSub = require('../../src') -const { GossipSubID } = require('../../src/constants') exports.first = (map) => map.values().next().value @@ -19,19 +16,17 @@ exports.expectSet = (set, subs) => { expect(Array.from(set.values())).to.eql(subs) } -const createPeerInfo = async (protocol = GossipSubID) => { +const createPeerId = async () => { const peerId = await PeerId.create({ bits: 1024 }) - const peerInfo = await PeerInfo.create(peerId) - peerInfo.protocols.add(protocol) - return peerInfo + return peerId } -exports.createPeerInfo = createPeerInfo +exports.createPeerId = createPeerId const createGossipsub = async (registrar, shouldStart = false, options) => { - const peerInfo = await createPeerInfo() - const gs = new GossipSub(peerInfo, registrar, options) + const peerId = await createPeerId() + const gs = new GossipSub(peerId, registrar, options) if (shouldStart) { await gs.start() @@ -68,8 +63,8 @@ const connectGossipsubNodes = (nodes, registrarRecords, multicodec) => { // Notice peers of connection const [d0, d1] = ConnectionPair() - onConnectI(nodes[j].peerInfo, d0) - onConnectJ(nodes[i].peerInfo, d1) + onConnectI(nodes[j].peerId, d0) + onConnectJ(nodes[i].peerId, d1) } } @@ -88,8 +83,8 @@ const createGossipsubConnectedNodes = async (n, multicodec, options) => { exports.createGossipsubConnectedNodes = createGossipsubConnectedNodes const createFloodsubNode = async (registrar, shouldStart = false, options) => { - const peerInfo = await createPeerInfo(floodsubMulticodec) - const fs = new FloodSub(peerInfo, registrar, options) + const peerId = await createPeerId() + const fs = new FloodSub(peerId, registrar, options) if (shouldStart) { await fs.start()