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

Commit 77a0957

Browse files
daviddiasAlan Shaw
authored and
Alan Shaw
committed
feat: interoperable DHT (#856)
* feat: dht ready * test: enable dht core test * feat: add dht cli * chore: enable dht by default * fix: code review * chore: increase silent timeout even more * fix: code review * fix: ci tests * fix: code review * fix: make ci happy again
1 parent 506f5be commit 77a0957

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1196
-243
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ You can check the development status at the [Kanban Board](https://waffle.io/ipf
3434

3535
[![Throughput Graph](https://graphs.waffle.io/ipfs/js-ipfs/throughput.svg)](https://waffle.io/ipfs/js-ipfs/metrics/throughput)
3636

37-
**Please read this:** DHT (automatic content discovery) and Circuit Relay (pierce through NATs and dial between any node in the network) are two fundamental pieces that are not finalized yet. There are multiple applications that can be built without these two services but nevertheless they are fundamental to get that magic IPFS experience. If you want to track progress or contribute, please follow:
38-
39-
- DHT: https://github.com/ipfs/js-ipfs/pull/856
40-
- ✅ Relay: https://github.com/ipfs/js-ipfs/pull/1063
41-
4237
[**`Weekly Core Dev Calls`**](https://github.com/ipfs/pm/issues/650)
4338

4439
## Tech Lead
@@ -319,7 +314,6 @@ Enable and configure experimental features.
319314
- `pubsub` (boolean): Enable libp2p pub-sub. (Default: `false`)
320315
- `ipnsPubsub` (boolean): Enable pub-sub on IPNS. (Default: `false`)
321316
- `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`.**
323317
324318
##### `options.config`
325319
@@ -600,7 +594,13 @@ The core API is grouped into several areas:
600594
- [`ipfs.bootstrap.add(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BOOTSTRAP.md#bootstrapadd)
601595
- [`ipfs.bootstrap.rm(peer, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BOOTSTRAP.md#bootstraprm)
602596
603-
- dht (not implemented yet)
597+
- [dht](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/)
598+
- [`ipfs.dht.findPeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
599+
- [`ipfs.dht.findProvs(multihash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
600+
- [`ipfs.dht.get(key, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtget)
601+
- [`ipfs.dht.provide(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtprovide)
602+
- [`ipfs.dht.put(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtput)
603+
- [`ipfs.dht.query(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtquery)
604604
605605
- [pubsub](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md)
606606
- [`ipfs.pubsub.subscribe(topic, handler, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubsubscribe)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
"joi": "^14.3.0",
130130
"joi-browser": "^13.4.0",
131131
"joi-multiaddr": "^4.0.0",
132-
"libp2p": "~0.24.1",
132+
"libp2p": "~0.25.0-rc.0",
133133
"libp2p-bootstrap": "~0.9.3",
134134
"libp2p-crypto": "~0.16.0",
135135
"libp2p-kad-dht": "~0.14.4",
@@ -141,7 +141,7 @@
141141
"libp2p-tcp": "~0.13.0",
142142
"libp2p-webrtc-star": "~0.15.5",
143143
"libp2p-websocket-star-multi": "~0.4.0",
144-
"libp2p-websockets": "~0.12.0",
144+
"libp2p-websockets": "~0.12.2",
145145
"lodash": "^4.17.11",
146146
"mafmt": "^6.0.2",
147147
"mime-types": "^2.1.21",

src/cli/commands/daemon.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ module.exports = {
1818
type: 'boolean',
1919
default: false
2020
})
21-
.option('enable-dht-experiment', {
22-
type: 'boolean',
23-
default: false
24-
})
2521
.option('offline', {
2622
desc: 'Run offline. Do not connect to the rest of the network but provide local API.',
2723
default: false

src/cli/commands/dht.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'dht <command>',
5+
6+
description: 'Issue commands directly through the DHT.',
7+
8+
builder (yargs) {
9+
return yargs.commandDir('dht')
10+
},
11+
12+
handler (argv) {
13+
}
14+
}

src/cli/commands/dht/find-peer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const print = require('../../utils').print
4+
5+
module.exports = {
6+
command: 'findpeer <peerID>',
7+
8+
describe: 'Find the multiaddresses associated with a Peer ID.',
9+
10+
builder: {},
11+
12+
handler ({ getIpfs, peerID, resolve }) {
13+
resolve((async () => {
14+
const ipfs = await getIpfs()
15+
const peers = await ipfs.dht.findPeer(peerID)
16+
const addresses = peers.multiaddrs.toArray().map((ma) => ma.toString())
17+
18+
addresses.forEach((addr) => {
19+
print(addr)
20+
})
21+
})())
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const print = require('../../utils').print
4+
5+
module.exports = {
6+
command: 'findprovs <key>',
7+
8+
describe: 'Find peers that can provide a specific value, given a key.',
9+
10+
builder: {
11+
'num-providers': {
12+
alias: 'n',
13+
describe: 'The number of providers to find. Default: 20.',
14+
default: 20
15+
}
16+
},
17+
18+
handler (argv) {
19+
const { getIpfs, key, resolve } = argv
20+
const opts = {
21+
maxNumProviders: argv['num-providers']
22+
}
23+
24+
resolve((async () => {
25+
const ipfs = await getIpfs()
26+
const provs = await ipfs.dht.findProvs(key, opts)
27+
28+
provs.forEach((element) => {
29+
print(element.id.toB58String())
30+
})
31+
})())
32+
}
33+
}

src/cli/commands/dht/get.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const print = require('../../utils').print
4+
5+
module.exports = {
6+
command: 'get <key>',
7+
8+
describe: 'Given a key, query the routing system for its best value.',
9+
10+
builder: {},
11+
12+
handler ({ getIpfs, key, resolve }) {
13+
resolve((async () => {
14+
const ipfs = await getIpfs()
15+
const value = await ipfs.dht.get(key)
16+
17+
print(value)
18+
})())
19+
}
20+
}

src/cli/commands/dht/provide.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'provide <key>',
5+
6+
describe: 'Announce to the network that you are providing given values.',
7+
8+
builder: {
9+
recursive: {
10+
alias: 'r',
11+
recursive: 'Recursively provide entire graph.',
12+
default: false
13+
}
14+
},
15+
16+
handler ({ getIpfs, key, recursive, resolve }) {
17+
const opts = {
18+
recursive
19+
}
20+
21+
resolve((async () => {
22+
const ipfs = await getIpfs()
23+
await ipfs.dht.provide(key, opts)
24+
})())
25+
}
26+
}

src/cli/commands/dht/put.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'put <key> <value>',
5+
6+
describe: 'Write a key/value pair to the routing system.',
7+
8+
builder: {},
9+
10+
handler ({ getIpfs, key, value, resolve }) {
11+
resolve((async () => {
12+
const ipfs = await getIpfs()
13+
await ipfs.dht.put(key, value)
14+
})())
15+
}
16+
}

src/cli/commands/dht/query.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const print = require('../../utils').print
4+
5+
module.exports = {
6+
command: 'query <peerID>',
7+
8+
describe: 'Find the closest Peer IDs to a given Peer ID by querying the DHT.',
9+
10+
builder: {},
11+
12+
handler ({ getIpfs, peerID, resolve }) {
13+
resolve((async () => {
14+
const ipfs = await getIpfs()
15+
const result = await ipfs.dht.query(peerID)
16+
17+
result.forEach((peerID) => {
18+
print(peerID.id.toB58String())
19+
})
20+
})())
21+
}
22+
}

0 commit comments

Comments
 (0)