autobee 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/migrations.js +56 -16
- package/package.json +1 -1
package/lib/migrations.js
CHANGED
|
@@ -15,6 +15,7 @@ const SystemInfoV2 = getEncoding('@autobase-compat/info-v2')
|
|
|
15
15
|
|
|
16
16
|
const EMPTY = b4a.alloc(0)
|
|
17
17
|
const INDEX_VERSION = 1
|
|
18
|
+
const INFO = b4a.from([0x00, 0x00, 0x69, 0x6e, 0x66, 0x6f])
|
|
18
19
|
const [NS_SIGNER_NAMESPACE] = crypto.namespace('autobase', 1)
|
|
19
20
|
|
|
20
21
|
// indexer rotations chain legacy systems - how many generations we chase
|
|
@@ -41,15 +42,13 @@ async function checkAutobaseMigration(store, local, bootstrap, legacyViews) {
|
|
|
41
42
|
const core = store.get({ key: key, encryption: null })
|
|
42
43
|
await core.ready()
|
|
43
44
|
|
|
44
|
-
const length = await findLocalSystemLength(core, systemLength)
|
|
45
|
-
assert(length > 0, 'Expected system block to exist locally')
|
|
46
|
-
|
|
47
|
-
const system = { key, length }
|
|
48
|
-
|
|
49
45
|
// setup encryption
|
|
50
46
|
const encryptionKey = await local.getUserData('autobase/encryption')
|
|
51
47
|
await AutobeeEncryption.setSystemEncryption(bootstrap.key, encryptionKey, core)
|
|
52
48
|
|
|
49
|
+
const length = await findLocalSystemLength(core, systemLength)
|
|
50
|
+
const system = { key, length }
|
|
51
|
+
|
|
53
52
|
const session = core.session({ name: 'batch' })
|
|
54
53
|
await session.ready()
|
|
55
54
|
|
|
@@ -284,8 +283,21 @@ function deriveNamespace(name, bootstrap, entropy, encryptionKey) {
|
|
|
284
283
|
// scan back for the newest block the signed core actually has locally
|
|
285
284
|
async function findLocalSystemLength(core, systemLength) {
|
|
286
285
|
let length = Math.min(core.length, systemLength)
|
|
287
|
-
|
|
288
|
-
|
|
286
|
+
|
|
287
|
+
while (length > 0) {
|
|
288
|
+
const seq = length - 1
|
|
289
|
+
const node = await core.get(seq, { wait: false })
|
|
290
|
+
|
|
291
|
+
if (node) {
|
|
292
|
+
const blk = decodeBlock(node)
|
|
293
|
+
const entry = blk.keys[0]
|
|
294
|
+
if (b4a.equals(entry.key, INFO)) return length
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
length--
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
assert(false, 'Expected system block to exist locally')
|
|
289
301
|
}
|
|
290
302
|
|
|
291
303
|
function coreLength(core, timeout) {
|
|
@@ -392,28 +404,56 @@ async function getPersistedView(store, view) {
|
|
|
392
404
|
return { key: view.key, length }
|
|
393
405
|
}
|
|
394
406
|
|
|
407
|
+
function getOplog(info, batch) {
|
|
408
|
+
if (batch.length === 1) return batch[0].keys[0]
|
|
409
|
+
|
|
410
|
+
const { heads } = encoding.decodeSystemInfo(info.value)
|
|
411
|
+
|
|
412
|
+
for (const b of batch) {
|
|
413
|
+
const entry = b.keys[0]
|
|
414
|
+
const w = encoding.decodeSystemWriter(entry.key, entry.value)
|
|
415
|
+
|
|
416
|
+
for (const { key } of heads) {
|
|
417
|
+
if (b4a.equals(w.key, key)) return b.keys[0]
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
throw new Error('Could not infer oplog node')
|
|
422
|
+
}
|
|
423
|
+
|
|
395
424
|
async function getCatchupHeads(session, from) {
|
|
396
425
|
const seen = new Map()
|
|
397
426
|
const nodes = []
|
|
398
427
|
|
|
428
|
+
let batch = []
|
|
429
|
+
|
|
399
430
|
// block 0 is the bee header, never a node
|
|
400
431
|
for (let i = Math.max(from, 1); i < session.length; i++) {
|
|
401
432
|
const node = await session.get(i, { wait: false })
|
|
402
433
|
if (!node) throw new Error('Expect nodes to exist locally')
|
|
403
434
|
|
|
404
|
-
const
|
|
405
|
-
|
|
435
|
+
const blk = decodeBlock(node)
|
|
436
|
+
const entry = blk.keys[0]
|
|
437
|
+
|
|
438
|
+
if (b4a.equals(entry.key, INFO)) {
|
|
439
|
+
const oplog = getOplog(entry, batch)
|
|
440
|
+
const { key, length } = encoding.decodeSystemWriter(oplog.key, oplog.value)
|
|
406
441
|
|
|
407
|
-
|
|
408
|
-
if (!length) continue
|
|
442
|
+
batch = []
|
|
409
443
|
|
|
410
|
-
|
|
411
|
-
const current = seen.get(id) || 0
|
|
444
|
+
if (!length) continue
|
|
412
445
|
|
|
413
|
-
|
|
446
|
+
const id = b4a.toString(key, 'hex')
|
|
447
|
+
const current = seen.get(id) || 0
|
|
448
|
+
|
|
449
|
+
if (current >= length) continue
|
|
450
|
+
|
|
451
|
+
seen.set(id, length)
|
|
452
|
+
nodes.push({ key, length })
|
|
453
|
+
continue
|
|
454
|
+
}
|
|
414
455
|
|
|
415
|
-
|
|
416
|
-
nodes.push({ key, length })
|
|
456
|
+
if (entry.key[0] === 0x01) batch.push(blk)
|
|
417
457
|
}
|
|
418
458
|
|
|
419
459
|
return nodes
|