hypercore 11.6.1 → 11.6.3

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.
package/lib/replicator.js CHANGED
@@ -705,9 +705,14 @@ class Peer {
705
705
  }
706
706
 
707
707
  const manifest = (msg.manifest && !this.core.compat) ? this.core.header.manifest : null
708
- const proof = await MerkleTree.proof(this.core.state, batch, msg)
709
708
 
710
- return new ProofRequest(msg, proof, block, manifest)
709
+ try {
710
+ const proof = await MerkleTree.proof(this.core.state, batch, msg)
711
+ return new ProofRequest(msg, proof, block, manifest)
712
+ } catch (err) {
713
+ batch.destroy()
714
+ throw err
715
+ }
711
716
  }
712
717
 
713
718
  async onrequest (msg) {
@@ -1340,6 +1345,8 @@ class Peer {
1340
1345
  }
1341
1346
 
1342
1347
  _requestForkProof (f) {
1348
+ if (!this.remoteLength) return
1349
+
1343
1350
  const req = this._makeRequest(false, 0, 0)
1344
1351
 
1345
1352
  req.upgrade = { start: 0, length: this.remoteLength }
@@ -287,14 +287,16 @@ module.exports = class SessionState {
287
287
 
288
288
  if (dependency) this.storage.setDependencyHead(dependency)
289
289
 
290
- const truncated = bitfield ? bitfield.truncated : -1
290
+ const b = bitfield
291
291
 
292
- if (truncated !== -1 && truncated < currLength) {
293
- this.ontruncate(tree, truncated, currLength, true)
294
- if (!bitfield || bitfield.length === 0) return
292
+ if (b && b.truncated && b.start < currLength) {
293
+ this.ontruncate(tree, b.start, currLength, true)
294
+ if (!b || b.appends === 0) return
295
295
  }
296
296
 
297
- this.onappend(tree, bitfield, true)
297
+ const append = b ? { start: b.start, length: b.appends, drop: false } : null
298
+
299
+ this.onappend(tree, append, true)
298
300
  } finally {
299
301
  this.mutex.unlock()
300
302
  this.core.checkIfIdle()
@@ -604,35 +606,36 @@ module.exports = class SessionState {
604
606
  const b = bitfield
605
607
 
606
608
  if (b.drop) {
607
- if (p === null) {
608
- this._pendingBitfield = { truncated: b.start, start: b.start, length: 0, drop: false }
609
- return
609
+ // truncation must be from end
610
+ if (p && (b.start + b.length !== p.start + p.appends)) {
611
+ throw INVALID_OPERATION('Atomic truncations must be contiguous')
610
612
  }
611
613
 
612
- if (p.drop || p.truncated !== -1) {
613
- if (p.truncated !== b.start + b.length) throw INVALID_OPERATION('Atomic truncations must be contiguous')
614
- p.truncated = b.start
614
+ // actual truncation
615
+ if (p === null || b.start < p.start) {
616
+ this._pendingBitfield = { truncated: true, start: b.start, appends: 0 }
615
617
  return
616
618
  }
617
619
 
618
- if (b.start < p.start) throw INVALID_OPERATION('Atomic truncations must be contiguous')
619
- p.length = b.start - p.start
620
+ // just clearing batch data
621
+ p.appends = b.start - p.start
622
+
623
+ // we cleared the current batch
624
+ if (p.appends === 0) this._pendingBitfield = null
620
625
 
621
- if (p.length === 0) this._pendingBitfield = null
622
626
  return
623
627
  }
624
628
 
625
629
  if (p === null) {
626
- this._pendingBitfield = { truncated: -1, start: b.start, length: b.length, drop: false }
630
+ this._pendingBitfield = { truncated: false, start: b.start, appends: b.length }
627
631
  return
628
632
  }
629
633
 
630
- const start = p.truncated === -1 ? p.start : p.truncated
631
- if (b.start !== start + p.length) {
634
+ if (b.start !== p.start + p.appends) {
632
635
  throw INVALID_OPERATION('Atomic operations must be contiguous')
633
636
  }
634
637
 
635
- p.length += b.length
638
+ p.appends += b.length
636
639
  }
637
640
 
638
641
  async catchup (length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.6.1",
3
+ "version": "11.6.3",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {