Skip to content

Commit

Permalink
feat: allow configurable validators and selectors to the dht
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 29, 2018
1 parent 581a1de commit c931c46
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ const OptionsSchema = Joi.object({
})
}).default(),
dht: Joi.object().keys({
kBucketSize: Joi.number().allow(null),
enabledDiscovery: Joi.boolean().default(true)
}),
kBucketSize: Joi.number().default(20),
enabledDiscovery: Joi.boolean().default(true),
validators: Joi.object().allow(null),
selectors: Joi.object().allow(null)
}).default(),
EXPERIMENTAL: Joi.object().keys({
dht: Joi.boolean().default(false),
pubsub: Joi.boolean().default(false)
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ class Node extends EventEmitter {
const enabledDiscovery = this._config.dht.enabledDiscovery !== false

this._dht = new DHT(this._switch, {
kBucketSize: this._config.dht.kBucketSize || 20,
kBucketSize: this._config.dht.kBucketSize,
enabledDiscovery,
datastore: this.datastore
datastore: this.datastore,
validators: this._config.dht.validators || {},
selectors: this._config.dht.selectors || {}
})
}

Expand Down
50 changes: 50 additions & 0 deletions test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const WS = require('libp2p-websockets')
const Bootstrap = require('libp2p-bootstrap')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
const DHT = require('libp2p-kad-dht')

const validateConfig = require('../src/config').validate

Expand Down Expand Up @@ -91,6 +92,10 @@ describe('configuration', () => {
pubsub: false,
dht: false
},
dht: {
kBucketSize: 20,
enabledDiscovery: true
},
relay: {
enabled: true
}
Expand Down Expand Up @@ -143,4 +148,49 @@ describe('configuration', () => {

expect(() => validateConfig(options)).to.throw()
})

it('should add defaults, validators and selectors for dht', () => {
const selectors = {}
const validators = {}

const options = {
peerInfo,
modules: {
transport: [WS],
dht: DHT
},
config: {
EXPERIMENTAL: {
dht: true
},
dht: {
selectors,
validators
}
}
}
const expected = {
peerInfo,
modules: {
transport: [WS],
dht: DHT
},
config: {
EXPERIMENTAL: {
pubsub: false,
dht: true
},
relay: {
enabled: true
},
dht: {
kBucketSize: 20,
enabledDiscovery: true,
selectors,
validators
}
}
}
expect(validateConfig(options)).to.deep.equal(expected)
})
})
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 c931c46

Please sign in to comment.