hypercore 10.24.10 → 10.24.11

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 +42 -1
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const b4a = require('b4a')
2
2
  const safetyCatch = require('safety-catch')
3
3
  const RandomIterator = require('random-array-iterator')
4
+ const flatTree = require('flat-tree')
4
5
  const ReceiverQueue = require('./receiver-queue')
5
6
  const RemoteBitfield = require('./remote-bitfield')
6
7
  const { REQUEST_CANCELLED, REQUEST_TIMEOUT, INVALID_CAPABILITY, SNAPSHOT_NOT_AVAILABLE } = require('hypercore-errors')
@@ -583,7 +584,7 @@ class Peer {
583
584
  proof = await this._getProof(msg)
584
585
  } catch (err) {
585
586
  safetyCatch(err)
586
- if (isCriticalError(err)) throw err
587
+ if (msg.fork === this.core.tree.fork && isCriticalError(err)) throw err
587
588
  }
588
589
  }
589
590
 
@@ -830,6 +831,7 @@ class Peer {
830
831
 
831
832
  if (this.remoteBitfield.get(index) === false) continue
832
833
  if (this.core.bitfield.get(index) === true) continue
834
+ if (!this._hasTreeParent(index)) continue
833
835
 
834
836
  // Check if this block is currently inflight - if so pick another
835
837
  const b = this.replicator._blocks.get(index)
@@ -860,6 +862,38 @@ class Peer {
860
862
  return this.remoteBitfield.get(b.index)
861
863
  }
862
864
 
865
+ _hasTreeParent (index) {
866
+ if (this.remoteLength >= this.core.tree.length) return true
867
+
868
+ const ite = flatTree.iterator(index * 2)
869
+
870
+ let span = 2
871
+ let length = 0
872
+
873
+ while (true) {
874
+ ite.parent()
875
+
876
+ const left = (ite.index - ite.factor / 2 + 1) / 2
877
+ length = left + span
878
+
879
+ // if larger than local AND larger than remote - they share the root so its ok
880
+ if (length > this.core.tree.length) {
881
+ if (length > this.remoteLength) return true
882
+ break
883
+ }
884
+
885
+ // its less than local but larger than remote so skip it
886
+ if (length > this.remoteLength) break
887
+
888
+ span *= 2
889
+ const first = this.core.bitfield.findFirst(true, left)
890
+ if (first > -1 && first < length) return true
891
+ }
892
+
893
+ // TODO: push to async queue and check against our local merkle tree if we actually can request this block
894
+ return false
895
+ }
896
+
863
897
  _requestBlock (b) {
864
898
  const { length, fork } = this.core.tree
865
899
 
@@ -867,6 +901,9 @@ class Peer {
867
901
  this._maybeWant(b.index)
868
902
  return false
869
903
  }
904
+ if (!this._hasTreeParent(b.index)) {
905
+ return false
906
+ }
870
907
 
871
908
  const req = this._makeRequest(b.index >= length, b.priority)
872
909
  if (req === null) return false
@@ -916,6 +953,10 @@ class Peer {
916
953
  continue
917
954
  }
918
955
 
956
+ if (!this._hasTreeParent(index)) {
957
+ continue
958
+ }
959
+
919
960
  const b = this.replicator._blocks.add(index, PRIORITY.NORMAL)
920
961
 
921
962
  if (b.inflight.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.24.10",
3
+ "version": "10.24.11",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {