This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcreate-node.spec.js
328 lines (278 loc) · 7.87 KB
/
create-node.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')
const { isNode } = require('ipfs-utils/src/env')
const tmpDir = require('ipfs-utils/src/temp-dir')
const PeerId = require('peer-id')
const { keys: { supportedKeys } } = require('libp2p-crypto')
const IPFS = require('../src')
const defer = require('p-defer')
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
const createTempRepo = require('./utils/create-repo')
describe('create node', function () {
/** @type {import('ipfs-repo').IPFSRepo} */
let tempRepo
beforeEach(async () => {
tempRepo = await createTempRepo()
})
it('should create a node with a custom repo path', async function () {
this.timeout(80 * 1000)
const node = await IPFS.create({
repo: tmpDir(),
init: { bits: 512 },
config: {
Addresses: {
Swarm: []
}
},
preload: { enabled: false }
})
const config = await node.config.getAll()
expect(config.Identity).to.exist()
await node.stop()
})
it('should create a node with a custom repo', async function () {
this.timeout(80 * 1000)
const node = await IPFS.create({
repo: tempRepo,
init: { bits: 512 },
config: {
Addresses: {
Swarm: []
}
},
preload: { enabled: false }
})
const config = await node.config.getAll()
expect(config.Identity).to.exist()
await node.stop()
})
it('should create and initialize with algorithm', async () => {
const ipfs = await IPFS.create({
init: { algorithm: 'Ed25519' },
start: false,
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})
const id = await ipfs.id()
const config = await ipfs.config.getAll()
const peerId = await PeerId.createFromPrivKey(`${config.Identity?.PrivKey}`)
expect(peerId.privKey).is.instanceOf(supportedKeys.ed25519.Ed25519PrivateKey)
expect(id.id).to.equal(peerId.toB58String())
})
it('should create and initialize but not start', async () => {
const ipfs = await IPFS.create({
init: { bits: 512 },
start: false,
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})
expect(ipfs.isOnline()).to.be.false()
})
it('should create but not start', async () => {
const ipfs = await IPFS.create({
start: false,
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})
expect(ipfs.isOnline()).to.be.false()
})
it('should throw on boot error', () => {
return expect(IPFS.create({
repo: tempRepo,
init: {
algorithm: 'RSA',
bits: 256
}, // Too few bits will cause error on boot
config: { Addresses: { Swarm: [] } }
})).to.eventually.be.rejected()
})
it('should init with 1024 key bits', async function () {
this.timeout(80 * 1000)
const node = await IPFS.create({
repo: tempRepo,
init: {
algorithm: 'RSA',
bits: 1024
},
config: {
Addresses: {
Swarm: []
}
},
preload: { enabled: false }
})
const config = await node.config.getAll()
expect(config.Identity).to.exist()
expect(config.Identity?.PrivKey.length).is.below(1024)
await node.stop()
})
it('should be silent', async function () {
if (process.env.DEBUG) return this.skip()
this.timeout(30 * 1000)
const spy = sinon.spy(console, 'log')
const ipfs = await IPFS.create({
silent: true,
repo: tempRepo,
init: { bits: 512 },
config: {
Addresses: {
Swarm: []
}
},
preload: { enabled: false }
})
// eslint-disable-next-line no-console
expect(spy.called).to.be.false()
// eslint-disable-next-line no-console
spy.restore()
await ipfs.stop()
})
it('should allow configuration of swarm and bootstrap addresses', async function () {
this.timeout(80 * 1000)
if (!isNode) return this.skip()
const node = await IPFS.create({
repo: tempRepo,
init: { bits: 512 },
config: {
Addresses: {
Swarm: ['/ip4/127.0.0.1/tcp/9977']
},
Bootstrap: []
},
preload: { enabled: false }
})
const config = await node.config.getAll()
expect(config.Addresses?.Swarm).to.eql(['/ip4/127.0.0.1/tcp/9977'])
expect(config.Bootstrap).to.eql([])
await node.stop()
})
it('should allow pubsub to be disabled', async function () {
this.timeout(80 * 1000)
if (!isNode) return this.skip()
const node = await IPFS.create({
repo: tempRepo,
init: { bits: 512 },
config: {
Pubsub: {
Enabled: false
}
}
})
await expect(node.pubsub.peers('topic'))
.to.eventually.be.rejected()
.with.property('code').that.equals('ERR_NOT_ENABLED')
await node.stop()
})
it('should start and stop, start and stop', async function () {
this.timeout(80 * 1000)
const node = await IPFS.create({
repo: tempRepo,
init: { bits: 512 },
config: {
Addresses: {
Swarm: []
},
Bootstrap: []
},
preload: { enabled: false }
})
await node.stop()
await node.start()
await node.stop()
})
it('should not share identity with a simultaneously created node', async function () {
this.timeout(2 * 60 * 1000)
let _nodeNumber = 0
/**
* @param {import('ipfs-repo').IPFSRepo} repo
* @returns
*/
function createNode (repo) {
_nodeNumber++
return IPFS.create({
repo,
init: { bits: 512, emptyRepo: true },
config: {
Addresses: {
API: `/ip4/127.0.0.1/tcp/${5010 + _nodeNumber}`,
Gateway: `/ip4/127.0.0.1/tcp/${9090 + _nodeNumber}`,
Swarm: isNode
? [
`/ip4/0.0.0.0/tcp/${4010 + _nodeNumber * 2}`
]
: []
},
Bootstrap: []
},
preload: { enabled: false }
})
}
const repoA = await createTempRepo()
const repoB = await createTempRepo()
const [nodeA, nodeB] = await Promise.all([createNode(repoA), createNode(repoB)])
const [idA, idB] = await Promise.all([nodeA.id(), nodeB.id()])
expect(idA.id).to.not.equal(idB.id)
await Promise.all([nodeA.stop(), nodeB.stop()])
})
it('should not error with empty IPLD config', async function () {
this.timeout(80 * 1000)
const node = await IPFS.create({
repo: tempRepo,
init: { bits: 512 },
config: {
Addresses: {
Swarm: []
}
},
ipld: {},
preload: { enabled: false }
})
await node.stop()
})
it('should error when receiving websocket-star swarm addresses', async () => {
const node = await IPFS.create({
repo: tempRepo,
init: { bits: 512 },
start: false,
config: {
Addresses: {
Swarm: ['/ip4/127.0.0.1/tcp/13579/wss/p2p-websocket-star']
},
Bootstrap: []
},
preload: { enabled: false }
})
await expect(node.start()).to.eventually.be.rejected().with.property('code', 'ERR_WEBSOCKET_STAR_SWARM_ADDR_NOT_SUPPORTED')
})
it('should auto-migrate repos by default', async function () {
this.timeout(80 * 1000)
const deferred = defer()
const id = await PeerId.create({
bits: 512
})
// create an old-looking repo
const repo = await createTempRepo({
version: 1,
spec: 1,
config: {
Identity: {
PeerID: id.toString(),
PrivKey: uint8ArrayToString(id.marshalPrivKey(), 'base64pad')
}
},
autoMigrate: true,
onMigrationProgress: () => {
// migrations are happening
deferred.resolve()
}
})
const node = await IPFS.create({
repo
})
await deferred.promise
await node.stop()
})
})