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 }, 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/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/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 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/core/name.js b/test/core/name.js index ed73592d28..99b8257251 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') @@ -193,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 @@ -532,4 +536,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() + }) + }) }) 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 diff --git a/test/http-api/inject/dht.js b/test/http-api/inject/dht.js index 455a4bb8b1..c57deaecd0 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(() => { diff --git a/test/http-api/interface.js b/test/http-api/interface.js index a33309b6c3..738b21cb83 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 in 0.36' + } }) tests.filesRegular(defaultCommonFactory)