hypercore 10.31.10 → 10.31.12

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 +36 -17
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -11,7 +11,7 @@ const caps = require('./caps')
11
11
  const DEFAULT_MAX_INFLIGHT = [32, 512]
12
12
  const SCALE_LATENCY = 50
13
13
  const DEFAULT_SEGMENT_SIZE = 256 * 1024 * 8 // 256 KiB in bits
14
- const NOT_DOWNLOADING_SLACK = 4000 + (Math.random() * 4000) | 0
14
+ const NOT_DOWNLOADING_SLACK = 20000 + (Math.random() * 20000) | 0
15
15
 
16
16
  const PRIORITY = {
17
17
  NORMAL: 0,
@@ -305,6 +305,8 @@ class Peer {
305
305
  this.needsSync = false
306
306
  this.syncsProcessing = 0
307
307
 
308
+ this._remoteContiguousLength = 0
309
+
308
310
  // TODO: tweak pipelining so that data sent BEFORE remoteOpened is not cap verified!
309
311
  // we might wanna tweak that with some crypto, ie use the cap to encrypt it...
310
312
  // or just be aware of that, to only push non leaky data
@@ -334,7 +336,7 @@ class Peer {
334
336
  }
335
337
 
336
338
  get remoteContiguousLength () {
337
- return this.remoteBitfield.findFirst(false, 0)
339
+ return this.remoteBitfield.findFirst(false, this._remoteContiguousLength)
338
340
  }
339
341
 
340
342
  getMaxInflight () {
@@ -736,6 +738,7 @@ class Peer {
736
738
  }
737
739
 
738
740
  onbitfield ({ start, bitfield }) {
741
+ if (start < this._remoteContiguousLength) this._remoteContiguousLength = start // bitfield is always the truth
739
742
  this.remoteBitfield.insert(start, bitfield)
740
743
  this.missingBlocks.insert(start, bitfield)
741
744
  this._clearLocalRange(start, bitfield.byteLength * 8)
@@ -744,7 +747,7 @@ class Peer {
744
747
 
745
748
  _clearLocalRange (start, length) {
746
749
  if (length === 1) {
747
- this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
750
+ this.missingBlocks.set(start, this._remoteHasBlock(start) && !this.core.bitfield.get(start))
748
751
  return
749
752
  }
750
753
 
@@ -776,7 +779,7 @@ class Peer {
776
779
 
777
780
  _unclearLocalRange (start, length) {
778
781
  if (length === 1) {
779
- this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
782
+ this.missingBlocks.set(start, this._remoteHasBlock(start) && !this.core.bitfield.get(start))
780
783
  return
781
784
  }
782
785
 
@@ -804,7 +807,13 @@ class Peer {
804
807
  onrange ({ drop, start, length }) {
805
808
  const has = drop === false
806
809
 
807
- if (length === 1) {
810
+ if (drop === true && start < this._remoteContiguousLength) {
811
+ this._remoteContiguousLength = start
812
+ }
813
+
814
+ if (start === 0 && drop === false && length > this._remoteContiguousLength) {
815
+ this._remoteContiguousLength = length
816
+ } else if (length === 1) {
808
817
  this.remoteBitfield.set(start, has)
809
818
  this.missingBlocks.set(start, has && !this.core.bitfield.get(start))
810
819
  } else {
@@ -947,11 +956,6 @@ class Peer {
947
956
  return false
948
957
  }
949
958
 
950
- // mb turn this into a YES/NO/MAYBE enum, could simplify ifavail logic
951
- _blockAvailable (b) { // TODO: fork also
952
- return this.remoteBitfield.get(b.index)
953
- }
954
-
955
959
  _hasTreeParent (index) {
956
960
  if (this.remoteLength >= this.core.tree.length) return true
957
961
 
@@ -984,13 +988,18 @@ class Peer {
984
988
  return false
985
989
  }
986
990
 
991
+ _remoteHasBlock (index) {
992
+ return index < this._remoteContiguousLength || this.remoteBitfield.get(index) === true
993
+ }
994
+
987
995
  _requestBlock (b) {
988
996
  const { length, fork } = this.core.tree
989
997
 
990
- if (this.remoteBitfield.get(b.index) === false || fork !== this.remoteFork) {
998
+ if (this._remoteHasBlock(b.index) === false || fork !== this.remoteFork) {
991
999
  this._maybeWant(b.index)
992
1000
  return false
993
1001
  }
1002
+
994
1003
  if (!this._hasTreeParent(b.index)) {
995
1004
  return false
996
1005
  }
@@ -1031,6 +1040,16 @@ class Peer {
1031
1040
  return true
1032
1041
  }
1033
1042
 
1043
+ _findNext (i) {
1044
+ if (i < this._remoteContiguousLength) {
1045
+ i = this.core.bitfield.findFirst(false, i)
1046
+ if (i < this._remoteContiguousLength && i > -1) return i
1047
+ i = this._remoteContiguousLength
1048
+ }
1049
+
1050
+ return this.missingBlocks.findFirst(true, i)
1051
+ }
1052
+
1034
1053
  _requestRange (r) {
1035
1054
  const { length, fork } = this.core.tree
1036
1055
 
@@ -1042,7 +1061,8 @@ class Peer {
1042
1061
  const index = r.blocks[i]
1043
1062
  if (min === -1 || index < min) min = index
1044
1063
  if (max === -1 || index > max) max = index
1045
- if (this.missingBlocks.get(index) === true && this._requestRangeBlock(index, length)) return true
1064
+ const has = index < this._remoteContiguousLength || this.missingBlocks.get(index) === true
1065
+ if (has === true && this._requestRangeBlock(index, length)) return true
1046
1066
  }
1047
1067
 
1048
1068
  if (min > -1) this._maybeWant(min, max - min)
@@ -1050,7 +1070,7 @@ class Peer {
1050
1070
  }
1051
1071
 
1052
1072
  const end = Math.min(r.end === -1 ? this.remoteLength : r.end, this.remoteLength)
1053
- if (end < r.start || fork !== this.remoteFork) return false
1073
+ if (end <= r.start || fork !== this.remoteFork) return false
1054
1074
 
1055
1075
  const len = end - r.start
1056
1076
  const off = r.start + (r.linear ? 0 : Math.floor(Math.random() * len))
@@ -1058,7 +1078,7 @@ class Peer {
1058
1078
  let i = off
1059
1079
 
1060
1080
  while (true) {
1061
- i = this.missingBlocks.findFirst(true, i)
1081
+ i = this._findNext(i)
1062
1082
  if (i === -1 || i >= end) break
1063
1083
 
1064
1084
  if (this._requestRangeBlock(i, length)) return true
@@ -1068,8 +1088,7 @@ class Peer {
1068
1088
  i = r.start
1069
1089
 
1070
1090
  while (true) {
1071
- i = this.missingBlocks.findFirst(true, i)
1072
-
1091
+ i = this._findNext(i)
1073
1092
  if (i === -1 || i >= off) break
1074
1093
 
1075
1094
  if (this._requestRangeBlock(i, length)) return true
@@ -1103,7 +1122,7 @@ class Peer {
1103
1122
  let index = off + i
1104
1123
  if (index >= end) index -= len
1105
1124
 
1106
- if (this.remoteBitfield.get(index) === false) continue
1125
+ if (this._remoteHasBlock(index) === false) continue
1107
1126
 
1108
1127
  const req = this._makeRequest(false, 0)
1109
1128
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.31.10",
3
+ "version": "10.31.12",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {