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

Commit

Permalink
fix: update dht responses (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored and Alan Shaw committed Dec 7, 2018
1 parent b4bc70b commit c4bea6f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
14 changes: 8 additions & 6 deletions SPEC/DHT.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

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

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

`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](https://github.com/libp2p/js-peer-info)
`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.

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

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

ipfs.dht.findpeer(id, function (err, peerInfo) {
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
})
```

Expand All @@ -46,16 +48,16 @@ Where `hash` is a multihash.
`options` an optional object with the following properties
- `timeout` - a maximum timeout in milliseconds

`callback` must follow `function (err, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` is an array of objects of type [PeerInfo](https://github.com/libp2p/js-peer-info)
`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.

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

**Example:**

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

ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, peerInfos) {})
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
6 changes: 3 additions & 3 deletions js/src/dht/findpeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ module.exports = (createCommon, options) => {
})

it('should find other peers', (done) => {
nodeA.dht.findpeer(nodeB.peerId.id, (err, peer) => {
nodeA.dht.findpeer(nodeB.peerId.id, (err, res) => {
expect(err).to.not.exist()
// TODO upgrade the answer, format is weird
expect(peer[0].Responses[0].ID).to.be.equal(nodeB.peerId.id)
expect(res.responses[0].id).to.be.eql(nodeB.peerId.id)
expect(res.responses[0].addrs).to.deep.include(nodeB.peerId.addresses[0])
done()
})
})
Expand Down
9 changes: 4 additions & 5 deletions js/src/dht/findprovs.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ module.exports = (createCommon, options) => {

waterfall([
(cb) => nodeB.object.new('unixfs-dir', cb),
(dagNode, cb) => {
const cidV0 = new CID(dagNode.toJSON().multihash)
nodeB.dht.provide(cidV0, (err) => cb(err, cidV0))
(cid, cb) => {
nodeB.dht.provide(cid, (err) => cb(err, cid))
},
(cidV0, cb) => nodeA.dht.findprovs(cidV0, cb),
(cid, cb) => nodeA.dht.findprovs(cid, cb),
(provs, cb) => {
expect(provs.map((p) => p.id.toB58String()))
expect(provs.responses.map((p) => p.id))
.to.eql([nodeB.peerId.id])
cb()
}
Expand Down

0 comments on commit c4bea6f

Please sign in to comment.