autobee 2.0.0-rc.20 → 2.0.0-rc.21

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
@@ -717,7 +724,7 @@ module.exports = class Autobee extends ReadyResource {
717
724
  value: m.node.value
718
725
  }
719
726
 
720
- if (m.digest === null || m.checkpoint === null) return op
727
+ if (m.digest === null || m.checkpoint === null || !m.checkpoint.system) return op
721
728
 
722
729
  const fetches = []
723
730
 
@@ -733,9 +740,12 @@ module.exports = class Autobee extends ReadyResource {
733
740
  const { digest } = encoding.decodeRawOplog(digestNode)
734
741
  const { checkpoint } = encoding.decodeRawOplog(checkpointNode)
735
742
 
743
+ if (!checkpoint.system || !checkpoint.system.checkpoint) return op
744
+
736
745
  op.views = {
737
746
  system: {
738
747
  key: digest.key,
748
+ start: 0,
739
749
  length: checkpoint.system.checkpoint.length
740
750
  },
741
751
  flushes: seq
@@ -990,6 +1000,9 @@ module.exports = class Autobee extends ReadyResource {
990
1000
  this.stats.undos++
991
1001
  this.trusted.clear()
992
1002
  t.view = await this.system.undo(t.undo)
1003
+ if (!t.view.length && this._migratedHead && this._migratedHead.view) {
1004
+ t.view = this._migratedHead.view
1005
+ }
993
1006
  }
994
1007
 
995
1008
  return t
@@ -1076,15 +1089,14 @@ module.exports = class Autobee extends ReadyResource {
1076
1089
  const boot = this.system.bootRecord()
1077
1090
  if (boot) {
1078
1091
  proms.push(this.local.setUserData('autobee/head', encoding.encodeBootRecord(boot)))
1079
- if (this.system.view) proms.push(this._storeMigratedView(EMPTY_HEAD))
1080
1092
  }
1081
1093
 
1082
1094
  return Promise.all(proms)
1083
1095
  }
1084
1096
 
1085
- _storeMigratedView(view) {
1086
- const value = view.key ? encoding.encodeBootRecord(view) : null
1087
- return this.local.setUserData('autobee/migrated-view', value)
1097
+ _storeMigratedHead() {
1098
+ const value = this._migratedHead ? encoding.encodeMigratedHead(this._migratedHead) : null
1099
+ return this.local.setUserData('autobee/migrated-head', value)
1088
1100
  }
1089
1101
 
1090
1102
  static decodeValue(buf, opts) {
@@ -1316,8 +1328,14 @@ module.exports = class Autobee extends ReadyResource {
1316
1328
 
1317
1329
  // migrate is set when fast-forwarding from a legacy head
1318
1330
  if (migrate) {
1319
- const view = (await this._handlers.migrate(migrate)) || EMPTY_HEAD
1320
- await this._storeMigratedView(view)
1331
+ const view = (await this._handlers.migrate(migrate, head)) || EMPTY_HEAD
1332
+
1333
+ this._migratedHead = {
1334
+ system: head,
1335
+ view: view.length ? view : (this._migratedHead && this._migratedHead.view) || null
1336
+ }
1337
+
1338
+ await this._storeMigratedHead()
1321
1339
  this.bee.move(view)
1322
1340
  this._workingBee.move(view)
1323
1341
  } 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/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.21",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",