hypercore 11.0.31 → 11.0.32
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 +1 -1
- package/lib/core.js +15 -1
- package/lib/session-state.js +36 -32
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1029,7 +1029,7 @@ class Hypercore extends EventEmitter {
|
|
|
1029
1029
|
|
|
1030
1030
|
_updateEncryption () {
|
|
1031
1031
|
const e = this.encryption
|
|
1032
|
-
this.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, block: e.
|
|
1032
|
+
this.encryption = new BlockEncryption(e.key, this.key, { compat: this.core.compat, block: b4a.equals(e.blockKey, e.key) })
|
|
1033
1033
|
if (e === this.core.encryption) this.core.encryption = this.encryption
|
|
1034
1034
|
}
|
|
1035
1035
|
}
|
package/lib/core.js
CHANGED
|
@@ -532,7 +532,21 @@ module.exports = class Core {
|
|
|
532
532
|
async verifyReorg (proof) {
|
|
533
533
|
const batch = new ReorgBatch(this.state)
|
|
534
534
|
await MerkleTree.reorg(this.state, proof, batch)
|
|
535
|
-
this._verifyBatchUpgrade(batch, proof.manifest)
|
|
535
|
+
const manifest = this._verifyBatchUpgrade(batch, proof.manifest)
|
|
536
|
+
|
|
537
|
+
if (manifest && !this.header.manifest) {
|
|
538
|
+
await this.state.mutex.lock()
|
|
539
|
+
try {
|
|
540
|
+
if (manifest && this.header.manifest === null) {
|
|
541
|
+
const tx = this.state.createWriteBatch()
|
|
542
|
+
this._setManifest(tx, Verifier.createManifest(manifest), null)
|
|
543
|
+
await this.state.flush()
|
|
544
|
+
}
|
|
545
|
+
} finally {
|
|
546
|
+
this.state._unlock()
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
536
550
|
return batch
|
|
537
551
|
}
|
|
538
552
|
|
package/lib/session-state.js
CHANGED
|
@@ -359,15 +359,15 @@ module.exports = class SessionState {
|
|
|
359
359
|
}
|
|
360
360
|
|
|
361
361
|
async truncate (length, fork, { signature, keyPair } = {}) {
|
|
362
|
-
if (this.prologue && length < this.prologue.length) {
|
|
363
|
-
throw INVALID_OPERATION('Truncation breaks prologue')
|
|
364
|
-
}
|
|
365
|
-
|
|
366
362
|
if (!keyPair && this.isDefault()) keyPair = this.core.header.keyPair
|
|
367
363
|
|
|
368
364
|
await this.mutex.lock()
|
|
369
365
|
|
|
370
366
|
try {
|
|
367
|
+
if (this.prologue && length < this.prologue.length) {
|
|
368
|
+
throw INVALID_OPERATION('Truncation breaks prologue')
|
|
369
|
+
}
|
|
370
|
+
|
|
371
371
|
const batch = this.createTreeBatch()
|
|
372
372
|
await MerkleTree.truncate(this, length, batch, fork)
|
|
373
373
|
|
|
@@ -759,31 +759,33 @@ module.exports = class SessionState {
|
|
|
759
759
|
|
|
760
760
|
const totalLength = Math.max(length, this.length)
|
|
761
761
|
|
|
762
|
-
|
|
763
|
-
|
|
762
|
+
if (totalLength > treeLength) {
|
|
763
|
+
const firstPage = getBitfieldPage(treeLength)
|
|
764
|
+
const lastPage = getBitfieldPage(totalLength - 1)
|
|
764
765
|
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
766
|
+
const srx = this.storage.read()
|
|
767
|
+
const bitfieldPagePromise = srx.getBitfieldPage(firstPage)
|
|
768
|
+
srx.tryFlush()
|
|
768
769
|
|
|
769
|
-
|
|
770
|
+
const bitfieldPage = await bitfieldPagePromise
|
|
770
771
|
|
|
771
|
-
|
|
772
|
+
let index = treeLength
|
|
772
773
|
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
774
|
+
for (let i = firstPage; i <= lastPage; i++) {
|
|
775
|
+
const page = b4a.alloc(Bitfield.BYTES_PER_PAGE)
|
|
776
|
+
tx.putBitfieldPage(i, page)
|
|
776
777
|
|
|
777
|
-
|
|
778
|
-
|
|
778
|
+
// copy existing bits in
|
|
779
|
+
if (i === firstPage && bitfieldPage) page.set(bitfieldPage)
|
|
779
780
|
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
781
|
+
if (index < length) {
|
|
782
|
+
index = fillBitfieldPage(page, index, length, i, true)
|
|
783
|
+
if (index < length) continue
|
|
784
|
+
}
|
|
784
785
|
|
|
785
|
-
|
|
786
|
-
|
|
786
|
+
if (index < this.length) {
|
|
787
|
+
index = fillBitfieldPage(page, index, this.length, i, false)
|
|
788
|
+
}
|
|
787
789
|
}
|
|
788
790
|
}
|
|
789
791
|
|
|
@@ -1027,16 +1029,14 @@ function getBitfieldPage (index) {
|
|
|
1027
1029
|
return Math.floor(index / Bitfield.BITS_PER_PAGE)
|
|
1028
1030
|
}
|
|
1029
1031
|
|
|
1030
|
-
function getBitfieldOffset (index) {
|
|
1031
|
-
return index & (Bitfield.BITS_PER_PAGE - 1)
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
1032
|
function fillBitfieldPage (page, start, end, pageIndex, value) {
|
|
1035
|
-
const
|
|
1036
|
-
const
|
|
1033
|
+
const offset = pageIndex * Bitfield.BITS_PER_PAGE
|
|
1034
|
+
const max = offset + Bitfield.BITS_PER_PAGE
|
|
1037
1035
|
|
|
1038
|
-
const index =
|
|
1039
|
-
|
|
1036
|
+
const index = max < end ? max : end
|
|
1037
|
+
|
|
1038
|
+
const from = start - offset
|
|
1039
|
+
const to = index - offset
|
|
1040
1040
|
|
|
1041
1041
|
quickbit.fill(page, value, from, to)
|
|
1042
1042
|
|
|
@@ -1044,8 +1044,10 @@ function fillBitfieldPage (page, start, end, pageIndex, value) {
|
|
|
1044
1044
|
}
|
|
1045
1045
|
|
|
1046
1046
|
async function storeBitfieldRange (storage, tx, from, to, value) {
|
|
1047
|
+
if (from >= to) return
|
|
1048
|
+
|
|
1047
1049
|
const firstPage = getBitfieldPage(from)
|
|
1048
|
-
const lastPage = getBitfieldPage(to)
|
|
1050
|
+
const lastPage = getBitfieldPage(to - 1)
|
|
1049
1051
|
|
|
1050
1052
|
let index = from
|
|
1051
1053
|
|
|
@@ -1057,9 +1059,11 @@ async function storeBitfieldRange (storage, tx, from, to, value) {
|
|
|
1057
1059
|
}
|
|
1058
1060
|
|
|
1059
1061
|
rx.tryFlush()
|
|
1062
|
+
|
|
1060
1063
|
const pages = await Promise.all(promises)
|
|
1064
|
+
const cnt = lastPage - firstPage + 1
|
|
1061
1065
|
|
|
1062
|
-
for (let i = 0; i
|
|
1066
|
+
for (let i = 0; i < cnt; i++) {
|
|
1063
1067
|
const pageIndex = i + firstPage
|
|
1064
1068
|
if (!pages[i]) pages[i] = b4a.alloc(Bitfield.BYTES_PER_PAGE)
|
|
1065
1069
|
|