autobee 2.9.2 → 2.9.4

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.
Files changed (2) hide show
  1. package/lib/migrations.js +18 -5
  2. package/package.json +1 -1
package/lib/migrations.js CHANGED
@@ -77,20 +77,21 @@ async function checkAutobaseMigration(store, local, bootstrap) {
77
77
  const encryptionKey = await local.getUserData('autobase/encryption')
78
78
  await AutobeeEncryption.setSystemEncryption(bootstrap.key, encryptionKey, core)
79
79
 
80
- const length = await findLocalSystemLength(core, systemLength)
81
- const system = { key, length }
82
-
83
80
  const session = core.session({ name: 'batch' })
84
81
  await session.ready()
85
82
 
83
+ let length
86
84
  let catchup
87
85
 
88
86
  try {
87
+ length = await findLocalSystemLength(core, session, systemLength)
89
88
  catchup = await getCatchupHeads(session, length)
90
89
  } finally {
91
90
  await session.close()
92
91
  }
93
92
 
93
+ const system = { key, length }
94
+
94
95
  // nothing reads the info here anymore, but a legacy system that will not
95
96
  // decode has to throw for migrateWithFallback pathway
96
97
  await readLegacySystemInfo(core, length, { wait: false })
@@ -175,6 +176,7 @@ function decodeLegacySystemInfo(buffer) {
175
176
  state.start--
176
177
 
177
178
  switch (version) {
179
+ case 0:
178
180
  case 1:
179
181
  return SystemInfoV1.decode(state)
180
182
  case 2:
@@ -201,7 +203,9 @@ function deriveNamespace(name, bootstrap, entropy, encryptionKey) {
201
203
  // the record's length can run past the signed core into the local batch
202
204
  // session, and the signed core can carry verified-but-undownloaded blocks -
203
205
  // scan back for the newest block the signed core actually has locally
204
- async function findLocalSystemLength(core, systemLength) {
206
+ // the newest INFO the signed core holds that the batch session agrees with -
207
+ // past its dependency the batch is speculative and may have diverged
208
+ async function findLocalSystemLength(core, session, systemLength) {
205
209
  let length = Math.min(core.length, systemLength)
206
210
 
207
211
  while (length > 0) {
@@ -211,7 +215,7 @@ async function findLocalSystemLength(core, systemLength) {
211
215
  if (node) {
212
216
  const blk = decodeBlock(node)
213
217
  const entry = blk.keys[0]
214
- if (b4a.equals(entry.key, INFO)) return length
218
+ if (b4a.equals(entry.key, INFO) && (await sameTree(core, session, length))) return length
215
219
  }
216
220
 
217
221
  length--
@@ -220,6 +224,12 @@ async function findLocalSystemLength(core, systemLength) {
220
224
  assert(false, 'Expected system block to exist locally')
221
225
  }
222
226
 
227
+ async function sameTree(core, session, length) {
228
+ if (length <= session.signedLength) return true
229
+ if (length > session.length) return false
230
+ return b4a.equals(await core.treeHash(length), await session.treeHash(length))
231
+ }
232
+
223
233
  function coreLength(core, timeout) {
224
234
  if (core.length) return core.length
225
235
 
@@ -292,6 +302,9 @@ async function getCatchupHeads(session, from) {
292
302
  const entry = blk.keys[0]
293
303
 
294
304
  if (b4a.equals(entry.key, INFO)) {
305
+ // a replayed node flushes an INFO without a member block
306
+ if (batch.length === 0) continue
307
+
295
308
  const oplog = getOplog(entry, batch)
296
309
  const { key, length } = encoding.decodeSystemWriter(oplog.key, oplog.value)
297
310
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "2.9.2",
3
+ "version": "2.9.4",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",