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

[WIP] feat: connections limit #1800

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
feat: add dht cli
  • Loading branch information
vasco-santos committed Dec 28, 2018
commit 10e31df5dac02dca85de796074f498e749e16e0e
17 changes: 17 additions & 0 deletions src/cli/commands/dht.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

/*
Issue commands directly through the DHT.
*/
module.exports = {
command: 'dht <command>',

description: 'Issue commands directly through the DHT.',

builder (yargs) {
return yargs.commandDir('dht')
},

handler (argv) {
}
}
23 changes: 23 additions & 0 deletions src/cli/commands/dht/find-peer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'findpeer <peerID>',

describe: 'Find the multiaddresses associated with a Peer ID.',

builder: {},

handler (argv) {
argv.ipfs.dht.findpeer(argv.peerID, (err, result) => {
if (err) {
throw err
}

result.responses[0].addrs.forEach((element) => {
print(element)
})
})
}
}
33 changes: 33 additions & 0 deletions src/cli/commands/dht/find-providers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'findprovs <key>',

describe: 'Find peers that can provide a specific value, given a key.',

builder: {
'num-providers': {
alias: 'n',
describe: 'The number of providers to find. Default: 20.',
default: 20
}
},

handler (argv) {
const opts = {
'num-providers': argv['num-providers']
}

argv.ipfs.dht.findprovs(argv.key, opts, (err, result) => {
if (err) {
throw err
}

result.responses.forEach((element) => {
print(element.id)
})
})
}
}
21 changes: 21 additions & 0 deletions src/cli/commands/dht/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'get <key>',

describe: 'Given a key, query the routing system for its best value.',

builder: {},

handler (argv) {
argv.ipfs.dht.get(argv.key, (err, result) => {
if (err) {
throw err
}

print(result)
})
}
}
23 changes: 23 additions & 0 deletions src/cli/commands/dht/provide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

module.exports = {
command: 'provide <key>',

describe: 'Announce to the network that you are providing given values.',

builder: {
recursive: {
alias: 'r',
recursive: 'Recursively provide entire graph.',
default: false
}
},

handler (argv) {
argv.ipfs.dht.provide(argv.key, (err, result) => {
if (err) {
throw err
}
})
}
}
17 changes: 17 additions & 0 deletions src/cli/commands/dht/put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

module.exports = {
command: 'put <key> <value>',

describe: 'Write a key/value pair to the routing system.',

builder: {},

handler (argv) {
argv.ipfs.dht.put(argv.key, argv.value, (err) => {
if (err) {
throw err
}
})
}
}
23 changes: 23 additions & 0 deletions src/cli/commands/dht/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'query <peerID>',

describe: 'Find the closest Peer IDs to a given Peer ID by querying the DHT.',

builder: {},

handler (argv) {
argv.ipfs.dht.query(argv.peerID, (err, result) => {
if (err) {
throw err
}

result.forEach((element) => {
print(element.ID)
})
})
}
}
46 changes: 29 additions & 17 deletions src/core/components/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const CID = require('cids')
const multihash = require('multihashes')
const each = require('async/each')
const setImmediate = require('async/setImmediate')
// const bsplit = require('buffer-split')
const errCode = require('err-code')

module.exports = (self) => {
Expand Down Expand Up @@ -35,7 +34,7 @@ module.exports = (self) => {
}
}

self._libp2pNode.dht.get(key, options.timeout, callback)
self._libp2pNode.dht.get(key, options, callback)
}),

/**
Expand Down Expand Up @@ -76,6 +75,7 @@ module.exports = (self) => {
}

opts = opts || {}
opts.maxNumProviders = opts['num-providers']

if (typeof key === 'string') {
try {
Expand All @@ -85,14 +85,23 @@ module.exports = (self) => {
}
}

if (typeof opts === 'function') {
callback = opts
opts = {}
}
self._libp2pNode.contentRouting.findProviders(key, opts, (err, res) => {
if (err) {
return callback(err)
}

opts = opts || {}
// convert to go-ipfs return value, we need to revisit
// this. For now will just conform.
const goResult = {
responses: res.map((peerInfo) => ({
id: peerInfo.id.toB58String(),
addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString())
})),
type: 4
}

self._libp2pNode.contentRouting.findProviders(key, opts.timeout || null, callback)
callback(null, goResult)
})
}),

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

// convert to go-ipfs return value, we need to revisit
// this. For now will just conform.
const goResult = [
{
Responses: [{
ID: info.id.toB58String(),
Addresses: info.multiaddrs.toArray().map((a) => a.toString())
}]
}
]
const goResult = {
responses: [{
id: info.id.toB58String(),
addrs: info.multiaddrs.toArray().map((a) => a.toString())
}],
type: 2
}

callback(null, goResult)
})
Expand Down Expand Up @@ -178,7 +186,11 @@ module.exports = (self) => {
*/
query: promisify((peerId, callback) => {
if (typeof peerId === 'string') {
peerId = PeerId.createFromB58String(peerId)
try {
peerId = PeerId.createFromB58String(peerId)
} catch (err) {
callback(err)
}
}

// TODO expose this method in peerRouting
Expand Down
Loading