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 ab028a2 commit ad1e8ab
Show file tree
Hide file tree
Showing 3 changed files with 72 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 @@ -404,7 +404,10 @@ class Node extends EventEmitter {
// have to set started here because DHT requires libp2p is already started
this._isStarted = true
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
67 changes: 67 additions & 0 deletions test/peer-discovery.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ const expect = chai.expect
const sinon = require('sinon')
const signalling = require('libp2p-webrtc-star/src/sig-server')
const parallel = require('async/parallel')
const retry = require('async/retry')
const series = require('async/series')
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 @@ -284,6 +289,68 @@ describe('peer discovery', () => {
})
})

describe('dht', () => {
setup({
config: {
peerDiscovery: {
mdns: {
enabled: false
},
webRTCStar: {
enabled: false
}
}
}
})

// connect two dhts
function connectNoSync (a, b, callback) {
const publicPeerId = new PeerId(b.peerInfo.id.id, null, b.peerInfo.id.pubKey)
const target = new PeerInfo(publicPeerId)
target.multiaddrs = b.peerInfo.multiaddrs
a.switch.dial(target, callback)
}

function find (a, b, cb) {
retry({ times: 50, interval: 100 }, (cb) => {
a.routingTable.find(b.peerInfo.id, (err, match) => {
if (err) {
return cb(err)
}
if (!match) {
return cb(new Error('not found'))
}

try {
expect(a.peerBook.get(b.peerInfo).multiaddrs.toArray()[0].toString())
.to.eql(b.peerInfo.multiaddrs.toArray()[0].toString())
} catch (err) {
return cb(err)
}

cb()
})
}, cb)
}

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 to dhts
series([
(cb) => connectNoSync(nodeA._dht, nodeB._dht, cb),
(cb) => find(nodeA._dht, nodeB._dht, cb),
(cb) => find(nodeB._dht, nodeA._dht, cb)
])
})
})

describe('MulticastDNS + WebRTCStar', () => {
setup({
config: {
Expand Down

0 comments on commit ad1e8ab

Please sign in to comment.