From 9a445d10eeaf3597e31cd7415d67aa1a58a0b2ea Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 15 Feb 2018 23:37:42 +1300 Subject: [PATCH] feat: Bootstrap API compliance (#1218) --- package.json | 2 +- src/core/components/bootstrap.js | 28 ++++++++++++++++++----- test/core/bootstrap.spec.js | 10 ++++---- test/core/interface/bootstrap.js | 33 +++++++++++++++++++++++++++ test/core/interface/interface.spec.js | 1 + test/http-api/interface/bootstrap.js | 31 +++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 test/core/interface/bootstrap.js create mode 100644 test/http-api/interface/bootstrap.js diff --git a/package.json b/package.json index 9878ce947f..d07e1d9694 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "form-data": "^2.3.2", "go-ipfs-dep": "^0.4.13", "hat": "0.0.3", - "interface-ipfs-core": "~0.50.1", + "interface-ipfs-core": "~0.51.0", "ipfsd-ctl": "~0.28.0", "left-pad": "^1.2.0", "lodash": "^4.17.5", diff --git a/src/core/components/bootstrap.js b/src/core/components/bootstrap.js index 8822c5997a..9ca1a76479 100644 --- a/src/core/components/bootstrap.js +++ b/src/core/components/bootstrap.js @@ -1,22 +1,31 @@ 'use strict' const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap +const MultiAddr = require('multiaddr') +const promisify = require('promisify-es6') module.exports = function bootstrap (self) { return { - list: (callback) => { + list: promisify((callback) => { self._repo.config.get((err, config) => { if (err) { return callback(err) } callback(null, {Peers: config.Bootstrap}) }) - }, - add: (multiaddr, args, callback) => { + }), + add: promisify((multiaddr, args, callback) => { if (typeof args === 'function') { callback = args args = {default: false} } + try { + if (multiaddr) + new MultiAddr(multiaddr) + } + catch (err) { + return setImmediate(() => callback(err)) + } self._repo.config.get((err, config) => { if (err) { return callback(err) @@ -36,12 +45,19 @@ module.exports = function bootstrap (self) { }) }) }) - }, - rm: (multiaddr, args, callback) => { + }), + rm: promisify((multiaddr, args, callback) => { if (typeof args === 'function') { callback = args args = {all: false} } + try { + if (multiaddr) + new MultiAddr(multiaddr) + } + catch (err) { + return setImmediate(() => callback(err)) + } self._repo.config.get((err, config) => { if (err) { return callback(err) @@ -65,6 +81,6 @@ module.exports = function bootstrap (self) { callback(null, {Peers: res}) }) }) - } + }) } } diff --git a/test/core/bootstrap.spec.js b/test/core/bootstrap.spec.js index e18ce0e8c0..698af528b4 100644 --- a/test/core/bootstrap.spec.js +++ b/test/core/bootstrap.spec.js @@ -81,7 +81,7 @@ describe('bootstrap', () => { '/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx', '/dns4/wss0.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', '/dns4/wss1.bootstrap.libp2p.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6', - '/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT' + '/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb' ] it('get bootstrap list', (done) => { @@ -93,9 +93,9 @@ describe('bootstrap', () => { }) it('add a peer to the bootstrap list', (done) => { - node.bootstrap.add('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err, res) => { + node.bootstrap.add('/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb', (err, res) => { expect(err).to.not.exist() - expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'] }) + expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'] }) node.bootstrap.list((err, list) => { expect(err).to.not.exist() expect(list.Peers).to.deep.equal(updatedList) @@ -105,9 +105,9 @@ describe('bootstrap', () => { }) it('remove a peer from the bootstrap list', (done) => { - node.bootstrap.rm('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err, res) => { + node.bootstrap.rm('/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb', (err, res) => { expect(err).to.not.exist() - expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'] }) + expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'] }) node.bootstrap.list((err, list) => { expect(err).to.not.exist() expect(list.Peers).to.deep.equal(defaultList) diff --git a/test/core/interface/bootstrap.js b/test/core/interface/bootstrap.js new file mode 100644 index 0000000000..10c95208f4 --- /dev/null +++ b/test/core/interface/bootstrap.js @@ -0,0 +1,33 @@ +/* eslint-env mocha */ +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const IPFS = require('../../../src') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create({ type: 'proc', exec: IPFS }) + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn((err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, _ipfsd.api) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.bootstrap(common) diff --git a/test/core/interface/interface.spec.js b/test/core/interface/interface.spec.js index 3d8e370981..810b7f2198 100644 --- a/test/core/interface/interface.spec.js +++ b/test/core/interface/interface.spec.js @@ -6,6 +6,7 @@ const isNode = require('detect-node') describe('interface-ipfs-core tests', () => { require('./block') + require('./bootstrap') require('./config') require('./files') require('./generic') diff --git a/test/http-api/interface/bootstrap.js b/test/http-api/interface/bootstrap.js new file mode 100644 index 0000000000..3f3df9b0d3 --- /dev/null +++ b/test/http-api/interface/bootstrap.js @@ -0,0 +1,31 @@ +/* eslint-env mocha */ +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create({ exec: 'src/cli/bin.js' }) + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn((err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, _ipfsd.api) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.bootstrap(common)