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

Commit c4bea6f

Browse files
vasco-santosAlan Shaw
authored and
Alan Shaw
committed
fix: update dht responses (#389)
1 parent b4bc70b commit c4bea6f

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

Diff for: SPEC/DHT.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

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

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

20-
`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)
20+
`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.
2121

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

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

29-
ipfs.dht.findpeer(id, function (err, peerInfo) {
29+
ipfs.dht.findpeer(id, function (err, res) {
3030
// peerInfo will contain the multiaddrs of that peer
31+
const id = res.responses[0].id
32+
const addrs = res.responses[0].addrs
3133
})
3234
```
3335

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

49-
`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)
51+
`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.
5052

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

5355
**Example:**
5456

5557
```JavaScript
56-
ipfs.dht.findprovs(multihash, function (err, peerInfos) {})
58+
ipfs.dht.findprovs(multihash, function (err, res) {})
5759

58-
ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, peerInfos) {})
60+
ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, res) {})
5961
```
6062

6163
A great source of [examples][] can be found in the tests for this API.

Diff for: js/src/dht/findpeer.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ module.exports = (createCommon, options) => {
4242
})
4343

4444
it('should find other peers', (done) => {
45-
nodeA.dht.findpeer(nodeB.peerId.id, (err, peer) => {
45+
nodeA.dht.findpeer(nodeB.peerId.id, (err, res) => {
4646
expect(err).to.not.exist()
47-
// TODO upgrade the answer, format is weird
48-
expect(peer[0].Responses[0].ID).to.be.equal(nodeB.peerId.id)
47+
expect(res.responses[0].id).to.be.eql(nodeB.peerId.id)
48+
expect(res.responses[0].addrs).to.deep.include(nodeB.peerId.addresses[0])
4949
done()
5050
})
5151
})

Diff for: js/src/dht/findprovs.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ module.exports = (createCommon, options) => {
5858

5959
waterfall([
6060
(cb) => nodeB.object.new('unixfs-dir', cb),
61-
(dagNode, cb) => {
62-
const cidV0 = new CID(dagNode.toJSON().multihash)
63-
nodeB.dht.provide(cidV0, (err) => cb(err, cidV0))
61+
(cid, cb) => {
62+
nodeB.dht.provide(cid, (err) => cb(err, cid))
6463
},
65-
(cidV0, cb) => nodeA.dht.findprovs(cidV0, cb),
64+
(cid, cb) => nodeA.dht.findprovs(cid, cb),
6665
(provs, cb) => {
67-
expect(provs.map((p) => p.id.toB58String()))
66+
expect(provs.responses.map((p) => p.id))
6867
.to.eql([nodeB.peerId.id])
6968
cb()
7069
}

0 commit comments

Comments
 (0)