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

Commit fd22c82

Browse files
refactor: use async peer-id and peer-info
1 parent 0e99029 commit fd22c82

File tree

7 files changed

+143
-106
lines changed

7 files changed

+143
-106
lines changed

src/core/components/init.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function init (self) {
1919
}
2020

2121
opts.emptyRepo = opts.emptyRepo || false
22-
opts.bits = opts.bits || 2048
22+
opts.bits = Number(opts.bits) || 2048
2323
opts.log = opts.log || function () {}
2424

2525
let config
@@ -50,17 +50,20 @@ module.exports = function init (self) {
5050
// Generate peer identity keypair + transform to desired format + add to config.
5151
function generateAndSetKeypair () {
5252
opts.log(`generating ${opts.bits}-bit RSA keypair...`, false)
53-
var keys = peerId.create({
53+
peerId.create({
5454
bits: opts.bits
55-
})
56-
config.Identity = {
57-
PeerID: keys.toB58String(),
58-
PrivKey: keys.privKey.bytes.toString('base64')
59-
}
60-
opts.log('done')
61-
opts.log('peer identity: ' + config.Identity.PeerID)
55+
}, (err, keys) => {
56+
if (err) return callback(err)
6257

63-
writeVersion()
58+
config.Identity = {
59+
PeerID: keys.toB58String(),
60+
PrivKey: keys.privKey.bytes.toString('base64')
61+
}
62+
opts.log('done')
63+
opts.log('peer identity: ' + config.Identity.PeerID)
64+
65+
writeVersion()
66+
})
6467
}
6568

6669
function writeVersion () {

src/core/components/load.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@
33
const peerId = require('peer-id')
44
const PeerInfo = require('peer-info')
55
const multiaddr = require('multiaddr')
6+
const waterfall = require('run-waterfall')
67

78
const utils = require('../utils')
89

910
module.exports = function load (self) {
1011
return (callback) => {
11-
utils.ifRepoExists(self._repo, (err) => {
12-
if (err) {
13-
return callback(err)
14-
}
12+
waterfall([
13+
(cb) => utils.ifRepoExists(self._repo, cb),
14+
(cb) => self._repo.config.get(cb),
15+
(config, cb) => {
16+
const id = peerId.createFromPrivKey(config.Identity.PrivKey)
1517

16-
self._repo.config.get((err, config) => {
17-
if (err) {
18-
return callback(err)
19-
}
20-
const pid = peerId.createFromPrivKey(config.Identity.PrivKey)
21-
self._peerInfo = new PeerInfo(pid)
18+
self._peerInfo = new PeerInfo(id)
2219
config.Addresses.Swarm.forEach((addr) => {
2320
self._peerInfo.multiaddr.add(multiaddr(addr))
2421
})
25-
callback()
26-
})
27-
})
22+
cb()
23+
}
24+
], callback)
2825
}
2926
}

test/cli/test-init.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ describe('init', function () {
3535
})
3636

3737
it('bits', (done) => {
38-
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64'], {env})
38+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '512'], {env})
3939
.run((err, stdout, exitcode) => {
4040
expect(err).to.not.exist
4141
done()
4242
})
4343
})
4444

