Skip to content

Commit

Permalink
fix: validate dht config
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 24, 2018
1 parent 581a1de commit ff5316d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"libp2p-ping": "~0.8.3",
"libp2p-switch": "~0.41.2",
"libp2p-websockets": "~0.12.0",
"lodash.get": "^4.4.2",
"mafmt": "^6.0.2",
"multiaddr": "^5.0.2",
"peer-book": "~0.8.0",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ log.error = debug('libp2p:error')
const each = require('async/each')
const series = require('async/series')
const parallel = require('async/parallel')
const get = require('lodash.get')

const PeerBook = require('peer-book')
const Switch = require('libp2p-switch')
Expand Down Expand Up @@ -99,10 +100,10 @@ class Node extends EventEmitter {
// dht provided components (peerRouting, contentRouting, dht)
if (this._config.EXPERIMENTAL.dht) {
const DHT = this._modules.dht
const enabledDiscovery = this._config.dht.enabledDiscovery !== false
const enabledDiscovery = Boolean(get(this._config, 'dht.enabledDiscovery', true))

this._dht = new DHT(this._switch, {
kBucketSize: this._config.dht.kBucketSize || 20,
kBucketSize: get(this._config, 'dht.kBucketSize', 20),
enabledDiscovery,
datastore: this.datastore
})
Expand Down
15 changes: 15 additions & 0 deletions test/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ describe('libp2p creation', () => {
})
})

it('should create using dht defaults', (done) => {
createNode([], {
config: {
EXPERIMENTAL: {
dht: true
},
dht: {}
}
}, (err, node) => {
expect(err).to.not.exist()
expect(node._dht).to.exist()
done()
})
})

it('should not throw errors from switch if node has no error listeners', (done) => {
createNode([], {}, (err, node) => {
expect(err).to.not.exist()
Expand Down
4 changes: 2 additions & 2 deletions test/transports.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ describe('transports', () => {
cb()
}),
(cb) => {
const wstar = new WRTCStar({wrtc: wrtc})
const wstar = new WRTCStar({ wrtc: wrtc })

createNode([
'/ip4/0.0.0.0/tcp/0',
Expand Down Expand Up @@ -474,7 +474,7 @@ describe('transports', () => {
}),

(cb) => {
const wstar = new WRTCStar({wrtc: wrtc})
const wstar = new WRTCStar({ wrtc: wrtc })

createNode([
'/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star'
Expand Down

0 comments on commit ff5316d

Please sign in to comment.