autobee 2.5.0 → 2.5.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.
Files changed (3) hide show
  1. package/index.js +23 -10
  2. package/lib/system.js +10 -2
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -1098,6 +1098,7 @@ module.exports = class Autobee extends ReadyResource {
1098
1098
 
1099
1099
  for await (const p of this._servableRequests(standing)) {
1100
1100
  approving.push(fetchApproval.call(this, p.key, p.amount, approvals))
1101
+ if (approving.length === 32) break
1101
1102
  }
1102
1103
 
1103
1104
  if (!approving.length) return null
@@ -1179,6 +1180,10 @@ module.exports = class Autobee extends ReadyResource {
1179
1180
  return true
1180
1181
  }
1181
1182
 
1183
+ // not added and not applicable optimistically: nothing can apply this
1184
+ // batch now, so drop the writer for this drain instead of reselecting it
1185
+ // (a refresh re-adds it once it has been added or has new blocks)
1186
+ w.removePending()
1182
1187
  return true
1183
1188
  }
1184
1189
 
@@ -1190,19 +1195,19 @@ module.exports = class Autobee extends ReadyResource {
1190
1195
  if (t.view) this._workingBee.move(t.view)
1191
1196
 
1192
1197
  for (const b of t.tip) {
1193
- let failed = true
1198
+ const optimistic = b[0].optimistic
1199
+ let accepted = false
1194
1200
  try {
1195
- if (await this.system.canApply(b[0].key, true)) {
1196
- await this._applyBatch(b, true)
1197
- failed = false
1198
- }
1199
- } catch {}
1201
+ accepted = await this._applyBatch(b, optimistic)
1202
+ } catch (err) {
1203
+ if (!optimistic) throw err
1204
+ }
1200
1205
 
1201
1206
  // only check if batch was successful
1202
1207
  if (b !== batch) continue
1203
1208
 
1204
- const w = failed ? null : await this.system.get(b[0].key)
1205
- if (!w || w.length < b[0].length) {
1209
+ // declined: apply threw, or never called addWriter/ackWriter for this writer
1210
+ if (!accepted) {
1206
1211
  this._workingBee.move(rollbackView)
1207
1212
  this.system.bee.move(rollbackSystem)
1208
1213
  await this.system.reset()
@@ -1289,10 +1294,16 @@ module.exports = class Autobee extends ReadyResource {
1289
1294
  if (this._hasApply && (await this.system.canApply(batch[0].key, optimistic))) {
1290
1295
  this.stats.applies++
1291
1296
  this._host.applying = batch
1292
- await this._workingView.apply(userBatch)
1293
- this._host.applying = null
1297
+ try {
1298
+ await this._workingView.apply(userBatch)
1299
+ } finally {
1300
+ this._host.applying = null
1301
+ }
1294
1302
  }
1295
1303
 
1304
+ // read before flush clears it
1305
+ const accepted = this.system.touched(batch[0].key)
1306
+
1296
1307
  const changed = await this.system.flush(batch, this._workingBee)
1297
1308
 
1298
1309
  if (this.system.promotions.changed) this._prefetchApprovals().catch(safetyCatch)
@@ -1307,6 +1318,8 @@ module.exports = class Autobee extends ReadyResource {
1307
1318
  if (added) await this.writers.add(key)
1308
1319
  else await this.writers.remove(key)
1309
1320
  }
1321
+
1322
+ return accepted
1310
1323
  }
1311
1324
 
1312
1325
  async _storeBoot() {
package/lib/system.js CHANGED
@@ -86,11 +86,14 @@ module.exports = class Systembee {
86
86
  }
87
87
 
88
88
  async ackWriter(key, { length = 0 } = {}) {
89
+ const info = await this.get(key, { unflushed: true })
89
90
  if (length === 0) {
90
- const info = await this.get(key, { unflushed: false })
91
91
  length = info ? info.length : 0
92
92
  }
93
- this.update(key, length, -1, -1, false, false, false, -1, null)
93
+ // acked but never granted: keep the op (record its length) but record the
94
+ // writer as removed so nothing else from it is applied
95
+ const isRemoved = !info || info.isAdded === false
96
+ this.update(key, length, -1, -1, false, false, isRemoved, -1, null)
94
97
  }
95
98
 
96
99
  async removeWriter(key, { length = 0 } = {}) {
@@ -224,6 +227,11 @@ module.exports = class Systembee {
224
227
  this.update(node.key, node.length, node.weight, -1, false, false, false, node.timestamp, null)
225
228
  }
226
229
 
230
+ // did this apply add or remove (ack) the writer
231
+ touched(key) {
232
+ return this.writers.has(b4a.toString(key, 'hex'))
233
+ }
234
+
227
235
  async canApply(key, optimistic) {
228
236
  const id = b4a.toString(key, 'hex')
229
237
  const w = this.writers.get(id)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",