4545
it('empty', (done) => {
46-
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--empty-repo', 'true'], {env})
46+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '512', '--empty-repo', 'true'], {env})
4747
.run((err, stdout, exitcode) => {
4848
expect(err).to.not.exist
4949
expect(repoExistsSync('blocks')).to.equal(false)

test/core/both/test-init.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('init', function () {
1313
const repo = createTempRepo()
1414
const ipfs = new IPFS(repo)
1515

16-
ipfs.init({ emptyRepo: true, bits: 128 }, (err) => {
16+
ipfs.init({ emptyRepo: true, bits: 512 }, (err) => {
1717
expect(err).to.not.exist
1818

1919
repo.exists((err, res) => {
@@ -35,10 +35,10 @@ describe('init', function () {
3535
const repo2 = createTempRepo()
3636
const ipfsShort = new IPFS(repo1)
3737
const ipfsLong = new IPFS(repo2)
38-
ipfsShort.init({ bits: 128, emptyRepo: true }, (err) => {
38+
ipfsShort.init({ bits: 512, emptyRepo: true }, (err) => {
3939
expect(err).to.not.exist
4040

41-
ipfsLong.init({ bits: 256, emptyRepo: true }, (err) => {
41+
ipfsLong.init({ bits: 1024, emptyRepo: true }, (err) => {
4242
expect(err).to.not.exist
4343

4444
repo1.config.get((err, config1) => {

test/core/node-only/test-init.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('init (Node.js specific)', function () {
2323
})
2424

2525
it('init docs are written', (done) => {
26-
ipfs.init({ bits: 64 }, (err) => {
26+
ipfs.init({ bits: 512 }, (err) => {
2727
expect(err).to.not.exist
2828
var multihash = new Buffer('12205e7c3ce237f936c76faf625e90f7751a9f5eeb048f59873303c215e9cce87599', 'hex')
2929
setTimeout(() => {
@@ -37,7 +37,7 @@ describe('init (Node.js specific)', function () {
3737
})
3838

3939
it('empty repo', (done) => {
40-
ipfs.init({ bits: 64, emptyRepo: true }, (err) => {
40+
ipfs.init({ bits: 512, emptyRepo: true }, (err) => {
4141
expect(err).to.not.exist
4242

4343
// Check for default assets

test/utils/factory-core/index.js

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,66 +34,85 @@ function Factory () {
3434
.substring(2, 8)
3535
}
3636

37-
if (!config) {
38-
config = JSON.parse(JSON.stringify(defaultConfig))
39-
const pId = PeerId.create({ bits: 512 }).toJSON()
40-
config.Identity.PeerID = pId.id
41-
config.Identity.PrivKey = pId.privKey
42-
}
43-
44-
// set up the repo
45-
let store
46-
let teardown
47-
48-
if (isNode) {
49-
store = require('fs-pull-blob-store')
50-
teardown = (done) => {
51-
cleanRepo(repoPath)
52-
done()
53-
}
54-
} else {
55-
const idb = window.indexedDB ||
56-
window.mozIndexedDB ||
57-
window.webkitIndexedDB ||
58-
window.msIndexedDB
59-
store = require('idb-pull-blob-store')
60-
teardown = (done) => {
61-
idb.deleteDatabase(repoPath)
62-
idb.deleteDatabase(repoPath + '/blocks')
63-
done()
37+
createConfig(config, (err, conf) => {
38+
if (err) {
39+
return callback(err)
6440
}
65-
}
6641

67-
const repo = new IPFSRepo(repoPath, { stores: store })
68-
repo.teardown = teardown
42+
config = conf
6943

70-
// create the IPFS node
71-
const ipfs = new IPFS(repo)
72-
ipfs.init({ emptyRepo: true, bits: 512 }, (err) => {
73-
if (err) {
74-
return callback(err)
44+
// set up the repo
45+
let store
46+
let teardown
47+
48+
if (isNode) {
49+
store = require('fs-pull-blob-store')
50+
teardown = (done) => {
51+
cleanRepo(repoPath)
52+
done()
53+
}
54+
} else {
55+
const idb = window.indexedDB ||
56+
window.mozIndexedDB ||
57+
window.webkitIndexedDB ||
58+
window.msIndexedDB
59+
store = require('idb-pull-blob-store')
60+
teardown = (done) => {
61+
idb.deleteDatabase(repoPath)
62+
idb.deleteDatabase(repoPath + '/blocks')
63+
done()
64+
}
7565
}
76-
repo.config.set(config, launchNode)
77-
})
7866

79-
function launchNode () {
80-
ipfs.load((err) => {
67+
const repo = new IPFSRepo(repoPath, { stores: store })
68+
repo.teardown = teardown
69+
70+
// create the IPFS node
71+
const ipfs = new IPFS(repo)
72+
ipfs.init({ emptyRepo: true, bits: 512 }, (err) => {
8173
if (err) {
8274
return callback(err)
8375
}
76+
repo.config.set(config, launchNode)
77+
})
8478

85-
ipfs.goOnline((err) => {
79+
function launchNode () {
80+
ipfs.load((err) => {
8681
if (err) {
8782
return callback(err)
8883
}
8984

90-
nodes.push({
91-
repo: repo,
92-
ipfs: ipfs
93-
})
85+
ipfs.goOnline((err) => {
86+
if (err) {
87+
return callback(err)
88+
}
89+
90+
nodes.push({
91+
repo: repo,
92+
ipfs: ipfs
93+
})
9494

95-
callback(null, ipfs)
95+
callback(null, ipfs)
96+
})
9697
})
98+
}
99+
})
100+
101+
function createConfig (config, cb) {
102+
if (config) {
103+
return cb(null, config)
104+
}
105+
const conf = JSON.parse(JSON.stringify(defaultConfig))
106+
107+
PeerId.create({ bits: 512 }, (err, id) => {
108+
if (err) {
109+
return cb(err)
110+
}
111+
112+
const pId = id.toJSON()
113+
conf.Identity.PeerID = pId.id
114+
conf.Identity.PrivKey = pId.privKey
115+
cb(null, conf)
97116
})
98117
}
99118
}

test/utils/factory-http/index.js

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,64 @@ function Factory () {
3535
.substring(2, 8)
3636
}
3737

38-
if (!config) {
39-
config = JSON.parse(JSON.stringify(defaultConfig))
40-
const pId = PeerId.create({ bits: 512 }).toJSON()
41-
config.Identity.PeerID = pId.id
42-
config.Identity.PrivKey = pId.privKey
43-
}
44-
45-
// set up the repo
46-
const repo = new IPFSRepo(repoPath, {
47-
stores: require('fs-pull-blob-store')
48-
})
49-
repo.teardown = (done) => {
50-
cleanRepo(repoPath)
51-
done()
52-
}
53-
54-
// create the IPFS node
55-
const ipfs = new IPFS(repo)
56-
ipfs.init({ emptyRepo: true, bits: 512 }, (err) => {
38+
createConfig(config, (err, conf) => {
5739
if (err) {
5840
return callback(err)
5941
}
60-
repo.config.set(config, launchNode)
61-
})
6242

63-
function launchNode () {
64-
// create the IPFS node through the HTTP-API
65-
const node = new HTTPAPI(repo)
66-
nodes.push({
67-
httpApi: node,
68-
repo: repo
43+
config = conf
44+
// set up the repo
45+
const repo = new IPFSRepo(repoPath, {
46+
stores: require('fs-pull-blob-store')
6947
})
48+
repo.teardown = (done) => {
49+
cleanRepo(repoPath)
50+
done()
51+
}
7052

71-
node.start((err) => {
53+
// create the IPFS node
54+
const ipfs = new IPFS(repo)
55+
ipfs.init({ emptyRepo: true, bits: 512 }, (err) => {
7256
if (err) {
7357
return callback(err)
7458
}
75-
console.log(node.apiMultiaddr)
76-
const ctl = IPFSAPI(node.apiMultiaddr)
77-
callback(null, ctl)
59+
repo.config.set(config, launchNode)
60+
})
61+
62+
function launchNode () {
63+
// create the IPFS node through the HTTP-API
64+
const node = new HTTPAPI(repo)
65+
nodes.push({
66+
httpApi: node,
67+
repo: repo
68+
})
69+
70+
node.start((err) => {
71+
if (err) {
72+
return callback(err)
73+
}
74+
console.log(node.apiMultiaddr)
75+
const ctl = IPFSAPI(node.apiMultiaddr)
76+
callback(null, ctl)
77+
})
78+
}
79+
})
80+
81+
function createConfig (config, cb) {
82+
if (config) {
83+
return cb(null, config)
84+
}
85+
const conf = JSON.parse(JSON.stringify(defaultConfig))
86+
87+
PeerId.create({ bits: 512 }, (err, id) => {
88+
if (err) {
89+
return cb(err)
90+
}
91+
92+
const pId = id.toJSON()
93+
conf.Identity.PeerID = pId.id
94+
conf.Identity.PrivKey = pId.privKey
95+
cb(null, conf)
7896
})
7997
}
8098
}

0 commit comments

Comments
 (0)