hypercore 11.13.1 → 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
@@ -30,7 +30,7 @@ const ReceiverQueue = require('./receiver-queue')
30
30
  const HotswapQueue = require('./hotswap-queue')
31
31
  const RemoteBitfield = require('./remote-bitfield')
32
32
  const { MerkleTree } = require('./merkle-tree')
33
- const { REQUEST_CANCELLED, REQUEST_TIMEOUT, INVALID_CAPABILITY, SNAPSHOT_NOT_AVAILABLE } = require('hypercore-errors')
33
+ const { REQUEST_CANCELLED, REQUEST_TIMEOUT, INVALID_CAPABILITY, SNAPSHOT_NOT_AVAILABLE, ASSERTION } = require('hypercore-errors')
34
34
  const m = require('./messages')
35
35
  const caps = require('./caps')
36
36
 
@@ -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,35 +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
-
1239
- const ite = flatTree.iterator(index * 2)
1240
-
1241
- let span = 2
1242
- let length = 0
1243
-
1244
- while (true) {
1245
- ite.parent()
1246
-
1247
- const left = (ite.index - ite.factor / 2 + 1) / 2
1248
- length = left + span
1249
-
1250
- // if larger than local AND larger than remote - they share the root so its ok
1251
- if (length > this.core.state.length) {
1252
- if (length > this.remoteLength) return true
1253
- break
1254
- }
1255
-
1256
- // its less than local but larger than remote so skip it
1257
- if (length > this.remoteLength) break
1258
-
1259
- span *= 2
1260
- const first = this.core.bitfield.findFirst(true, left)
1261
- if (first > -1 && first < length) return true
1262
- }
1263
-
1264
- // 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
1265
1257
  return false
1266
1258
  }
1267
1259
 
@@ -1286,9 +1278,7 @@ class Peer {
1286
1278
  return false
1287
1279
  }
1288
1280
 
1289
- if (!this._hasTreeParent(b.index)) {
1290
- return false
1291
- }
1281
+ if (!this._canRequest(b.index)) return false
1292
1282
 
1293
1283
  const req = this._makeRequest(b.index >= length, b.priority, b.index + 1)
1294
1284
  if (req === null) return false
@@ -1299,7 +1289,7 @@ class Peer {
1299
1289
  }
1300
1290
 
1301
1291
  _requestRangeBlock (index, length) {
1302
- 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
1303
1293
 
1304
1294
  const b = this.replicator._blocks.add(index, PRIORITY.NORMAL)
1305
1295
  if (b.inflight.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.13.1",
3
+ "version": "11.13.3",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {