autobee 2.0.0-rc.20 → 2.0.0-rc.22

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.
@@ -1048,6 +1048,33 @@ const encoding32 = {
1048
1048
  }
1049
1049
  }
1050
1050
 
1051
+ // @autobee/migrated-head
1052
+ const encoding33 = {
1053
+ preencode(state, m) {
1054
+ encoding22.preencode(state, m.system)
1055
+ state.end++ // flags are fixed size
1056
+
1057
+ if (m.view) encoding22.preencode(state, m.view)
1058
+ },
1059
+ encode(state, m) {
1060
+ const flags = m.view ? 1 : 0
1061
+
1062
+ encoding22.encode(state, m.system)
1063
+ c.uint8.encode(state, flags)
1064
+
1065
+ if (m.view) encoding22.encode(state, m.view)
1066
+ },
1067
+ decode(state) {
1068
+ const r0 = encoding22.decode(state)
1069
+ const flags = c.uint8.decode(state)
1070
+
1071
+ return {
1072
+ system: r0,
1073
+ view: (flags & 1) !== 0 ? encoding22.decode(state) : null
1074
+ }
1075
+ }
1076
+ }
1077
+
1051
1078
  // @autobee/system-info-v3.heads, deferred due to recusive use
1052
1079
  const encoding18_3 = c.array(encoding22)
1053
1080
  // @autobee/system-info-v3.indexers, deferred due to recusive use
@@ -1146,6 +1173,8 @@ function getEncoding(name) {
1146
1173
  return encoding31
1147
1174
  case '@autobee/trusted-head':
1148
1175
  return encoding32
1176
+ case '@autobee/migrated-head':
1177
+ return encoding33
1149
1178
  default:
1150
1179
  throw new Error('Encoder not found ' + name)
1151
1180
  }
@@ -836,6 +836,26 @@
836
836
  "version": 1
837
837
  }
838
838
  ]
839
+ },
840
+ {
841
+ "name": "migrated-head",
842
+ "namespace": "autobee",
843
+ "compact": true,
844
+ "flagsPosition": 1,
845
+ "fields": [
846
+ {
847
+ "name": "system",
848
+ "required": true,
849
+ "type": "@autobee/link",
850
+ "version": 1
851
+ },
852
+ {
853
+ "name": "view",
854
+ "required": false,
855
+ "type": "@autobee/link",
856
+ "version": 1
857
+ }
858
+ ]
839
859
  }
840
860
  ],
841
861
  "namespaces": [
package/index.js CHANGED
@@ -132,6 +132,7 @@ module.exports = class Autobee extends ReadyResource {
132
132
  this.previousDrain = 0
133
133
 
134
134
  this._catchupMigratedNodes = null
135
+ this._migratedHead = null
135
136
 
136
137
  this.ready().catch(noop)
137
138
  }
@@ -390,20 +391,26 @@ module.exports = class Autobee extends ReadyResource {
390
391
 
391
392
  await this.system.boot(system)
392
393
 
393
- // Use the view position from the system info (authoritative, post-processing)
394
- // rather than from the oplog (stale, captured at append time before _bump)
394
+ const migrated = await this.local.getUserData('autobee/migrated-head')
395
+ if (migrated) this._migratedHead = encoding.decodeMigratedHead(migrated)
396
+
395
397
  let view = this.system.view
396
- if (!view) {
397
- const migrated = await this.local.getUserData('autobee/migrated-view')
398
- view = migrated ? encoding.decodeBootRecord(migrated) : EMPTY_HEAD
399
- }
398
+ if (!view) view = (this._migratedHead && this._migratedHead.view) || EMPTY_HEAD
400
399
 
401
400
  // @todo migration
402
401
  if (result.migration) {
403
402
  if (this._handlers.migrate) {
404
- view = (await this._handlers.migrate(result.migration.views)) || EMPTY_HEAD
403
+ view =
404
+ (await this._handlers.migrate(result.migration.views, result.migration.system)) ||
405
+ EMPTY_HEAD
405
406
  this._catchupMigratedNodes = result.migration.catchup
406
- await this._storeMigratedView(view)
407
+
408
+ this._migratedHead = {
409
+ system: result.migration.system,
410
+ view: view.length ? view : (this._migratedHead && this._migratedHead.view) || null
411
+ }
412
+
413
+ await this._storeMigratedHead()
407
414
  }
408
415
 
409
416
  // ff boot invalidated by migration
@@ -429,9 +436,8 @@ module.exports = class Autobee extends ReadyResource {
429
436
  async _bootAll() {
430
437
  if (!(await this._bootReady())) return
431
438
 
432
- for await (const node of this.system.list()) {
433
- if (node.isAnchor) continue
434
- await this.writers.add(node.key)
439
+ for (const head of this.system.heads) {
440
+ await this.writers.add(head.key)
435
441
  }
436
442
  await this._bump()
437
443
  }
@@ -717,7 +723,7 @@ module.exports = class Autobee extends ReadyResource {
717
723
  value: m.node.value
718
724
  }
719
725
 
720
- if (m.digest === null || m.checkpoint === null) return op
726
+ if (m.digest === null || m.checkpoint === null || !m.checkpoint.system) return op
721
727
 
722
728
  const fetches = []
723
729
 
@@ -733,9 +739,12 @@ module.exports = class Autobee extends ReadyResource {
733
739
  const { digest } = encoding.decodeRawOplog(digestNode)
734
740
  const { checkpoint } = encoding.decodeRawOplog(checkpointNode)
735
741
 
742
+ if (!checkpoint.system || !checkpoint.system.checkpoint) return op
743
+
736
744
  op.views = {
737
745
  system: {
738
746
  key: digest.key,
747
+ start: 0,
739
748
  length: checkpoint.system.checkpoint.length
740
749
  },
741
750
  flushes: seq
@@ -990,6 +999,9 @@ module.exports = class Autobee extends ReadyResource {
990
999
  this.stats.undos++
991
1000
  this.trusted.clear()
992
1001
  t.view = await this.system.undo(t.undo)
1002
+ if (!t.view.length && this._migratedHead && this._migratedHead.view) {
1003
+ t.view = this._migratedHead.view
1004
+ }
993
1005
  }
994
1006
 
995
1007
  return t
@@ -1076,15 +1088,14 @@ module.exports = class Autobee extends ReadyResource {
1076
1088
  const boot = this.system.bootRecord()
1077
1089
  if (boot) {
1078
1090
  proms.push(this.local.setUserData('autobee/head', encoding.encodeBootRecord(boot)))
1079
- if (this.system.view) proms.push(this._storeMigratedView(EMPTY_HEAD))
1080
1091
  }
1081
1092
 
1082
1093
  return Promise.all(proms)
1083
1094
  }
1084
1095
 
1085
- _storeMigratedView(view) {
1086
- const value = view.key ? encoding.encodeBootRecord(view) : null
1087
- return this.local.setUserData('autobee/migrated-view', value)
1096
+ _storeMigratedHead() {
1097
+ const value = this._migratedHead ? encoding.encodeMigratedHead(this._migratedHead) : null
1098
+ return this.local.setUserData('autobee/migrated-head', value)
1088
1099
  }
1089
1100
 
1090
1101
  static decodeValue(buf, opts) {
@@ -1316,8 +1327,14 @@ module.exports = class Autobee extends ReadyResource {
1316
1327
 
1317
1328
  // migrate is set when fast-forwarding from a legacy head
1318
1329
  if (migrate) {
1319
- const view = (await this._handlers.migrate(migrate)) || EMPTY_HEAD
1320
- await this._storeMigratedView(view)
1330
+ const view = (await this._handlers.migrate(migrate, head)) || EMPTY_HEAD
1331
+
1332
+ this._migratedHead = {
1333
+ system: head,
1334
+ view: view.length ? view : (this._migratedHead && this._migratedHead.view) || null
1335
+ }
1336
+
1337
+ await this._storeMigratedHead()
1321
1338
  this.bee.move(view)
1322
1339
  this._workingBee.move(view)
1323
1340
  } else {
package/lib/boot.js CHANGED
@@ -257,7 +257,9 @@ async function getWriterBatch(store, head, key, encryptionKey, nodes = []) {
257
257
  const core = store.get(head.key)
258
258
  await core.ready()
259
259
 
260
- await core.setEncryption(AutobeeEncryption.getWriterEncryption(key, encryptionKey))
260
+ if (encryptionKey) {
261
+ await core.setEncryption(AutobeeEncryption.getWriterEncryption(key, encryptionKey))
262
+ }
261
263
 
262
264
  let seq = head.length - 1
263
265
  const block = await core.get(seq--, { wait: false })
package/lib/encoding.js CHANGED
@@ -12,6 +12,7 @@ const SystemInfo = getEncoding('@autobee/system-info')
12
12
  const SystemWriter = getEncoding('@autobee/system-writer')
13
13
  const ManifestData = getEncoding('@autobee/manifest-data')
14
14
  const AutobaseBootRecord = getEncoding('@autobase-compat/boot-record')
15
+ const MigratedHead = getEncoding('@autobee/migrated-head')
15
16
 
16
17
  exports.OPLOG_VERSION = OPLOG_VERSION
17
18
 
@@ -21,6 +22,9 @@ exports.ManifestData = ManifestData
21
22
  exports.encodeBootRecord = encodeBootRecord
22
23
  exports.decodeBootRecord = decodeBootRecord
23
24
 
25
+ exports.encodeMigratedHead = encodeMigratedHead
26
+ exports.decodeMigratedHead = decodeMigratedHead
27
+
24
28
  exports.encodePreviousDrain = encodePreviousDrain
25
29
  exports.decodePreviousDrain = decodePreviousDrain
26
30
 
@@ -39,6 +43,14 @@ function decodeBootRecord(value) {
39
43
  return c.decode(Link, value)
40
44
  }
41
45
 
46
+ function encodeMigratedHead(m) {
47
+ return c.encode(MigratedHead, m)
48
+ }
49
+
50
+ function decodeMigratedHead(value) {
51
+ return c.decode(MigratedHead, value)
52
+ }
53
+
42
54
  function encodePreviousDrain(d) {
43
55
  return c.encode(c.uint, d)
44
56
  }
@@ -234,8 +234,15 @@ module.exports = class FastForward {
234
234
 
235
235
  // a legacy system needs migrating rather than a plain fast-forward
236
236
  if (this.system.version <= LEGACY_AUTOBASE_VERSION) {
237
- // already migrated, don't migrate again
238
- if (this.auto.system.version > LEGACY_AUTOBASE_VERSION) return null
237
+ const migrated = this.auto._migratedHead && this.auto._migratedHead.system
238
+ if (
239
+ migrated &&
240
+ b4a.equals(migrated.key, this.head.key) &&
241
+ migrated.length >= this.head.length
242
+ ) {
243
+ return null
244
+ }
245
+
239
246
  return this._migrate()
240
247
  }
241
248
 
package/lib/system.js CHANGED
@@ -411,6 +411,7 @@ module.exports = class Systembee {
411
411
 
412
412
  w.tryPut(INFO_KEY, encoding.encodeSystemInfo(info))
413
413
 
414
+ this.version = info.version
414
415
  this.view = info.view
415
416
 
416
417
  await w.flush()
package/lib/writers.js CHANGED
@@ -633,7 +633,15 @@ class Writer {
633
633
  const promises = new Array(links.length)
634
634
 
635
635
  for (let i = 0; i < links.length; i++) {
636
- promises[i] = this.system.has(links[i])
636
+ const link = links[i]
637
+
638
+ // legacy batch nodes link their own previous node, satisfied by oplog order
639
+ if (link.length < node.length && b4a.equals(link.key, this.core.key)) {
640
+ promises[i] = true
641
+ continue
642
+ }
643
+
644
+ promises[i] = this.system.has(link)
637
645
  }
638
646
 
639
647
  const has = await Promise.all(promises)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "2.0.0-rc.20",
3
+ "version": "2.0.0-rc.22",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "brittle": "^4.1.0",
41
- "corestore": "^7.11.0",
41
+ "corestore": "^7.12.2",
42
42
  "hypercore-crypto": "^3.6.1",
43
43
  "lunte": "^1.6.0",
44
44
  "prettier": "^3.8.1",