Skip to content

Commit c931c46

Browse files
committed
feat: allow configurable validators and selectors to the dht
1 parent 581a1de commit c931c46

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

src/config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ const OptionsSchema = Joi.object({
3232
})
3333
}).default(),
3434
dht: Joi.object().keys({
35-
kBucketSize: Joi.number().allow(null),
36-
enabledDiscovery: Joi.boolean().default(true)
37-
}),
35+
kBucketSize: Joi.number().default(20),
36+
enabledDiscovery: Joi.boolean().default(true),
37+
validators: Joi.object().allow(null),
38+
selectors: Joi.object().allow(null)
39+
}).default(),
3840
EXPERIMENTAL: Joi.object().keys({
3941
dht: Joi.boolean().default(false),
4042
pubsub: Joi.boolean().default(false)

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ class Node extends EventEmitter {
102102
const enabledDiscovery = this._config.dht.enabledDiscovery !== false
103103

104104
this._dht = new DHT(this._switch, {
105-
kBucketSize: this._config.dht.kBucketSize || 20,
105+
kBucketSize: this._config.dht.kBucketSize,
106106
enabledDiscovery,
107-
datastore: this.datastore
107+
datastore: this.datastore,
108+
validators: this._config.dht.validators || {},
109+
selectors: this._config.dht.selectors || {}
108110
})
109111
}
110112

test/config.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const WS = require('libp2p-websockets')
1111
const Bootstrap = require('libp2p-bootstrap')
1212
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
1313
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
14+
const DHT = require('libp2p-kad-dht')
1415

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

@@ -91,6 +92,10 @@ describe('configuration', () => {
9192
pubsub: false,
9293
dht: false
9394
},
95+
dht: {
96+
kBucketSize: 20,
97+
enabledDiscovery: true
98+
},
9499
relay: {
95100
enabled: true
96101
}
@@ -143,4 +148,49 @@ describe('configuration', () => {
143148

144149
expect(() => validateConfig(options)).to.throw()
145150
})
151+
152+
it('should add defaults, validators and selectors for dht', () => {
153+
const selectors = {}
154+
const validators = {}
155+
156+
const options = {
157+
peerInfo,
158+
modules: {
159+
transport: [WS],
160+
dht: DHT
161+
},
162+
config: {
163+
EXPERIMENTAL: {
164+
dht: true
165+
},
166+
dht: {
167+
selectors,
168+
validators
169+
}
170+
}
171+
}
172+
const expected = {
173+
peerInfo,
174+
modules: {
175+
transport: [WS],
176+
dht: DHT
177+
},
178+
config: {
179+
EXPERIMENTAL: {
180+
pubsub: false,
181+
dht: true
182+
},
183+
relay: {
184+
enabled: true
185+
},
186+
dht: {
187+
kBucketSize: 20,
188+
enabledDiscovery: true,
189+
selectors,
190+
validators
191+
}
192+
}
193+
}
194+
expect(validateConfig(options)).to.deep.equal(expected)
195+
})
146196
})

test/transports.node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ describe('transports', () => {
427427
cb()
428428
}),
429429
(cb) => {
430-
const wstar = new WRTCStar({wrtc: wrtc})
430+
const wstar = new WRTCStar({ wrtc: wrtc })
431431

432432
createNode([
433433
'/ip4/0.0.0.0/tcp/0',
@@ -474,7 +474,7 @@ describe('transports', () => {
474474
}),
475475

476476
(cb) => {
477-
const wstar = new WRTCStar({wrtc: wrtc})
477+
const wstar = new WRTCStar({ wrtc: wrtc })
478478

479479
createNode([
480480
'/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star'

0 commit comments

Comments
 (0)