From af0907aee6c43c276dae4cc92aaa58144226a834 Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Tue, 10 Apr 2018 09:41:47 -0700 Subject: [PATCH 1/5] fix(bitswap.unwant) Parse CID from arg CID parsing is done just like block.add does This also removes the local tests and moves reliance on testing to the tests that are being added to interface-ipfs-core --- src/bitswap/unwant.js | 20 ++++++++++ test/bitswap.spec.js | 69 ---------------------------------- test/interface/bitswap.spec.js | 32 ++++++++++++++++ 3 files changed, 52 insertions(+), 69 deletions(-) delete mode 100644 test/bitswap.spec.js create mode 100644 test/interface/bitswap.spec.js diff --git a/src/bitswap/unwant.js b/src/bitswap/unwant.js index 267568ee1..aa3c575cf 100644 --- a/src/bitswap/unwant.js +++ b/src/bitswap/unwant.js @@ -1,6 +1,7 @@ 'use strict' const promisify = require('promisify-es6') +const CID = require('cids') module.exports = (send) => { return promisify((args, opts, callback) => { @@ -8,6 +9,25 @@ module.exports = (send) => { callback = opts opts = {} } + + // Mirrors block.get's parsing of the CID + let cid + try { + if (CID.isCID(args)) { + cid = args + args = cid.toBaseEncodedString() + } else if (Buffer.isBuffer(args)) { + cid = new CID(args) + args = cid.toBaseEncodedString() + } else if (typeof args === 'string') { + cid = new CID(args) + } else { + return callback(new Error('invalid argument')) + } + } catch (err) { + return callback(err) + } + send({ path: 'bitswap/unwant', args: args, diff --git a/test/bitswap.spec.js b/test/bitswap.spec.js deleted file mode 100644 index 2f7844289..000000000 --- a/test/bitswap.spec.js +++ /dev/null @@ -1,69 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) - -const IPFSApi = require('../src') - -const f = require('./utils/factory') - -describe('.bitswap', function () { - this.timeout(20 * 1000) // slow CI - - let ipfs - let ipfsd = null - - before(function (done) { - this.timeout(20 * 1000) // slow CI - - f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => { - expect(err).to.not.exist() - ipfsd = _ipfsd - ipfs = IPFSApi(_ipfsd.apiAddr) - done() - }) - }) - - after((done) => { - if (!ipfsd) return done() - ipfsd.stop(done) - }) - - it('.wantlist', (done) => { - ipfs.bitswap.wantlist((err, res) => { - expect(err).to.not.exist() - expect(res).to.have.to.eql({ - Keys: [] - }) - done() - }) - }) - - it('.stat', (done) => { - ipfs.bitswap.stat((err, res) => { - expect(err).to.not.exist() - expect(res).to.have.a.property('provideBufLen') - expect(res).to.have.a.property('wantlist') - expect(res).to.have.a.property('peers') - expect(res).to.have.a.property('blocksReceived') - expect(res).to.have.a.property('dataReceived') - expect(res).to.have.a.property('blocksSent') - expect(res).to.have.a.property('dataSent') - expect(res).to.have.a.property('dupBlksReceived') - expect(res).to.have.a.property('dupDataReceived') - - done() - }) - }) - - it('.unwant', (done) => { - const key = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' - ipfs.bitswap.unwant(key, (err) => { - expect(err).to.not.exist() - done() - }) - }) -}) diff --git a/test/interface/bitswap.spec.js b/test/interface/bitswap.spec.js new file mode 100644 index 000000000..82eaacb6d --- /dev/null +++ b/test/interface/bitswap.spec.js @@ -0,0 +1,32 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const IPFSApi = require('../../src') +const f = require('../utils/factory') + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, IPFSApi(_ipfsd.apiAddr)) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.bitswap(common) From ed5b651e529cd3e1c7b69ef38afe32bf0d05926e Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Wed, 9 May 2018 11:39:37 -0700 Subject: [PATCH 2/5] feat(bitswap.wantlist) add peer parameter --- src/bitswap/wantlist.js | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/bitswap/wantlist.js b/src/bitswap/wantlist.js index c96aaaaac..41a805c47 100644 --- a/src/bitswap/wantlist.js +++ b/src/bitswap/wantlist.js @@ -1,11 +1,46 @@ 'use strict' const promisify = require('promisify-es6') +const CID = require('cids') module.exports = (send) => { - return promisify((callback) => { + return promisify((args, opts, callback) => { + + if (typeof (args) === 'function') { + callback = args + opts = {} + args = null + } else { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + } + + // Mirrors block.get's parsing of the CID + let cid + if (args) { + try { + if (CID.isCID(args)) { + cid = args + args = cid.toBaseEncodedString() + } else if (Buffer.isBuffer(args)) { + cid = new CID(args) + args = cid.toBaseEncodedString() + } else if (typeof args === 'string') { + cid = new CID(args) + } else { + return callback(new Error('invalid argument')) + } + } catch (err) { + return callback(err) + } + opts.peer = args + } send({ - path: 'bitswap/wantlist' + path: 'bitswap/wantlist', + args: args, + qs: opts }, callback) }) } From 8d3f52a2ad2b0bca4c6b832d9e6067ca694f0af9 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Jun 2018 15:40:22 +0100 Subject: [PATCH 3/5] chore: simplify CID parsing for wantlist and unwant License: MIT Signed-off-by: Alan Shaw --- src/bitswap/unwant.js | 18 +++--------------- src/bitswap/wantlist.js | 36 ++++++++++-------------------------- 2 files changed, 13 insertions(+), 41 deletions(-) diff --git a/src/bitswap/unwant.js b/src/bitswap/unwant.js index aa3c575cf..865fbc27b 100644 --- a/src/bitswap/unwant.js +++ b/src/bitswap/unwant.js @@ -4,33 +4,21 @@ const promisify = require('promisify-es6') const CID = require('cids') module.exports = (send) => { - return promisify((args, opts, callback) => { + return promisify((cid, opts, callback) => { if (typeof (opts) === 'function') { callback = opts opts = {} } - // Mirrors block.get's parsing of the CID - let cid try { - if (CID.isCID(args)) { - cid = args - args = cid.toBaseEncodedString() - } else if (Buffer.isBuffer(args)) { - cid = new CID(args) - args = cid.toBaseEncodedString() - } else if (typeof args === 'string') { - cid = new CID(args) - } else { - return callback(new Error('invalid argument')) - } + cid = new CID(cid) } catch (err) { return callback(err) } send({ path: 'bitswap/unwant', - args: args, + args: cid.toBaseEncodedString(), qs: opts }, callback) }) diff --git a/src/bitswap/wantlist.js b/src/bitswap/wantlist.js index 41a805c47..6cd1e3868 100644 --- a/src/bitswap/wantlist.js +++ b/src/bitswap/wantlist.js @@ -4,42 +4,26 @@ const promisify = require('promisify-es6') const CID = require('cids') module.exports = (send) => { - return promisify((args, opts, callback) => { - - if (typeof (args) === 'function') { - callback = args + return promisify((peerId, opts, callback) => { + if (typeof (peerId) === 'function') { + callback = peerId + opts = {} + peerId = null + } else if (typeof (opts) === 'function') { + callback = opts opts = {} - args = null - } else { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } } - // Mirrors block.get's parsing of the CID - let cid - if (args) { + if (peerId) { try { - if (CID.isCID(args)) { - cid = args - args = cid.toBaseEncodedString() - } else if (Buffer.isBuffer(args)) { - cid = new CID(args) - args = cid.toBaseEncodedString() - } else if (typeof args === 'string') { - cid = new CID(args) - } else { - return callback(new Error('invalid argument')) - } + opts.peer = new CID(peerId).toBaseEncodedString() } catch (err) { return callback(err) } - opts.peer = args } + send({ path: 'bitswap/wantlist', - args: args, qs: opts }, callback) }) From 828bc68c841544ca9fad62f37aeb2ef7c3f487c0 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Jun 2018 15:58:25 +0100 Subject: [PATCH 4/5] chore: update interface-ipfs-core dependency License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fda509b7e..f70df844d 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "eslint-plugin-react": "^7.9.1", "go-ipfs-dep": "~0.4.15", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.67.0", + "interface-ipfs-core": "~0.68.0", "ipfsd-ctl": "~0.37.3", "pull-stream": "^3.6.8", "socket.io": "^2.1.1", From fab9c6ef2d2e4a79073860ad6155a089dc1bb0b2 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Jun 2018 16:48:09 +0100 Subject: [PATCH 5/5] chore: upgrades interface-ipfs-core dependency License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f70df844d..3b720b0fe 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "eslint-plugin-react": "^7.9.1", "go-ipfs-dep": "~0.4.15", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.68.0", + "interface-ipfs-core": "~0.68.1", "ipfsd-ctl": "~0.37.3", "pull-stream": "^3.6.8", "socket.io": "^2.1.1",