From 4d12931aa7d8421d23cce812c934c9b9056740e2 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Mon, 6 Apr 2020 17:04:25 +0200 Subject: [PATCH] chore: peer-discovery not using peer-info BREAKING CHANGE: peer event emitted with id and multiaddrs properties instead of peer-info --- package.json | 3 +- src/compat/index.js | 9 +-- src/compat/querier.js | 18 ++---- src/compat/responder.js | 13 ++-- src/index.js | 16 +++-- src/query.js | 41 ++++++------ test/compat/go-multicast-dns.spec.js | 37 ++++++----- test/compat/querier.spec.js | 70 ++++++++++---------- test/compat/responder.spec.js | 42 ++++++------ test/compliance.spec.js | 2 +- test/multicast-dns.spec.js | 95 ++++++++++++++++++---------- 11 files changed, 183 insertions(+), 163 deletions(-) diff --git a/package.json b/package.json index f5b3b81..da5ea15 100644 --- a/package.json +++ b/package.json @@ -45,8 +45,7 @@ "debug": "^4.1.1", "multiaddr": "^7.1.0", "multicast-dns": "^7.2.0", - "peer-id": "~0.13.3", - "peer-info": "~0.17.0" + "peer-id": "~0.13.3" }, "contributors": [ "David Dias ", diff --git a/src/compat/index.js b/src/compat/index.js index c170acc..4dd3596 100644 --- a/src/compat/index.js +++ b/src/compat/index.js @@ -7,10 +7,11 @@ const Responder = require('./responder') const Querier = require('./querier') class GoMulticastDNS extends EE { - constructor (peerInfo) { + constructor (peerId, multiaddrs) { super() this._started = false - this._peerInfo = peerInfo + this._peerId = peerId + this._multiaddrs = multiaddrs this._onPeer = this._onPeer.bind(this) } @@ -20,8 +21,8 @@ class GoMulticastDNS extends EE { } this._started = true - this._responder = new Responder(this._peerInfo) - this._querier = new Querier(this._peerInfo.id) + this._responder = new Responder(this._peerId, this._multiaddrs) + this._querier = new Querier(this._peerId) this._querier.on('peer', this._onPeer) diff --git a/src/compat/querier.js b/src/compat/querier.js index eaa63de..45f7be6 100644 --- a/src/compat/querier.js +++ b/src/compat/querier.js @@ -3,7 +3,6 @@ const EE = require('events') const MDNS = require('multicast-dns') const Multiaddr = require('multiaddr') -const PeerInfo = require('peer-info') const PeerId = require('peer-id') const debug = require('debug') const log = debug('libp2p:mdns:compat:querier') @@ -13,9 +12,11 @@ const { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } = require('./constants class Querier extends EE { constructor (peerId, options) { super() + if (!peerId) { throw new Error('missing peerId parameter') } + options = options || {} this._peerIdStr = peerId.toB58String() // Re-query every 60s, in leu of network change detection @@ -57,7 +58,7 @@ class Querier extends EE { }) } - async _onResponse (event, info) { + _onResponse (event, info) { const answers = event.answers || [] const ptrRecord = answers.find(a => a.type === 'PTR' && a.name === SERVICE_TAG_LOCAL) @@ -87,13 +88,6 @@ class Querier extends EE { return log('failed to create peer ID from TXT record data', peerIdStr, err) } - let peerInfo - try { - peerInfo = await PeerInfo.create(peerId) - } catch (err) { - return log.error('failed to create peer info from peer ID', peerId, err) - } - const srvRecord = answers.find(a => a.type === 'SRV') if (!srvRecord) return log('missing SRV record in response') @@ -115,8 +109,10 @@ class Querier extends EE { return addrs }, []) - multiaddrs.forEach(addr => peerInfo.multiaddrs.add(addr)) - this.emit('peer', peerInfo) + this.emit('peer', { + id: peerId, + multiaddrs + }) } stop () { diff --git a/src/compat/responder.js b/src/compat/responder.js index 2349fb5..2c9da21 100644 --- a/src/compat/responder.js +++ b/src/compat/responder.js @@ -6,13 +6,14 @@ const log = require('debug')('libp2p:mdns:compat:responder') const { SERVICE_TAG_LOCAL } = require('./constants') class Responder { - constructor (peerInfo) { - if (!peerInfo) { - throw new Error('missing peerInfo parameter') + constructor (peerId, multiaddrs) { + if (!peerId) { + throw new Error('missing peerId parameter') } - this._peerInfo = peerInfo - this._peerIdStr = peerInfo.id.toB58String() + this._peerId = peerId + this._peerIdStr = peerId.toB58String() + this._multiaddrs = multiaddrs this._onQuery = this._onQuery.bind(this) } @@ -22,7 +23,7 @@ class Responder { } _onQuery (event, info) { - const addresses = this._peerInfo.multiaddrs.toArray().map(ma => ma.toOptions()) + const addresses = this._multiaddrs.map(ma => ma.toOptions()) // Only announce TCP for now if (!addresses.length) return diff --git a/src/index.js b/src/index.js index b82dc65..87952f4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ 'use strict' const multicastDNS = require('multicast-dns') -const EventEmitter = require('events').EventEmitter +const { EventEmitter } = require('events') const debug = require('debug') const log = debug('libp2p:mdns') const query = require('./query') @@ -10,20 +10,22 @@ const GoMulticastDNS = require('./compat') class MulticastDNS extends EventEmitter { constructor (options = {}) { super() - if (!options.peerInfo) { - throw new Error('needs a PeerInfo to work') + + if (!options.peerId) { + throw new Error('needs own PeerId to work') } this.broadcast = options.broadcast !== false this.interval = options.interval || (1e3 * 10) this.serviceTag = options.serviceTag || 'ipfs.local' this.port = options.port || 5353 - this.peerInfo = options.peerInfo + this.peerId = options.peerId + this.peerMultiaddrs = options.multiaddrs || [] this._queryInterval = null this._onPeer = this._onPeer.bind(this) if (options.compat !== false) { - this._goMdns = new GoMulticastDNS(options.peerInfo, { + this._goMdns = new GoMulticastDNS(options.peerId, this.peerMultiaddrs, { queryPeriod: options.compatQueryPeriod, queryInterval: options.compatQueryInterval }) @@ -51,12 +53,12 @@ class MulticastDNS extends EventEmitter { } _onMdnsQuery (event) { - query.gotQuery(event, this.mdns, this.peerInfo, this.serviceTag, this.broadcast) + query.gotQuery(event, this.mdns, this.peerId, this.peerMultiaddrs, this.serviceTag, this.broadcast) } async _onMdnsResponse (event) { try { - const foundPeer = await query.gotResponse(event, this.peerInfo, this.serviceTag) + const foundPeer = await query.gotResponse(event, this.peerId, this.serviceTag) if (foundPeer) { this.emit('peer', foundPeer) diff --git a/src/query.js b/src/query.js index 8f7489a..49f39f7 100644 --- a/src/query.js +++ b/src/query.js @@ -1,6 +1,5 @@ 'use strict' -const Peer = require('peer-info') const os = require('os') const debug = require('debug') const log = debug('libp2p:mdns') @@ -26,7 +25,7 @@ module.exports = { return setInterval(query, interval) }, - gotResponse: async function (rsp, peerInfo, serviceTag) { + gotResponse: function (rsp, localPeerId, serviceTag) { if (!rsp.answers) { return } const answers = { @@ -57,33 +56,37 @@ module.exports = { const multiaddrs = [] answers.a.forEach((a) => { - multiaddrs.push(new Multiaddr('/ip4/' + a.data + '/tcp/' + port)) + const ma = new Multiaddr('/ip4/' + a.data + '/tcp/' + port) + + if (!multiaddrs.some((m) => m.equals(ma))) { + multiaddrs.push(ma) + } }) + answers.aaaa.forEach((a) => { - multiaddrs.push(new Multiaddr('/ip6/' + a.data + '/tcp/' + port)) + const ma = new Multiaddr('/ip6/' + a.data + '/tcp/' + port) + + if (!multiaddrs.some((m) => m.equals(ma))) { + multiaddrs.push(ma) + } }) - if (peerInfo.id.toB58String() === b58Id) { + if (localPeerId.toB58String() === b58Id) { return // replied to myself, ignore } log('peer found -', b58Id) - const peerId = Id.createFromB58String(b58Id) - - try { - const peerFound = await Peer.create(peerId) - multiaddrs.forEach((addr) => peerFound.multiaddrs.add(addr)) - return peerFound - } catch (err) { - log.error('Error creating PeerInfo from new found peer', err) + return { + id: Id.createFromB58String(b58Id), + multiaddrs } }, - gotQuery: function (qry, mdns, peerInfo, serviceTag, broadcast) { + gotQuery: function (qry, mdns, peerId, multiaddrs, serviceTag, broadcast) { if (!broadcast) { return } - const addresses = peerInfo.multiaddrs.toArray().map(ma => ma.toOptions()) + const addresses = multiaddrs.map(ma => ma.toOptions()) // Only announce TCP for now if (addresses.length === 0) { return } @@ -95,14 +98,14 @@ module.exports = { type: 'PTR', class: 'IN', ttl: 120, - data: peerInfo.id.toB58String() + '.' + serviceTag + data: peerId.toB58String() + '.' + serviceTag }) // Only announce TCP multiaddrs for now const port = addresses[0].port answers.push({ - name: peerInfo.id.toB58String() + '.' + serviceTag, + name: peerId.toB58String() + '.' + serviceTag, type: 'SRV', class: 'IN', ttl: 120, @@ -115,11 +118,11 @@ module.exports = { }) answers.push({ - name: peerInfo.id.toB58String() + '.' + serviceTag, + name: peerId.toB58String() + '.' + serviceTag, type: 'TXT', class: 'IN', ttl: 120, - data: peerInfo.id.toB58String() + data: peerId.toB58String() }) addresses.forEach((addr) => { diff --git a/test/compat/go-multicast-dns.spec.js b/test/compat/go-multicast-dns.spec.js index b1d8cea..50f48b8 100644 --- a/test/compat/go-multicast-dns.spec.js +++ b/test/compat/go-multicast-dns.spec.js @@ -5,38 +5,36 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const PeerInfo = require('peer-info') + +const multiaddr = require('multiaddr') +const PeerId = require('peer-id') const pDefer = require('p-defer') const GoMulticastDNS = require('../../src/compat') describe('GoMulticastDNS', () => { const peerAddrs = [ - '/ip4/127.0.0.1/tcp/20001', - '/ip4/127.0.0.1/tcp/20002' + multiaddr('/ip4/127.0.0.1/tcp/20001'), + multiaddr('/ip4/127.0.0.1/tcp/20002') ] - let peerInfos + let peerIds before(async () => { - peerInfos = await Promise.all([ - PeerInfo.create(), - PeerInfo.create() + peerIds = await Promise.all([ + PeerId.create(), + PeerId.create() ]) - - peerInfos.forEach((peer, index) => { - peer.multiaddrs.add(peerAddrs[index]) - }) }) it('should start and stop', async () => { - const mdns = new GoMulticastDNS(peerInfos[0]) + const mdns = new GoMulticastDNS(peerIds[0], [peerAddrs[0]]) await mdns.start() return mdns.stop() }) it('should ignore multiple start calls', async () => { - const mdns = new GoMulticastDNS(peerInfos[0]) + const mdns = new GoMulticastDNS(peerIds[0], [peerAddrs[0]]) await mdns.start() await mdns.start() @@ -45,18 +43,19 @@ describe('GoMulticastDNS', () => { }) it('should ignore unnecessary stop calls', async () => { - const mdns = new GoMulticastDNS(peerInfos[0]) + const mdns = new GoMulticastDNS(peerIds[0], [peerAddrs[0]]) await mdns.stop() }) it('should emit peer info when peer is discovered', async () => { - const mdnsA = new GoMulticastDNS(peerInfos[0]) - const mdnsB = new GoMulticastDNS(peerInfos[1]) + const mdnsA = new GoMulticastDNS(peerIds[0], [peerAddrs[0]]) + const mdnsB = new GoMulticastDNS(peerIds[1], [peerAddrs[1]]) const defer = pDefer() - mdnsA.on('peer', info => { - if (!info.id.isEqual(peerInfos[1].id)) return - expect(info.multiaddrs.has(peerAddrs[1])).to.be.true() + mdnsA.on('peer', ({ id, multiaddrs }) => { + if (!id.isEqual(peerIds[1])) return + + expect(multiaddrs.some((m) => m.equals(peerAddrs[1]))).to.be.true() defer.resolve() }) diff --git a/test/compat/querier.spec.js b/test/compat/querier.spec.js index b167c09..ab68bb4 100644 --- a/test/compat/querier.spec.js +++ b/test/compat/querier.spec.js @@ -5,7 +5,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const PeerInfo = require('peer-info') +const PeerId = require('peer-id') const MDNS = require('multicast-dns') const OS = require('os') const delay = require('delay') @@ -19,17 +19,13 @@ describe('Querier', () => { '/ip4/127.0.0.1/tcp/20001', '/ip4/127.0.0.1/tcp/20002' ] - let peerInfos + let peerIds before(async () => { - peerInfos = await Promise.all([ - PeerInfo.create(), - PeerInfo.create() + peerIds = await Promise.all([ + PeerId.create(), + PeerId.create() ]) - - peerInfos.forEach((peer, index) => { - peer.multiaddrs.add(peerAddrs[index]) - }) }) afterEach(() => { @@ -40,14 +36,14 @@ describe('Querier', () => { }) it('should start and stop', async () => { - const querier = new Querier(peerInfos[0].id) + const querier = new Querier(peerIds[0]) await querier.start() await querier.stop() }) it('should query on interval', async () => { - querier = new Querier(peerInfos[0].id, { queryPeriod: 0, queryInterval: 10 }) + querier = new Querier(peerIds[0], { queryPeriod: 0, queryInterval: 10 }) mdns = MDNS() let queryCount = 0 @@ -66,7 +62,7 @@ describe('Querier', () => { it('should not emit peer for responses with non matching service tags', () => { return ensureNoPeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` const bogusServiceTagLocal = '_ifps-discovery._udp' return [{ @@ -81,7 +77,7 @@ describe('Querier', () => { it('should not emit peer for responses with missing TXT record', () => { return ensureNoPeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` return [{ name: SERVICE_TAG_LOCAL, @@ -95,7 +91,7 @@ describe('Querier', () => { it('should not emit peer for responses with missing peer ID in TXT record', () => { return ensureNoPeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` return [{ name: SERVICE_TAG_LOCAL, @@ -115,7 +111,7 @@ describe('Querier', () => { it('should not emit peer for responses to self', () => { return ensureNoPeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` return [{ name: SERVICE_TAG_LOCAL, @@ -128,7 +124,7 @@ describe('Querier', () => { type: 'TXT', class: 'IN', ttl: 120, - data: peerInfos[0].id.toB58String() + data: peerIds[0].toB58String() }] }) }) @@ -136,7 +132,7 @@ describe('Querier', () => { // TODO: unskip when https://github.com/libp2p/js-peer-id/issues/83 is resolved it.skip('should not emit peer for responses with invalid peer ID in TXT record', () => { return ensureNoPeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` return [{ name: SERVICE_TAG_LOCAL, @@ -156,7 +152,7 @@ describe('Querier', () => { it('should not emit peer for responses with missing SRV record', () => { return ensureNoPeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` return [{ name: SERVICE_TAG_LOCAL, @@ -169,14 +165,14 @@ describe('Querier', () => { type: 'TXT', class: 'IN', ttl: 120, - data: peerInfos[1].id.toB58String() + data: peerIds[1].toB58String() }] }) }) it('should emit peer for responses even if no multiaddrs', () => { return ensurePeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` return [{ name: SERVICE_TAG_LOCAL, @@ -189,7 +185,7 @@ describe('Querier', () => { type: 'TXT', class: 'IN', ttl: 120, - data: peerInfos[1].id.toB58String() + data: peerIds[1].toB58String() }, { name: peerServiceTagLocal, type: 'SRV', @@ -207,7 +203,7 @@ describe('Querier', () => { it('should emit peer for responses with valid multiaddrs', () => { return ensurePeer(event => { - const peerServiceTagLocal = `${peerInfos[1].id.toB58String()}.${SERVICE_TAG_LOCAL}` + const peerServiceTagLocal = `${peerIds[1].toB58String()}.${SERVICE_TAG_LOCAL}` return [{ name: SERVICE_TAG_LOCAL, @@ -220,7 +216,7 @@ describe('Querier', () => { type: 'TXT', class: 'IN', ttl: 120, - data: peerInfos[1].id.toB58String() + data: peerIds[1].toB58String() }, { name: peerServiceTagLocal, type: 'SRV', @@ -243,11 +239,11 @@ describe('Querier', () => { }) /** - * Ensure peerInfos[1] are emitted from `querier` + * Ensure peerIds[1] are emitted from `querier` * @param {Function} getResponse Given a query, construct a response to test the querier */ async function ensurePeer (getResponse) { - querier = new Querier(peerInfos[0].id) + querier = new Querier(peerIds[0]) mdns = MDNS() mdns.on('query', (event, info) => { @@ -256,17 +252,17 @@ describe('Querier', () => { mdns.respond(getResponse(event, info), info) }) - let peerInfo + let peerId - querier.on('peer', info => { + querier.on('peer', ({ id }) => { // Ignore non-test peers - if (!info.id.isEqual(peerInfos[1].id)) return - peerInfo = info + if (!id.isEqual(peerIds[1])) return + peerId = id }) await querier.start() await delay(100) - if (!peerInfo) throw new Error('Missing peer') + if (!peerId) throw new Error('Missing peer') } /** @@ -274,7 +270,7 @@ describe('Querier', () => { * @param {Function} getResponse Given a query, construct a response to test the querier */ async function ensureNoPeer (getResponse) { - querier = new Querier(peerInfos[0].id) + querier = new Querier(peerIds[0]) mdns = MDNS() mdns.on('query', (event, info) => { @@ -283,18 +279,18 @@ describe('Querier', () => { mdns.respond(getResponse(event, info), info) }) - let peerInfo + let peerId - querier.on('peer', info => { + querier.on('peer', ({ id }) => { // Ignore non-test peers - if (!info.id.isEqual(peerInfos[0].id) && !info.id.isEqual(peerInfos[1].id)) return - peerInfo = info + if (!id.isEqual(peerIds[0]) && !id.isEqual(peerIds[1])) return + peerId = id }) await querier.start() await delay(100) - if (!peerInfo) return - throw Object.assign(new Error('Unexpected peer'), { peerInfo }) + if (!peerId) return + throw Object.assign(new Error('Unexpected peer'), { peerId }) } }) diff --git a/test/compat/responder.spec.js b/test/compat/responder.spec.js index ad03d10..98e76fc 100644 --- a/test/compat/responder.spec.js +++ b/test/compat/responder.spec.js @@ -5,7 +5,9 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const PeerInfo = require('peer-info') + +const multiaddr = require('multiaddr') +const PeerId = require('peer-id') const MDNS = require('multicast-dns') const delay = require('delay') const pDefer = require('p-defer') @@ -16,20 +18,16 @@ const { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } = require('../../src/c describe('Responder', () => { let responder, mdns const peerAddrs = [ - '/ip4/127.0.0.1/tcp/20001', - '/ip4/127.0.0.1/tcp/20002' + multiaddr('/ip4/127.0.0.1/tcp/20001'), + multiaddr('/ip4/127.0.0.1/tcp/20002') ] - let peerInfos + let peerIds before(async () => { - peerInfos = await Promise.all([ - PeerInfo.create(), - PeerInfo.create() + peerIds = await Promise.all([ + PeerId.create(), + PeerId.create() ]) - - peerInfos.forEach((peer, index) => { - peer.multiaddrs.add(peerAddrs[index]) - }) }) afterEach(() => { @@ -40,15 +38,15 @@ describe('Responder', () => { }) it('should start and stop', async () => { - const responder = new Responder(peerInfos[0]) + const responder = new Responder(peerIds[0], [peerAddrs[0]]) await responder.start() await responder.stop() }) it('should not respond to a query if no TCP addresses', async () => { - const peerInfo = await PeerInfo.create() - responder = new Responder(peerInfo) + const peerId = await PeerId.create() + responder = new Responder(peerId, []) mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 }) await responder.start() @@ -56,7 +54,7 @@ describe('Responder', () => { let response mdns.on('response', event => { - if (isResponseFrom(event, peerInfo)) { + if (isResponseFrom(event, peerId)) { response = event } }) @@ -74,7 +72,7 @@ describe('Responder', () => { }) it('should not respond to a query with non matching service tag', async () => { - responder = new Responder(peerInfos[0]) + responder = new Responder(peerIds[0], [peerAddrs[0]]) mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 }) await responder.start() @@ -82,7 +80,7 @@ describe('Responder', () => { let response mdns.on('response', event => { - if (isResponseFrom(event, peerInfos[0])) { + if (isResponseFrom(event, peerIds[0])) { response = event } }) @@ -102,14 +100,14 @@ describe('Responder', () => { }) it('should respond correctly', async () => { - responder = new Responder(peerInfos[0]) + responder = new Responder(peerIds[0], [peerAddrs[0]]) mdns = MDNS({ multicast: false, interface: '0.0.0.0', port: 0 }) await responder.start() const defer = pDefer() mdns.on('response', event => { - if (!isResponseFrom(event, peerInfos[0])) return + if (!isResponseFrom(event, peerIds[0])) return const srvRecord = event.answers.find(a => a.type === 'SRV') if (!srvRecord) return defer.reject(new Error('Missing SRV record')) @@ -121,7 +119,7 @@ describe('Responder', () => { .filter(a => ['A', 'AAAA'].includes(a.type)) .map(a => `/${protos[a.type]}/${a.data}/tcp/${port}`) - if (!addrs.includes(peerAddrs[0])) { + if (!addrs.includes(peerAddrs[0].toString())) { return defer.reject(new Error('Missing peer address in response: ' + peerAddrs[0])) } @@ -140,7 +138,7 @@ describe('Responder', () => { }) }) -function isResponseFrom (res, fromPeerInfo) { +function isResponseFrom (res, fromPeerId) { const answers = res.answers || [] const ptrRecord = answers.find(a => a.type === 'PTR' && a.name === SERVICE_TAG_LOCAL) if (!ptrRecord) return false // Ignore irrelevant @@ -156,7 +154,7 @@ function isResponseFrom (res, fromPeerInfo) { } // Ignore response from someone else - if (fromPeerInfo.id.toB58String() !== peerIdStr) return false + if (fromPeerId.toB58String() !== peerIdStr) return false return true } diff --git a/test/compliance.spec.js b/test/compliance.spec.js index fc93036..1a54d84 100644 --- a/test/compliance.spec.js +++ b/test/compliance.spec.js @@ -9,7 +9,7 @@ const common = { async setup () { const peerInfo = await PeerInfo.create() mdns = new MulticastDNS({ - peerInfo, + peerId: peerInfo.id, broadcast: false, port: 50001, compat: true diff --git a/test/multicast-dns.spec.js b/test/multicast-dns.spec.js index f62e69d..563ee02 100644 --- a/test/multicast-dns.spec.js +++ b/test/multicast-dns.spec.js @@ -6,49 +6,59 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const multiaddr = require('multiaddr') -const PeerInfo = require('peer-info') +const PeerId = require('peer-id') const MulticastDNS = require('./../src') describe('MulticastDNS', () => { - let pA - let pB - let pC - let pD + let pA, aMultiaddrs + let pB, bMultiaddrs + let pC, cMultiaddrs + let pD, dMultiaddrs before(async function () { this.timeout(80 * 1000) ;[pA, pB, pC, pD] = await Promise.all([ - PeerInfo.create(), - PeerInfo.create(), - PeerInfo.create(), - PeerInfo.create() + PeerId.create(), + PeerId.create(), + PeerId.create(), + PeerId.create() ]) - pA.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/20001')) + aMultiaddrs = [ + multiaddr('/ip4/127.0.0.1/tcp/20001') + ] - pB.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/20002')) - pB.multiaddrs.add(multiaddr('/ip6/::1/tcp/20002')) + bMultiaddrs = [ + multiaddr('/ip4/127.0.0.1/tcp/20002'), + multiaddr('/ip6/::1/tcp/20002') + ] - pC.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/20003')) - pC.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/30003/ws')) + cMultiaddrs = [ + multiaddr('/ip4/127.0.0.1/tcp/20003'), + multiaddr('/ip4/127.0.0.1/tcp/30003/ws') + ] - pD.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/30003/ws')) + dMultiaddrs = [ + multiaddr('/ip4/127.0.0.1/tcp/30003/ws') + ] }) it('find another peer', async function () { this.timeout(40 * 1000) const mdnsA = new MulticastDNS({ - peerInfo: pA, + peerId: pA, + multiaddrs: aMultiaddrs, broadcast: false, // do not talk to ourself port: 50001, compat: false }) const mdnsB = new MulticastDNS({ - peerInfo: pB, + peerId: pB, + multiaddrs: bMultiaddrs, port: 50001, // port must be the same compat: false }) @@ -56,9 +66,9 @@ describe('MulticastDNS', () => { mdnsA.start() mdnsB.start() - const peerInfo = await new Promise((resolve) => mdnsA.once('peer', resolve)) + const { id } = await new Promise((resolve) => mdnsA.once('peer', resolve)) - expect(pB.id.toB58String()).to.eql(peerInfo.id.toB58String()) + expect(pB.toB58String()).to.eql(id.toB58String()) await Promise.all([mdnsA.stop(), mdnsB.stop()]) }) @@ -67,18 +77,21 @@ describe('MulticastDNS', () => { this.timeout(40 * 1000) const mdnsA = new MulticastDNS({ - peerInfo: pA, + peerId: pA, + multiaddrs: aMultiaddrs, broadcast: false, // do not talk to ourself port: 50003, compat: false }) const mdnsC = new MulticastDNS({ - peerInfo: pC, + peerId: pC, + multiaddrs: cMultiaddrs, port: 50003, // port must be the same compat: false }) const mdnsD = new MulticastDNS({ - peerInfo: pD, + peerId: pD, + multiaddrs: dMultiaddrs, port: 50003, // port must be the same compat: false }) @@ -87,10 +100,10 @@ describe('MulticastDNS', () => { mdnsC.start() mdnsD.start() - const peerInfo = await new Promise((resolve) => mdnsA.once('peer', resolve)) + const { id, multiaddrs } = await new Promise((resolve) => mdnsA.once('peer', resolve)) - expect(pC.id.toB58String()).to.eql(peerInfo.id.toB58String()) - expect(peerInfo.multiaddrs.size).to.equal(1) + expect(pC.toB58String()).to.eql(id.toB58String()) + expect(multiaddrs.length).to.equal(1) await Promise.all([ mdnsA.stop(), @@ -103,14 +116,16 @@ describe('MulticastDNS', () => { this.timeout(40 * 1000) const mdnsA = new MulticastDNS({ - peerInfo: pA, + peerId: pA, + multiaddrs: aMultiaddrs, broadcast: false, // do not talk to ourself port: 50001, compat: false }) const mdnsB = new MulticastDNS({ - peerInfo: pB, + peerId: pB, + multiaddrs: bMultiaddrs, port: 50001, compat: false }) @@ -118,10 +133,10 @@ describe('MulticastDNS', () => { mdnsA.start() mdnsB.start() - const peerInfo = await new Promise((resolve) => mdnsA.once('peer', resolve)) + const { id, multiaddrs } = await new Promise((resolve) => mdnsA.once('peer', resolve)) - expect(pB.id.toB58String()).to.eql(peerInfo.id.toB58String()) - expect(peerInfo.multiaddrs.size).to.equal(2) + expect(pB.toB58String()).to.eql(id.toB58String()) + expect(multiaddrs.length).to.equal(2) await Promise.all([mdnsA.stop(), mdnsB.stop()]) }) @@ -130,13 +145,15 @@ describe('MulticastDNS', () => { this.timeout(40 * 1000) const mdnsA = new MulticastDNS({ - peerInfo: pA, + peerId: pA, + multiaddrs: aMultiaddrs, port: 50004, // port must be the same compat: false }) const mdnsC = new MulticastDNS({ - peerInfo: pC, + peerId: pC, + multiaddrs: cMultiaddrs, port: 50004, compat: false }) @@ -146,7 +163,7 @@ describe('MulticastDNS', () => { await mdnsA.stop() mdnsC.start() - mdnsC.once('peer', (peerInfo) => { + mdnsC.once('peer', () => { throw new Error('Should not receive new peer.') }) @@ -155,14 +172,22 @@ describe('MulticastDNS', () => { }) it('should start and stop with go-libp2p-mdns compat', async () => { - const mdns = new MulticastDNS({ peerInfo: pA, port: 50004 }) + const mdns = new MulticastDNS({ + peerId: pA, + multiaddrs: aMultiaddrs, + port: 50004 + }) await mdns.start() await mdns.stop() }) it('should not emit undefined peer ids', async () => { - const mdns = new MulticastDNS({ peerInfo: pA, port: 50004 }) + const mdns = new MulticastDNS({ + peerId: pA, + multiaddrs: aMultiaddrs, + port: 50004 + }) await mdns.start() return new Promise((resolve, reject) => {