hypercore 10.18.2 → 10.18.4

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.
@@ -56,6 +56,23 @@ class MerkleTreeBatch {
56
56
  this.upgraded = false
57
57
  }
58
58
 
59
+ clone () {
60
+ const b = new MerkleTreeBatch(this.tree)
61
+
62
+ b.fork = this.fork
63
+ b.roots = [...this.roots]
64
+ b.length = this.length
65
+ b.byteLength = this.byteLength
66
+ b.signature = this.signature
67
+ b.treeLength = this.treeLength
68
+ b.treeFork = this.treeFork
69
+ b.tree = this.tree
70
+ b.nodes = [...this.nodes]
71
+ b.upgraded = this.upgraded
72
+
73
+ return b
74
+ }
75
+
59
76
  hash () {
60
77
  return this.tree.crypto.tree(this.roots)
61
78
  }
package/lib/replicator.js CHANGED
@@ -141,7 +141,7 @@ class RangeRequest extends Attachable {
141
141
  const i = this.ranges.indexOf(this)
142
142
  if (i === -1) return
143
143
  const h = this.ranges.pop()
144
- if (i < this.ranges.length - 1) this.ranges[i] = h
144
+ if (i < this.ranges.length) this.ranges[i] = h
145
145
  }
146
146
 
147
147
  _cancel (r) {
@@ -183,7 +183,7 @@ class SeekRequest extends Attachable {
183
183
  const i = this.seeks.indexOf(this)
184
184
  if (i === -1) return
185
185
  const h = this.seeks.pop()
186
- if (i < this.seeks.length - 1) this.seeks[i] = h
186
+ if (i < this.seeks.length) this.seeks[i] = h
187
187
  }
188
188
  }
189
189
 
@@ -305,6 +305,7 @@ class Peer {
305
305
 
306
306
  this.remoteOpened = false
307
307
  this.remoteBitfield = new RemoteBitfield()
308
+ this.skipList = new RemoteBitfield()
308
309
 
309
310
  this.remoteFork = 0
310
311
  this.remoteLength = 0
@@ -678,6 +679,7 @@ class Peer {
678
679
 
679
680
  onbitfield ({ start, bitfield }) {
680
681
  if (this.remoteBitfield.insert(start, bitfield)) {
682
+ this.skipList.setRange(start, bitfield.byteLength * 8, false)
681
683
  this._update()
682
684
  }
683
685
  }
@@ -687,12 +689,14 @@ class Peer {
687
689
 
688
690
  if (length === 1) {
689
691
  this.remoteBitfield.setRange(start, length, has)
692
+ this.skipList.set(start, false)
690
693
  } else {
691
694
  const rangeStart = this.remoteBitfield.findFirst(!has, start)
692
695
  const rangeLength = length - (rangeStart - start)
693
696
 
694
697
  if (rangeLength > 0) {
695
698
  this.remoteBitfield.setRange(rangeStart, rangeLength, has)
699
+ this.skipList.setRange(rangeStart, rangeLength, false)
696
700
  }
697
701
  }
698
702
 
@@ -850,17 +854,36 @@ class Peer {
850
854
  // as they are "cheaper" and will trigger an auto upgrade if possible
851
855
  // If no blocks < .length is avaible then try the "needs upgrade" range
852
856
 
853
- for (let i = 0; i < len; i++) {
857
+ let wrapped = 0
858
+
859
+ for (let i = 0; i < len && wrapped < 2; i++) {
854
860
  let index = off + i
855
861
  if (index >= end) index -= len
856
862
 
857
- if (r.blocks !== null) index = r.blocks[index]
863
+ if (r.blocks !== null) {
864
+ index = r.blocks[index]
865
+ } else { // TODO: make this loop better (something like a for loop that skips with the skip list)
866
+ index = this.skipList.findFirst(false, index)
867
+ if (index === -1 || index >= end) {
868
+ wrapped++
869
+ index = this.skipList.findFirst(false, r.start)
870
+ if (index === -1 || index >= end) {
871
+ return false
872
+ }
873
+ }
874
+ }
858
875
 
859
- if (this.remoteBitfield.get(index) === false) continue
860
- if (this.core.bitfield.get(index) === true) continue
876
+ if (this.remoteBitfield.get(index) === false || this.core.bitfield.get(index) === true) {
877
+ this.skipList.set(index, true)
878
+ continue
879
+ }
861
880
 
862
881
  const b = this.replicator._blocks.add(index, PRIORITY.NORMAL)
863
- if (b.inflight.length > 0) continue
882
+
883
+ if (b.inflight.length > 0) {
884
+ this.skipList.set(index, true)
885
+ continue
886
+ }
864
887
 
865
888
  const req = this._makeRequest(index >= length, b.priority)
866
889
 
@@ -870,6 +893,8 @@ class Peer {
870
893
  return false
871
894
  }
872
895
 
896
+ this.skipList.set(index, true)
897
+
873
898
  req.block = { index, nodes: 0 }
874
899
 
875
900
  b.inflight.push(req)
@@ -1332,6 +1357,10 @@ module.exports = class Replicator {
1332
1357
 
1333
1358
  if (b === null || removeInflight(b.inflight, req) === false) return
1334
1359
 
1360
+ if (isBlock && this.core.bitfield.get(index) === false) {
1361
+ for (const peer of this.peers) peer.skipList.set(index, false)
1362
+ }
1363
+
1335
1364
  if (b.refs.length > 0 && isBlock === true) {
1336
1365
  this._queueBlock(b)
1337
1366
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.18.2",
3
+ "version": "10.18.4",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {