From 1f0f34cdb9d81a132e3f8d8f2d5e624b062e6d64 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 28 Dec 2018 10:54:56 +0000 Subject: [PATCH] fix: emit peer discovery for dht discovery --- package.json | 2 +- src/index.js | 5 ++++- test/peer-discovery.node.js | 45 +++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6e86d93ce7..d273d0c419 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "libp2p-circuit": "~0.3.0", "libp2p-delegated-content-routing": "~0.2.2", "libp2p-delegated-peer-routing": "~0.2.2", - "libp2p-kad-dht": "~0.14.2", + "libp2p-kad-dht": "~0.14.5", "libp2p-mdns": "~0.12.0", "libp2p-mplex": "~0.8.4", "libp2p-secio": "~0.11.0", diff --git a/src/index.js b/src/index.js index b7e4bb0599..8222e42447 100644 --- a/src/index.js +++ b/src/index.js @@ -400,7 +400,10 @@ class Node extends EventEmitter { }, (cb) => { if (this._dht) { - this._dht.start(cb) + this._dht.start(() => { + this._dht.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo)) + cb() + }) } else { cb() } diff --git a/test/peer-discovery.node.js b/test/peer-discovery.node.js index 891e6dfec0..b1aaa53e0e 100644 --- a/test/peer-discovery.node.js +++ b/test/peer-discovery.node.js @@ -9,6 +9,9 @@ const signalling = require('libp2p-webrtc-star/src/sig-server') const parallel = require('async/parallel') const crypto = require('crypto') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') + const createNode = require('./utils/create-node') const echo = require('./utils/echo') @@ -241,6 +244,9 @@ describe('peer discovery', () => { describe('MulticastDNS', () => { setup({ config: { + dht: { + enabled: false + }, peerDiscovery: { mdns: { enabled: true, @@ -266,6 +272,9 @@ describe('peer discovery', () => { describe.skip('WebRTCStar', () => { setup({ config: { + dht: { + enabled: false + }, peerDiscovery: { webRTCStar: { enabled: true @@ -287,6 +296,9 @@ describe('peer discovery', () => { describe('MulticastDNS + WebRTCStar', () => { setup({ config: { + dht: { + enabled: false + }, peerDiscovery: { mdns: { enabled: true, @@ -309,4 +321,37 @@ describe('peer discovery', () => { }) }) }) + + describe('dht', () => { + setup({ + config: { + peerDiscovery: { + mdns: { + enabled: false + }, + webRTCStar: { + enabled: false + } + } + } + }) + + it('find a peer', function (done) { + this.timeout(15 * 1000) + + nodeA.once('peer:discovery', (peerInfo) => { + expect(nodeB.peerInfo.id.toB58String()) + .to.eql(peerInfo.id.toB58String()) + done() + }) + + // connect two dhts + const publicPeerId = new PeerId(nodeB._dht.peerInfo.id.id, null, nodeB._dht.peerInfo.id.pubKey) + const target = new PeerInfo(publicPeerId) + target.multiaddrs = nodeB._dht.peerInfo.multiaddrs + nodeA._dht.switch.dial(target, (err) => { + expect(err).to.not.exist() + }) + }) + }) })