Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit b9200f1

Browse files
finish bootstrap interface
1 parent 33d0339 commit b9200f1

File tree

9 files changed

+203
-71
lines changed

9 files changed

+203
-71
lines changed

src/cli/commands/bootstrap/add.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@ const utils = require('../../utils')
66
log.error = debug('cli:bootstrap:error')
77

88
module.exports = {
9-
command: 'add <peer>',
9+
command: 'add [<peer>]',
1010

1111
describe: 'Add peers to the bootstrap list',
1212

13-
builder: {},
13+
builder: {
14+
default: {
15+
describe: 'Add default bootstrap nodes.',
16+
type: 'boolean',
17+
default: false
18+
}
19+
},
1420

1521
handler (argv) {
1622
utils.getIPFS((err, ipfs) => {
1723
if (err) {
1824
throw err
1925
}
2026

21-
ipfs.bootstrap.add(argv.peer, (err, list) => {
27+
ipfs.bootstrap.add(argv.peer, {default: argv.default}, (err, list) => {
2228
if (err) {
2329
throw err
2430
}
31+
32+
list.Peers.forEach((l) => console.log(l))
2533
})
2634
})
2735
}

src/cli/commands/bootstrap/list.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ module.exports = {
2121
if (err) {
2222
throw err
2323
}
24-
list.forEach((node) => {
24+
25+
list.Peers.forEach((node) => {
2526
console.log(node)
2627
})
2728
})

src/cli/commands/bootstrap/rm.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module.exports = {
2828
if (err) {
2929
throw err
3030
}
31+
32+
list.Peers.forEach((l) => console.log(l))
3133
})
3234
})
3335
}

src/core/components/bootstrap.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
'use strict'
22

3+
const defaultNodes = require('../../init-files/default-config.json').Bootstrap
4+
35
module.exports = function bootstrap (self) {
46
return {
57
list: (callback) => {
68
self._repo.config.get((err, config) => {
79
if (err) {
810
return callback(err)
911
}
10-
callback(null, config.Bootstrap)
12+
callback(null, {Peers: config.Bootstrap})
1113
})
1214
},
13-
add: (multiaddr, callback) => {
15+
add: (multiaddr, args, callback) => {
16+
if (typeof args === 'function') {
17+
callback = args
18+
args = {default: false}
19+
}
1420
self._repo.config.get((err, config) => {
1521
if (err) {
1622
return callback(err)
1723
}
18-
config.Bootstrap.push(multiaddr)
19-
self._repo.config.set(config, callback)
24+
if (args.default) {
25+
config.Bootstrap = defaultNodes
26+
} else {
27+
config.Bootstrap.push(multiaddr)
28+
}
29+
self._repo.config.set(config, (err) => {
30+
if (err) {
31+
return callback(err)
32+
}
33+
34+
callback(null, {
35+
Peers: args.default ? defaultNodes : [multiaddr]
36+
})
37+
})
2038
})
2139
},
2240
rm: (multiaddr, args, callback) => {
@@ -34,7 +52,18 @@ module.exports = function bootstrap (self) {
3452
config.Bootstrap = config.Bootstrap.filter((mh) => mh !== multiaddr)
3553
}
3654

37-
self._repo.config.set(config, callback)
55+
self._repo.config.set(config, (err) => {
56+
if (err) {
57+
return callback(err)
58+
}
59+
60+
const res = []
61+
if (!args.all && multiaddr) {
62+
res.push(multiaddr)
63+
}
64+
65+
callback(null, {Peers: res})
66+
})
3867
})
3968
}
4069
}

src/http-api/resources/bootstrap.js

+56-16
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,105 @@
11
'use strict'
22

3-
const boom = require('boom')
43
const multiaddr = require('multiaddr')
54

65
exports = module.exports
76

