-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathcore.js
More file actions
461 lines (340 loc) · 13.1 KB
/
Copy pathcore.js
File metadata and controls
461 lines (340 loc) · 13.1 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
const test = require('brittle')
const b4a = require('b4a')
const CoreStorage = require('hypercore-storage')
const { MerkleTree } = require('../lib/merkle-tree')
const Core = require('../lib/core')
test('core - append', async function (t) {
const { core } = await create(t)
{
const info = await core.state.append([b4a.from('hello'), b4a.from('world')])
t.alike(info, { length: 2, byteLength: 10 })
t.is(core.state.length, 2)
t.is(core.state.byteLength, 10)
t.alike(
[await getBlock(core, 0), await getBlock(core, 1)],
[b4a.from('hello'), b4a.from('world')]
)
}
{
const info = await core.state.append([b4a.from('hej')])
t.alike(info, { length: 3, byteLength: 13 })
t.is(core.state.length, 3)
t.is(core.state.byteLength, 13)
t.alike(
[await getBlock(core, 0), await getBlock(core, 1), await getBlock(core, 2)],
[b4a.from('hello'), b4a.from('world'), b4a.from('hej')]
)
}
})
test('core - append and truncate', async function (t) {
const { core, reopen } = await create(t)
await core.state.append([b4a.from('hello'), b4a.from('world'), b4a.from('fo'), b4a.from('ooo')])
t.is(core.state.lastTruncation, null)
await core.state.truncate(3, 1)
t.is(core.state.lastTruncation.from, 4)
t.is(core.state.lastTruncation.to, 3)
t.is(core.state.length, 3)
t.is(core.state.byteLength, 12)
t.is(core.state.fork, 1)
await core.state.append([b4a.from('a'), b4a.from('b'), b4a.from('c'), b4a.from('d')])
await core.state.truncate(3, 2)
t.is(core.state.lastTruncation.from, 7)
t.is(core.state.lastTruncation.to, 3)
t.is(core.state.length, 3)
t.is(core.state.byteLength, 12)
t.is(core.state.fork, 2)
await core.state.truncate(2, 3)
t.is(core.state.lastTruncation.from, 3)
t.is(core.state.lastTruncation.to, 2)
await core.state.append([b4a.from('a')])
t.is(core.state.lastTruncation, null)
await core.state.truncate(2, 4)
t.is(core.state.lastTruncation.from, 3)
t.is(core.state.lastTruncation.to, 2)
await core.state.append([b4a.from('a')])
t.is(core.state.lastTruncation, null)
await core.state.truncate(2, 5)
t.is(core.state.lastTruncation.from, 3)
t.is(core.state.lastTruncation.to, 2)
await core.state.append([b4a.from('a')])
t.is(core.state.lastTruncation, null)
await core.state.truncate(2, 6)
t.is(core.state.lastTruncation.from, 3)
t.is(core.state.lastTruncation.to, 2)
await core.state.append([b4a.from('a')])
t.is(core.state.lastTruncation, null)
await core.state.truncate(2, 7)
t.is(core.state.lastTruncation.from, 3)
t.is(core.state.lastTruncation.to, 2)
// check that it was persisted
const coreReopen = await reopen()
t.is(coreReopen.state.length, 2)
t.is(coreReopen.state.byteLength, 10)
t.is(coreReopen.state.fork, 7)
t.is(coreReopen.state.lastTruncation, null)
// t.is(coreReopen.header.hints.reorgs.length, 4)
})
test('core - user data', async function (t) {
const { core, reopen } = await create(t)
await putUserData(core.storage, 'hello', b4a.from('world'))
for await (const { key, value } of core.createUserDataStream()) {
t.alike(key, 'hello')
t.alike(value, b4a.from('world'))
}
t.is(await countEntries(core.createUserDataStream({ gte: 'x', lt: 'z' })), 0)
await putUserData(core.storage, 'hej', b4a.from('verden'))
t.is(await countEntries(core.createUserDataStream()), 2)
for await (const { key, value } of core.createUserDataStream({ gte: 'hello' })) {
t.alike(key, 'hello')
t.alike(value, b4a.from('world'))
}
await putUserData(core.storage, 'hello', null)
t.is(await countEntries(core.createUserDataStream()), 1)
t.is(await countEntries(core.createUserDataStream({ gte: 'hello' })), 0)
await putUserData(core.storage, 'hej', b4a.from('world'))
// check that it was persisted
const coreReopen = await reopen()
for await (const { key, value } of coreReopen.createUserDataStream()) {
t.alike(key, 'hej')
t.alike(value, b4a.from('world'))
}
t.is(await countEntries(coreReopen.createUserDataStream({ gte: 'hello' })), 0)
function putUserData(storage, key, value) {
const tx = storage.write()
tx.putUserData(key, value)
return tx.flush()
}
async function countEntries(stream) {
let count = 0
// eslint-disable-next-line no-unused-vars
for await (const entry of stream) count++
return count
}
})
test('core - header does not retain slabs', async function (t) {
const { core, reopen } = await create(t)
t.is(core.header.key.buffer.byteLength, 32, 'unslabbed key')
t.is(core.header.keyPair.publicKey.buffer.byteLength, 32, 'unslabbed public key')
t.is(core.header.keyPair.secretKey.buffer.byteLength, 64, 'unslabbed private key')
t.is(
core.header.manifest.signers[0].namespace.buffer.byteLength,
32,
'unslabbed signers namespace'
)
t.is(
core.header.manifest.signers[0].publicKey.buffer.byteLength,
32,
'unslabbed signers publicKey'
)
// check the different code path when re-opening
const coreReopen = await reopen()
t.is(coreReopen.header.key.buffer.byteLength, 32, 'reopen unslabbed key')
t.is(coreReopen.header.keyPair.publicKey.buffer.byteLength, 32, 'reopen unslabbed public key')
t.is(coreReopen.header.keyPair.secretKey.buffer.byteLength, 64, 'reopen unslabbed secret key')
t.is(
coreReopen.header.manifest.signers[0].namespace.buffer.byteLength,
32,
'reopen unslabbed signers namespace'
)
t.is(
coreReopen.header.manifest.signers[0].publicKey.buffer.byteLength,
32,
'reopen unslabbed signers publicKey'
)
await coreReopen.close()
})
test('core - verify', async function (t) {
const { core } = await create(t)
const { core: clone } = await create(t, { keyPair: { publicKey: core.header.keyPair.publicKey } })
t.is(clone.header.keyPair.publicKey, core.header.keyPair.publicKey)
await core.state.append([b4a.from('a'), b4a.from('b')])
{
const p = await getProof(core, { upgrade: { start: 0, length: 2 } })
await clone.verify(p)
}
const tree1 = await getCoreHead(core.storage)
const tree2 = await getCoreHead(clone.storage)
t.is(tree1.length, 2)
t.alike(tree1.signature, tree2.signature)
{
const nodes = await MerkleTree.missingNodes(clone.state, 2, clone.state.length)
const p = await getProof(core, { block: { index: 1, nodes, value: true } })
await clone.verify(p)
}
})
test('core - verify parallel upgrades', async function (t) {
const { core } = await create(t)
const { core: clone } = await create(t, { keyPair: { publicKey: core.header.keyPair.publicKey } })
t.is(clone.header.keyPair.publicKey, core.header.keyPair.publicKey)
await core.state.append([b4a.from('a'), b4a.from('b'), b4a.from('c'), b4a.from('d')])
{
const p1 = await getProof(core, { upgrade: { start: 0, length: 2 } })
const p2 = await getProof(core, { upgrade: { start: 0, length: 3 } })
const v1 = clone.verify(p1)
const v2 = clone.verify(p2)
await v1
await v2
}
const tree1 = await getCoreHead(core.storage)
const tree2 = await getCoreHead(clone.storage)
t.is(tree2.length, tree1.length)
t.alike(tree2.signature, tree1.signature)
})
test('core - clone', async function (t) {
const { core } = await create(t)
await core.state.append([b4a.from('hello'), b4a.from('world')])
const manifest = { prologue: { hash: await core.state.hash(), length: core.state.length } }
const { core: copy } = await create(t, { manifest })
await copy.copyPrologue(core.state)
t.alike(
[await getBlock(copy, 0), await getBlock(copy, 1)],
[b4a.from('hello'), b4a.from('world')]
)
const signature = copy.state.signature
const roots = copy.state.roots.map((r) => r.index)
for (let i = 0; i <= core.state.length * 2; i++) {
t.alike(await MerkleTree.get(copy.state, i, false), await MerkleTree.get(core.state, i, false))
}
await core.state.append([b4a.from('c')])
// copy should be independent
t.alike(copy.state.signature, signature)
t.alike(
copy.state.roots.map((r) => r.index),
roots
)
t.is(copy.header.hints.contiguousLength, 2)
})
test('core - clone verify', async function (t) {
const { core } = await create(t)
await core.state.append([b4a.from('a'), b4a.from('b')])
const manifest = { prologue: { hash: await core.state.hash(), length: core.state.length } }
const { core: copy } = await create(t, { manifest })
const { core: clone } = await create(t, { manifest })
await copy.copyPrologue(core.state)
// copy should be independent
await core.state.append([b4a.from('c')])
{
const p = await getProof(copy, { upgrade: { start: 0, length: 2 } })
t.ok(await clone.verify(p))
}
t.is(clone.header.tree.length, 2)
{
const nodes = await MerkleTree.missingNodes(clone.state, 2, clone.state.length)
const p = await getProof(copy, { block: { index: 1, nodes, value: true } })
p.block.value = await getBlock(copy, 1)
await clone.verify(p)
}
t.is(core.header.hints.contiguousLength, 3)
t.is(copy.header.hints.contiguousLength, 2)
t.is(clone.header.hints.contiguousLength, 0)
t.pass('verified')
})
test('core - partial clone', async function (t) {
const { core } = await create(t)
await core.state.append([b4a.from('0')])
await core.state.append([b4a.from('1')])
const manifest = { prologue: { hash: await core.state.hash(), length: core.state.length } }
await core.state.append([b4a.from('2')])
await core.state.append([b4a.from('3')])
const { core: copy } = await create(t, { manifest })
await copy.copyPrologue(core.state)
t.is(core.state.length, 4)
t.is(copy.state.length, 2)
t.is(core.header.hints.contiguousLength, 4)
t.is(copy.header.hints.contiguousLength, 2)
t.alike(
[await getBlock(copy, 0), await getBlock(copy, 1), await getBlock(copy, 2)],
[b4a.from('0'), b4a.from('1'), null]
)
})
test('core - copyPrologue bails if core is not the same', async function (t) {
const { core } = await create(t)
const { core: copy } = await create(t, {
manifest: { prologue: { hash: b4a.alloc(32), length: 1 } }
})
// copy should be independent
await core.state.append([b4a.from('a')])
await t.exception(copy.copyPrologue(core.state))
t.is(copy.header.hints.contiguousLength, 0)
})
test('core - copyPrologue many', async function (t) {
const { core } = await create(t, { compat: false, version: 1 })
await core.state.append([b4a.from('a'), b4a.from('b')])
const manifest = { ...core.header.manifest }
manifest.prologue = { length: core.state.length, hash: core.state.hash() }
const { core: copy } = await create(t, { manifest })
const { core: copy2 } = await create(t, { manifest })
const { core: copy3 } = await create(t, { manifest })
await copy.copyPrologue(core.state)
t.alike(copy.header.manifest.signers[0].publicKey, core.header.manifest.signers[0].publicKey)
t.is(copy.state.length, core.state.length)
t.is(copy.state.byteLength, core.state.byteLength)
// copy should be independent
await core.state.append([b4a.from('c')])
// upgrade clone
{
const batch = core.state.createTreeBatch()
const p = await getProof(core, { upgrade: { start: 0, length: 3 } })
p.upgrade.signature = copy2.verifier.sign(batch, core.header.keyPair)
t.ok(await copy2.verify(p))
}
await t.execution(copy2.copyPrologue(core.state))
await t.execution(copy3.copyPrologue(core.state))
t.is(copy2.state.length, core.state.length)
t.is(copy.state.length, copy3.state.length)
t.is(copy2.header.tree.length, core.header.tree.length)
t.is(copy.header.tree.length, copy3.header.tree.length)
t.is(copy2.state.byteLength, core.state.byteLength)
t.is(copy.state.byteLength, copy3.state.byteLength)
manifest.prologue = { length: core.state.length, hash: core.state.hash() }
const { core: copy4 } = await create(t, { manifest })
await copy4.copyPrologue(copy2.state)
t.is(copy4.state.length, 3)
t.is(copy4.header.tree.length, 3)
t.is(core.header.hints.contiguousLength, 3)
t.is(copy.header.hints.contiguousLength, 2)
t.is(copy2.header.hints.contiguousLength, 2)
t.is(copy3.header.hints.contiguousLength, 2)
t.is(copy4.header.hints.contiguousLength, 2)
t.alike(await getBlock(copy4, 0), b4a.from('a'))
t.alike(await getBlock(copy4, 1), b4a.from('b'))
})
async function create(t, opts = {}) {
const dir = opts.dir || (await t.tmp())
let db = null
t.teardown(teardown, { order: 1 })
const reopen = async () => {
if (db) await db.close()
db = new CoreStorage(dir)
const core = new Core(db, opts)
await core.ready()
t.teardown(() => core.close())
return core
}
const core = await reopen()
return { core, reopen }
async function teardown() {
if (db) await db.close()
}
}
async function getBlock(core, i) {
const r = core.storage.read()
const p = r.getBlock(i)
r.tryFlush()
return p
}
async function getProof(core, req) {
const batch = core.storage.read()
const p = await MerkleTree.proof(core.state, batch, req)
const block = req.block ? batch.getBlock(req.block.index) : null
batch.tryFlush()
const proof = await p.settle()
if (block) proof.block.value = await block
return proof
}
function getCoreHead(storage) {
const b = storage.read()
const p = b.getHead()
b.tryFlush()
return p
}