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

Commit c3d2d1e

Browse files
Mr0grogdaviddias
authored andcommitted
fix: change default config from JSON file to JS module to prevent having it doubly used (#1324)
* Change default config from JSON file to JS module Because the default config was a JSON file, it was treated as a singleton object. In some parts of the code, data got added to it, leading to race conditions when creating multiple nodes simultaneously. To make that harder to do accidentally, the default config is now a *function* that returns a unique configuration object on each call. Fixes #1316. License: MIT Signed-off-by: Rob Brackett <[email protected]> * Fix lint errors License: MIT Signed-off-by: Rob Brackett <[email protected]>
1 parent 47285a7 commit c3d2d1e

File tree

11 files changed

+129
-75
lines changed

11 files changed

+129
-75
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ Creates and returns an instance of an IPFS node. Use the `options` argument to s
231231
- `enabled` (boolean): Make this node a relay (other nodes can connect *through* it). (Default: `false`)
232232
- `active` (boolean): Make this an *active* relay node. Active relay nodes will attempt to dial a destination peer even if that peer is not yet connected to the relay. (Default: `false`)
233233

234-
- `config` (object) Modify the default IPFS node config. Find the Node.js defaults at [`src/core/runtime/config-nodejs.json`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/config-nodejs.json) and the browser defaults at [`src/core/runtime/config-browser.json`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/config-browser.json). This object will be *merged* with the default config; it will not replace it.
234+
- `config` (object) Modify the default IPFS node config. Find the Node.js defaults at [`src/core/runtime/config-nodejs.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/config-nodejs.js) and the browser defaults at [`src/core/runtime/config-browser.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/config-browser.js). This object will be *merged* with the default config; it will not replace it.
235235

236236
- `libp2p` (object) add custom modules to the libp2p stack of your node
237237
- `modules` (object):

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"main": "src/core/index.js",
99
"browser": {
1010
"./src/core/components/init-assets.js": false,
11-
"./src/core/runtime/config-nodejs.json": "./src/core/runtime/config-browser.json",
11+
"./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js",
1212
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
1313
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
1414
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",

src/core/components/bootstrap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap
3+
const defaultConfig = require('../runtime/config-nodejs.js')
44
const isMultiaddr = require('mafmt').IPFS.matches
55
const promisify = require('promisify-es6')
66

@@ -41,7 +41,7 @@ module.exports = function bootstrap (self) {
4141
return callback(err)
4242
}
4343
if (args.default) {
44-
config.Bootstrap = defaultNodes
44+
config.Bootstrap = defaultConfig().Bootstrap
4545
} else if (multiaddr && config.Bootstrap.indexOf(multiaddr) === -1) {
4646
config.Bootstrap.push(multiaddr)
4747
}
@@ -51,7 +51,7 @@ module.exports = function bootstrap (self) {
5151
}
5252

5353
callback(null, {
54-
Peers: args.default ? defaultNodes : [multiaddr]
54+
Peers: args.default ? defaultConfig().Bootstrap : [multiaddr]
5555
})
5656
})
5757
})

src/core/components/init.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const peerId = require('peer-id')
44
const waterfall = require('async/waterfall')
55
const parallel = require('async/parallel')
66
const promisify = require('promisify-es6')
7-
const config = require('../runtime/config-nodejs.json')
7+
const defaultConfig = require('../runtime/config-nodejs.js')
88
const Keychain = require('libp2p-keychain')
99

