From 4870685288da9fbc0398d1e5aa06ce8977f565c9 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 28 Jun 2016 10:21:55 +0100 Subject: [PATCH 1/8] init DHT API --- API/dht/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 API/dht/README.md diff --git a/API/dht/README.md b/API/dht/README.md new file mode 100644 index 00000000..d36ad216 --- /dev/null +++ b/API/dht/README.md @@ -0,0 +1,3 @@ +DHT API +======= + From daf892d625c4cadf52360f57da6bd54c2c43aec2 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 9 Aug 2016 08:26:40 +0100 Subject: [PATCH 2/8] feat(dht): add dht.findpeer spec --- API/dht/README.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/API/dht/README.md b/API/dht/README.md index d36ad216..1853acdd 100644 --- a/API/dht/README.md +++ b/API/dht/README.md @@ -1,3 +1,83 @@ DHT API ======= +#### `findpeer` + +> Retrieve the Peer Info of a reachable node in the network. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.dht.findpeer(peerId, [callback]) + +Where `peerId` is a IPFS/libp2p Id of type [PeerId](https://github.com/libp2p/js-peer-id). + +`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) + +If no `callback` is passed, a promise is returned. + +Example: + +```JavaScript +var id = PeerId.create() +ipfs.dht.findPeer(id, function (err, peerInfo) { + // peerInfo will contain the multiaddrs of that peer +}) +``` + +#### `findprovs` + +> Retrieve the providers for content that is addressed by an hash. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.dht.findprovs(hash, [callback]) + +If no `callback` is passed, a promise is returned. + +Example: + + + +#### `get` + +> + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.dht.get(key, [callback]) + + +If no `callback` is passed, a promise is returned. + +Example: + + +#### `put` + +> + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.dht.put(key, value, [callback]) + + +If no `callback` is passed, a promise is returned. + +Example: + + +#### `query` + +> + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.dht.query(peerId, [callback]) + + + +If no `callback` is passed, a promise is returned. + +Example: + + From 9878a927e2de8401280ff6271371d81be95a1246 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 9 Aug 2016 08:28:21 +0100 Subject: [PATCH 3/8] feat(dht): add dht.findprovs spec --- API/dht/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/API/dht/README.md b/API/dht/README.md index 1853acdd..e716d1bb 100644 --- a/API/dht/README.md +++ b/API/dht/README.md @@ -32,11 +32,19 @@ ipfs.dht.findPeer(id, function (err, peerInfo) { ##### `JavaScript` - ipfs.dht.findprovs(hash, [callback]) +Where `hash` is a multihash. + +`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) + If no `callback` is passed, a promise is returned. Example: - +```JavaScript +ipfs.dht.findProvs(hash, function (err, peerInfos) { + // peerInfo will contain the multiaddrs of that peer +}) +``` #### `get` From 9ec30643526da8aa725cdc163cb9d6fb173c4c4a Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 9 Aug 2016 08:31:55 +0100 Subject: [PATCH 4/8] feat(dht): add dht.get spec --- API/dht/README.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/API/dht/README.md b/API/dht/README.md index e716d1bb..27b44e85 100644 --- a/API/dht/README.md +++ b/API/dht/README.md @@ -41,28 +41,32 @@ If no `callback` is passed, a promise is returned. Example: ```JavaScript -ipfs.dht.findProvs(hash, function (err, peerInfos) { - // peerInfo will contain the multiaddrs of that peer -}) +ipfs.dht.findProvs(hash, function (err, peerInfos) {}) ``` #### `get` -> +> Retrieve a value from DHT ##### `Go` **WIP** ##### `JavaScript` - ipfs.dht.get(key, [callback]) +Where `key` is a string. + +`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. If no `callback` is passed, a promise is returned. Example: +```JavaScript +ipfs.dht.get(key, function (err, value) {}) +``` #### `put` -> +> Store a value on the DHT ##### `Go` **WIP** From 841c2c3510402aba08fdf24d631be9a7abd5b0cd Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 9 Aug 2016 08:34:42 +0100 Subject: [PATCH 5/8] feat(dht): add dht.put + .query spec --- API/dht/README.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/API/dht/README.md b/API/dht/README.md index 27b44e85..afabcf9b 100644 --- a/API/dht/README.md +++ b/API/dht/README.md @@ -72,24 +72,37 @@ ipfs.dht.get(key, function (err, value) {}) ##### `JavaScript` - ipfs.dht.put(key, value, [callback]) +Where `key` is a string and `value` can be of any type. + +`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful. If no `callback` is passed, a promise is returned. Example: +```JavaScript +ipfs.dht.put(key, value, function (err) {}) +``` + #### `query` -> +> Queries the network for the 'closest peers' to a given key. 'closest' is defined by the rules of the underlying Peer Routing mechanism. ##### `Go` **WIP** ##### `JavaScript` - ipfs.dht.query(peerId, [callback]) +Where `peerId` is a IPFS/libp2p Id of type [PeerId](https://github.com/libp2p/js-peer-id). +`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) If no `callback` is passed, a promise is returned. Example: - +```JavaScript +var id = PeerId.create() +ipfs.dht.query(id, function (err, peerInfos) { +}) +``` From 3e5ed076fc7e656652755f47db131b57c64d464c Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Sat, 17 Sep 2016 17:49:13 +0200 Subject: [PATCH 6/8] Move tests from js-ipfs-api to interface-ipfs-core --- src/dht.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 1 + 2 files changed, 63 insertions(+) create mode 100644 src/dht.js diff --git a/src/dht.js b/src/dht.js new file mode 100644 index 00000000..0953d517 --- /dev/null +++ b/src/dht.js @@ -0,0 +1,62 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect + +module.exports = (common) => { + describe.only('.dht', () => { + let ipfs + + before((done) => { + common.setup((err, factory) => { + expect(err).to.not.exists + factory.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + }) + + after((done) => { + common.teardown(done) + }) + xdescribe('.findpeer', () => {}) + describe('.get', (done) => { + it('errors when getting a non-existent key from the DHT', (done) => { + ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => { + expect(err).to.be.an.instanceof(Error) + done() + }) + }) + // belongs in put or integration + it('puts and gets a key value pair in the DHT', (done) => { + ipfs.dht.put('scope', 'interplanetary', (err, res) => { + expect(err).to.not.exist + + expect(res).to.be.an('array') + + done() + // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 + // apiClients.a.dht.get('scope', (err, value) => { + // expect(err).to.not.exist + // expect(value).to.be.equal('interplanetary') + // done() + // }) + }) + }) + }) + xdescribe('.put', () => {}) + xdescribe('.query', () => {}) + describe('.findprovs', () => { + it('finds providers', (done) => { + ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { + expect(err).to.not.exist + + expect(res).to.be.an('array') + done() + }) + }) + }) + }) +} diff --git a/src/index.js b/src/index.js index ee35b234..33865f9b 100644 --- a/src/index.js +++ b/src/index.js @@ -7,3 +7,4 @@ exports.pin = require('./pin') exports.generic = require('./generic') exports.swarm = require('./swarm') exports.block = require('./block') +exports.dht = require('./dht') From 0feb3971217a8ae9f013493214f7332a3305009d Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Tue, 8 Nov 2016 17:36:00 +0100 Subject: [PATCH 7/8] Add promise/callback tests --- src/dht.js | 88 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 25 deletions(-) diff --git a/src/dht.js b/src/dht.js index 0953d517..23037b3c 100644 --- a/src/dht.js +++ b/src/dht.js @@ -4,8 +4,9 @@ const expect = require('chai').expect module.exports = (common) => { - describe.only('.dht', () => { + describe('.dht', () => { let ipfs + let peers before((done) => { common.setup((err, factory) => { @@ -21,42 +22,79 @@ module.exports = (common) => { after((done) => { common.teardown(done) }) - xdescribe('.findpeer', () => {}) - describe('.get', (done) => { - it('errors when getting a non-existent key from the DHT', (done) => { - ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => { - expect(err).to.be.an.instanceof(Error) - done() + + describe('callback API', () => { + describe('.get', (done) => { + it('errors when getting a non-existent key from the DHT', (done) => { + ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => { + expect(err).to.be.an.instanceof(Error) + done() + }) }) }) - // belongs in put or integration - it('puts and gets a key value pair in the DHT', (done) => { - ipfs.dht.put('scope', 'interplanetary', (err, res) => { - expect(err).to.not.exist - - expect(res).to.be.an('array') + describe('.findprovs', () => { + it('finds providers', (done) => { + ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { + expect(err).to.not.exist + expect(res).to.be.an('array') + done() + }) + }) + }) + }) + describe('promise API', () => { + describe('.get', (done) => { + it('errors when getting a non-existent key from the DHT', (done) => { + ipfs.dht.get('non-existing', {timeout: '100ms'}).catch((err) => { + expect(err).to.be.an.instanceof(Error) + done() + }) + }) + }) + describe('.findprovs', () => { + it('finds providers', (done) => { + ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP').then((res) => { + expect(res).to.be.an('array') + done() + }).catch(done) + }) + }) + }) + // Tests below are tests that haven't been implemented yet or is not + // passing currently + xdescribe('.findpeer', () => { + it('finds other peers', (done) => { + peers.a.ipfs.dht.findpeer(peers.b.peerID, (err, foundPeer) => { + expect(err).to.be.empty + expect(foundPeer.peerID).to.be.equal(peers.b.peerID) + done() + }) + }) + it('fails to find other peer, if peer doesnt exists', (done) => { + peers.a.ipfs.dht.findpeer('ARandomPeerID', (err, foundPeer) => { + expect(err).to.be.instanceof(Error) + expect(foundPeer).to.be.equal(null) done() - // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 - // apiClients.a.dht.get('scope', (err, value) => { - // expect(err).to.not.exist - // expect(value).to.be.equal('interplanetary') - // done() - // }) }) }) }) - xdescribe('.put', () => {}) - xdescribe('.query', () => {}) - describe('.findprovs', () => { - it('finds providers', (done) => { - ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { + xdescribe('.put & .get', () => { + it('puts and gets a key value pair in the DHT', (done) => { + peers.a.ipfs.dht.put('scope', 'interplanetary', (err, res) => { expect(err).to.not.exist expect(res).to.be.an('array') - done() + + // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 + peers.b.ipfs.dht.get('scope', (err, value) => { + expect(err).to.not.exist + expect(value).to.be.equal('interplanetary') + done() + }) }) }) }) + xdescribe('.query', () => {}) }) } From 143333b6468eb842defe1f8b64058e4e976664e8 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Tue, 8 Nov 2016 18:05:49 +0100 Subject: [PATCH 8/8] Remove one level of callback nesting --- src/dht.js | 64 +++++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/src/dht.js b/src/dht.js index 23037b3c..dd2dc7cc 100644 --- a/src/dht.js +++ b/src/dht.js @@ -24,41 +24,33 @@ module.exports = (common) => { }) describe('callback API', () => { - describe('.get', (done) => { - it('errors when getting a non-existent key from the DHT', (done) => { - ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => { - expect(err).to.be.an.instanceof(Error) - done() - }) + it('.get errors when getting a non-existent key from the DHT', (done) => { + ipfs.dht.get('non-existing', {timeout: '100ms'}, (err, value) => { + expect(err).to.be.an.instanceof(Error) + done() }) }) - describe('.findprovs', () => { - it('finds providers', (done) => { - ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { - expect(err).to.not.exist + it('.findprovs', (done) => { + ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { + expect(err).to.not.exist - expect(res).to.be.an('array') - done() - }) + expect(res).to.be.an('array') + done() }) }) }) describe('promise API', () => { - describe('.get', (done) => { - it('errors when getting a non-existent key from the DHT', (done) => { - ipfs.dht.get('non-existing', {timeout: '100ms'}).catch((err) => { - expect(err).to.be.an.instanceof(Error) - done() - }) + it('.get errors when getting a non-existent key from the DHT', (done) => { + ipfs.dht.get('non-existing', {timeout: '100ms'}).catch((err) => { + expect(err).to.be.an.instanceof(Error) + done() }) }) - describe('.findprovs', () => { - it('finds providers', (done) => { - ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP').then((res) => { - expect(res).to.be.an('array') - done() - }).catch(done) - }) + it('.findprovs', (done) => { + ipfs.dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP').then((res) => { + expect(res).to.be.an('array') + done() + }).catch(done) }) }) // Tests below are tests that haven't been implemented yet or is not @@ -79,19 +71,17 @@ module.exports = (common) => { }) }) }) - xdescribe('.put & .get', () => { - it('puts and gets a key value pair in the DHT', (done) => { - peers.a.ipfs.dht.put('scope', 'interplanetary', (err, res) => { - expect(err).to.not.exist + xit('.put and .get a key value pair in the DHT', (done) => { + peers.a.ipfs.dht.put('scope', 'interplanetary', (err, res) => { + expect(err).to.not.exist - expect(res).to.be.an('array') + expect(res).to.be.an('array') - // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 - peers.b.ipfs.dht.get('scope', (err, value) => { - expect(err).to.not.exist - expect(value).to.be.equal('interplanetary') - done() - }) + // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 + peers.b.ipfs.dht.get('scope', (err, value) => { + expect(err).to.not.exist + expect(value).to.be.equal('interplanetary') + done() }) }) })