Skip to content

Commit 52be7e4

Browse files
richardschneiderJonKrone
authored andcommitted
feat: Bootstrap API compliance (ipfs#1218)
1 parent 25f3948 commit 52be7e4

File tree

6 files changed

+93
-12
lines changed

6 files changed

+93
-12
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"form-data": "^2.3.2",
7474
"go-ipfs-dep": "^0.4.13",
7575
"hat": "0.0.3",
76-
"interface-ipfs-core": "~0.50.1",
76+
"interface-ipfs-core": "~0.51.0",
7777
"ipfsd-ctl": "~0.28.0",
7878
"left-pad": "^1.2.0",
7979
"lodash": "^4.17.5",

src/core/components/bootstrap.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
'use strict'
22

33
const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap
4+
const MultiAddr = require('multiaddr')
5+
const promisify = require('promisify-es6')
46

57
module.exports = function bootstrap (self) {
68
return {
7-
list: (callback) => {
9+
list: promisify((callback) => {
810
self._repo.config.get((err, config) => {
911
if (err) {
1012
return callback(err)
1113
}
1214
callback(null, {Peers: config.Bootstrap})
1315
})
14-
},
15-
add: (multiaddr, args, callback) => {
16+
}),
17+
add: promisify((multiaddr, args, callback) => {
1618
if (typeof args === 'function') {
1719
callback = args
1820
args = {default: false}
1921
}
22+
try {
23+
if (multiaddr)
24+
new MultiAddr(multiaddr)
25+
}
26+
catch (err) {
27+
return setImmediate(() => callback(err))
28+
}
2029
self._repo.config.get((err, config) => {
2130
if (err) {
2231
return callback(err)
@@ -36,12 +45,19 @@ module.exports = function bootstrap (self) {
3645
})
3746
})
3847
})
39-
},
40-
rm: (multiaddr, args, callback) => {
48+
}),
49+
rm: promisify((multiaddr, args, callback) => {
4150
if (typeof args === 'function') {
4251
callback = args
4352
args = {all: false}
4453
}
54+
try {
55+
if (multiaddr)
56+
new MultiAddr(multiaddr)
57+
}
58+
catch (err) {
59+
return setImmediate(() => callback(err))
60+
}
4561
self._repo.config.get((err, config) => {
4662
if (err) {
4763
return callback(err)
@@ -65,6 +81,6 @@ module.exports = function bootstrap (self) {
6581
callback(null, {Peers: res})
6682
})
6783
})
68-
}
84+
})
6985
}
7086
}

test/core/bootstrap.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('bootstrap', () => {
8181
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
8282
'/dns4/wss0.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
8383
'/dns4/wss1.bootstrap.libp2p.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
84-
'/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'
84+
'/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'
8585
]
8686

8787
it('get bootstrap list', (done) => {
@@ -93,9 +93,9 @@ describe('bootstrap', () => {
9393
})
9494

9595
it('add a peer to the bootstrap list', (done) => {
96-
node.bootstrap.add('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err, res) => {
96+
node.bootstrap.add('/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb', (err, res) => {
9797
expect(err).to.not.exist()
98-
expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'] })
98+
expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'] })
9999
node.bootstrap.list((err, list) => {
100100
expect(err).to.not.exist()
101101
expect(list.Peers).to.deep.equal(updatedList)
@@ -105,9 +105,9 @@ describe('bootstrap', () => {
105105
})
106106

107107
it('remove a peer from the bootstrap list', (done) => {
108-
node.bootstrap.rm('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err, res) => {
108+
node.bootstrap.rm('/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb', (err, res) => {
109109
expect(err).to.not.exist()
110-
expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT'] })
110+
expect(res).to.be.eql({ Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb'] })
111111
node.bootstrap.list((err, list) => {
112112
expect(err).to.not.exist()
113113
expect(list.Peers).to.deep.equal(defaultList)

test/core/interface/bootstrap.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const test = require('interface-ipfs-core')
5+
const parallel = require('async/parallel')
6+
7+
const IPFS = require('../../../src')
8+
9+
const DaemonFactory = require('ipfsd-ctl')
10+
const df = DaemonFactory.create({ type: 'proc', exec: IPFS })
11+
12+
const nodes = []
13+
const common = {
14+
setup: function (callback) {
15+
callback(null, {
16+
spawnNode: (cb) => {
17+
df.spawn((err, _ipfsd) => {
18+
if (err) {
19+
return cb(err)
20+
}
21+
22+
nodes.push(_ipfsd)
23+
cb(null, _ipfsd.api)
24+
})
25+
}
26+
})
27+
},
28+
teardown: function (callback) {
29+
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
30+
}
31+
}
32+
33+
test.bootstrap(common)

test/core/interface/interface.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const isNode = require('detect-node')
66

77
describe('interface-ipfs-core tests', () => {
88
require('./block')
9+
require('./bootstrap')
910
require('./config')
1011
require('./files')
1112
require('./generic')

test/http-api/interface/bootstrap.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const test = require('interface-ipfs-core')
5+
const parallel = require('async/parallel')
6+
7+
const DaemonFactory = require('ipfsd-ctl')
8+
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })
9+
10+
const nodes = []
11+
const common = {
12+
setup: function (callback) {
13+
callback(null, {
14+
spawnNode: (cb) => {
15+
df.spawn((err, _ipfsd) => {
16+
if (err) {
17+
return cb(err)
18+
}
19+
20+
nodes.push(_ipfsd)
21+
cb(null, _ipfsd.api)
22+
})
23+
}
24+
})
25+
},
26+
teardown: function (callback) {
27+
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
28+
}
29+
}
30+
31+
test.bootstrap(common)

0 commit comments

Comments
 (0)