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

Commit

Permalink
fix: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Feb 6, 2019
1 parent e859a81 commit 9a4083e
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 252 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"form-data": "^2.3.3",
"hat": "0.0.3",
"interface-ipfs-core": "~0.96.0",
"ipfsd-ctl": "~0.40.2",
"ipfsd-ctl": "~0.41.0",
"ncp": "^2.0.0",
"qs": "^6.5.2",
"rimraf": "^2.6.2",
Expand Down Expand Up @@ -128,7 +128,7 @@
"joi": "^14.3.0",
"joi-browser": "^13.4.0",
"joi-multiaddr": "^4.0.0",
"libp2p": "~0.24.3",
"libp2p": "libp2p/js-libp2p#master",
"libp2p-bootstrap": "~0.9.3",
"libp2p-crypto": "~0.16.0",
"libp2p-kad-dht": "~0.14.4",
Expand Down
4 changes: 0 additions & 4 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ module.exports = {
type: 'boolean',
default: false
})
.option('enable-dht-experiment', {
type: 'boolean',
default: false
})
.option('offline', {
desc: 'Run offline. Do not connect to the rest of the network but provide local API.',
default: false
Expand Down
4 changes: 3 additions & 1 deletion src/cli/commands/dht/find-peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module.exports = {
const peers = await ipfs.dht.findPeer(peerID)
const addresses = peers.multiaddrs.toArray().map((ma) => ma.toString())

print(addresses)
addresses.forEach((addr) => {
print(addr)
})
})())
}
}
8 changes: 5 additions & 3 deletions src/cli/commands/dht/provide.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ module.exports = {
}
},

handler ({ ipfs, key, resolve }) {
// TODO add recursive option
handler ({ ipfs, key, recursive, resolve }) {
const opts = {
recursive
}

resolve((async () => {
await ipfs.dht.provide(key)
await ipfs.dht.provide(key, opts)
})())
}
}
9 changes: 5 additions & 4 deletions src/core/components/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const CID = require('cids')
const each = require('async/each')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')

const errcode = require('err-code')

const debug = require('debug')
Expand Down Expand Up @@ -38,7 +39,7 @@ module.exports = (self) => {
} catch (err) {
log.error(err)

return setImmediate(() => callback(errcode(err, 'ERR_INVALID_CID')))
return nextTick(() => callback(errcode(err, 'ERR_INVALID_CID')))
}
}

Expand All @@ -64,7 +65,7 @@ module.exports = (self) => {
} catch (err) {
log.error(err)

return setImmediate(() => callback(errcode(err, 'ERR_INVALID_CID')))
return nextTick(() => callback(errcode(err, 'ERR_INVALID_CID')))
}
}

Expand Down Expand Up @@ -95,7 +96,7 @@ module.exports = (self) => {
} catch (err) {
log.error(err)

return setImmediate(() => callback(errcode(err, 'ERR_INVALID_CID')))
return nextTick(() => callback(errcode(err, 'ERR_INVALID_CID')))
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
},
dht: {
kBucketSize: get(options, 'dht.kBucketSize', 20),
enabledDiscovery: get(options, 'dht.enabledDiscovery', true),
enabled: get(options, 'dht.enabled', true) && !(get(options, 'offline', false)),
randomWalk: {
enabled: get(options, 'dht.randomWalk.enabled', true)
},
validators: {
ipns: ipnsUtils.validator
},
Expand All @@ -86,7 +89,6 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
}
},
EXPERIMENTAL: {
dht: !(get(options, 'local', false)),
pubsub: get(options, 'EXPERIMENTAL.pubsub', false)
}
},
Expand Down
4 changes: 1 addition & 3 deletions src/core/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ class Node extends libp2p {
}
},
dht: {
kBucketSize: 20,
enabledDiscovery: true
enabled: false
},
EXPERIMENTAL: {
dht: true,
pubsub: false
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/core/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ class Node extends libp2p {
},
dht: {
kBucketSize: 20,
enabledDiscovery: true
enabled: true,
randomWalk: {
enabled: true
}
},
EXPERIMENTAL: {
dht: true,
pubsub: false
}
}
Expand Down
150 changes: 47 additions & 103 deletions src/http/api/resources/dht.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const Joi = require('joi')
const Boom = require('boom')

const CID = require('cids')

