hypercore 11.0.31 → 11.0.33

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
@@ -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.block })
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
 
@@ -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
 
@@ -630,8 +630,6 @@ module.exports = class SessionState {
630
630
  await this.mutex.lock()
631
631
 
632
632
  try {
633
- if (length < this.length) throw INVALID_OPERATION('Can not catchup back in time')
634
-
635
633
  const origLength = this.length
636
634
 
637
635
  let sharedLength = 0
@@ -754,36 +752,39 @@ module.exports = class SessionState {
754
752
  }
755
753
 
756
754
  for (let i = 0; i < blocks.length; i++) {
755
+ assert(blocks[i] !== null, 'has block')
757
756
  tx.putBlock(i + treeLength, blocks[i])
758
757
  }
759
758
 
760
759
  const totalLength = Math.max(length, this.length)
761
760
 
762
- const firstPage = getBitfieldPage(treeLength)
763
- const lastPage = getBitfieldPage(totalLength)
761
+ if (totalLength > treeLength) {
762
+ const firstPage = getBitfieldPage(treeLength)
763
+ const lastPage = getBitfieldPage(totalLength - 1)
764
764
 
765
- const srx = this.storage.read()
766
- const bitfieldPagePromise = srx.getBitfieldPage(firstPage)
767
- srx.tryFlush()
765
+ const srx = this.storage.read()
766
+ const bitfieldPagePromise = srx.getBitfieldPage(firstPage)
767
+ srx.tryFlush()
768
768
 
769
- const bitfieldPage = await bitfieldPagePromise
769
+ const bitfieldPage = await bitfieldPagePromise
770
770
 
771
- let index = treeLength
771
+ let index = treeLength
772
772
 
773
- for (let i = firstPage; i <= lastPage; i++) {
774
- const page = b4a.alloc(Bitfield.BYTES_PER_PAGE)
775
- tx.putBitfieldPage(i, page)
773
+ for (let i = firstPage; i <= lastPage; i++) {
774
+ const page = b4a.alloc(Bitfield.BYTES_PER_PAGE)
775
+ tx.putBitfieldPage(i, page)
776
776
 
777
- // copy existing bits in
778
- if (i === firstPage && bitfieldPage) page.set(bitfieldPage)
777
+ // copy existing bits in
778
+ if (i === firstPage && bitfieldPage) page.set(bitfieldPage)
779
779
 
780
- if (index < length) {
781
- index = fillBitfieldPage(page, index, length, i, true)
782
- if (index < length) continue
783
- }
780
+ if (index < length) {
781
+ index = fillBitfieldPage(page, index, length, i, true)
782
+ if (index < length) continue
783
+ }
784
784
 
785
- if (index < this.length) {
786
- index = fillBitfieldPage(page, index, this.length, i, false)
785
+ if (index < this.length) {
786
+ index = fillBitfieldPage(page, index, this.length, i, false)
787
+ }
787
788
  }
788
789
  }
789
790
 
@@ -1027,16 +1028,14 @@ function getBitfieldPage (index) {
1027
1028
  return Math.floor(index / Bitfield.BITS_PER_PAGE)
1028
1029
  }
1029
1030
 
1030
- function getBitfieldOffset (index) {
1031
- return index & (Bitfield.BITS_PER_PAGE - 1)
1032
- }
1033
-
1034
1031
  function fillBitfieldPage (page, start, end, pageIndex, value) {
1035
- const last = ((pageIndex + 1) * Bitfield.BITS_PER_PAGE) - 1
1036
- const from = getBitfieldOffset(start)
1032
+ const offset = pageIndex * Bitfield.BITS_PER_PAGE
1033
+ const max = offset + Bitfield.BITS_PER_PAGE
1037
1034
 
1038
- const index = last < end ? last : end
1039
- const to = getBitfieldOffset(index)
1035
+ const index = max < end ? max : end
1036
+
1037
+ const from = start - offset
1038
+ const to = index - offset
1040
1039
 
1041
1040
  quickbit.fill(page, value, from, to)
1042
1041
 
@@ -1044,8 +1043,10 @@ function fillBitfieldPage (page, start, end, pageIndex, value) {
1044
1043
  }
1045
1044
 
1046
1045
  async function storeBitfieldRange (storage, tx, from, to, value) {
1046
+ if (from >= to) return
1047
+
1047
1048
  const firstPage = getBitfieldPage(from)
1048
- const lastPage = getBitfieldPage(to)
1049
+ const lastPage = getBitfieldPage(to - 1)
1049
1050
 
1050
1051
  let index = from
1051
1052
 
@@ -1057,9 +1058,11 @@ async function storeBitfieldRange (storage, tx, from, to, value) {
1057
1058
  }
1058
1059
 
1059
1060
  rx.tryFlush()
1061
+
1060
1062
  const pages = await Promise.all(promises)
1063
+ const cnt = lastPage - firstPage + 1
1061
1064
 
1062
- for (let i = 0; i <= lastPage - firstPage; i++) {
1065
+ for (let i = 0; i < cnt; i++) {
1063
1066
  const pageIndex = i + firstPage
1064
1067
  if (!pages[i]) pages[i] = b4a.alloc(Bitfield.BYTES_PER_PAGE)
1065
1068
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.0.31",
3
+ "version": "11.0.33",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {