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

fix: dht responses #414

Merged
merged 2 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions SPEC/DHT.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

##### Go **WIP**

##### JavaScript - `ipfs.dht.findpeer(peerId, [callback])`
##### JavaScript - `ipfs.dht.findPeer(peerId, [callback])`

Where `peerId` is a IPFS/libp2p Id from [PeerId](https://github.com/libp2p/js-peer-id) type.

`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` is an object containing `responses` as an array of peer responses. In this case, as we are looking for a particular peer, there will be only one response. This response is composed by the peerId, as well as an array with its adresses.
`callback` must follow `function (err, peerInfo) {}` signature, where `err` is an error if the operation was not successful. `peerInfo` is an object of type `PeerInfo`.

If no `callback` is passed, a promise is returned.

Expand All @@ -26,10 +26,10 @@ If no `callback` is passed, a promise is returned.
```JavaScript
var id = PeerId.create()

ipfs.dht.findpeer(id, function (err, res) {
ipfs.dht.findPeer(id, function (err, peerInfo) {
// peerInfo will contain the multiaddrs of that peer
const id = res.responses[0].id
const addrs = res.responses[0].addrs
const id = peerInfo.id
const addrs = peerInfo.multiaddrs
})
```

Expand All @@ -41,23 +41,24 @@ A great source of [examples][] can be found in the tests for this API.

##### Go **WIP**

##### JavaScript - `ipfs.dht.findprovs(hash, [options], [callback])`
##### JavaScript - `ipfs.dht.findProvs(hash, [options], [callback])`

Where `hash` is a multihash.

`options` an optional object with the following properties
- `timeout` - a maximum timeout in milliseconds
- `maxNumProviders` - a maximum number of providers to find

`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` is an object containing `responses` as an array of peer responses. Each entry of this array is composed by the peerId, as well as an array with its adresses.
`callback` must follow `function (err, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` is an array of type `[PeerInfo]`. Each entry of this array is composed by the peerId, as well as an array with its adresses.

If no `callback` is passed, a promise is returned.

**Example:**

```JavaScript
ipfs.dht.findprovs(multihash, function (err, res) {})
ipfs.dht.findProvs(multihash, function (err, res) {})

ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, res) {})
ipfs.dht.findProvs(multihash, { timeout: 4000 }, function (err, res) {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout => maxTimeout? ...or just stick with timeout ;)

```

A great source of [examples][] can be found in the tests for this API.
Expand Down
16 changes: 11 additions & 5 deletions js/src/dht/findpeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const { spawnNodesWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { connect } = require('../utils/swarm')

const checkAll = (bits) => string => bits.every(bit => string.includes(bit))

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.dht.findpeer', function () {
describe('.dht.findPeer', function () {
this.timeout(80 * 1000)

let nodeA
Expand Down Expand Up @@ -42,16 +44,20 @@ module.exports = (createCommon, options) => {
})

it('should find other peers', (done) => {
nodeA.dht.findpeer(nodeB.peerId.id, (err, res) => {
nodeA.dht.findPeer(nodeB.peerId.id, (err, res) => {
expect(err).to.not.exist()
expect(res.responses[0].id).to.be.eql(nodeB.peerId.id)
expect(res.responses[0].addrs).to.deep.include(nodeB.peerId.addresses[0])

const id = res.id.toB58String()
const addrs = res.multiaddrs.toArray().map((ma) => ma.toString())

expect(id).to.be.eql(nodeB.peerId.id)
expect(nodeB.peerId.addresses[0]).to.satisfy(checkAll([addrs[0]]))
done()
})
})

it('should fail to find other peer if peer does not exist', (done) => {
nodeA.dht.findpeer('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ', (err, peer) => {
nodeA.dht.findPeer('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ', (err, peer) => {
expect(err).to.exist()
expect(peer).to.not.exist()
done()
Expand Down
10 changes: 5 additions & 5 deletions js/src/dht/findprovs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = (createCommon, options) => {
const it = getIt(options)
const common = createCommon()

describe('.dht.findprovs', function () {
describe('.dht.findProvs', function () {
let nodeA
let nodeB

Expand Down Expand Up @@ -61,9 +61,9 @@ module.exports = (createCommon, options) => {
(cid, cb) => {
nodeB.dht.provide(cid, (err) => cb(err, cid))
},
(cid, cb) => nodeA.dht.findprovs(cid, cb),
(cid, cb) => nodeA.dht.findProvs(cid, cb),
(provs, cb) => {
expect(provs.responses.map((p) => p.id))
expect(provs.map((p) => p.id.toB58String()))
.to.eql([nodeB.peerId.id])
cb()
}
Expand All @@ -72,11 +72,11 @@ module.exports = (createCommon, options) => {

it('should take options to override timeout config', function (done) {
const options = {
timeout: 1
maxTimeout: 1
}
waterfall([
(cb) => fakeCid(cb),
(cidV0, cb) => nodeA.dht.findprovs(cidV0, options, (err) => {
(cidV0, cb) => nodeA.dht.findProvs(cidV0, options, (err) => {
expect(err).to.exist()
cb(null)
})
Expand Down
2 changes: 1 addition & 1 deletion js/src/dht/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = (createCommon, options) => {
if (skipped) return
clearTimeout(timeoutId)
expect(err).to.not.exist()
expect(peers.map((p) => p.ID)).to.include(nodeB.peerId.id)
expect(peers.map((p) => p.id.toB58String())).to.include(nodeB.peerId.id)
done()
})
})
Expand Down