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

Commit

Permalink
fix: dht responses
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 10, 2018
1 parent 81a5798 commit 19a0c6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
20 changes: 10 additions & 10 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, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` is an array of type `[PeerInfo]`. In this case, as we are looking for a particular peer, there will be only one entry. This entry is composed by the peerId, as well as its adresses.

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) {
// peerInfo will contain the multiaddrs of that peer
const id = res.responses[0].id
const addrs = res.responses[0].addrs
ipfs.dht.findPeer(id, function (err, peerInfos) {
// peerInfos will contain the multiaddrs of that peer in the first entry of the array
const id = peerInfos[0].id
const addrs = peerInfos[0].multiaddrs
})
```

Expand All @@ -41,23 +41,23 @@ 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

`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) {})
```

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[0].id.toB58String()
const addrs = res[0].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
8 changes: 4 additions & 4 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 @@ -76,7 +76,7 @@ module.exports = (createCommon, options) => {
}
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

0 comments on commit 19a0c6e

Please sign in to comment.