7+
function applyError (reply, err) {
8+
reply({
9+
Message: err.message,
10+
Code: 0
11+
}).code(500).takeover()
12+
}
13+
814
// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
915
exports.parseKey = (request, reply) => {
1016
if (!request.query.arg) {
11-
return reply("Argument 'multiaddr' is required").code(400).takeover()
17+
return applyError(reply, new Error("Argument 'multiaddr' is required"))
1218
}
1319

1420
try {
1521
return reply({
1622
addr: multiaddr(request.query.arg)
1723
})
1824
} catch (err) {
19-
return reply({
20-
Message: 'Not a valid multiaddr',
21-
Code: 0
22-
}).code(500).takeover()
25+
return applyError(reply, new Error('Not a valid multiaddr'))
2326
}
2427
}
2528

2629
exports.list = (request, reply) => {
2730
const ipfs = request.server.app.ipfs
31+
2832
ipfs.bootstrap.list((err, list) => {
2933
if (err) {
30-
return reply(boom.badRequest(err))
34+
return applyError(reply, err)
3135
}
36+
3237
return reply(list)
3338
})
3439
}
3540

3641
exports.add = {
37-
parseArgs: exports.parseKey,
42+
parseArgs (request, reply) {
43+
const q = request.query
44+
const def = q.default === 'true'
45+
46+
if (q.arg != null) {
47+
try {
48+
return reply({
49+
addr: multiaddr(q.arg),
50+
default: def
51+
})
52+
} catch (err) {
53+
return applyError(reply, new Error('Not a valid multiaddr'))
54+
}
55+
} else {
56+
reply({default: def})
57+
}
58+
},
3859
handler (request, reply) {
3960
const ipfs = request.server.app.ipfs
4061
const addr = request.pre.args.addr
41-
console.log('Handler is called', addr.toString())
62+
const def = request.pre.args.default
4263

43-
ipfs.bootstrap.add(addr.toString(), (err, list) => {
64+
ipfs.bootstrap.add(addr && addr.toString(), {default: def}, (err, list) => {
4465
if (err) {
45-
return reply(boom.badRequest(err))
66+
return applyError(reply, err)
4667
}
47-
return reply()
68+
69+
return reply(list)
4870
})
4971
}
5072
}
5173

5274
exports.rm = {
53-
parseArgs: exports.parseKey,
75+
parseArgs (request, reply) {
76+
const q = request.query
77+
const all = q.all === 'true'
78+
79+
if (q.arg != null) {
80+
try {
81+
return reply({
82+
addr: multiaddr(q.arg),
83+
all: all
84+
})
85+
} catch (err) {
86+
return applyError(reply, new Error('Not a valid multiaddr'))
87+
}
88+
} else {
89+
reply({all: all})
90+
}
91+
},
5492
handler (request, reply) {
5593
const ipfs = request.server.app.ipfs
5694
const addr = request.pre.args.addr
95+
const all = request.pre.args.all
5796

58-
ipfs.bootstrap.rm(addr.toString(), (err, list) => {
97+
ipfs.bootstrap.rm(addr && addr.toString(), {all: all}, (err, list) => {
5998
if (err) {
60-
return reply(boom.badRequest(err))
99+
return applyError(reply, err)
61100
}
62-
return reply()
101+
102+
return reply(list)
63103
})
64104
}
65105
}

src/http-api/routes/bootstrap.js

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

3-
const Joi = require('joi')
43
const resources = require('./../resources')
54

65
module.exports = (server) => {
@@ -21,14 +20,7 @@ module.exports = (server) => {
2120
pre: [
2221
{ method: resources.bootstrap.add.parseArgs, assign: 'args' }
2322
],
24-
handler: resources.bootstrap.add.handler,
25-
validate: {
26-
query: {
27-
arg: Joi.string().required(),
28-
default: Joi.boolean(),
29-
'stream-channels': Joi.boolean()
30-
}
31-
}
23+
handler: resources.bootstrap.add.handler
3224
}
3325
})
3426

@@ -47,14 +39,7 @@ module.exports = (server) => {
4739
pre: [
4840
{ method: resources.bootstrap.rm.parseArgs, assign: 'args' }
4941
],
50-
handler: resources.bootstrap.rm.handler,
51-
validate: {
52-
query: {
53-
arg: Joi.string().required(),
54-
default: Joi.boolean(),
55-
'stream-channels': Joi.boolean()
56-
}
57-
}
42+
handler: resources.bootstrap.rm.handler
5843
}
5944
})
6045
}

