Skip to content

Commit

Permalink
fix: emit peer discovery for dht discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Feb 5, 2019
1 parent 558e598 commit 1f0f34c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
45 changes: 45 additions & 0 deletions test/peer-discovery.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -241,6 +244,9 @@ describe('peer discovery', () => {
describe('MulticastDNS', () => {
setup({
config: {
dht: {
enabled: false
},
peerDiscovery: {
mdns: {
enabled: true,
Expand All @@ -266,6 +272,9 @@ describe('peer discovery', () => {
describe.skip('WebRTCStar', () => {
setup({
config: {
dht: {
enabled: false
},
peerDiscovery: {
webRTCStar: {
enabled: true
Expand All @@ -287,6 +296,9 @@ describe('peer discovery', () => {
describe('MulticastDNS + WebRTCStar', () => {
setup({
config: {
dht: {
enabled: false
},
peerDiscovery: {
mdns: {
enabled: true,
Expand All @@ -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()
})
})
})
})

0 comments on commit 1f0f34c

Please sign in to comment.