Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: emit peer discovery for dht discovery #303

Merged
merged 1 commit into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
})
})