Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: response for findpeer and findprovs (#1039)
Browse files Browse the repository at this point in the history
* fix: response for findpeer and findprovs

Pick out the correct item from the response, do not assume the first is the one we want.

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* fix: allow CID instance to be passed

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* docs: add comments for magic Type numbers

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* fix: reinstate not found error

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* fix: module name

* fix: add skip for dht.findprovs timeout test

Go IPFS does not implement this option and the error that was being checked for was a false positive - it was an error to report no providers were found, which is not an error just a fact and this PR fixes this by removing that error and thus causing this test to now fail.

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Jul 10, 2019
1 parent 1a8bcdd commit 5252f50
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
13 changes: 7 additions & 6 deletions src/dht/findpeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ module.exports = (send) => {
const handleResult = (res, callback) => {
// Inconsistent return values in the browser
if (Array.isArray(res)) {
res = res[0]
res = res.find(r => r.Type === 2)
}

// Type 2 keys
if (res.Type !== 2) {
const errMsg = `key was not found (type 2)`

return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
// 2 = FinalPeer
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
if (!res || res.Type !== 2) {
const errMsg = `key was not found (type 4)`
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
}

const responseReceived = res.Responses[0]
Expand All @@ -49,7 +50,7 @@ module.exports = (send) => {

send({
path: 'dht/findpeer',
args: peerId,
args: peerId.toString(),
qs: opts
}, (err, result) => {
if (err) {
Expand Down
19 changes: 6 additions & 13 deletions src/dht/findprovs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const streamToValueWithTransformer = require('../utils/stream-to-value-with-tran
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const errcode = require('err-code')

module.exports = (send) => {
return promisify((cid, opts, callback) => {
Expand All @@ -25,20 +24,14 @@ module.exports = (send) => {
const handleResult = (res, callback) => {
// Inconsistent return values in the browser vs node
if (Array.isArray(res)) {
res = res[0]
res = res.find(r => r.Type === 4)
}

// callback with an empty array if no providers are found
if (!res) {
const responses = []
return callback(null, responses)
}

// Type 4 keys
if (res.Type !== 4) {
const errMsg = `key was not found (type 4)`

return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
// 4 = Provider
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L20
if (!res || res.Type !== 4) {
return callback(null, [])
}

const responses = res.Responses.map((r) => {
Expand All @@ -60,7 +53,7 @@ module.exports = (send) => {

send({
path: 'dht/findprovs',
args: cid,
args: cid.toString(),
qs: opts
}, (err, result) => {
if (err) {
Expand Down
4 changes: 4 additions & 0 deletions test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ describe('interface-ipfs-core tests', () => {
name: 'should provide from one node and find it through another node',
reason: 'FIXME go-ipfs endpoint doesn\'t conform with the others https://github.com/ipfs/go-ipfs/issues/5047'
},
{
name: 'should take options to override timeout config',
reason: 'FIXME go-ipfs does not support a timeout option'
},
// dht.get
{
name: 'should get a value after it was put on another node',
Expand Down

0 comments on commit 5252f50

Please sign in to comment.