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

Commit 50b614a

Browse files
authored
Merge pull request #36 from ipfs/dht-api
DHT API
2 parents 055f6c1 + 143333b commit 50b614a

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

API/dht/README.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
DHT API
2+
=======
3+
4+
#### `findpeer`
5+
6+
> Retrieve the Peer Info of a reachable node in the network.
7+
8+
##### `Go` **WIP**
9+
10+
##### `JavaScript` - ipfs.dht.findpeer(peerId, [callback])
11+
12+
Where `peerId` is a IPFS/libp2p Id of type [PeerId](https://github.com/libp2p/js-peer-id).
13+
14+
`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)
15+
16+
If no `callback` is passed, a promise is returned.
17+
18+
Example:
19+
20+
```JavaScript
21+
var id = PeerId.create()
22+
ipfs.dht.findPeer(id, function (err, peerInfo) {
23+
// peerInfo will contain the multiaddrs of that peer
24+
})
25+
```
26+
27+
#### `findprovs`
28+
29+
> Retrieve the providers for content that is addressed by an hash.
30+
31+
##### `Go` **WIP**
32+
33+
##### `JavaScript` - ipfs.dht.findprovs(hash, [callback])
34+
35+
Where `hash` is a multihash.
36+
37+
`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)
38+
39+
If no `callback` is passed, a promise is returned.
40+
41+
Example:
42+
43+
```JavaScript
44+
ipfs.dht.findProvs(hash, function (err, peerInfos) {})
45+
```
46+
47+
#### `get`
48+
49+
> Retrieve a value from DHT
50+
51+
##### `Go` **WIP**
52+
53+
##### `JavaScript` - ipfs.dht.get(key, [callback])
54+
55+
Where `key` is a string.
56+
57+
`callback` must follow `function (err, value) {}` signature, where `err` is an error if the operation was not successful. `value` is the value that was stored under that key.
58+
59+
If no `callback` is passed, a promise is returned.
60+
61+
Example:
62+
63+
```JavaScript
64+
ipfs.dht.get(key, function (err, value) {})
65+
```
66+
67+
#### `put`
68+
69+
> Store a value on the DHT
70+
71+
##### `Go` **WIP**
72+
73+
##### `JavaScript` - ipfs.dht.put(key, value, [callback])
74+
75+
Where `key` is a string and `value` can be of any type.
76+
77+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
78+
79+
If no `callback` is passed, a promise is returned.
80+
81+
Example:
82+
83+
```JavaScript
84+
ipfs.dht.put(key, value, function (err) {})
85+
```
86+
87+
88+
#### `query`
89+
90+
> Queries the network for the 'closest peers' to a given key. 'closest' is defined by the rules of the underlying Peer Routing mechanism.
91+
92+
##### `Go` **WIP**
93+
94+
##### `JavaScript` - ipfs.dht.query(peerId, [callback])
95+
96+
Where `peerId` is a IPFS/libp2p Id of type [PeerId](https://github.com/libp2p/js-peer-id).
97+
98+
`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)
99+
100+
If no `callback` is passed, a promise is returned.
101+
102+
Example:
103+
104+
```JavaScript
105+
var id = PeerId.create()
106+
ipfs.dht.query(id, function (err, peerInfos) {
107+
})
108+
```

src/dht.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
6+
module.exports = (common) => {
7+
describe('.dht', () => {
8+
let ipfs
9+
let peers
10+
11+
before((done) => {
12+
common.setup((err, factory) => {
13+
expect(err).to.not.exists
14+
factory.spawnNode((err, node) => {
15+
expect(err).to.not.exist
16+
ipfs = node
17+
done()
18+
})
19+
})
20+
})
21+
22+
after((done) => {
23+
common.teardown(done)
24+
})
25+
26+
describe('callback API', () => {
27+
it('.get errors when getting a non-existent key from the DHT', (done) => {
28+
ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => {
29+
expect(err).to.be.an.instanceof(Error)
30+
done()
31+
})
32+
})
33+
it('.findprovs', (done) => {
34+
ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
35+
expect(err).to.not.exist
36+
37+
expect(res).to.be.an('array')
38+
done()
39+
})
40+
})
41+
})
42+
describe('promise API', () => {
43+
it('.get errors when getting a non-existent key from the DHT', (done) => {
44+
ipfs.dht.get('non-existing', {timeout: '100ms'}).catch((err) => {
45+
expect(err).to.be.an.instanceof(Error)
46+
done()
47+
})
48+
})
49+
it('.findprovs', (done) => {
50+
ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP').then((res) => {
51+
expect(res).to.be.an('array')
52+
done()
53+
}).catch(done)
54+
})
55+
})
56+
// Tests below are tests that haven't been implemented yet or is not
57+
// passing currently
58+
xdescribe('.findpeer', () => {
59+
it('finds other peers', (done) => {
60+
peers.a.ipfs.dht.findpeer(peers.b.peerID, (err, foundPeer) => {
61+
expect(err).to.be.empty
62+
expect(foundPeer.peerID).to.be.equal(peers.b.peerID)
63+
done()
64+
})
65+
})
66+
it('fails to find other peer, if peer doesnt exists', (done) => {
67+
peers.a.ipfs.dht.findpeer('ARandomPeerID', (err, foundPeer) => {
68+
expect(err).to.be.instanceof(Error)
69+
expect(foundPeer).to.be.equal(null)
70+
done()
71+
})
72+
})
73+
})
74+
xit('.put and .get a key value pair in the DHT', (done) => {
75+
peers.a.ipfs.dht.put('scope', 'interplanetary', (err, res) => {
76+
expect(err).to.not.exist
77+
78+
expect(res).to.be.an('array')
79+
80+
// bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234
81+
peers.b.ipfs.dht.get('scope', (err, value) => {
82+
expect(err).to.not.exist
83+
expect(value).to.be.equal('interplanetary')
84+
done()
85+
})
86+
})
87+
})
88+
xdescribe('.query', () => {})
89+
})
90+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ exports.pin = require('./pin')
77
exports.generic = require('./generic')
88
exports.swarm = require('./swarm')
99
exports.block = require('./block')
10+
exports.dht = require('./dht')

0 commit comments

Comments
 (0)