hypercore 10.38.2 → 11.0.0

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/multisig.js CHANGED
@@ -20,9 +20,9 @@ function inflate (data) {
20
20
  return c.decode(multiSignature, data)
21
21
  }
22
22
 
23
- async function partialSignature (tree, signer, from, to = tree.length, signature = tree.signature) {
24
- if (from > tree.length) return null
25
- const nodes = to <= from ? null : await upgradeNodes(tree, from, to)
23
+ async function partialSignature (core, signer, from, to = core.state.length, signature = core.state.signature) {
24
+ if (from > core.tree.length) return null
25
+ const nodes = to <= from ? null : await upgradeNodes(core, from, to)
26
26
 
27
27
  if (signature.byteLength !== 64) signature = c.decode(multiSignature, signature).proofs[0].signature
28
28
 
@@ -34,9 +34,12 @@ async function partialSignature (tree, signer, from, to = tree.length, signature
34
34
  }
35
35
  }
36
36
 
37
- async function upgradeNodes (tree, from, to) {
38
- const p = await tree.proof({ upgrade: { start: from, length: to - from } })
39
- return p.upgrade.nodes
37
+ async function upgradeNodes (core, from, to) {
38
+ const batch = core.storage.read()
39
+ const treeBatch = core.state.createTreeBatch()
40
+ const p = await core.tree.proof(batch, treeBatch, { upgrade: { start: from, length: to - from } })
41
+ batch.tryFlush()
42
+ return (await p.settle()).upgrade.nodes
40
43
  }
41
44
 
42
45
  function signableLength (lengths, quorum) {
package/lib/mutex.js CHANGED
@@ -9,6 +9,10 @@ module.exports = class Mutex {
9
9
  this._enqueue = (resolve, reject) => this._queue.push([resolve, reject])
10
10
  }
11
11
 
12
+ idle () {
13
+ return this._queue.length === 0 && this.locked === false
14
+ }
15
+
12
16
  lock () {
13
17
  if (this.destroyed) return Promise.reject(this._destroyError || new Error('Mutex has been destroyed'))
14
18
  if (this.locked) return new Promise(this._enqueue)
@@ -32,11 +32,11 @@ class RemoteBitfieldPage {
32
32
  }
33
33
  }
34
34
 
35
- setRange (start, length, val) {
36
- quickbit.fill(this.bitfield, val, start, start + length)
35
+ setRange (start, end, val) {
36
+ quickbit.fill(this.bitfield, val, start, end)
37
37
 
38
38
  let i = Math.floor(start / 128)
39
- const n = i + Math.ceil(length / 128)
39
+ const n = i + Math.ceil((end - start) / 128)
40
40
 
41
41
  while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
42
42
  }
@@ -188,11 +188,11 @@ module.exports = class RemoteBitfield {
188
188
  if (p) p.set(j, val)
189
189
  }
190
190
 
191
- setRange (start, length, val) {
191
+ setRange (start, end, val) {
192
192
  let j = start & (BITS_PER_PAGE - 1)
193
193
  let i = (start - j) / BITS_PER_PAGE
194
194
 
195
- while (length > 0) {
195
+ while (start < end) {
196
196
  let p = this._pages.get(i)
197
197
 
198
198
  if (!p && val) {
@@ -203,14 +203,14 @@ module.exports = class RemoteBitfield {
203
203
  p = this._pages.set(i, new RemoteBitfieldPage(i, new Uint32Array(WORDS_PER_PAGE), s))
204
204
  }
205
205
 
206
- const end = Math.min(j + length, BITS_PER_PAGE)
207
- const range = end - j
206
+ const last = Math.min(end, BITS_PER_PAGE)
207
+ const range = last - j
208
208
 
209
- if (p) p.setRange(j, range, val)
209
+ if (p) p.setRange(j, last, val)
210
210
 
211
211
  j = 0
212
212
  i++
213
- length -= range
213
+ end -= range
214
214
  }
215
215
  }
216
216