hypercore 11.23.1 → 11.24.0
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/index.js +9 -1
- package/lib/replicator.js +33 -13
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -318,6 +318,11 @@ class Hypercore extends EventEmitter {
|
|
|
318
318
|
this.encodeBatch = opts.encodeBatch
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
+
// one session sets for pushOnly for all
|
|
322
|
+
if (opts.pushOnly === true) {
|
|
323
|
+
this.core.replicator.setPushOnly(true)
|
|
324
|
+
}
|
|
325
|
+
|
|
321
326
|
if (parent) {
|
|
322
327
|
if (parent._stateIndex === -1) await parent.ready()
|
|
323
328
|
if (!this.keyPair) this.keyPair = parent.keyPair
|
|
@@ -858,7 +863,8 @@ class Hypercore extends EventEmitter {
|
|
|
858
863
|
|
|
859
864
|
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
|
|
860
865
|
|
|
861
|
-
const
|
|
866
|
+
const force = opts ? opts.force === true : false
|
|
867
|
+
const req = this.core.replicator.addBlock(activeRequests, index, force)
|
|
862
868
|
req.snapshot = index < this.length
|
|
863
869
|
|
|
864
870
|
const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
|
|
@@ -980,6 +986,7 @@ class Hypercore extends EventEmitter {
|
|
|
980
986
|
async treeHash(length = -1) {
|
|
981
987
|
if (this.opened === false) await this.opening
|
|
982
988
|
if (length === -1) length = this.length
|
|
989
|
+
if (length > 0 && !(await this.has(length - 1))) await this.get(length - 1)
|
|
983
990
|
|
|
984
991
|
const roots = await MerkleTree.getRoots(this.state, length)
|
|
985
992
|
return crypto.tree(roots)
|
|
@@ -1150,6 +1157,7 @@ function initOnce(session, storage, key, opts) {
|
|
|
1150
1157
|
notDownloadingLinger: opts.notDownloadingLinger,
|
|
1151
1158
|
allowFork: opts.allowFork !== false,
|
|
1152
1159
|
allowPush: !!opts.allowPush,
|
|
1160
|
+
pushOnly: !!opts.pushOnly,
|
|
1153
1161
|
alwaysLatestBlock: !!opts.allowLatestBlock,
|
|
1154
1162
|
inflightRange: opts.inflightRange,
|
|
1155
1163
|
compat: opts.compat === true,
|
package/lib/replicator.js
CHANGED
|
@@ -197,7 +197,7 @@ class Attachable {
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
class BlockRequest extends Attachable {
|
|
200
|
-
constructor(replicator, tracker, index, priority) {
|
|
200
|
+
constructor(replicator, tracker, index, priority, force) {
|
|
201
201
|
super(replicator)
|
|
202
202
|
|
|
203
203
|
this.index = index
|
|
@@ -206,6 +206,7 @@ class BlockRequest extends Attachable {
|
|
|
206
206
|
this.queued = false
|
|
207
207
|
this.hotswap = null
|
|
208
208
|
this.tracker = tracker
|
|
209
|
+
this.force = force
|
|
209
210
|
}
|
|
210
211
|
|
|
211
212
|
_unref() {
|
|
@@ -409,11 +410,11 @@ class BlockTracker {
|
|
|
409
410
|
return this._map.get(index) || null
|
|
410
411
|
}
|
|
411
412
|
|
|
412
|
-
add(index, priority) {
|
|
413
|
+
add(index, priority, force) {
|
|
413
414
|
let b = this._map.get(index)
|
|
414
415
|
if (b) return b
|
|
415
416
|
|
|
416
|
-
b = new BlockRequest(this._replicator, this, index, priority)
|
|
417
|
+
b = new BlockRequest(this._replicator, this, index, priority, force)
|
|
417
418
|
this._map.set(index, b)
|
|
418
419
|
|
|
419
420
|
return b
|
|
@@ -1452,6 +1453,8 @@ class Peer {
|
|
|
1452
1453
|
|
|
1453
1454
|
_requestManifest() {
|
|
1454
1455
|
const req = this._makeRequest(false, 0, 0)
|
|
1456
|
+
if (req === null) return
|
|
1457
|
+
|
|
1455
1458
|
this._send(req)
|
|
1456
1459
|
}
|
|
1457
1460
|
|
|
@@ -1465,15 +1468,17 @@ class Peer {
|
|
|
1465
1468
|
|
|
1466
1469
|
// do the normal checks
|
|
1467
1470
|
if (!this._remoteHasBlock(index)) return null
|
|
1468
|
-
if (!this._canRequest(index)) return null
|
|
1471
|
+
if (!this._canRequest(index, false)) return null
|
|
1469
1472
|
|
|
1470
1473
|
// atm we only ever do one upgrade request in parallel, if that changes
|
|
1471
1474
|
// then we should add some check here for inflights to avoid over requesting
|
|
1472
|
-
const b = this.replicator._blocks.add(index, PRIORITY.NORMAL)
|
|
1475
|
+
const b = this.replicator._blocks.add(index, PRIORITY.NORMAL, false)
|
|
1473
1476
|
return b
|
|
1474
1477
|
}
|
|
1475
1478
|
|
|
1476
1479
|
_requestUpgrade(u) {
|
|
1480
|
+
if (this.replicator.pushOnly) return false
|
|
1481
|
+
|
|
1477
1482
|
const req = this._makeRequest(true, 0, 0)
|
|
1478
1483
|
if (req === null) return false
|
|
1479
1484
|
|
|
@@ -1488,6 +1493,7 @@ class Peer {
|
|
|
1488
1493
|
_requestSeek(s) {
|
|
1489
1494
|
// if replicator is updating the seeks etc, bail and wait for it to drain
|
|
1490
1495
|
if (this.replicator._updatesPending > 0) return false
|
|
1496
|
+
if (this.replicator.pushOnly) return false
|
|
1491
1497
|
|
|
1492
1498
|
const { length, fork } = this.core.state
|
|
1493
1499
|
|
|
@@ -1518,14 +1524,14 @@ class Peer {
|
|
|
1518
1524
|
|
|
1519
1525
|
if (this._remoteHasBlock(index) === false) continue
|
|
1520
1526
|
if (this.core.bitfield.get(index) === true) continue
|
|
1521
|
-
if (!this._canRequest(index)) continue
|
|
1527
|
+
if (!this._canRequest(index, false)) continue
|
|
1522
1528
|
|
|
1523
1529
|
// Check if this block is currently inflight - if so pick another
|
|
1524
1530
|
const b = this.replicator._blocks.get(index)
|
|
1525
1531
|
if (b !== null && b.inflight.length > 0) continue
|
|
1526
1532
|
|
|
1527
1533
|
// Block is not inflight, but we only want the hash, check if that is inflight
|
|
1528
|
-
const h = this.replicator._hashes.add(index, PRIORITY.NORMAL)
|
|
1534
|
+
const h = this.replicator._hashes.add(index, PRIORITY.NORMAL, false)
|
|
1529
1535
|
if (h.inflight.length > 0) continue
|
|
1530
1536
|
|
|
1531
1537
|
const req = this._makeRequest(false, h.priority, index + 1)
|
|
@@ -1549,9 +1555,13 @@ class Peer {
|
|
|
1549
1555
|
return false
|
|
1550
1556
|
}
|
|
1551
1557
|
|
|
1552
|
-
_canRequest(index) {
|
|
1558
|
+
_canRequest(index, force) {
|
|
1553
1559
|
if (!(index >= 0)) throw ASSERTION('bad index to _canRequest: ' + index)
|
|
1554
1560
|
|
|
1561
|
+
if (!force && this.replicator.pushOnly) {
|
|
1562
|
+
return false
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1555
1565
|
if (this.remoteLength >= this.core.state.length) {
|
|
1556
1566
|
return true
|
|
1557
1567
|
}
|
|
@@ -1588,7 +1598,7 @@ class Peer {
|
|
|
1588
1598
|
return false
|
|
1589
1599
|
}
|
|
1590
1600
|
|
|
1591
|
-
if (!this._canRequest(b.index)) return false
|
|
1601
|
+
if (!this._canRequest(b.index, b.force)) return false
|
|
1592
1602
|
|
|
1593
1603
|
const req = this._makeRequest(b.index >= length, b.priority, b.index + 1)
|
|
1594
1604
|
if (req === null) return false
|
|
@@ -1636,9 +1646,9 @@ class Peer {
|
|
|
1636
1646
|
}
|
|
1637
1647
|
|
|
1638
1648
|
_requestRangeBlock(index, length) {
|
|
1639
|
-
if (this.core.bitfield.get(index) === true || !this._canRequest(index)) return false
|
|
1649
|
+
if (this.core.bitfield.get(index) === true || !this._canRequest(index, false)) return false
|
|
1640
1650
|
|
|
1641
|
-
const b = this.replicator._blocks.add(index, PRIORITY.NORMAL)
|
|
1651
|
+
const b = this.replicator._blocks.add(index, PRIORITY.NORMAL, false)
|
|
1642
1652
|
if (b.inflight.length > 0) {
|
|
1643
1653
|
this.missingBlocks.set(index, false) // in case we missed some states just set them ondemand, nbd
|
|
1644
1654
|
return false
|
|
@@ -1789,8 +1799,10 @@ class Peer {
|
|
|
1789
1799
|
|
|
1790
1800
|
_requestForkProof(f) {
|
|
1791
1801
|
if (!this.remoteLength) return
|
|
1802
|
+
if (this.replicator.pushOnly) return
|
|
1792
1803
|
|
|
1793
1804
|
const req = this._makeRequest(false, 0, 0)
|
|
1805
|
+
if (req === null) return
|
|
1794
1806
|
|
|
1795
1807
|
req.upgrade = { start: 0, length: this.remoteLength }
|
|
1796
1808
|
req.manifest = !this.core.header.manifest
|
|
@@ -1801,6 +1813,7 @@ class Peer {
|
|
|
1801
1813
|
|
|
1802
1814
|
_requestForkRange(f) {
|
|
1803
1815
|
if (f.fork !== this.remoteFork || f.batch.want === null) return false
|
|
1816
|
+
if (this.replicator.pushOnly) return false
|
|
1804
1817
|
|
|
1805
1818
|
const end = Math.min(f.batch.want.end, this.remoteLength)
|
|
1806
1819
|
if (end < f.batch.want.start) return false
|
|
@@ -1815,6 +1828,7 @@ class Peer {
|
|
|
1815
1828
|
if (this._remoteHasBlock(index) === false) continue
|
|
1816
1829
|
|
|
1817
1830
|
const req = this._makeRequest(false, 0, 0)
|
|
1831
|
+
if (req === null) return false
|
|
1818
1832
|
|
|
1819
1833
|
req.hash = { index: 2 * index, nodes: f.batch.want.nodes }
|
|
1820
1834
|
|
|
@@ -1901,6 +1915,7 @@ module.exports = class Replicator {
|
|
|
1901
1915
|
this.findingPeers = 0 // updatable from the outside
|
|
1902
1916
|
this.destroyed = false
|
|
1903
1917
|
this.downloading = false
|
|
1918
|
+
this.pushOnly = false
|
|
1904
1919
|
this.activeSessions = 0
|
|
1905
1920
|
|
|
1906
1921
|
this.hotswaps = new HotswapQueue()
|
|
@@ -1988,6 +2003,11 @@ module.exports = class Replicator {
|
|
|
1988
2003
|
return this.downloading
|
|
1989
2004
|
}
|
|
1990
2005
|
|
|
2006
|
+
setPushOnly(pushOnly) {
|
|
2007
|
+
if (pushOnly === this.pushOnly) return
|
|
2008
|
+
this.pushOnly = pushOnly
|
|
2009
|
+
}
|
|
2010
|
+
|
|
1991
2011
|
setDownloading(downloading) {
|
|
1992
2012
|
const allowPush = this.isAllowingPush()
|
|
1993
2013
|
const linger = this._notDownloadingLinger
|
|
@@ -2124,8 +2144,8 @@ module.exports = class Replicator {
|
|
|
2124
2144
|
return ref
|
|
2125
2145
|
}
|
|
2126
2146
|
|
|
2127
|
-
addBlock(session, index) {
|
|
2128
|
-
const b = this._blocks.add(index, PRIORITY.HIGH)
|
|
2147
|
+
addBlock(session, index, force) {
|
|
2148
|
+
const b = this._blocks.add(index, PRIORITY.HIGH, force)
|
|
2129
2149
|
const ref = b.attach(session)
|
|
2130
2150
|
|
|
2131
2151
|
this._queueBlock(b)
|