hypercore 11.13.2 → 11.13.3

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 CHANGED
@@ -1112,7 +1112,7 @@ function initOnce (session, storage, key, opts) {
1112
1112
 
1113
1113
  session.core = new Core(Hypercore.defaultStorage(storage), {
1114
1114
  preopen: opts.preopen,
1115
- eagerUpgrade: true,
1115
+ eagerUpgrade: opts.eagerUpgrade !== false,
1116
1116
  notDownloadingLinger: opts.notDownloadingLinger,
1117
1117
  allowFork: opts.allowFork !== false,
1118
1118
  inflightRange: opts.inflightRange,
package/lib/replicator.js CHANGED
@@ -429,6 +429,8 @@ class Peer {
429
429
 
430
430
  this.needsSync = false
431
431
  this.syncsProcessing = 0
432
+ this.lastUpgradableLength = 0
433
+ this.lastUpgradableFork = 0
432
434
 
433
435
  this._remoteContiguousLength = 0
434
436
 
@@ -662,6 +664,10 @@ class Peer {
662
664
 
663
665
  if (length === this.remoteLength && fork === this.core.state.fork) {
664
666
  this.canUpgrade = upgrade
667
+ if (upgrade) {
668
+ this.lastUpgradableFork = fork
669
+ this.lastUpgradableLength = length
670
+ }
665
671
  }
666
672
 
667
673
  if (--this.syncsProcessing !== 0) return // ie not latest
@@ -683,16 +689,26 @@ class Peer {
683
689
  async _updateCanUpgradeAndSync () {
684
690
  const { length, fork } = this.core.state
685
691
 
692
+ const remoteLength = this.remoteLength
693
+ const remoteFork = this.remoteFork
686
694
  const canUpgrade = await this._canUpgrade(this.remoteLength, this.remoteFork)
687
695
 
688
696
  if (this.syncsProcessing > 0 || length !== this.core.state.length || fork !== this.core.state.fork) {
689
697
  return
690
698
  }
699
+ if (remoteLength !== this.remoteLength || remoteFork !== this.remoteFork) {
700
+ return
701
+ }
691
702
  if (canUpgrade === this.canUpgrade) {
692
703
  return
693
704
  }
694
705
 
695
706
  this.canUpgrade = canUpgrade
707
+ if (canUpgrade) {
708
+ this.lastUpgradableLength = this.remoteLength
709
+ this.lastUpgradableFork = this.remoteFork
710
+ }
711
+
696
712
  this.sendSync()
697
713
  }
698
714
 
@@ -1204,7 +1220,7 @@ class Peer {
1204
1220
 
1205
1221
  if (this._remoteHasBlock(index) === false) continue
1206
1222
  if (this.core.bitfield.get(index) === true) continue
1207
- if (!this._hasTreeParent(index)) continue
1223
+ if (!this._canRequest(index)) continue
1208
1224
 
1209
1225
  // Check if this block is currently inflight - if so pick another
1210
1226
  const b = this.replicator._blocks.get(index)
@@ -1233,42 +1249,11 @@ class Peer {
1233
1249
  return false
1234
1250
  }
1235
1251
 
1236
- _hasTreeParent (index) {
1252
+ _canRequest (index) {
1253
+ if (!(index >= 0)) throw ASSERTION('bad index to _canRequest: ' + index)
1237
1254
  if (this.remoteLength >= this.core.state.length) return true
1238
- if (!(index >= 0)) throw ASSERTION('bad index to _hasTreeParent: ' + index)
1239
-
1240
- const ite = flatTree.iterator(index * 2)
1241
-
1242
- let span = 2
1243
- let length = 0
1244
- let cnt = 0
1245
-
1246
- while (true) {
1247
- if (++cnt >= 64) {
1248
- throw ASSERTION('_hasTreeParent is stuck, index=' + index + ', length=' + length +
1249
- ', span=' + span + ', remoteLength=' + this.remoteLength + ', length=' + this.core.state.length)
1250
- }
1251
-
1252
- ite.parent()
1253
-
1254
- const left = (ite.index - ite.factor / 2 + 1) / 2
1255
- length = left + span
1256
-
1257
- // if larger than local AND larger than remote - they share the root so its ok
1258
- if (length > this.core.state.length) {
1259
- if (length > this.remoteLength) return true
1260
- break
1261
- }
1262
-
1263
- // its less than local but larger than remote so skip it
1264
- if (length > this.remoteLength) break
1265
-
1266
- span *= 2
1267
- const first = this.core.bitfield.findFirst(true, left)
1268
- if (first > -1 && first < length) return true
1269
- }
1270
-
1271
- // TODO: push to async queue and check against our local merkle tree if we actually can request this block
1255
+ if (index < this.lastUpgradableLength && this.lastUpgradableFork === this.core.state.fork) return true
1256
+ if (this.canUpgrade && this.syncsProcessing === 0) return true
1272
1257
  return false
1273
1258
  }
1274
1259
 
@@ -1293,9 +1278,7 @@ class Peer {
1293
1278
  return false
1294
1279
  }
1295
1280
 
1296
- if (!this._hasTreeParent(b.index)) {
1297
- return false
1298
- }
1281
+ if (!this._canRequest(b.index)) return false
1299
1282
 
1300
1283
  const req = this._makeRequest(b.index >= length, b.priority, b.index + 1)
1301
1284
  if (req === null) return false
@@ -1306,7 +1289,7 @@ class Peer {
1306
1289
  }
1307
1290
 
1308
1291
  _requestRangeBlock (index, length) {
1309
- if (this.core.bitfield.get(index) === true || !this._hasTreeParent(index)) return false
1292
+ if (this.core.bitfield.get(index) === true || !this._canRequest(index)) return false
1310
1293
 
1311
1294
  const b = this.replicator._blocks.add(index, PRIORITY.NORMAL)
1312
1295
  if (b.inflight.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.13.2",
3
+ "version": "11.13.3",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {