From 8617d6593e0fe860671a3bdc368e2865cfae2ef2 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 11 Apr 2019 18:23:01 +0100 Subject: [PATCH 1/8] fix: ipns using offline datastore if no dht --- src/core/components/start.js | 32 ++----------- src/core/ipns/routing/config.js | 34 +++++++++++++ test/core/name.js | 85 +++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 src/core/ipns/routing/config.js diff --git a/src/core/components/start.js b/src/core/components/start.js index 64ee8c73e0..8fc220cdbc 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -2,14 +2,11 @@ const series = require('async/series') const Bitswap = require('ipfs-bitswap') -const get = require('dlv') const setImmediate = require('async/setImmediate') const promisify = require('promisify-es6') -const { TieredDatastore } = require('datastore-core') const IPNS = require('../ipns') -const PubsubDatastore = require('../ipns/routing/pubsub-datastore') -const OfflineDatastore = require('../ipns/routing/offline-datastore') +const routingConfig = require('../ipns/routing/config') const createLibp2pBundle = require('./libp2p') module.exports = (self) => { @@ -53,31 +50,8 @@ module.exports = (self) => { }) }, (cb) => { - // Setup online routing for IPNS with a tiered routing composed by a DHT and a Pubsub router (if properly enabled) - const ipnsStores = [] - - // Add IPNS pubsub if enabled - let pubsubDs - if (get(self._options, 'EXPERIMENTAL.ipnsPubsub', false)) { - const pubsub = self.libp2p.pubsub - const localDatastore = self._repo.datastore - const peerId = self._peerInfo.id - - pubsubDs = new PubsubDatastore(pubsub, localDatastore, peerId) - ipnsStores.push(pubsubDs) - } - - // DHT should be added as routing if we are not running with local flag - if (!self._options.offline) { - ipnsStores.push(self.libp2p.dht) - } else { - const offlineDatastore = new OfflineDatastore(self._repo) - ipnsStores.push(offlineDatastore) - } - - // Create ipns routing with a set of datastores - const routing = new TieredDatastore(ipnsStores) - self._ipns = new IPNS(routing, self._repo.datastore, self._peerInfo, self._keychain, self._options) + const ipnsRouting = routingConfig(self) + self._ipns = new IPNS(ipnsRouting, self._repo.datastore, self._peerInfo, self._keychain, self._options) self._bitswap = new Bitswap( self.libp2p, diff --git a/src/core/ipns/routing/config.js b/src/core/ipns/routing/config.js new file mode 100644 index 0000000000..7faa469258 --- /dev/null +++ b/src/core/ipns/routing/config.js @@ -0,0 +1,34 @@ +'use strict' + +const { TieredDatastore } = require('datastore-core') +const get = require('dlv') + +const PubsubDatastore = require('./pubsub-datastore') +const OfflineDatastore = require('./offline-datastore') + +module.exports = (ipfs) => { + // Setup online routing for IPNS with a tiered routing composed by a DHT and a Pubsub router (if properly enabled) + const ipnsStores = [] + + // Add IPNS pubsub if enabled + let pubsubDs + if (get(ipfs._options, 'EXPERIMENTAL.ipnsPubsub', false)) { + const pubsub = ipfs.libp2p.pubsub + const localDatastore = ipfs._repo.datastore + const peerId = ipfs._peerInfo.id + + pubsubDs = new PubsubDatastore(pubsub, localDatastore, peerId) + ipnsStores.push(pubsubDs) + } + + // DHT should not be added as routing if we are offline or it is disabled + if (get(ipfs._options, 'offline') || !get(ipfs._options, 'libp2p.dht.enabled', false)) { + const offlineDatastore = new OfflineDatastore(ipfs._repo) + ipnsStores.push(offlineDatastore) + } else { + ipnsStores.push(ipfs.libp2p.dht) + } + + // Create ipns routing with a set of datastores + return new TieredDatastore(ipnsStores) +} diff --git a/test/core/name.js b/test/core/name.js index ed73592d28..1ad7bc3b7b 100644 --- a/test/core/name.js +++ b/test/core/name.js @@ -16,6 +16,9 @@ const series = require('async/series') const isNode = require('detect-node') const IPFS = require('../../src') const ipnsPath = require('../../src/core/ipns/path') +const ipnsRouting = require('../../src/core/ipns/routing/config') +const OfflineDatastore = require('../../src/core/ipns/routing/offline-datastore') +const PubsubDatastore = require('../../src/core/ipns/routing/pubsub-datastore') const { Key } = require('interface-datastore') const DaemonFactory = require('ipfsd-ctl') @@ -532,4 +535,86 @@ describe('name', function () { }) }) }) + + describe('ipns.routing', function () { + it('should use only the offline datastore by default', function (done) { + const ipfs = {} + const config = ipnsRouting(ipfs) + + expect(config.stores).to.have.lengthOf(1) + expect(config.stores[0] instanceof OfflineDatastore).to.eql(true) + + done() + }) + + it('should use only the offline datastore if offline', function (done) { + const ipfs = { + _options: { + offline: true + } + } + const config = ipnsRouting(ipfs) + + expect(config.stores).to.have.lengthOf(1) + expect(config.stores[0] instanceof OfflineDatastore).to.eql(true) + + done() + }) + + it('should use the pubsub datastore if enabled', function (done) { + const ipfs = { + libp2p: { + pubsub: {} + }, + _peerInfo: { + id: {} + }, + _repo: { + datastore: {} + }, + _options: { + EXPERIMENTAL: { + ipnsPubsub: true + } + } + } + const config = ipnsRouting(ipfs) + + expect(config.stores).to.have.lengthOf(2) + expect(config.stores[0] instanceof PubsubDatastore).to.eql(true) + expect(config.stores[1] instanceof OfflineDatastore).to.eql(true) + + done() + }) + + it('should use the dht if enabled', function (done) { + const dht = {} + + const ipfs = { + libp2p: { + dht + }, + _peerInfo: { + id: {} + }, + _repo: { + datastore: {} + }, + _options: { + libp2p: { + dht: { + enabled: true + } + } + } + } + + const config = ipnsRouting(ipfs) + + expect(config.stores).to.have.lengthOf(1) + expect(config.stores[0]).to.eql(dht) + + done() + }) + }) }) From 6960a0c142a5ad0b3377871a20764bc27942170d Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Apr 2019 10:33:27 +0100 Subject: [PATCH 2/8] fix: really disable DHT License: MIT Signed-off-by: Alan Shaw --- src/core/components/libp2p.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 447205a147..3ca4b525f3 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -66,7 +66,8 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { }, dht: { kBucketSize: get(options, 'dht.kBucketSize', 20), - enabled: !get(options, 'offline', false), // disable if offline, on by default + // enabled: !get(options, 'offline', false), // disable if offline, on by default + enabled: false, randomWalk: { enabled: false // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86 }, From c11230bf749b7cbe14db285b72588c98c22327df Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Apr 2019 11:25:44 +0100 Subject: [PATCH 3/8] fix: skip the DHT tests while DHT is disabled License: MIT Signed-off-by: Alan Shaw --- test/cli/dht.js | 3 ++- test/core/interface.spec.js | 10 +++------- test/http-api/interface.js | 10 +++------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/test/cli/dht.js b/test/cli/dht.js index 0f12be3abc..45f13cadc3 100644 --- a/test/cli/dht.js +++ b/test/cli/dht.js @@ -31,7 +31,8 @@ const daemonOpts = { initOptions: { bits: 512 } } -describe('dht', () => { +// TODO: unskip when DHT is enabled in 0.36 +describe.skip('dht', () => { let nodes = [] let ipfsA let ipfsB diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index 9a59921b61..e8e669eb01 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -52,13 +52,9 @@ describe('interface-ipfs-core tests', function () { initOptions: { bits: 512 } } }), { - skip: isNode ? [ - // dht.get - { - name: 'should get a value after it was put on another node', - reason: 'Needs https://github.com/ipfs/interface-ipfs-core/pull/383' - } - ] : true + skip: { + reason: 'TODO: unskip when DHT is enabled in 0.36' + } }) tests.filesRegular(defaultCommonFactory, { diff --git a/test/http-api/interface.js b/test/http-api/interface.js index a33309b6c3..83d8615658 100644 --- a/test/http-api/interface.js +++ b/test/http-api/interface.js @@ -43,13 +43,9 @@ describe('interface-ipfs-core over ipfs-http-client tests', () => { } } }), { - skip: [ - // dht.get - { - name: 'should get a value after it was put on another node', - reason: 'Needs https://github.com/ipfs/interface-ipfs-core/pull/383' - } - ] + skip: { + reason: 'TODO: unskip when DHT is enabled' + } }) tests.filesRegular(defaultCommonFactory) From 5462619c0972006e6100ea1587e9c041a2989557 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Apr 2019 11:27:02 +0100 Subject: [PATCH 4/8] fix: more skip the DHT tests while DHT is disabled License: MIT Signed-off-by: Alan Shaw --- test/core/dht.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/core/dht.spec.js b/test/core/dht.spec.js index b1a3207429..ba437a28bc 100644 --- a/test/core/dht.spec.js +++ b/test/core/dht.spec.js @@ -12,7 +12,8 @@ const isNode = require('detect-node') const IPFSFactory = require('ipfsd-ctl') const IPFS = require('../../src/core') -describe('dht', () => { +// TODO: unskip when DHT is enabled in 0.36 +describe.skip('dht', () => { describe('enabled', () => { let ipfsd, ipfs From a64318f9affab9373ff5759ffbef4ed3b9004b3e Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Apr 2019 11:31:18 +0100 Subject: [PATCH 5/8] fix: more skip the DHT tests while DHT is disabled License: MIT Signed-off-by: Alan Shaw --- test/http-api/interface.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/http-api/interface.js b/test/http-api/interface.js index 83d8615658..738b21cb83 100644 --- a/test/http-api/interface.js +++ b/test/http-api/interface.js @@ -44,7 +44,7 @@ describe('interface-ipfs-core over ipfs-http-client tests', () => { } }), { skip: { - reason: 'TODO: unskip when DHT is enabled' + reason: 'TODO: unskip when DHT is enabled in 0.36' } }) From 8c55f07d1546c5e1ec44e8f787baf1dd99c5cc27 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Apr 2019 12:03:32 +0100 Subject: [PATCH 6/8] fix: disable DHT ping tests License: MIT Signed-off-by: Alan Shaw --- test/core/ping.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/core/ping.spec.js b/test/core/ping.spec.js index 5b784c5428..3b477e05ee 100644 --- a/test/core/ping.spec.js +++ b/test/core/ping.spec.js @@ -194,7 +194,8 @@ describe('ping', function () { }) }) - describe('DHT enabled', function () { + // TODO: unskip when DHT enabled in 0.36 + describe.skip('DHT enabled', function () { // Our bootstrap process will run 3 IPFS daemons where // A ----> B ----> C // Allowing us to test the ping command using the DHT peer routing From b782160d198090945469eab8f745e2b5250c0d12 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Apr 2019 12:27:36 +0100 Subject: [PATCH 7/8] fix: more skip License: MIT Signed-off-by: Alan Shaw --- test/http-api/inject/dht.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/http-api/inject/dht.js b/test/http-api/inject/dht.js index 04cb501227..bb41185b30 100644 --- a/test/http-api/inject/dht.js +++ b/test/http-api/inject/dht.js @@ -8,7 +8,8 @@ const expect = chai.expect chai.use(dirtyChai) module.exports = (http) => { - describe('/dht', () => { + // TODO: unskip when DHT is enabled in 0.36 + describe.skip('/dht', () => { let api before(() => { From d43b5736ff8fdc3b5714fed34a930326189071ed Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Apr 2019 12:47:52 +0100 Subject: [PATCH 8/8] fix: more skip License: MIT Signed-off-by: Alan Shaw --- test/core/name.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/core/name.js b/test/core/name.js index 1ad7bc3b7b..99b8257251 100644 --- a/test/core/name.js +++ b/test/core/name.js @@ -196,7 +196,8 @@ describe('name', function () { }) }) - describe('work with dht', () => { + // TODO: unskip when DHT is enabled in 0.36 + describe.skip('work with dht', () => { let nodes let nodeA let nodeB