hypercore 10.29.0 → 10.29.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 (2) hide show
  1. package/lib/replicator.js +33 -16
  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)
@@ -1061,23 +1065,30 @@ class Peer {
1061
1065
  this.inflight++
1062
1066
  this.replicator._inflight.add(req)
1063
1067
 
1064
- if (req.upgrade !== null && req.fork === fork) {
1065
- const u = this.replicator._addUpgrade()
1066
- u.inflight.push(req)
1067
- }
1068
-
1069
1068
  try {
1070
1069
  if (req.block !== null && req.fork === fork) {
1071
1070
  req.block.nodes = await this.core.tree.missingNodes(2 * req.block.index)
1072
1071
  }
1073
- if (req.hash !== null && req.fork === fork) {
1072
+ if (req.hash !== null && req.fork === fork && req.hash.nodes === 0) {
1074
1073
  req.hash.nodes = await this.core.tree.missingNodes(req.hash.index)
1074
+
1075
+ // nodes === 0, we already have it, bail
1076
+ if (req.hash.nodes === 0 && (req.hash.index & 1) === 0) {
1077
+ this.inflight--
1078
+ this.replicator._resolveHashLocally(this, req)
1079
+ return
1080
+ }
1075
1081
  }
1076
1082
  } catch (err) {
1077
1083
  this.stream.destroy(err)
1078
1084
  return
1079
1085
  }
1080
1086
 
1087
+ if (req.upgrade !== null && req.fork === fork) {
1088
+ const u = this.replicator._addUpgrade()
1089
+ u.inflight.push(req)
1090
+ }
1091
+
1081
1092
  this.wireRequest.send(req)
1082
1093
  }
1083
1094
  }
@@ -1188,7 +1199,7 @@ module.exports = class Replicator {
1188
1199
  for (const peer of this.peers) peer.signalUpgrade()
1189
1200
  if (this._blocks.isEmpty() === false) this._resolveBlocksLocally()
1190
1201
  if (this._upgrade !== null) this._resolveUpgradeRequest(null)
1191
- if (this._ranges.length !== 0 || this._seeks.length !== 0) this._updateNonPrimary()
1202
+ if (this._ranges.length !== 0 || this._seeks.length !== 0) this._updateNonPrimary(true)
1192
1203
  }
1193
1204
 
1194
1205
  // Called externally when a conflict has been detected and verified
@@ -1272,7 +1283,7 @@ module.exports = class Replicator {
1272
1283
 
1273
1284
  // Trigger this to see if this is already resolved...
1274
1285
  // Also auto compresses the range based on local bitfield
1275
- this._updateNonPrimary()
1286
+ this._updateNonPrimary(true)
1276
1287
 
1277
1288
  return ref
1278
1289
  }
@@ -1422,6 +1433,12 @@ module.exports = class Replicator {
1422
1433
  this._queued.push(b)
1423
1434
  }
1424
1435
 
1436
+ _resolveHashLocally (peer, req) {
1437
+ this._removeInflight(req.id)
1438
+ this._resolveBlockRequest(this._hashes, req.hash.index / 2, null, req)
1439
+ this.updatePeer(peer)
1440
+ }
1441
+
1425
1442
  // Runs in the background - not allowed to throw
1426
1443
  async _resolveBlocksLocally () {
1427
1444
  // TODO: check if fork compat etc. Requires that we pass down truncation info
@@ -1530,7 +1547,7 @@ module.exports = class Replicator {
1530
1547
  }
1531
1548
 
1532
1549
  // "slow" updates here - async but not allowed to ever throw
1533
- async _updateNonPrimary () {
1550
+ async _updateNonPrimary (updateAll) {
1534
1551
  // Check if running, if so skip it and the running one will issue another update for us (debounce)
1535
1552
  while (++this._updatesPending === 1) {
1536
1553
  for (let i = 0; i < this._ranges.length; i++) {
@@ -1566,13 +1583,13 @@ module.exports = class Replicator {
1566
1583
  else s.resolve(res)
1567
1584
  }
1568
1585
 
1569
- if (this._inflight.idle) this.updateAll()
1570
-
1571
- // No additional updates scheduled - return
1572
- if (--this._updatesPending === 0) return
1586
+ // No additional updates scheduled - break
1587
+ if (--this._updatesPending === 0) break
1573
1588
  // Debounce the additional updates - continue
1574
1589
  this._updatesPending = 0
1575
1590
  }
1591
+
1592
+ if (this._inflight.idle || updateAll) this.updateAll()
1576
1593
  }
1577
1594
 
1578
1595
  _maybeResolveIfAvailableRanges () {
@@ -1643,8 +1660,8 @@ module.exports = class Replicator {
1643
1660
  this._manifestPeer = null
1644
1661
  }
1645
1662
 
1646
- if (this._seeks.length > 0 || this._ranges.length > 0) this._updateNonPrimary()
1647
- else this.updatePeer(peer)
1663
+ if (this._seeks.length > 0 || this._ranges.length > 0) this._updateNonPrimary(this._seeks.length > 0)
1664
+ this.updatePeer(peer)
1648
1665
  }
1649
1666
 
1650
1667
  _onwant (peer, start, length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.29.0",
3
+ "version": "10.29.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {