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

Commit f9cd739

Browse files
committed
feat: add dht cli
1 parent 8ecb7b5 commit f9cd739

File tree

18 files changed

+766
-27
lines changed

18 files changed

+766
-27
lines changed

src/cli/commands/dht.js

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

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 (argv) {
13+
argv.ipfs.dht.findpeer(argv.peerID, (err, result) => {
14+
if (err) {
15+
throw err
16+
}
17+
18+
result.responses[0].addrs.forEach((element) => {
19+
print(element)
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 opts = {
20+
'num-providers': argv['num-providers']
21+
}
22+
23+
argv.ipfs.dht.findprovs(argv.key, opts, (err, result) => {
24+
if (err) {
25+
throw err
26+
}
27+
28+
result.responses.forEach((element) => {
29+
print(element.id)
30+
})
31+
})
32+
}
33+
}

src/cli/commands/dht/get.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 (argv) {
13+
argv.ipfs.dht.get(argv.key, (err, result) => {
14+
if (err) {
15+
throw err
16+
}
17+
18+
print(result)
19+
})
20+
}
21+
}

src/cli/commands/dht/provide.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 (argv) {
17+
argv.ipfs.dht.provide(argv.key, (err, result) => {
18+
if (err) {
19+
throw err
20+
}
21+
})
22+
}
23+
}

src/cli/commands/dht/put.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 (argv) {
11+
argv.ipfs.dht.put(argv.key, argv.value, (err) => {
12+
if (err) {
13+
throw err
14+
}
15+
})
16+
}
17+
}

src/cli/commands/dht/query.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: '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 (argv) {
13+
argv.ipfs.dht.query(argv.peerID, (err, result) => {
14+
if (err) {
15+
throw err
16+
}
17+
18+
result.forEach((element) => {
19+
print(element.ID)
20+
})
21+
})
22+
}
23+
}

src/core/components/dht.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const CID = require('cids')
77
const multihash = require('multihashes')
88
const each = require('async/each')
99
const setImmediate = require('async/setImmediate')
10-
// const bsplit = require('buffer-split')
1110
const errCode = require('err-code')
1211

1312
module.exports = (self) => {
@@ -35,7 +34,7 @@ module.exports = (self) => {
3534
}
3635
}
3736

38-
self.libp2p.dht.get(key, options.timeout, callback)
37+
self.libp2p.dht.get(key, options, callback)
3938
}),
4039

4140
/**
@@ -76,6 +75,7 @@ module.exports = (self) => {
7675
}
7776

7877
opts = opts || {}
78+
opts.maxNumProviders = opts['num-providers']
7979

8080
if (typeof key === 'string') {
8181
try {
@@ -85,14 +85,23 @@ module.exports = (self) => {
8585
}
8686
}
8787

88-
if (typeof opts === 'function') {
89-
callback = opts
90-
opts = {}
91-
}
88+
self.libp2p.contentRouting.findProviders(key, opts, (err, res) => {
89+
if (err) {
90+
return callback(err)
91+
}
9292

93-
opts = opts || {}
93+
// convert to go-ipfs return value, we need to revisit
94+
// this. For now will just conform.
95+
const goResult = {
96+
responses: res.map((peerInfo) => ({
97+
id: peerInfo.id.toB58String(),
98+
addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString())
99+
})),
100+
type: 4
101+
}
94102

95-
self.libp2p.contentRouting.findProviders(key, opts.timeout || null, callback)
103+
callback(null, goResult)
104+
})
96105
}),
97106

98107
/**
@@ -114,14 +123,13 @@ module.exports = (self) => {
114123

115124
// convert to go-ipfs return value, we need to revisit
116125
// this. For now will just conform.
117-
const goResult = [
118-
{
119-
Responses: [{
120-
ID: info.id.toB58String(),
121-
Addresses: info.multiaddrs.toArray().map((a) => a.toString())
122-
}]
123-
}
124-
]
126+
const goResult = {
127+
responses: [{
128+
id: info.id.toB58String(),
129+
addrs: info.multiaddrs.toArray().map((a) => a.toString())
130+
}],
131+
type: 2
132+
}
125133

126134
callback(null, goResult)
127135
})
@@ -178,7 +186,11 @@ module.exports = (self) => {
178186
*/
179187
query: promisify((peerId, callback) => {
180188
if (typeof peerId === 'string') {
181-
peerId = PeerId.createFromB58String(peerId)
189+
try {
190+
peerId = PeerId.createFromB58String(peerId)
191+
} catch (err) {
192+
callback(err)
193+
}
182194
}
183195

184196
// TODO expose this method in peerRouting

0 commit comments

Comments
 (0)