autobee 2.0.0-rc.10 → 2.0.0-rc.11

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 (3) hide show
  1. package/index.js +11 -4
  2. package/lib/topo.js +25 -2
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -88,6 +88,7 @@ module.exports = class Autobee extends ReadyResource {
88
88
 
89
89
  this._localSystemStart = 0
90
90
  this._localSystemLength = 0
91
+ this._localFlushes = 0
91
92
  this._localViewStart = 0
92
93
  this._localViewLength = 0
93
94
 
@@ -163,6 +164,7 @@ module.exports = class Autobee extends ReadyResource {
163
164
 
164
165
  this._localSystemStart = this.system.bee.context.local.length
165
166
  this._localViewStart = this._workingBee.context.local.length
167
+ this._localFlushes = this.system.flushes
166
168
 
167
169
  this.bumpSoon()
168
170
  }
@@ -967,6 +969,7 @@ module.exports = class Autobee extends ReadyResource {
967
969
  if (local) {
968
970
  this._localSystemLength = this.system.bee.context.local.length - this._localSystemStart
969
971
  this._localViewLength = this._workingBee.context.local.length - this._localViewStart
972
+ this._localFlushes = this.system.flushes
970
973
  }
971
974
 
972
975
  this.writers.attest(witnessed)
@@ -1081,7 +1084,7 @@ module.exports = class Autobee extends ReadyResource {
1081
1084
 
1082
1085
  async _flushLocal() {
1083
1086
  await this.writers.flushLocal({
1084
- flushes: this.system.flushes,
1087
+ flushes: this._localFlushes,
1085
1088
  system: {
1086
1089
  key: this.system.bee.context.local.key,
1087
1090
  start: this._localSystemStart,
@@ -1096,6 +1099,7 @@ module.exports = class Autobee extends ReadyResource {
1096
1099
 
1097
1100
  this._localSystemStart = this.system.bee.context.local.length
1098
1101
  this._localSystemLength = 0
1102
+ this._localFlushes = this.system.flushes
1099
1103
  this._localViewStart = this._workingBee.context.local.length
1100
1104
  this._localViewLength = 0
1101
1105
  }
@@ -1168,13 +1172,15 @@ module.exports = class Autobee extends ReadyResource {
1168
1172
  return null
1169
1173
  }
1170
1174
 
1171
- const moved = await this._moveTo(batchToHead(verified.op.views.system), {
1175
+ const tip = {
1172
1176
  system: batchToHead(oplog.op.views.system),
1173
1177
  verified: {
1174
1178
  op: trusted || head,
1175
1179
  flushes: verified.op.views.flushes
1176
1180
  }
1177
- })
1181
+ }
1182
+
1183
+ const moved = await this._moveTo(batchToHead(verified.op.views.system), tip)
1178
1184
 
1179
1185
  if (moved && wait) return this.reboot.promise
1180
1186
 
@@ -1277,7 +1283,8 @@ module.exports = class Autobee extends ReadyResource {
1277
1283
  const t = await topo.rollback(this, sys, verified)
1278
1284
  await sys.close()
1279
1285
 
1280
- await this.applyBacklog(t.tip)
1286
+ if (t !== null) await this.applyBacklog(t.tip)
1287
+
1281
1288
  return this._update(changes)
1282
1289
  }
1283
1290
 
package/lib/topo.js CHANGED
@@ -42,20 +42,35 @@ async function getMostRecentHead(auto, system) {
42
42
  }
43
43
 
44
44
  async function rollback(auto, system, verified) {
45
+ if (!(await isApplied(system, verified.op))) return null
46
+
45
47
  const tip = []
46
48
  const ch = system.createChangesStream()
47
49
 
50
+ let found = false
51
+
48
52
  for await (const data of ch) {
49
53
  const { ref, info } = getOplog(data)
54
+
50
55
  if (sameNode(ref.oplog, verified.op)) {
51
- asserts.assert(info.flushes === verified.flushes, 'Expect tip to be continuous')
56
+ // stale views.flushes - the verified head describes a prefix this system
57
+ // does not share, so nothing above it is ours to reapply
58
+ if (info.flushes === verified.flushes) found = true
59
+ break
60
+ }
61
+
62
+ // walking back, a writer's lengths only decrease - once this writer is
63
+ // behind the verified head the marker cannot be further down
64
+ if (b4a.equals(ref.oplog.key, verified.op.key) && ref.oplog.length < verified.op.length) {
52
65
  break
53
66
  }
54
67
 
55
68
  tip.push(ref)
56
69
  }
57
70
 
58
- const inflated = await inflate(auto, tip)
71
+ if (!found) return null
72
+
73
+ const inflated = await inflate(auto, tip.reverse())
59
74
 
60
75
  // @todo: verify sort
61
76
 
@@ -86,6 +101,14 @@ async function replay(auto) {
86
101
  return inflated.flatMap((b) => (b === null ? [null] : b.batch))
87
102
  }
88
103
 
104
+ async function isApplied(system, node) {
105
+ const entry = await system.get(encoding.encodeSystemWriterKey(node.key))
106
+ if (entry === null) return false
107
+
108
+ const info = encoding.decodeSystemWriter(entry.key, entry.value)
109
+ return info.length >= node.length
110
+ }
111
+
89
112
  // tolerates legacy nodes instead of asserting
90
113
  async function* oplogIterator(bee) {
91
114
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "2.0.0-rc.10",
3
+ "version": "2.0.0-rc.11",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",