hypercore 11.0.44 → 11.0.46
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/lib/core.js +49 -8
- package/lib/replicator.js +9 -1
- package/lib/session-state.js +1 -3
- package/package.json +1 -1
package/lib/core.js
CHANGED
|
@@ -171,6 +171,7 @@ module.exports = class Core {
|
|
|
171
171
|
key: opts.key || (compat ? manifest.signers[0].publicKey : Verifier.manifestHash(manifest)),
|
|
172
172
|
manifest,
|
|
173
173
|
keyPair: keyPair ? { publicKey: keyPair.publicKey, secretKey: keyPair.secretKey || null } : null,
|
|
174
|
+
frozen: false,
|
|
174
175
|
userData: [],
|
|
175
176
|
tree: {
|
|
176
177
|
fork: 0,
|
|
@@ -190,6 +191,7 @@ module.exports = class Core {
|
|
|
190
191
|
key: header.key,
|
|
191
192
|
manifest,
|
|
192
193
|
keyPair,
|
|
194
|
+
frozen: false,
|
|
193
195
|
discoveryKey,
|
|
194
196
|
userData: opts.userData || [],
|
|
195
197
|
alias: opts.alias || null
|
|
@@ -494,6 +496,11 @@ module.exports = class Core {
|
|
|
494
496
|
return false
|
|
495
497
|
}
|
|
496
498
|
|
|
499
|
+
// sanity check -> no manifest, no way to verify
|
|
500
|
+
if (!this.header.manifest) {
|
|
501
|
+
return false
|
|
502
|
+
}
|
|
503
|
+
|
|
497
504
|
const batch = MerkleTree.verifyFullyRemote(this.state, proof)
|
|
498
505
|
|
|
499
506
|
try {
|
|
@@ -502,24 +509,58 @@ module.exports = class Core {
|
|
|
502
509
|
return true
|
|
503
510
|
}
|
|
504
511
|
|
|
512
|
+
const roots = await MerkleTree.getRootsFromStorage(this.storage, proof.upgrade.length)
|
|
513
|
+
const remoteTreeHash = crypto.tree(proof.upgrade.nodes)
|
|
514
|
+
const localTreeHash = crypto.tree(roots)
|
|
515
|
+
|
|
516
|
+
try {
|
|
517
|
+
const rx = this.state.storage.read()
|
|
518
|
+
const treeProofPromise = MerkleTree.proof(this.state, rx, {
|
|
519
|
+
block: null,
|
|
520
|
+
hash: null,
|
|
521
|
+
seek: null,
|
|
522
|
+
upgrade: {
|
|
523
|
+
start: 0,
|
|
524
|
+
length: proof.upgrade.length
|
|
525
|
+
}
|
|
526
|
+
})
|
|
527
|
+
|
|
528
|
+
rx.tryFlush()
|
|
529
|
+
|
|
530
|
+
const treeProof = await treeProofPromise
|
|
531
|
+
|
|
532
|
+
const verifyBatch = MerkleTree.verifyFullyRemote(this.state, await treeProof.settle())
|
|
533
|
+
this._verifyBatchUpgrade(verifyBatch, this.header.manifest)
|
|
534
|
+
} catch {
|
|
535
|
+
return true
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// both proofs are valid, now check if they forked
|
|
539
|
+
if (b4a.equals(localTreeHash, remoteTreeHash)) return false
|
|
540
|
+
|
|
505
541
|
await this.state.mutex.lock()
|
|
506
542
|
|
|
507
543
|
try {
|
|
508
544
|
const tx = this.state.createWriteBatch()
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
545
|
+
|
|
546
|
+
this.header.frozen = true
|
|
547
|
+
|
|
548
|
+
tx.setAuth({
|
|
549
|
+
key: this.header.key,
|
|
550
|
+
discoveryKey: this.discoveryKey,
|
|
551
|
+
manifest: this.header.manifest,
|
|
552
|
+
keyPair: this.header.keyPair,
|
|
553
|
+
frozen: true
|
|
554
|
+
})
|
|
512
555
|
|
|
513
556
|
await this.state.flush()
|
|
514
557
|
} finally {
|
|
515
558
|
this.state.mutex.unlock()
|
|
516
559
|
}
|
|
517
560
|
|
|
518
|
-
|
|
519
|
-
const
|
|
520
|
-
|
|
521
|
-
if (b4a.equals(localTreeHash, remoteTreeHash)) return false
|
|
522
|
-
|
|
561
|
+
// tmp log so we can see these
|
|
562
|
+
const id = b4a.toString(this.discoveryKey, 'hex')
|
|
563
|
+
console.log('[hypercore] conflict detected in ' + id + ' (writable=' + !!this.header.keyPair + ',quorum=' + this.header.manifest.quorum + ')')
|
|
523
564
|
await this._onconflict(proof)
|
|
524
565
|
return true
|
|
525
566
|
}
|
package/lib/replicator.js
CHANGED
|
@@ -475,6 +475,8 @@ class Peer {
|
|
|
475
475
|
}
|
|
476
476
|
|
|
477
477
|
broadcastRange (start, length, drop) {
|
|
478
|
+
if (!this.isActive()) return
|
|
479
|
+
|
|
478
480
|
if (drop) this._unclearLocalRange(start, length)
|
|
479
481
|
else this._clearLocalRange(start, length)
|
|
480
482
|
|
|
@@ -779,6 +781,10 @@ class Peer {
|
|
|
779
781
|
// sync from now on, so safe to delete from the map
|
|
780
782
|
this.remoteRequests.delete(req.msg.id)
|
|
781
783
|
|
|
784
|
+
if (!this.isActive() && proof.block !== null) {
|
|
785
|
+
return
|
|
786
|
+
}
|
|
787
|
+
|
|
782
788
|
if (proof === null) {
|
|
783
789
|
if (req.msg.manifest && this.core.header.manifest) {
|
|
784
790
|
const manifest = this.core.header.manifest
|
|
@@ -1389,7 +1395,7 @@ class Peer {
|
|
|
1389
1395
|
}
|
|
1390
1396
|
|
|
1391
1397
|
isActive () {
|
|
1392
|
-
if (this.paused || this.removed) return false
|
|
1398
|
+
if (this.paused || this.removed || this.core.header.frozen) return false
|
|
1393
1399
|
return true
|
|
1394
1400
|
}
|
|
1395
1401
|
|
|
@@ -2120,6 +2126,8 @@ module.exports = class Replicator {
|
|
|
2120
2126
|
}
|
|
2121
2127
|
|
|
2122
2128
|
_onwant (peer, start, length) {
|
|
2129
|
+
if (!peer.isActive()) return
|
|
2130
|
+
|
|
2123
2131
|
const contig = Math.min(this.core.state.length, this.core.header.hints.contiguousLength)
|
|
2124
2132
|
|
|
2125
2133
|
if (start + length < contig || (this.core.state.length === contig)) {
|
package/lib/session-state.js
CHANGED
|
@@ -27,8 +27,6 @@ module.exports = class SessionState {
|
|
|
27
27
|
this.atomized = null
|
|
28
28
|
this.mutex = new Mutex()
|
|
29
29
|
|
|
30
|
-
this.treeFork = -1 // only updated if truncated below dependency
|
|
31
|
-
|
|
32
30
|
// merkle state
|
|
33
31
|
this.roots = treeInfo.roots.length ? treeInfo.roots : []
|
|
34
32
|
this.fork = treeInfo.fork || 0
|
|
@@ -111,7 +109,7 @@ module.exports = class SessionState {
|
|
|
111
109
|
}
|
|
112
110
|
|
|
113
111
|
get encryptionFork () {
|
|
114
|
-
return this.
|
|
112
|
+
return this.core.header.tree.fork
|
|
115
113
|
}
|
|
116
114
|
|
|
117
115
|
async updateSnapshotStorage (storage) {
|