hypercore 11.40.0 → 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.
@@ -12,6 +12,11 @@ const {
12
12
  ASSERTION
13
13
  } = require('hypercore-errors')
14
14
 
15
+ // the length can be 2 ** 64 in practice, but js numbers limit it to 2 ** 52
16
+ const MAX_LENGTH = 2 ** 52
17
+ // a proof needs at most two nodes per level, for the 64 levels a tree can have
18
+ const MAX_NODES = 2 * 64
19
+
15
20
  class NodeQueue {
16
21
  constructor(nodes, root = null) {
17
22
  this.i = 0
@@ -551,7 +556,7 @@ class TreeProof {
551
556
  if (from >= to || to > head) {
552
557
  throw INVALID_OPERATION('Invalid upgrade')
553
558
  }
554
- if (seek && upgrade && node !== null && node.index >= from) {
559
+ if (seek && upgrade && node !== null && node.lastIndex >= upgrade.start) {
555
560
  throw INVALID_OPERATION('Cannot both do a seek and block/hash request when upgrading')
556
561
  }
557
562
 
@@ -575,7 +580,7 @@ class TreeProof {
575
580
 
576
581
  if (head > to) {
577
582
  pending.additionalUpgrade = []
578
- upgradeProof(this, node, seek, to, head, subTree, pending.additionalUpgrade)
583
+ upgradeProof(this, node, seek, to, head, seek ? head : subTree, pending.additionalUpgrade)
579
584
  }
580
585
  }
581
586
 
@@ -643,6 +648,8 @@ class RefitTreeProof extends TreeProof {
643
648
 
644
649
  this.insufficient = false
645
650
 
651
+ if (hasTooManyNodes(proof)) throw INVALID_OPERATION('Too many nodes')
652
+
646
653
  const nodes = []
647
654
  verifyTree(proof, nodes)
648
655
  nodes.push(...proof.upgrade.nodes, ...proof.upgrade.additionalNodes)
@@ -656,6 +663,8 @@ class RefitTreeProof extends TreeProof {
656
663
  length = Math.max(length, flat.rightSpan(node.index) / 2 + 1)
657
664
  }
658
665
 
666
+ if (length > MAX_LENGTH) throw INVALID_OPERATION('Upgrade is out of bounds')
667
+
659
668
  if (length > this.length) {
660
669
  this.upgrade = { start: this.length, length: length - this.length }
661
670
  this.length = length
@@ -1067,6 +1076,8 @@ function verifyTree({ block, hash, seek }, nodes) {
1067
1076
  }
1068
1077
 
1069
1078
  function verifyUpgrade({ fork, upgrade }, blockRoot, batch) {
1079
+ if (upgrade.length === 0) throw INVALID_OPERATION('Invalid upgrade')
1080
+
1070
1081
  const prologue = batch.prologue
1071
1082
 
1072
1083
  if (prologue) {
@@ -1299,6 +1310,13 @@ function upgradeProof(proof, node, seek, from, to, subTree, nodes) {
1299
1310
  }
1300
1311
  }
1301
1312
 
1313
+ function hasTooManyNodes(proof) {
1314
+ if (proof.block && proof.block.nodes.length > MAX_NODES) return true
1315
+ if (proof.hash && proof.hash.nodes.length > MAX_NODES) return true
1316
+ if (!proof.upgrade) return false
1317
+ return proof.upgrade.nodes.length > MAX_NODES || proof.upgrade.additionalNodes.length > MAX_NODES
1318
+ }
1319
+
1302
1320
  function nodesToRoot(index, nodes, head) {
1303
1321
  const ite = flat.iterator(index)
1304
1322
 
@@ -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
@@ -1110,7 +1110,7 @@ class Peer {
1110
1110
  return
1111
1111
  }
1112
1112
 
1113
- if (!this.isActive() && proof.block !== null) {
1113
+ if (!this.isActive() && proof !== null && proof.block !== null) {
1114
1114
  return
1115
1115
  }
1116
1116
 
@@ -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.0",
3
+ "version": "11.40.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {