From dd934b9690ae8f1498bb9aa586863e8d6f092a62 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 4 Oct 2018 13:40:32 +0100 Subject: [PATCH] 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 --- README.md | 3 ++- package.json | 2 +- src/config.js | 3 ++- src/index.js | 3 +++ test/create.spec.js | 4 ++++ test/utils/bundle-browser.js | 3 ++- test/utils/bundle-nodejs.js | 3 ++- 7 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ddb0dfa9..7924fb9a 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,8 @@ class Node extends libp2p { } }, dht: { - kBucketSize: 20 + kBucketSize: 20, + enabledDiscovery: true // Allows to disable discovery (enabled by default) }, // Enable/Disable Experimental features EXPERIMENTAL: { // Experimental features ("behind a flag") diff --git a/package.json b/package.json index 889dc797..e9f27741 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "dirty-chai": "^2.0.1", "electron-webrtc": "~0.3.0", "libp2p-circuit": "~0.2.1", - "libp2p-kad-dht": "~0.10.3", + "libp2p-kad-dht": "~0.10.5", "libp2p-mdns": "~0.12.0", "libp2p-mplex": "~0.8.2", "libp2p-bootstrap": "~0.9.3", diff --git a/src/config.js b/src/config.js index 6eaa7f7e..7cb9def5 100644 --- a/src/config.js +++ b/src/config.js @@ -29,7 +29,8 @@ const OptionsSchema = Joi.object({ }) }).default(), dht: Joi.object().keys({ - kBucketSize: Joi.number().allow(null) + kBucketSize: Joi.number().allow(null), + enabledDiscovery: Joi.boolean().default(true) }), EXPERIMENTAL: Joi.object().keys({ dht: Joi.boolean().default(false), diff --git a/src/index.js b/src/index.js index 893669a5..12056fd9 100644 --- a/src/index.js +++ b/src/index.js @@ -85,8 +85,11 @@ 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 + this._dht = new DHT(this._switch, { kBucketSize: this._config.dht.kBucketSize || 20, + enabledDiscovery, // TODO make datastore an option of libp2p itself so // that other things can use it as well datastore: dht.datastore diff --git a/test/create.spec.js b/test/create.spec.js index ac5b3e93..17b1d52d 100644 --- a/test/create.spec.js +++ b/test/create.spec.js @@ -28,10 +28,12 @@ describe('libp2p creation', () => { sinon.spy(sw, 'start') sinon.spy(cm, 'start') sinon.spy(dht, 'start') + sinon.spy(dht.randomWalk, 'start') sinon.spy(pub, 'start') sinon.spy(sw, 'stop') sinon.spy(cm, 'stop') sinon.spy(dht, 'stop') + sinon.spy(dht.randomWalk, 'stop') sinon.spy(pub, 'stop') sinon.spy(node, 'emit') @@ -41,6 +43,7 @@ describe('libp2p creation', () => { expect(sw.start.calledOnce).to.equal(true) expect(cm.start.calledOnce).to.equal(true) expect(dht.start.calledOnce).to.equal(true) + expect(dht.randomWalk.start.calledOnce).to.equal(true) expect(pub.start.calledOnce).to.equal(true) expect(node.emit.calledWith('start')).to.equal(true) @@ -53,6 +56,7 @@ describe('libp2p creation', () => { expect(sw.stop.calledOnce).to.equal(true) expect(cm.stop.calledOnce).to.equal(true) expect(dht.stop.calledOnce).to.equal(true) + expect(dht.randomWalk.stop.called).to.equal(true) expect(pub.stop.calledOnce).to.equal(true) expect(node.emit.calledWith('stop')).to.equal(true) diff --git a/test/utils/bundle-browser.js b/test/utils/bundle-browser.js index 6fcd7a24..159b6f80 100644 --- a/test/utils/bundle-browser.js +++ b/test/utils/bundle-browser.js @@ -79,7 +79,8 @@ class Node extends libp2p { } }, dht: { - kBucketSize: 20 + kBucketSize: 20, + enabledDiscovery: true }, EXPERIMENTAL: { dht: false, diff --git a/test/utils/bundle-nodejs.js b/test/utils/bundle-nodejs.js index f1dbcf2c..b018007b 100644 --- a/test/utils/bundle-nodejs.js +++ b/test/utils/bundle-nodejs.js @@ -72,7 +72,8 @@ class Node extends libp2p { } }, dht: { - kBucketSize: 20 + kBucketSize: 20, + enabledDiscovery: true }, EXPERIMENTAL: { dht: false,