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

Commit c200ea5

Browse files
committed
feat: dht ready
1 parent 381ac13 commit c200ea5

File tree

6 files changed

+65
-37
lines changed

6 files changed

+65
-37
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Enable and configure experimental features.
319319
- `pubsub` (boolean): Enable libp2p pub-sub. (Default: `false`)
320320
- `ipnsPubsub` (boolean): Enable pub-sub on IPNS. (Default: `false`)
321321
- `sharding` (boolean): Enable directory sharding. Directories that have many child objects will be represented by multiple DAG nodes instead of just one. It can improve lookup performance when a directory has several thousand files or more. (Default: `false`)
322-
- `dht` (boolean): Enable KadDHT. **This is currently not interoperable with `go-ipfs`.**
322+
- `dht` (boolean): Enable KadDHT.
323323
324324
##### `options.config`
325325
@@ -600,7 +600,13 @@ The core API is grouped into several areas:
600600
- [`ipfs.bootstrap.add(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BOOTSTRAP.md#bootstrapadd)
601601
- [`ipfs.bootstrap.rm(peer, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BOOTSTRAP.md#bootstraprm)
602602
603-
- dht (not implemented yet)
603+
- [dht](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/)
604+
- [`ipfs.dht.findpeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
605+
- [`ipfs.dht.findprovs(multihash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
606+
- [`ipfs.dht.get(key, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtget)
607+
- [`ipfs.dht.provide(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtprovide)
608+
- [`ipfs.dht.put(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtput)
609+
- [`ipfs.dht.query(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtquery)
604610
605611
- [pubsub](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md)
606612
- [`ipfs.pubsub.subscribe(topic, handler, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubsubscribe)

src/core/components/dht.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const promisify = require('promisify-es6')
44
const every = require('async/every')
55
const PeerId = require('peer-id')
66
const CID = require('cids')
7+
const multihash = require('multihashes')
78
const each = require('async/each')
89
const setImmediate = require('async/setImmediate')
910
// const bsplit = require('buffer-split')
@@ -19,17 +20,21 @@ module.exports = (self) => {
1920
* @returns {Promise|void}
2021
*/
2122
get: promisify((key, options, callback) => {
22-
if (!Buffer.isBuffer(key)) {
23-
return callback(new Error('Not valid key'))
24-
}
25-
2623
if (typeof options === 'function') {
2724
callback = options
2825
options = {}
2926
}
3027

3128
options = options || {}
3229

30+
if (!Buffer.isBuffer(key)) {
31+
try {
32+
key = multihash.fromB58String(key)
33+
} catch (err) {
34+
return callback(err)
35+
}
36+
}
37+
3338
self.libp2p.dht.get(key, options.timeout, callback)
3439
}),
3540

@@ -47,7 +52,11 @@ module.exports = (self) => {
4752
*/
4853
put: promisify((key, value, callback) => {
4954
if (!Buffer.isBuffer(key)) {
50-
return callback(new Error('Not valid key'))
55+
try {
56+
key = multihash.fromB58String(key)
57+
} catch (err) {
58+
return callback(err)
59+
}
5160
}
5261

5362
self.libp2p.dht.put(key, value, callback)

src/core/components/libp2p.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,37 @@ module.exports = function libp2p (self, config) {
4747

4848
function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
4949
const libp2pDefaults = {
50-
datastore,
51-
peerInfo,
52-
peerBook,
50+
datastore: opts.datastore,
51+
peerInfo: opts.peerInfo,
52+
peerBook: opts.peerBook,
5353
config: {
5454
peerDiscovery: {
5555
mdns: {
56-
enabled: get(options, 'config.Discovery.MDNS.Enabled',
57-
get(config, 'Discovery.MDNS.Enabled', true))
56+
enabled: get(opts.options, 'config.Discovery.MDNS.Enabled',
57+
get(opts.config, 'Discovery.MDNS.Enabled', true))
5858
},
5959
webRTCStar: {
60-
enabled: get(options, 'config.Discovery.webRTCStar.Enabled',
61-
get(config, 'Discovery.webRTCStar.Enabled', true))
60+
enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled',
61+
get(opts.config, 'Discovery.webRTCStar.Enabled', true))
6262
},
6363
bootstrap: {
64-
list: get(options, 'config.Bootstrap',
65-
get(config, 'Bootstrap', []))
64+
list: get(opts.options, 'config.Bootstrap',
65+
get(opts.config, 'Bootstrap', []))
6666
}
6767
},
6868
relay: {
69-
enabled: get(options, 'relay.enabled',
70-
get(config, 'relay.enabled', false)),
69+
enabled: get(opts.options, 'relay.enabled',
70+
get(opts.config, 'relay.enabled', false)),
7171
hop: {
72-
enabled: get(options, 'relay.hop.enabled',
73-
get(config, 'relay.hop.enabled', false)),
74-
active: get(options, 'relay.hop.active',
75-
get(config, 'relay.hop.active', false))
72+
enabled: get(opts.options, 'relay.hop.enabled',
73+
get(opts.config, 'relay.hop.enabled', false)),
74+
active: get(opts.options, 'relay.hop.active',
75+
get(opts.config, 'relay.hop.active', false))
7676
}
7777
},
7878
dht: {
79+
kBucketSize: get(opts.options, 'dht.kBucketSize', 20),
80+
enabledDiscovery: get(opts.options, 'dht.enabledDiscovery', true),
7981
validators: {
8082
ipns: ipnsUtils.validator
8183
},
@@ -84,12 +86,12 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
8486
}
8587
},
8688
EXPERIMENTAL: {
87-
dht: get(options, 'EXPERIMENTAL.dht', false),
88-
pubsub: get(options, 'EXPERIMENTAL.pubsub', false)
89+
dht: get(opts.options, 'EXPERIMENTAL.dht', false),
90+
pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false)
8991
}
9092
},
91-
connectionManager: get(options, 'connectionManager',
92-
get(config, 'connectionManager', {}))
93+
connectionManager: get(opts.options, 'connectionManager',
94+
get(opts.config, 'connectionManager', {}))
9395
}
9496

9597
const libp2pOptions = defaultsDeep(get(options, 'libp2p', {}), libp2pDefaults)

src/core/runtime/libp2p-browser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const WebSocketStarMulti = require('libp2p-websocket-star-multi')
66
const Multiplex = require('libp2p-mplex')
77
const SECIO = require('libp2p-secio')
88
const Bootstrap = require('libp2p-bootstrap')
9+
const KadDHT = require('libp2p-kad-dht')
910
const libp2p = require('libp2p')
1011
const defaultsDeep = require('@nodeutils/defaults-deep')
1112
const multiaddr = require('multiaddr')
@@ -37,7 +38,8 @@ class Node extends libp2p {
3738
wrtcstar.discovery,
3839
wsstar.discovery,
3940
Bootstrap
40-
]
41+
],
42+
dht: KadDHT
4143
},
4244
config: {
4345
peerDiscovery: {
@@ -51,6 +53,10 @@ class Node extends libp2p {
5153
enabled: true
5254
}
5355
},
56+
dht: {
57+
kBucketSize: 20,
58+
enabledDiscovery: true
59+
},
5460
EXPERIMENTAL: {
5561
dht: false,
5662
pubsub: false

src/core/runtime/libp2p-nodejs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class Node extends libp2p {
5353
}
5454
},
5555
dht: {
56-
kBucketSize: 20
56+
kBucketSize: 20,
57+
enabledDiscovery: true
5758
},
5859
EXPERIMENTAL: {
5960
dht: false,

test/core/interface.spec.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,23 @@ describe('interface-ipfs-core tests', () => {
3636

3737
const dhtCommonFactory = CommonFactory.create({
3838
spawnOptions: {
39-
initOptions: { bits: 512 },
40-
EXPERIMENTAL: {
41-
dht: true
42-
},
4339
config: {
44-
Bootstrap: []
45-
}
40+
Bootstrap: [],
41+
Discovery: {
42+
MDNS: {
43+
Enabled: false
44+
},
45+
webRTCStar: {
46+
Enabled: false
47+
}
48+
}
49+
},
50+
args: ['--enable-dht-experiment'],
51+
initOptions: { bits: 512 }
4652
}
4753
})
4854

49-
tests.dht(dhtCommonFactory, {
50-
skip: { reason: 'TODO: unskip when https://github.com/ipfs/js-ipfs/pull/856 is merged' }
51-
})
55+
tests.dht(dhtCommonFactory)
5256

5357
tests.filesRegular(defaultCommonFactory, {
5458
skip: isNode ? null : [{

0 commit comments

Comments
 (0)