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

Commit 4a48726

Browse files
committed
fix: dht responses
1 parent 81a5798 commit 4a48726

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

SPEC/DHT.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
1414
##### Go **WIP**
1515

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

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

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

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

@@ -26,10 +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, res) {
30-
// peerInfo will contain the multiaddrs of that peer
31-
const id = res.responses[0].id
32-
const addrs = res.responses[0].addrs
29+
ipfs.dht.findPeer(id, function (err, peerInfos) {
30+
// peerInfos will contain the multiaddrs of that peer in the first entry of the array
31+
const id = peerInfos[0].id
32+
const addrs = peerInfos[0].multiaddrs
3333
})
3434
```
3535

@@ -41,23 +41,23 @@ A great source of [examples][] can be found in the tests for this API.
4141
4242
##### Go **WIP**
4343

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

4646
Where `hash` is a multihash.
4747

4848
`options` an optional object with the following properties
4949
- `timeout` - a maximum timeout in milliseconds
5050

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.
51+
`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.
5252

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

5555
**Example:**
5656

5757
```JavaScript
58-
ipfs.dht.findprovs(multihash, function (err, res) {})
58+
ipfs.dht.findProvs(multihash, function (err, res) {})
5959

60-
ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, res) {})
60+
ipfs.dht.findProvs(multihash, { timeout: 4000 }, function (err, res) {})
6161
```
6262

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

js/src/dht/findpeer.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const { spawnNodesWithId } = require('../utils/spawn')
55
const { getDescribe, getIt, expect } = require('../utils/mocha')
66
const { connect } = require('../utils/swarm')
77

8+
const checkAll = (bits) => string => bits.every(bit => string.includes(bit))
9+
810
module.exports = (createCommon, options) => {
911
const describe = getDescribe(options)
1012
const it = getIt(options)
1113
const common = createCommon()
1214

13-
describe('.dht.findpeer', function () {
15+
describe('.dht.findPeer', function () {
1416
this.timeout(80 * 1000)
1517

1618
let nodeA
@@ -42,16 +44,20 @@ module.exports = (createCommon, options) => {
4244
})
4345

4446
it('should find other peers', (done) => {
45-
nodeA.dht.findpeer(nodeB.peerId.id, (err, res) => {
47+
nodeA.dht.findPeer(nodeB.peerId.id, (err, res) => {
4648
expect(err).to.not.exist()
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])
49+
50+
const id = res[0].id.toB58String()
51+
const addrs = res[0].multiaddrs.toArray().map((ma) => ma.toString())
52+
53+
expect(id).to.be.eql(nodeB.peerId.id)
54+
expect(nodeB.peerId.addresses[0]).to.satisfy(checkAll([addrs[0]]))
4955
done()
5056
})
5157
})
5258

5359
it('should fail to find other peer if peer does not exist', (done) => {
54-
nodeA.dht.findpeer('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ', (err, peer) => {
60+
nodeA.dht.findPeer('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ', (err, peer) => {
5561
expect(err).to.exist()
5662
expect(peer).to.not.exist()
5763
done()

js/src/dht/findprovs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = (createCommon, options) => {
2424
const it = getIt(options)
2525
const common = createCommon()
2626

27-
describe('.dht.findprovs', function () {
27+
describe('.dht.findProvs', function () {
2828
let nodeA
2929
let nodeB
3030

@@ -61,7 +61,7 @@ module.exports = (createCommon, options) => {
6161
(cid, cb) => {
6262
nodeB.dht.provide(cid, (err) => cb(err, cid))
6363
},
64-
(cid, cb) => nodeA.dht.findprovs(cid, cb),
64+
(cid, cb) => nodeA.dht.findProvs(cid, cb),
6565
(provs, cb) => {
6666
expect(provs.responses.map((p) => p.id))
6767
.to.eql([nodeB.peerId.id])
@@ -76,7 +76,7 @@ module.exports = (createCommon, options) => {
7676
}
7777
waterfall([
7878
(cb) => fakeCid(cb),
79-
(cidV0, cb) => nodeA.dht.findprovs(cidV0, options, (err) => {
79+
(cidV0, cb) => nodeA.dht.findProvs(cidV0, options, (err) => {
8080
expect(err).to.exist()
8181
cb(null)
8282
})

0 commit comments

Comments
 (0)