hypercore 10.37.3 → 10.37.5

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.
@@ -3,6 +3,7 @@ const crypto = require('hypercore-crypto')
3
3
  const c = require('compact-encoding')
4
4
  const Xache = require('xache')
5
5
  const b4a = require('b4a')
6
+ const unslab = require('unslab')
6
7
  const caps = require('./caps')
7
8
  const { INVALID_PROOF, INVALID_CHECKSUM, INVALID_OPERATION, BAD_ARGUMENT, ASSERTION } = require('hypercore-errors')
8
9
 
@@ -1224,7 +1225,13 @@ function getStoredNode (storage, index, cache, error) {
1224
1225
  }
1225
1226
 
1226
1227
  const node = { index, size, hash }
1227
- if (cache !== null) cache.set(index, node)
1228
+
1229
+ if (cache !== null) {
1230
+ // Copy hash to a new buffer to avoid blocking gc of its original slab
1231
+ node.hash = unslab(hash)
1232
+ cache.set(index, node)
1233
+ }
1234
+
1228
1235
  resolve(node)
1229
1236
  })
1230
1237
  })
package/lib/replicator.js CHANGED
@@ -41,7 +41,8 @@ const NOT_DOWNLOADING_SLACK = 20000 + (Math.random() * 20000) | 0
41
41
  const PRIORITY = {
42
42
  NORMAL: 0,
43
43
  HIGH: 1,
44
- VERY_HIGH: 2
44
+ VERY_HIGH: 2,
45
+ CANCELLED: 255 // reserved to mark cancellation
45
46
  }
46
47
 
47
48
  class Attachable {
@@ -140,8 +141,10 @@ class BlockRequest extends Attachable {
140
141
  }
141
142
 
142
143
  _unref () {
144
+ this.queued = false
145
+
143
146
  for (const req of this.inflight) {
144
- req.peer._cancelRequest(req.id)
147
+ req.peer._cancelRequest(req)
145
148
  }
146
149
 
147
150
  this.tracker.remove(this.index)
@@ -744,17 +747,18 @@ class Peer {
744
747
  })
745
748
  }
746
749
 
747
- _cancelRequest (id) {
748
- const req = this.replicator._inflight.get(id)
749
- if (!req) return
750
+ _cancelRequest (req) {
751
+ if (req.priority === PRIORITY.CANCELLED) return
752
+ // mark as cancelled also and avoid re-entry
753
+ req.priority = PRIORITY.CANCELLED
750
754
 
751
755
  this.inflight--
752
- this.replicator._removeInflight(id, false)
756
+ this.replicator._requestDone(req.id, false)
753
757
  if (isBlockRequest(req)) this.replicator._unmarkInflight(req.block.index)
754
758
 
755
759
  if (this.roundtripQueue === null) this.roundtripQueue = new RoundtripQueue()
756
- this.roundtripQueue.add(id)
757
- this.wireCancel.send({ request: id })
760
+ this.roundtripQueue.add(req.id)
761
+ this.wireCancel.send({ request: req.id })
758
762
  }
759
763
 
760
764
  _checkIfConflict () {
@@ -859,8 +863,12 @@ class Peer {
859
863
  }
860
864
 
861
865
  _onrequestroundtrip (req) {
866
+ if (req.priority === PRIORITY.CANCELLED) return
867
+ // to avoid re-entry we also just mark it as cancelled
868
+ req.priority = PRIORITY.CANCELLED
869
+
862
870
  this.inflight--
863
- this.replicator._removeInflight(req.id, true)
871
+ this.replicator._requestDone(req.id, true)
864
872
  if (this.roundtripQueue === null) return
865
873
  const flushed = this.roundtripQueue.flush(req.rt)
866
874
  if (flushed === null) return
@@ -1327,9 +1335,11 @@ class Peer {
1327
1335
  try {
1328
1336
  if (req.block !== null && req.fork === fork) {
1329
1337
  req.block.nodes = await this.core.tree.missingNodes(2 * req.block.index)
1338
+ if (req.priority === PRIORITY.CANCELLED) return
1330
1339
  }
1331
1340
  if (req.hash !== null && req.fork === fork && req.hash.nodes === 0) {
1332
1341
  req.hash.nodes = await this.core.tree.missingNodes(req.hash.index)
1342
+ if (req.priority === PRIORITY.CANCELLED) return
1333
1343
 
1334
1344
  // nodes === 0, we already have it, bail
1335
1345
  if (req.hash.nodes === 0 && (req.hash.index & 1) === 0) {
@@ -1694,7 +1704,7 @@ module.exports = class Replicator {
1694
1704
  this.onpeerupdate(true, peer)
1695
1705
  }
1696
1706
 
1697
- _removeInflight (id, roundtrip) {
1707
+ _requestDone (id, roundtrip) {
1698
1708
  this._inflight.remove(id, roundtrip)
1699
1709
  if (this.isDownloading() === true) return
1700
1710
  for (const peer of this.peers) peer.signalUpgrade()
@@ -1722,7 +1732,7 @@ module.exports = class Replicator {
1722
1732
  }
1723
1733
 
1724
1734
  _resolveHashLocally (peer, req) {
1725
- this._removeInflight(req.id, false)
1735
+ this._requestDone(req.id, false)
1726
1736
  this._resolveBlockRequest(this._hashes, req.hash.index / 2, null, req)
1727
1737
  this.updatePeer(peer)
1728
1738
  }
@@ -1794,10 +1804,6 @@ module.exports = class Replicator {
1794
1804
 
1795
1805
  if (b === null || removeInflight(b.inflight, req) === false) return
1796
1806
 
1797
- // if (isBlock && this.core.bitfield.get(index) === false) {
1798
- // for (const peer of this.peers) peer.skipList.set(index, false)
1799
- // }
1800
-
1801
1807
  if (b.refs.length > 0 && isBlock === true) {
1802
1808
  this._queueBlock(b)
1803
1809
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.37.3",
3
+ "version": "10.37.5",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -61,6 +61,7 @@
61
61
  "safety-catch": "^1.0.1",
62
62
  "sodium-universal": "^4.0.0",
63
63
  "streamx": "^2.12.4",
64
+ "unslab": "^1.0.0",
64
65
  "xache": "^1.1.0",
65
66
  "z32": "^1.0.0"
66
67
  },