Expand All @@ -16,34 +17,27 @@ exports.findPeer = {
arg: Joi.string().required()
}).unknown()
},
handler: (request, reply) => {
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query

ipfs.dht.findPeer(arg, (err, res) => {
if (err) {
log.error(err)

if (err.code === 'ERR_LOOKUP_FAILED') {
return reply({
Message: err.toString(),
Code: 0
}).code(404)
}

return reply({
Message: err.toString(),
Code: 0
}).code(500)
let res

try {
res = await ipfs.dht.findPeer(arg)
} catch (err) {
if (err.code === 'ERR_LOOKUP_FAILED') {
throw Boom.notFound(err.toString())
} else {
throw Boom.boomify(err, { message: err.toString() })
}
}

reply({
Responses: [{
ID: res.id.toB58String(),
Addrs: res.multiaddrs.toArray().map((a) => a.toString())
}],
Type: 2
})
return h.response({
Responses: [{
ID: res.id.toB58String(),
Addrs: res.multiaddrs.toArray().map((a) => a.toString())
}],
Type: 2
})
}
}
Expand All @@ -56,29 +50,20 @@ exports.findProvs = {
timeout: Joi.number()
}).unknown()
},
handler: (request, reply) => {
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query

request.query.maxNumProviders = request.query['num-providers']

ipfs.dht.findProvs(arg, request.query, (err, res) => {
if (err) {
log.error(err)

return reply({
Message: err.toString(),
Code: 0
}).code(500)
}
const res = await ipfs.dht.findProvs(arg, request.query)

reply({
Responses: res.map((peerInfo) => ({
ID: peerInfo.id.toB58String(),
Addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString())
})),
Type: 4
})
return h.response({
Responses: res.map((peerInfo) => ({
ID: peerInfo.id.toB58String(),
Addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString())
})),
Type: 4
})
}
}
Expand All @@ -90,24 +75,15 @@ exports.get = {
timeout: Joi.number()
}).unknown()
},
handler: (request, reply) => {
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query

ipfs.dht.get(Buffer.from(arg), (err, res) => {
if (err) {
log.error(err)
const res = await ipfs.dht.get(Buffer.from(arg))

return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

reply({
Extra: res.toString(),
Type: 5
})
return h.response({
Extra: res.toString(),
Type: 5
})
}
}
Expand All @@ -118,7 +94,7 @@ exports.provide = {
arg: Joi.string().required()
}).unknown()
},
handler: (request, reply) => {
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query
let cid
Expand All @@ -127,25 +103,12 @@ exports.provide = {
cid = new CID(arg)
} catch (err) {
log.error(err)

return reply({
Message: err.toString(),
Code: 0
}).code(500)
throw Boom.boomify(err, { message: err.toString() })
}

ipfs.dht.provide(cid, request.query, (err) => {
if (err) {
log.error(err)

return reply({
Message: err.toString(),
Code: 0
}).code(500)
}
await ipfs.dht.provide(cid)

reply({})
})
return h.response()
}
}

Expand All @@ -155,29 +118,20 @@ exports.put = {
arg: Joi.array().items(Joi.string()).length(2).required()
}).unknown()
},
parseArgs: (request, reply) => {
return reply({
parseArgs: (request, h) => {
return {
key: request.query.arg[0],
value: request.query.arg[1]
})
}
},
handler: (request, reply) => {
async handler (request, h) {
const key = request.pre.args.key
const value = request.pre.args.value
const ipfs = request.server.app.ipfs

ipfs.dht.put(Buffer.from(key), Buffer.from(value), (err) => {
if (err) {
log.error(err)

return reply({
Message: err.toString(),
Code: 0
}).code(500)
}
await ipfs.dht.put(Buffer.from(key), Buffer.from(value))

reply({})
})
return h.response()
}
}

Expand All @@ -187,25 +141,15 @@ exports.query = {
arg: Joi.string().required()
}).unknown()
},
handler: (request, reply) => {
async handler (request, h) {
const ipfs = request.server.app.ipfs
const { arg } = request.query

ipfs.dht.query(arg, (err, res) => {
if (err) {
log.error(err)

return reply({
Message: err.toString(),
Code: 0
}).code(500)
}
const res = await ipfs.dht.query(arg)
const response = res.map((peerInfo) => ({
ID: peerInfo.id.toB58String()
}))

const response = res.map((peerInfo) => ({
ID: peerInfo.id.toB58String()
}))

reply(response)
})
return h.response(response)
}
}
7 changes: 5 additions & 2 deletions src/http/api/routes/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ module.exports = [
method: '*',
path: '/api/v0/dht/findprovs',
options: {
validate: resources.dht.findprovs.validate
validate: resources.dht.findProvs.validate
},
handler: resources.dht.findprovs.handler
handler: resources.dht.findProvs.handler
},
{
method: '*',
Expand All @@ -39,6 +39,9 @@ module.exports = [
method: '*',
path: '/api/v0/dht/put',
options: {
pre: [
{ method: resources.dht.put.parseArgs, assign: 'args' }
],
validate: resources.dht.put.validate
},
handler: resources.dht.put.handler
Expand Down
Loading

0 comments on commit 9a4083e

Please sign in to comment.