hypercore 11.39.1 → 11.39.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 +2 -2
- package/lib/replicator.js +10 -4
- package/lib/session-state.js +44 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -709,9 +709,9 @@ class Hypercore extends EventEmitter {
|
|
|
709
709
|
return this.opening
|
|
710
710
|
}
|
|
711
711
|
|
|
712
|
-
async recover() {
|
|
712
|
+
async recover(opts) {
|
|
713
713
|
if (this.opened === false) await this.opening
|
|
714
|
-
return this.state.waitForRecovery(this, false)
|
|
714
|
+
return this.state.waitForRecovery(this, false, opts)
|
|
715
715
|
}
|
|
716
716
|
|
|
717
717
|
async setUserData(key, value) {
|
package/lib/replicator.js
CHANGED
|
@@ -1009,6 +1009,8 @@ class Peer {
|
|
|
1009
1009
|
|
|
1010
1010
|
await this.replicator._txLock.lock()
|
|
1011
1011
|
try {
|
|
1012
|
+
if (this.replicator.destroyed) return
|
|
1013
|
+
|
|
1012
1014
|
const remoteLength = Math.max(this.remoteLength, this.pushedLength)
|
|
1013
1015
|
|
|
1014
1016
|
msg.block = {
|
|
@@ -1270,7 +1272,7 @@ class Peer {
|
|
|
1270
1272
|
return
|
|
1271
1273
|
}
|
|
1272
1274
|
|
|
1273
|
-
if (this.core.closed && !isCriticalError(err)) return
|
|
1275
|
+
if ((this.core.closed || this.replicator.destroyed) && !isCriticalError(err)) return
|
|
1274
1276
|
|
|
1275
1277
|
if (err.code !== 'INVALID_OPERATION') {
|
|
1276
1278
|
// might be a fork, verify
|
|
@@ -2203,6 +2205,8 @@ module.exports = class Replicator {
|
|
|
2203
2205
|
}
|
|
2204
2206
|
|
|
2205
2207
|
async applyPendingReorg() {
|
|
2208
|
+
if (this.destroyed) return false
|
|
2209
|
+
|
|
2206
2210
|
if (this._applyingReorg !== null) {
|
|
2207
2211
|
await this._applyingReorg
|
|
2208
2212
|
return true
|
|
@@ -2498,6 +2502,8 @@ module.exports = class Replicator {
|
|
|
2498
2502
|
|
|
2499
2503
|
// Runs in the background - not allowed to throw
|
|
2500
2504
|
async _resolveBlocksLocally() {
|
|
2505
|
+
if (this.destroyed) return
|
|
2506
|
+
|
|
2501
2507
|
// TODO: check if fork compat etc. Requires that we pass down truncation info
|
|
2502
2508
|
|
|
2503
2509
|
const clear = []
|
|
@@ -2946,11 +2952,11 @@ module.exports = class Replicator {
|
|
|
2946
2952
|
await this._applyingReorg
|
|
2947
2953
|
} catch (err) {
|
|
2948
2954
|
this._upgrade = null
|
|
2949
|
-
u.reject(err)
|
|
2955
|
+
if (u !== null) u.reject(err)
|
|
2956
|
+
} finally {
|
|
2957
|
+
this._applyingReorg = null
|
|
2950
2958
|
}
|
|
2951
2959
|
|
|
2952
|
-
this._applyingReorg = null
|
|
2953
|
-
|
|
2954
2960
|
if (this._upgrade !== null) {
|
|
2955
2961
|
this._resolveUpgradeRequest(null)
|
|
2956
2962
|
}
|
package/lib/session-state.js
CHANGED
|
@@ -6,13 +6,19 @@ const quickbit = require('quickbit-universal')
|
|
|
6
6
|
const HypercoreStorage = require('hypercore-storage')
|
|
7
7
|
|
|
8
8
|
const {
|
|
9
|
+
BAD_ARGUMENT,
|
|
9
10
|
INVALID_OPERATION,
|
|
10
11
|
INVALID_SIGNATURE,
|
|
11
12
|
ASSERTION,
|
|
13
|
+
REQUEST_TIMEOUT,
|
|
12
14
|
SESSION_CLOSED,
|
|
13
15
|
SESSION_NOT_WRITABLE
|
|
14
16
|
} = require('hypercore-errors')
|
|
15
17
|
|
|
18
|
+
const RECOVERY_RETRY = 5000 // between looks for a recovery peer
|
|
19
|
+
const RECOVERY_POLL = 250 // between checks of the recovery peers
|
|
20
|
+
const RECOVERY_SETTLE = 8000 // for which the recovery peers must have nothing more for us
|
|
21
|
+
|
|
16
22
|
const Mutex = require('./mutex')
|
|
17
23
|
const Bitfield = require('./bitfield')
|
|
18
24
|
const { MerkleTree, MerkleTreeBatch } = require('./merkle-tree')
|
|
@@ -611,23 +617,36 @@ class SessionState {
|
|
|
611
617
|
}
|
|
612
618
|
}
|
|
613
619
|
|
|
614
|
-
//
|
|
615
|
-
async waitForRecovery(session, writable) {
|
|
620
|
+
// waits until the recovery peers have nothing more for us
|
|
621
|
+
async waitForRecovery(session, writable, { timeout = 0, settle = RECOVERY_SETTLE } = {}) {
|
|
622
|
+
if (!writable && !this.isDefault()) {
|
|
623
|
+
throw BAD_ARGUMENT('Only the default session can recover', this.discoveryKey)
|
|
624
|
+
}
|
|
625
|
+
if (!writable && !SessionState.RECOVERY_PEERS) {
|
|
626
|
+
throw BAD_ARGUMENT('No recovery peers configured', this.discoveryKey)
|
|
627
|
+
}
|
|
628
|
+
|
|
616
629
|
const length = this.length
|
|
617
630
|
const replicator = this.core.replicator
|
|
631
|
+
const deadline = timeout > 0 ? Date.now() + timeout : 0
|
|
632
|
+
const polls = Math.max(1, Math.ceil(settle / RECOVERY_POLL))
|
|
618
633
|
|
|
619
634
|
while (session === null ? this.core.closing === null : session.closing === null) {
|
|
635
|
+
if (deadline !== 0 && Date.now() >= deadline) {
|
|
636
|
+
throw REQUEST_TIMEOUT('Recovery timed out', this.discoveryKey)
|
|
637
|
+
}
|
|
638
|
+
|
|
620
639
|
let peers = getRecoveryPeers(replicator.peers)
|
|
621
640
|
|
|
622
641
|
if (peers.length < 1) {
|
|
623
|
-
await
|
|
642
|
+
await sleepOrClose(bounded(RECOVERY_RETRY, deadline), session)
|
|
624
643
|
continue
|
|
625
644
|
}
|
|
626
645
|
|
|
627
646
|
let restart = false
|
|
628
647
|
|
|
629
|
-
for (let i = 0; i <
|
|
630
|
-
await
|
|
648
|
+
for (let i = 0; i < polls; i++) {
|
|
649
|
+
await sleepOrClose(bounded(RECOVERY_POLL, deadline), session)
|
|
631
650
|
|
|
632
651
|
peers = getRecoveryPeers(replicator.peers)
|
|
633
652
|
|
|
@@ -644,6 +663,8 @@ class SessionState {
|
|
|
644
663
|
throw SESSION_NOT_WRITABLE('core recovered during append')
|
|
645
664
|
}
|
|
646
665
|
|
|
666
|
+
if (!this.core.header.hints.recovering) return
|
|
667
|
+
|
|
647
668
|
this.core.header.hints.recovering = 0
|
|
648
669
|
this.core.hintsChanged = true
|
|
649
670
|
|
|
@@ -1380,6 +1401,24 @@ function isRootIndex(index, roots) {
|
|
|
1380
1401
|
return false
|
|
1381
1402
|
}
|
|
1382
1403
|
|
|
1404
|
+
// wakes early when the session closes, so recovery tears down at once
|
|
1405
|
+
function sleepOrClose(ms, session) {
|
|
1406
|
+
return new Promise((resolve) => {
|
|
1407
|
+
const timer = setTimeout(done, ms)
|
|
1408
|
+
if (session !== null) session.once('close', done)
|
|
1409
|
+
|
|
1410
|
+
function done() {
|
|
1411
|
+
clearTimeout(timer)
|
|
1412
|
+
if (session !== null) session.off('close', done)
|
|
1413
|
+
resolve()
|
|
1414
|
+
}
|
|
1415
|
+
})
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
function bounded(ms, deadline) {
|
|
1419
|
+
return deadline === 0 ? ms : Math.max(0, Math.min(ms, deadline - Date.now()))
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1383
1422
|
function getRecoveryPeers(peers) {
|
|
1384
1423
|
if (!SessionState.RECOVERY_PEERS) return peers
|
|
1385
1424
|
|