From 5252f506ff405891c6c8619fd046cbde6490e56c Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 10 Jul 2019 13:52:32 +0100 Subject: [PATCH] fix: response for findpeer and findprovs (#1039) * 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 * fix: allow CID instance to be passed License: MIT Signed-off-by: Alan Shaw * docs: add comments for magic Type numbers License: MIT Signed-off-by: Alan Shaw * fix: reinstate not found error License: MIT Signed-off-by: Alan Shaw * 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 --- src/dht/findpeer.js | 13 +++++++------ src/dht/findprovs.js | 19 ++++++------------- test/interface.spec.js | 4 ++++ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/dht/findpeer.js b/src/dht/findpeer.js index 98611468c..046664bda 100644 --- a/src/dht/findpeer.js +++ b/src/dht/findpeer.js @@ -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] @@ -49,7 +50,7 @@ module.exports = (send) => { send({ path: 'dht/findpeer', - args: peerId, + args: peerId.toString(), qs: opts }, (err, result) => { if (err) { diff --git a/src/dht/findprovs.js b/src/dht/findprovs.js index 84c1df8f9..23be84068 100644 --- a/src/dht/findprovs.js +++ b/src/dht/findprovs.js @@ -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) => { @@ -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) => { @@ -60,7 +53,7 @@ module.exports = (send) => { send({ path: 'dht/findprovs', - args: cid, + args: cid.toString(), qs: opts }, (err, result) => { if (err) { diff --git a/test/interface.spec.js b/test/interface.spec.js index abc57e962..adc928289 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -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',