Skip to content

Commit dd934b9

Browse files
vasco-santosjacobheun
authored andcommitted
fix: start kad dht random walk (#251)
* fix: start kad dht random walk * chore: added tests and stop random walk * chore: allows to disable discovery for dht * chore: upgrade kad-dht version
1 parent cef3c8b commit dd934b9

7 files changed

+16
-5
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ class Node extends libp2p {
162162
}
163163
},
164164
dht: {
165-
kBucketSize: 20
165+
kBucketSize: 20,
166+
enabledDiscovery: true // Allows to disable discovery (enabled by default)
166167
},
167168
// Enable/Disable Experimental features
168169
EXPERIMENTAL: { // Experimental features ("behind a flag")

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"dirty-chai": "^2.0.1",
6060
"electron-webrtc": "~0.3.0",
6161
"libp2p-circuit": "~0.2.1",
62-
"libp2p-kad-dht": "~0.10.3",
62+
"libp2p-kad-dht": "~0.10.5",
6363
"libp2p-mdns": "~0.12.0",
6464
"libp2p-mplex": "~0.8.2",
6565
"libp2p-bootstrap": "~0.9.3",

src/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const OptionsSchema = Joi.object({
2929
})
3030
}).default(),
3131
dht: Joi.object().keys({
32-
kBucketSize: Joi.number().allow(null)
32+
kBucketSize: Joi.number().allow(null),
33+
enabledDiscovery: Joi.boolean().default(true)
3334
}),
3435
EXPERIMENTAL: Joi.object().keys({
3536
dht: Joi.boolean().default(false),

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ class Node extends EventEmitter {
8585
// dht provided components (peerRouting, contentRouting, dht)
8686
if (this._config.EXPERIMENTAL.dht) {
8787
const DHT = this._modules.dht
88+
const enabledDiscovery = this._config.dht.enabledDiscovery !== false
89+
8890
this._dht = new DHT(this._switch, {
8991
kBucketSize: this._config.dht.kBucketSize || 20,
92+
enabledDiscovery,
9093
// TODO make datastore an option of libp2p itself so
9194
// that other things can use it as well
9295
datastore: dht.datastore

test/create.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ describe('libp2p creation', () => {
2828
sinon.spy(sw, 'start')
2929
sinon.spy(cm, 'start')
3030
sinon.spy(dht, 'start')
31+
sinon.spy(dht.randomWalk, 'start')
3132
sinon.spy(pub, 'start')
3233
sinon.spy(sw, 'stop')
3334
sinon.spy(cm, 'stop')
3435
sinon.spy(dht, 'stop')
36+
sinon.spy(dht.randomWalk, 'stop')
3537
sinon.spy(pub, 'stop')
3638
sinon.spy(node, 'emit')
3739

@@ -41,6 +43,7 @@ describe('libp2p creation', () => {
4143
expect(sw.start.calledOnce).to.equal(true)
4244
expect(cm.start.calledOnce).to.equal(true)
4345
expect(dht.start.calledOnce).to.equal(true)
46+
expect(dht.randomWalk.start.calledOnce).to.equal(true)
4447
expect(pub.start.calledOnce).to.equal(true)
4548
expect(node.emit.calledWith('start')).to.equal(true)
4649

@@ -53,6 +56,7 @@ describe('libp2p creation', () => {
5356
expect(sw.stop.calledOnce).to.equal(true)
5457
expect(cm.stop.calledOnce).to.equal(true)
5558
expect(dht.stop.calledOnce).to.equal(true)
59+
expect(dht.randomWalk.stop.called).to.equal(true)
5660
expect(pub.stop.calledOnce).to.equal(true)
5761
expect(node.emit.calledWith('stop')).to.equal(true)
5862

test/utils/bundle-browser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class Node extends libp2p {
7979
}
8080
},
8181
dht: {
82-
kBucketSize: 20
82+
kBucketSize: 20,
83+
enabledDiscovery: true
8384
},
8485
EXPERIMENTAL: {
8586
dht: false,

test/utils/bundle-nodejs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class Node extends libp2p {
7272
}
7373
},
7474
dht: {
75-
kBucketSize: 20
75+
kBucketSize: 20,
76+
enabledDiscovery: true
7677
},
7778
EXPERIMENTAL: {
7879
dht: false,

0 commit comments

Comments
 (0)