test/cli/test-bootstrap.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const repoPath = require('./index').repoPath
77
const ipfs = require('../utils/ipfs-exec')(repoPath)
88
const describeOnlineAndOffline = require('../utils/on-and-off')
99

10-
describe.only('bootstrap', () => {
10+
describe('bootstrap', () => {
1111
describeOnlineAndOffline(repoPath, () => {
1212
const defaultList = [
1313
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
@@ -34,16 +34,21 @@ describe.only('bootstrap', () => {
3434
'/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD'
3535
]
3636

37+
it('add default', () => {
38+
return ipfs('bootstrap add --default').then((out) => {
39+
expect(out).to.be.eql(defaultList.join('\n'))
40+
})
41+
})
42+
3743
it('list the bootstrap nodes', () => {
3844
return ipfs('bootstrap list').then((out) => {
3945
expect(out).to.eql(defaultList.join('\n'))
4046
})
4147
})
4248

43-
// TODO need https://github.com/ipfs/interface-ipfs-core/issues/97
44-
// to happen, otherwise it is a cat an mouse game
45-
it.skip('add another bootstrap node', () => {
49+
it('add another bootstrap node', () => {
4650
return ipfs('bootstrap add /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => {
51+
expect(out).to.be.eql('/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD')
4752
return ipfs('bootstrap list')
4853
}).then((out) => {
4954
expect(out).to.be.eql(updatedList.join('\n'))
@@ -52,6 +57,7 @@ describe.only('bootstrap', () => {
5257

5358
it('rm a bootstrap node', () => {
5459
return ipfs('bootstrap rm /ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD').then((out) => {
60+
expect(out).to.be.eql('/ip4/111.111.111.111/tcp/1001/ipfs/QmcyFFKfLDGJKwufn2GeitxvhricsBQyNKTkrD14psikoD')
5561
return ipfs('bootstrap list')
5662
}).then((out) => {
5763
expect(out).to.deep.equal(defaultList.join('\n'))
@@ -60,6 +66,7 @@ describe.only('bootstrap', () => {
6066

6167
it('rm all bootstrap nodes', () => {
6268
return ipfs('bootstrap rm --all').then((out) => {
69+
expect(out).to.be.eql('')
6370
return ipfs('bootstrap list')
6471
}).then((out) => {
6572
expect(out).to.deep.equal('')

test/core/both/test-bootstrap.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,30 @@ describe('bootstrap', () => {
4141
it('get bootstrap list', (done) => {
4242
ipfs.bootstrap.list((err, list) => {
4343
expect(err).to.not.exist
44-
expect(list).to.deep.equal(defaultList)
44+
expect(list.Peers).to.deep.equal(defaultList)
4545
done()
4646
})
4747
})
4848

4949
it('add a peer to the bootstrap list', (done) => {
50-
ipfs.bootstrap.add('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err) => {
50+
ipfs.bootstrap.add('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err, res) => {
5151
expect(err).to.not.exist
52+
expect(res).to.be.eql({Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT']})
5253
ipfs.bootstrap.list((err, list) => {
5354
expect(err).to.not.exist
54-
expect(list).to.deep.equal(updatedList)
55+
expect(list.Peers).to.deep.equal(updatedList)
5556
done()
5657
})
5758
})
5859
})
5960

6061
it('remove a peer from the bootstrap list', (done) => {
61-
ipfs.bootstrap.rm('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err) => {
62+
ipfs.bootstrap.rm('/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT', (err, res) => {
6263
expect(err).to.not.exist
64+
expect(res).to.be.eql({Peers: ['/ip4/111.111.111.111/tcp/1001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLUVIT']})
6365
ipfs.bootstrap.list((err, list) => {
6466
expect(err).to.not.exist
65-
expect(list).to.deep.equal(defaultList)
67+
expect(list.Peers).to.deep.equal(defaultList)
6668
done()
6769
})
6870
})

0 commit comments

Comments
 (0)