hypercore 10.29.0 → 10.29.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.
Files changed (2) hide show
  1. package/lib/replicator.js +36 -13
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -813,6 +813,9 @@ class Peer {
813
813
  }
814
814
 
815
815
  _requestSeek (s) {
816
+ // if replicator is updating the seeks etc, bail and wait for it to drain
817
+ if (this.replicator._updatesPending > 0) return false
818
+
816
819
  const { length, fork } = this.core.tree
817
820
 
818
821
  if (fork !== this.remoteFork) return false
@@ -851,8 +854,9 @@ class Peer {
851
854
  if (h.inflight.length > 0) continue
852
855
 
853
856
  const req = this._makeRequest(false, h.priority)
857
+ const nodes = flatTree.depth(s.seeker.start + s.seeker.end - 1)
854
858
 
855
- req.hash = { index: 2 * index, nodes: 0 }
859
+ req.hash = { index: 2 * index, nodes }
856
860
  req.seek = this.remoteSupportsSeeks ? { bytes: s.seeker.bytes, padding: s.seeker.padding } : null
857
861
 
858
862
  s.inflight.push(req)
@@ -1055,6 +1059,11 @@ class Peer {
1055
1059
  }
1056
1060
  }
1057
1061
 
1062
+ isActive () {
1063
+ if (this.paused || this.removed) return false
1064
+ return true
1065
+ }
1066
+
1058
1067
  async _send (req) {
1059
1068
  const fork = this.core.tree.fork
1060
1069
 
@@ -1070,8 +1079,15 @@ class Peer {
1070
1079
  if (req.block !== null && req.fork === fork) {
1071
1080
  req.block.nodes = await this.core.tree.missingNodes(2 * req.block.index)
1072
1081
  }
1073
- if (req.hash !== null && req.fork === fork) {
1082
+ if (req.hash !== null && req.fork === fork && req.hash.nodes === 0) {
1074
1083
  req.hash.nodes = await this.core.tree.missingNodes(req.hash.index)
1084
+
1085
+ // nodes === 0, we already have it, bail
1086
+ if (req.hash.nodes === 0 && (req.hash.index & 1) === 0) {
1087
+ this.inflight--
1088
+ this.replicator._resolveHashLocally(this, req)
1089
+ return
1090
+ }
1075
1091
  }
1076
1092
  } catch (err) {
1077
1093
  this.stream.destroy(err)
@@ -1188,7 +1204,7 @@ module.exports = class Replicator {
1188
1204
  for (const peer of this.peers) peer.signalUpgrade()
1189
1205
  if (this._blocks.isEmpty() === false) this._resolveBlocksLocally()
1190
1206
  if (this._upgrade !== null) this._resolveUpgradeRequest(null)
1191
- if (this._ranges.length !== 0 || this._seeks.length !== 0) this._updateNonPrimary()
1207
+ if (this._ranges.length !== 0 || this._seeks.length !== 0) this._updateNonPrimary(true)
1192
1208
  }
1193
1209
 
1194
1210
  // Called externally when a conflict has been detected and verified
@@ -1272,7 +1288,7 @@ module.exports = class Replicator {
1272
1288
 
1273
1289
  // Trigger this to see if this is already resolved...
1274
1290
  // Also auto compresses the range based on local bitfield
1275
- this._updateNonPrimary()
1291
+ this._updateNonPrimary(true)
1276
1292
 
1277
1293
  return ref
1278
1294
  }
@@ -1403,6 +1419,7 @@ module.exports = class Replicator {
1403
1419
 
1404
1420
  _removePeer (peer) {
1405
1421
  this.peers.splice(this.peers.indexOf(peer), 1)
1422
+ peer.removed = true
1406
1423
 
1407
1424
  if (this._manifestPeer === peer) this._manifestPeer = null
1408
1425
 
@@ -1422,6 +1439,12 @@ module.exports = class Replicator {
1422
1439
  this._queued.push(b)
1423
1440
  }
1424
1441
 
1442
+ _resolveHashLocally (peer, req) {
1443
+ this._removeInflight(req.id)
1444
+ this._resolveBlockRequest(this._hashes, req.hash.index / 2, null, req)
1445
+ this.updatePeer(peer)
1446
+ }
1447
+
1425
1448
  // Runs in the background - not allowed to throw
1426
1449
  async _resolveBlocksLocally () {
1427
1450
  // TODO: check if fork compat etc. Requires that we pass down truncation info
@@ -1530,7 +1553,7 @@ module.exports = class Replicator {
1530
1553
  }
1531
1554
 
1532
1555
  // "slow" updates here - async but not allowed to ever throw
1533
- async _updateNonPrimary () {
1556
+ async _updateNonPrimary (updateAll) {
1534
1557
  // Check if running, if so skip it and the running one will issue another update for us (debounce)
1535
1558
  while (++this._updatesPending === 1) {
1536
1559
  for (let i = 0; i < this._ranges.length; i++) {
@@ -1566,13 +1589,13 @@ module.exports = class Replicator {
1566
1589
  else s.resolve(res)
1567
1590
  }
1568
1591
 
1569
- if (this._inflight.idle) this.updateAll()
1570
-
1571
- // No additional updates scheduled - return
1572
- if (--this._updatesPending === 0) return
1592
+ // No additional updates scheduled - break
1593
+ if (--this._updatesPending === 0) break
1573
1594
  // Debounce the additional updates - continue
1574
1595
  this._updatesPending = 0
1575
1596
  }
1597
+
1598
+ if (this._inflight.idle || updateAll) this.updateAll()
1576
1599
  }
1577
1600
 
1578
1601
  _maybeResolveIfAvailableRanges () {
@@ -1643,8 +1666,8 @@ module.exports = class Replicator {
1643
1666
  this._manifestPeer = null
1644
1667
  }
1645
1668
 
1646
- if (this._seeks.length > 0 || this._ranges.length > 0) this._updateNonPrimary()
1647
- else this.updatePeer(peer)
1669
+ if (this._seeks.length > 0 || this._ranges.length > 0) this._updateNonPrimary(this._seeks.length > 0)
1670
+ this.updatePeer(peer)
1648
1671
  }
1649
1672
 
1650
1673
  _onwant (peer, start, length) {
@@ -1748,7 +1771,7 @@ module.exports = class Replicator {
1748
1771
  }
1749
1772
 
1750
1773
  _updatePeer (peer) {
1751
- if (peer.paused || peer.inflight >= peer.getMaxInflight()) {
1774
+ if (!peer.isActive() || peer.inflight >= peer.getMaxInflight()) {
1752
1775
  return false
1753
1776
  }
1754
1777
 
@@ -1780,7 +1803,7 @@ module.exports = class Replicator {
1780
1803
  }
1781
1804
 
1782
1805
  _updatePeerNonPrimary (peer) {
1783
- if (peer.paused || peer.inflight >= peer.getMaxInflight()) {
1806
+ if (!peer.isActive() || peer.inflight >= peer.getMaxInflight()) {
1784
1807
  return false
1785
1808
  }
1786
1809
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.29.0",
3
+ "version": "10.29.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {