Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 45f1597

Browse files
committed
chore: allow disabling dht and make sure we have an ipns datastore available
1 parent 7d0a126 commit 45f1597

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

packages/interface-ipfs-core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"it-to-buffer": "^2.0.0",
9292
"libp2p-crypto": "^0.21.1",
9393
"libp2p-websockets": "^0.16.2",
94+
"merge-options": "^3.0.4",
9495
"multiaddr": "^10.0.0",
9596
"multiformats": "^9.4.13",
9697
"nanoid": "^3.1.23",

packages/interface-ipfs-core/src/miscellaneous/resolve.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ export function testResolve (factory, options) {
3232
let ipfsId
3333

3434
before(async () => {
35-
ipfs = (await factory.spawn({ type: 'proc', ipfsOptions: merge(ipfsOptions, {
36-
config: {
37-
Routing: {
38-
Type: 'none'
35+
ipfs = (await factory.spawn({
36+
type: 'proc',
37+
ipfsOptions: merge(ipfsOptions, {
38+
config: {
39+
Routing: {
40+
Type: 'none'
41+
}
3942
}
40-
}
41-
})})).api
43+
})
44+
})).api
4245
ipfsId = await ipfs.id()
4346
})
4447

@@ -104,13 +107,16 @@ export function testResolve (factory, options) {
104107
// @ts-ignore this is mocha
105108
this.timeout(20 * 1000)
106109
// webworkers are not dialable because webrtc is not available
107-
const node = (await factory.spawn({ type: isWebWorker ? 'go' : undefined, ipfsOptions: {
108-
config: {
109-
Routing: {
110-
Type: 'none'
110+
const node = (await factory.spawn({
111+
type: isWebWorker ? 'go' : undefined,
112+
ipfsOptions: {
113+
config: {
114+
Routing: {
115+
Type: 'none'
116+
}
111117
}
112118
}
113-
} })).api
119+
})).api
114120
const nodeId = await node.id()
115121
await ipfs.swarm.connect(nodeId.addresses[0])
116122
const { path } = await ipfs.add(uint8ArrayFromString('should resolve a record recursive === true'))
@@ -127,13 +133,16 @@ export function testResolve (factory, options) {
127133
// @ts-ignore this is mocha
128134
this.timeout(20 * 1000)
129135
// webworkers are not dialable because webrtc is not available
130-
const node = (await factory.spawn({ type: isWebWorker ? 'go' : undefined, ipfsOptions: {
131-
config: {
132-
Routing: {
133-
Type: 'none'
136+
const node = (await factory.spawn({
137+
type: isWebWorker ? 'go' : undefined,
138+
ipfsOptions: {
139+
config: {
140+
Routing: {
141+
Type: 'none'
142+
}
134143
}
135144
}
136-
} })).api
145+
})).api
137146
const nodeId = await node.id()
138147
await ipfs.swarm.connect(nodeId.addresses[0])
139148
const { path } = await ipfs.add(uint8ArrayFromString('should resolve an IPNS key if recursive === false'))

packages/ipfs-core/src/components/libp2p.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
3232
* @typedef {import('../types').Options} IPFSOptions
3333
* @typedef {import('libp2p')} LibP2P
3434
* @typedef {import('libp2p').Libp2pOptions & import('libp2p').CreateOptions} Libp2pOptions
35+
* @typedef {import('libp2p').Libp2pConfig} Libp2pConfig
3536
* @typedef {import('ipfs-core-types/src/config').Config} IPFSConfig
3637
*/
3738

@@ -128,7 +129,7 @@ function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, p
128129
}
129130
},
130131
dht: {
131-
enabled: get(config, 'Routing.Type', 'none') !== 'none',
132+
enabled: get(config, 'Routing.Type', 'dhtclient') !== 'none',
132133
clientMode: get(config, 'Routing.Type', 'dht') !== 'dhtserver',
133134
kBucketSize: get(options, 'dht.kBucketSize', 20),
134135
validators: {

packages/ipfs-core/src/ipns/routing/config.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ export function createRouting ({ libp2p, repo, peerId, options }) {
3030
ipnsStores.push(pubsubDs)
3131
}
3232

33-
// DHT should not be added as routing if we are offline or it is disabled
34-
if (get(options, 'offline') || get(options, 'config.Routing.Type', 'none') === 'none') {
33+
// Add DHT datastore if enabled
34+
if (!get(options, 'offline', false) && libp2p._config && libp2p._config.dht && libp2p._config.dht.enabled) {
35+
ipnsStores.push(new DHTDatastore(libp2p._dht))
36+
}
37+
38+
// Add an offline datastore if we are offline or no other datastores are configured
39+
if (get(options, 'offline', false) || ipnsStores.length === 0) {
3540
const offlineDatastore = new OfflineDatastore(repo)
3641
ipnsStores.push(offlineDatastore)
37-
} else {
38-
ipnsStores.push(new DHTDatastore(libp2p._dht))
3942
}
4043

4144
// Create ipns routing with a set of datastores

packages/ipfs-core/test/name.spec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('name', function () {
157157
// @ts-expect-error invalid argument
158158
await expect(publisher.publish(peerId.privKey, ipfsRef))
159159
.to.eventually.be.rejected()
160-
.with.property('code', 'ERR_STORING_IN_DATASTORE')
160+
.with.property('code', 'ERR_CREATING_IPNS_RECORD')
161161
})
162162
})
163163

@@ -283,17 +283,16 @@ describe('name', function () {
283283
}
284284
})
285285

286-
expect(config.stores).to.have.lengthOf(2)
286+
expect(config.stores).to.have.lengthOf(1)
287287
expect(config.stores[0] instanceof IpnsPubsubDatastore).to.eql(true)
288-
expect(config.stores[1] instanceof OfflineDatastore).to.eql(true)
289288
})
290289

291290
it('should use the dht if enabled', () => {
292291
const dht = sinon.stub()
293292

294293
const config = createRouting({
295294
// @ts-expect-error sinon.stub() is not complete implementation
296-
libp2p: { _dht: dht },
295+
libp2p: { _dht: dht, _config: { dht: { enabled: true } } },
297296
// @ts-expect-error sinon.stub() is not complete implementation
298297
repo: sinon.stub(),
299298
// @ts-expect-error sinon.stub() is not complete implementation

0 commit comments

Comments
 (0)