1010
const addDefaultAssets = require('./init-assets')
@@ -37,6 +37,7 @@ module.exports = function init (self) {
3737
opts.emptyRepo = opts.emptyRepo || false
3838
opts.bits = Number(opts.bits) || 2048
3939
opts.log = opts.log || function () {}
40+
const config = defaultConfig()
4041
let privateKey
4142
waterfall([
4243
// Verify repo does not yet exist.

src/core/runtime/config-browser.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
module.exports = () => ({
4+
Addresses: {
5+
Swarm: [
6+
],
7+
API: '',
8+
Gateway: ''
9+
},
10+
Discovery: {
11+
MDNS: {
12+
Enabled: false,
13+
Interval: 10
14+
},
15+
webRTCStar: {
16+
Enabled: true
17+
}
18+
},
19+
Bootstrap: [
20+
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
21+
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
22+
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
23+
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
24+
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
25+
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
26+
'/dns4/wss0.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
27+
'/dns4/wss1.bootstrap.libp2p.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
28+
]
29+
})

src/core/runtime/config-browser.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/core/runtime/config-nodejs.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
module.exports = () => ({
4+
Addresses: {
5+
Swarm: [
6+
'/ip4/0.0.0.0/tcp/4002',
7+
'/ip4/127.0.0.1/tcp/4003/ws'
8+
],
9+
API: '/ip4/127.0.0.1/tcp/5002',
10+
Gateway: '/ip4/127.0.0.1/tcp/9090'
11+
},
12+
Discovery: {
13+
MDNS: {
14+
Enabled: true,
15+
Interval: 10
16+
},
17+
webRTCStar: {
18+
Enabled: true
19+
}
20+
},
21+
Bootstrap: [
22+
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
23+
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
24+
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
25+
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
26+
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
27+
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
28+
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
29+
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
30+
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
31+
'/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
32+
'/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
33+
'/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
34+
'/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
35+
'/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
36+
'/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
37+
'/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
38+
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
39+
'/dns4/wss0.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
40+
'/dns4/wss1.bootstrap.libp2p.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
41+
]
42+
})

src/core/runtime/config-nodejs.json

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/bootstrapers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const dirtyChai = require('dirty-chai')
66
const expect = chai.expect
77
chai.use(dirtyChai)
88
const IPFS = require('..')
9-
const list = require('../src/core/runtime/config-browser.json').Bootstrap
9+
const list = require('../src/core/runtime/config-browser.js')().Bootstrap
1010

1111
/*
1212
* These tests were graciously made for lgierth, so that he can test the

test/core/create-node.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
99
const series = require('async/series')
10+
const waterfall = require('async/waterfall')
11+
const parallel = require('async/parallel')
1012
const os = require('os')
1113
const path = require('path')
1214
const hat = require('hat')
@@ -305,4 +307,51 @@ describe('create node', function () {
305307
(cb) => node.stop(cb)
306308
], done)
307309
})
310+
311+
it('does not share identity with a simultaneously created node', function (done) {
312+
this.timeout(80 * 1000)
313+
314+
let _nodeNumber = 0
315+
function createNode () {
316+
_nodeNumber++
317+
return new IPFS({
318+
repo: createTempRepo(),
319+
init: { emptyRepo: true },
320+
config: {
321+
Addresses: {
322+
API: `/ip4/127.0.0.1/tcp/${5010 + _nodeNumber}`,
323+
Gateway: `/ip4/127.0.0.1/tcp/${9090 + _nodeNumber}`,
324+
Swarm: [
325+
`/ip4/0.0.0.0/tcp/${4010 + _nodeNumber * 2}`,
326+
`/ip4/127.0.0.1/tcp/${4011 + _nodeNumber * 2}/ws`
327+
]
328+
},
329+
Bootstrap: []
330+
}
331+
})
332+
}
333+
334+
const nodeA = createNode()
335+
const nodeB = createNode()
336+
337+
waterfall([
338+
(cb) => parallel([
339+
(cb) => nodeA.once('start', cb),
340+
(cb) => nodeB.once('start', cb)
341+
], cb),
342+
(_, cb) => parallel([
343+
(cb) => nodeA.id(cb),
344+
(cb) => nodeB.id(cb)
345+
], cb),
346+
([idA, idB], cb) => {
347+
expect(idA.id).to.not.equal(idB.id)
348+
cb()
349+
}
350+
], (error) => {
351+
parallel([
352+
(cb) => nodeA.stop(cb),
353+
(cb) => nodeB.stop(cb)
354+
], (stopError) => done(error || stopError))
355+
})
356+
})
308357
})

test/http-api/inject/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const expect = require('chai').expect
55
const qs = require('qs')
6-
const defaultList = require('../../../src/core/runtime/config-nodejs.json').Bootstrap
6+
const defaultList = require('../../../src/core/runtime/config-nodejs.js')().Bootstrap
77

88
module.exports = (http) => {
99
describe('/bootstrap', () => {

0 commit comments

Comments
 (0)