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 23, 2018
1 parent 581a1de commit 3e9db30
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 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

0 comments on commit 3e9db30

Please sign in to comment.