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

Commit 3500961

Browse files
vasco-santosAlan Shaw
authored and
Alan Shaw
committed
refactor: dht api changes (#414)
1 parent 81a5798 commit 3500961

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

SPEC/DHT.md

Lines changed: 10 additions & 9 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, peerInfo) {}` signature, where `err` is an error if the operation was not successful. `peerInfo` is an object of type `PeerInfo`.
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) {
29+
ipfs.dht.findPeer(id, function (err, peerInfo) {
3030
// peerInfo will contain the multiaddrs of that peer
31-
const id = res.responses[0].id
32-
const addrs = res.responses[0].addrs
31+
const id = peerInfo.id
32+
const addrs = peerInfo.multiaddrs
3333
})
3434
```
3535

@@ -41,23 +41,24 @@ 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
50+
- `maxNumProviders` - a maximum number of providers to find
5051

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.
52+
`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.
5253

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

5556
**Example:**
5657

5758
```JavaScript
58-
ipfs.dht.findprovs(multihash, function (err, res) {})
59+
ipfs.dht.findProvs(multihash, function (err, res) {})
5960

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

6364
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.id.toB58String()
51+
const addrs = res.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: 5 additions & 5 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,9 +61,9 @@ 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) => {
66-
expect(provs.responses.map((p) => p.id))
66+
expect(provs.map((p) => p.id.toB58String()))
6767
.to.eql([nodeB.peerId.id])
6868
cb()
6969
}
@@ -72,11 +72,11 @@ module.exports = (createCommon, options) => {
7272

7373
it('should take options to override timeout config', function (done) {
7474
const options = {
75-
timeout: 1
75+
maxTimeout: 1
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
})

js/src/dht/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports = (createCommon, options) => {
5959
if (skipped) return
6060
clearTimeout(timeoutId)
6161
expect(err).to.not.exist()
62-
expect(peers.map((p) => p.ID)).to.include(nodeB.peerId.id)
62+
expect(peers.map((p) => p.id.toB58String())).to.include(nodeB.peerId.id)
6363
done()
6464
})
6565
})

0 commit comments

Comments
 (0)