hypercore 11.40.1 → 11.40.2

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.
@@ -556,7 +556,7 @@ class TreeProof {
556
556
  if (from >= to || to > head) {
557
557
  throw INVALID_OPERATION('Invalid upgrade')
558
558
  }
559
- if (seek && upgrade && node !== null && node.index >= from) {
559
+ if (seek && upgrade && node !== null && node.lastIndex >= upgrade.start) {
560
560
  throw INVALID_OPERATION('Cannot both do a seek and block/hash request when upgrading')
561
561
  }
562
562
 
@@ -1076,6 +1076,8 @@ function verifyTree({ block, hash, seek }, nodes) {
1076
1076
  }
1077
1077
 
1078
1078
  function verifyUpgrade({ fork, upgrade }, blockRoot, batch) {
1079
+ if (upgrade.length === 0) throw INVALID_OPERATION('Invalid upgrade')
1080
+
1079
1081
  const prologue = batch.prologue
1080
1082
 
1081
1083
  if (prologue) {
@@ -116,7 +116,9 @@ class RemoteBitfieldSegment {
116
116
  i++
117
117
  }
118
118
 
119
- return val || this.pagesLength === PAGES_PER_SEGMENT ? -1 : this.pagesLength * BITS_PER_PAGE
119
+ return val || this.pagesLength === PAGES_PER_SEGMENT
120
+ ? -1
121
+ : Math.max(position, this.pagesLength * BITS_PER_PAGE)
120
122
  }
121
123
 
122
124
  findLast(val, position) {
package/lib/replicator.js CHANGED
@@ -1337,6 +1337,7 @@ class Peer {
1337
1337
  if (start < this._remoteContiguousLength) this._remoteContiguousLength = start // bitfield is always the truth
1338
1338
  this.remoteBitfield.insert(start, bitfield)
1339
1339
  this.missingBlocks.insert(start, bitfield)
1340
+ this._remoteContiguousLength = this.remoteContiguousLength
1340
1341
  this._clearLocalRange(start, bitfield.byteLength * 8)
1341
1342
  this._update()
1342
1343
  }
@@ -1422,12 +1423,13 @@ class Peer {
1422
1423
  if (start === 0 && drop === false) {
1423
1424
  if (length > this._remoteContiguousLength) {
1424
1425
  this._remoteContiguousLength = length
1425
- if (
1426
- this.remoteFork === this.core.state.fork &&
1427
- length > this.core.header.hints.remoteContiguousLength
1428
- ) {
1429
- this.core.updateRemoteContiguousLength(length)
1430
- }
1426
+ }
1427
+
1428
+ if (
1429
+ this.remoteFork === this.core.state.fork &&
1430
+ length > this.core.header.hints.remoteContiguousLength
1431
+ ) {
1432
+ this.core.updateRemoteContiguousLength(length)
1431
1433
  }
1432
1434
  } else if (length === 1) {
1433
1435
  const bitfield = this.core.skipBitfield === null ? this.core.bitfield : this.core.skipBitfield
@@ -1441,9 +1443,21 @@ class Peer {
1441
1443
  this.remoteBitfield.setRange(rangeStart, rangeEnd, has)
1442
1444
  this.missingBlocks.setRange(rangeStart, rangeEnd, has)
1443
1445
  if (has) this._clearLocalRange(rangeStart, rangeEnd - rangeStart)
1446
+
1447
+ if (
1448
+ has &&
1449
+ start <= this._remoteContiguousLength &&
1450
+ this._remoteContiguousLength < rangeEnd
1451
+ ) {
1452
+ this._remoteContiguousLength = rangeEnd
1453
+ }
1444
1454
  }
1445
1455
  }
1446
1456
 
1457
+ if (has && start <= this._remoteContiguousLength) {
1458
+ this._remoteContiguousLength = this.remoteContiguousLength
1459
+ }
1460
+
1447
1461
  if (this.core.hintsChanged) await this.core.flushHints()
1448
1462
  if (drop === false) this._update()
1449
1463
 
@@ -2178,7 +2192,9 @@ module.exports = class Replicator {
2178
2192
  }
2179
2193
 
2180
2194
  for (const blk of notify) {
2181
- for (const r of blk.refs) {
2195
+ // detach swaps the last ref into the removed slot, so walk backwards
2196
+ for (let i = blk.refs.length - 1; i >= 0; i--) {
2197
+ const r = blk.refs[i]
2182
2198
  if (r.snapshot === false) continue
2183
2199
  blk.detach(r, SNAPSHOT_NOT_AVAILABLE())
2184
2200
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.40.1",
3
+ "version": "11.40.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {