hypercore 11.16.1 → 11.17.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/README.md +31 -29
- package/index.js +212 -127
- package/lib/audit.js +18 -7
- package/lib/bit-interlude.js +17 -7
- package/lib/bitfield.js +73 -53
- package/lib/caps.js +5 -1
- package/lib/copy-prologue.js +14 -10
- package/lib/core.js +113 -59
- package/lib/default-encryption.js +14 -28
- package/lib/download.js +10 -10
- package/lib/fully-remote-proof.js +3 -3
- package/lib/hotswap-queue.js +5 -5
- package/lib/info.js +4 -4
- package/lib/merkle-tree.js +143 -104
- package/lib/messages.js +163 -143
- package/lib/multisig.js +19 -12
- package/lib/mutex.js +9 -7
- package/lib/receiver-queue.js +6 -6
- package/lib/remote-bitfield.js +30 -32
- package/lib/replicator.js +383 -265
- package/lib/session-state.js +112 -75
- package/lib/streams.js +16 -16
- package/lib/verifier.js +69 -43
- package/package.json +5 -3
package/lib/multisig.js
CHANGED
|
@@ -13,19 +13,26 @@ module.exports = {
|
|
|
13
13
|
signableLength
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
function inflatev0
|
|
16
|
+
function inflatev0(data) {
|
|
17
17
|
return c.decode(multiSignaturev0, data)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function inflate
|
|
20
|
+
function inflate(data) {
|
|
21
21
|
return c.decode(multiSignature, data)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
async function partialSignature
|
|
24
|
+
async function partialSignature(
|
|
25
|
+
core,
|
|
26
|
+
signer,
|
|
27
|
+
from,
|
|
28
|
+
to = core.state.length,
|
|
29
|
+
signature = core.state.signature
|
|
30
|
+
) {
|
|
25
31
|
if (from > core.state.length) return null
|
|
26
32
|
const nodes = to <= from ? null : await upgradeNodes(core, from, to)
|
|
27
33
|
|
|
28
|
-
if (signature.byteLength !== 64)
|
|
34
|
+
if (signature.byteLength !== 64)
|
|
35
|
+
signature = c.decode(multiSignature, signature).proofs[0].signature
|
|
29
36
|
|
|
30
37
|
return {
|
|
31
38
|
signer,
|
|
@@ -35,7 +42,7 @@ async function partialSignature (core, signer, from, to = core.state.length, sig
|
|
|
35
42
|
}
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
async function upgradeNodes
|
|
45
|
+
async function upgradeNodes(core, from, to) {
|
|
39
46
|
const rx = core.state.storage.read()
|
|
40
47
|
let p = null
|
|
41
48
|
try {
|
|
@@ -48,18 +55,18 @@ async function upgradeNodes (core, from, to) {
|
|
|
48
55
|
return (await p.settle()).upgrade.nodes
|
|
49
56
|
}
|
|
50
57
|
|
|
51
|
-
function signableLength
|
|
58
|
+
function signableLength(lengths, quorum) {
|
|
52
59
|
if (quorum <= 0) quorum = 1
|
|
53
60
|
if (quorum > lengths.length) return 0
|
|
54
61
|
|
|
55
62
|
return lengths.sort(cmp)[quorum - 1]
|
|
56
63
|
}
|
|
57
64
|
|
|
58
|
-
function cmp
|
|
65
|
+
function cmp(a, b) {
|
|
59
66
|
return b - a
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
function assemblev0
|
|
69
|
+
function assemblev0(inputs) {
|
|
63
70
|
const proofs = []
|
|
64
71
|
const patch = []
|
|
65
72
|
|
|
@@ -70,7 +77,7 @@ function assemblev0 (inputs) {
|
|
|
70
77
|
return c.encode(multiSignaturev0, { proofs, patch })
|
|
71
78
|
}
|
|
72
79
|
|
|
73
|
-
function assemble
|
|
80
|
+
function assemble(inputs) {
|
|
74
81
|
const proofs = []
|
|
75
82
|
const patch = []
|
|
76
83
|
const seen = new Set()
|
|
@@ -94,13 +101,13 @@ function assemble (inputs) {
|
|
|
94
101
|
return c.encode(multiSignature, { proofs, patch })
|
|
95
102
|
}
|
|
96
103
|
|
|
97
|
-
function compareNode
|
|
104
|
+
function compareNode(a, b) {
|
|
98
105
|
if (a.index !== b.index) return false
|
|
99
106
|
if (a.size !== b.size) return false
|
|
100
107
|
return b4a.equals(a.hash, b.hash)
|
|
101
108
|
}
|
|
102
109
|
|
|
103
|
-
function compressProof
|
|
110
|
+
function compressProof(proof, nodes) {
|
|
104
111
|
return {
|
|
105
112
|
signer: proof.signer,
|
|
106
113
|
signature: proof.signature,
|
|
@@ -108,7 +115,7 @@ function compressProof (proof, nodes) {
|
|
|
108
115
|
}
|
|
109
116
|
}
|
|
110
117
|
|
|
111
|
-
function compressUpgrade
|
|
118
|
+
function compressUpgrade(p, nodes) {
|
|
112
119
|
const u = {
|
|
113
120
|
start: flat.rightSpan(p.nodes[p.nodes.length - 1].index) / 2 + 1,
|
|
114
121
|
length: p.patch,
|
package/lib/mutex.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
module.exports = class Mutex {
|
|
2
|
-
constructor
|
|
2
|
+
constructor() {
|
|
3
3
|
this.locked = false
|
|
4
4
|
this.destroyed = false
|
|
5
5
|
|
|
@@ -9,18 +9,19 @@ module.exports = class Mutex {
|
|
|
9
9
|
this._enqueue = (resolve, reject) => this._queue.push([resolve, reject])
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
idle
|
|
12
|
+
idle() {
|
|
13
13
|
return this._queue.length === 0 && this.locked === false
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
lock
|
|
17
|
-
if (this.destroyed)
|
|
16
|
+
lock() {
|
|
17
|
+
if (this.destroyed)
|
|
18
|
+
return Promise.reject(this._destroyError || new Error('Mutex has been destroyed'))
|
|
18
19
|
if (this.locked) return new Promise(this._enqueue)
|
|
19
20
|
this.locked = true
|
|
20
21
|
return Promise.resolve()
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
unlock
|
|
24
|
+
unlock() {
|
|
24
25
|
if (!this._queue.length) {
|
|
25
26
|
this.locked = false
|
|
26
27
|
return
|
|
@@ -28,8 +29,9 @@ module.exports = class Mutex {
|
|
|
28
29
|
this._queue.shift()[0]()
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
destroy
|
|
32
|
-
if (!this._destroying)
|
|
32
|
+
destroy(err) {
|
|
33
|
+
if (!this._destroying)
|
|
34
|
+
this._destroying = this.locked ? this.lock().catch(() => {}) : Promise.resolve()
|
|
33
35
|
|
|
34
36
|
this.destroyed = true
|
|
35
37
|
if (err) this._destroyError = err
|
package/lib/receiver-queue.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
const FIFO = require('fast-fifo')
|
|
2
2
|
|
|
3
3
|
module.exports = class ReceiverQueue {
|
|
4
|
-
constructor
|
|
4
|
+
constructor() {
|
|
5
5
|
this.queue = new FIFO()
|
|
6
6
|
this.priority = []
|
|
7
7
|
this.requests = new Map()
|
|
8
8
|
this.length = 0
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
push
|
|
11
|
+
push(req) {
|
|
12
12
|
// TODO: use a heap at some point if we wanna support multiple prios
|
|
13
13
|
if (req.priority > 0) this.priority.push(req)
|
|
14
14
|
else this.queue.push(req)
|
|
@@ -17,7 +17,7 @@ module.exports = class ReceiverQueue {
|
|
|
17
17
|
this.length++
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
shift
|
|
20
|
+
shift() {
|
|
21
21
|
while (this.priority.length > 0) {
|
|
22
22
|
const msg = this.priority.pop()
|
|
23
23
|
const req = this._processRequest(msg)
|
|
@@ -33,7 +33,7 @@ module.exports = class ReceiverQueue {
|
|
|
33
33
|
return null
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
_processRequest
|
|
36
|
+
_processRequest(req) {
|
|
37
37
|
if (req.block || req.hash || req.seek || req.upgrade || req.manifest) {
|
|
38
38
|
this.requests.delete(req.id)
|
|
39
39
|
this.length--
|
|
@@ -43,14 +43,14 @@ module.exports = class ReceiverQueue {
|
|
|
43
43
|
return null
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
clear
|
|
46
|
+
clear() {
|
|
47
47
|
this.queue.clear()
|
|
48
48
|
this.priority = []
|
|
49
49
|
this.length = 0
|
|
50
50
|
this.requests.clear()
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
delete
|
|
53
|
+
delete(id) {
|
|
54
54
|
const req = this.requests.get(id)
|
|
55
55
|
if (!req) return
|
|
56
56
|
|
package/lib/remote-bitfield.js
CHANGED
|
@@ -9,7 +9,7 @@ const BYTES_PER_SEGMENT = BITS_PER_SEGMENT / 8
|
|
|
9
9
|
const PAGES_PER_SEGMENT = BITS_PER_SEGMENT / BITS_PER_PAGE
|
|
10
10
|
|
|
11
11
|
class RemoteBitfieldPage {
|
|
12
|
-
constructor
|
|
12
|
+
constructor(index, bitfield, segment) {
|
|
13
13
|
this.index = index
|
|
14
14
|
this.offset = index * BYTES_PER_PAGE - segment.offset
|
|
15
15
|
this.bitfield = bitfield
|
|
@@ -18,21 +18,21 @@ class RemoteBitfieldPage {
|
|
|
18
18
|
segment.add(this)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
get tree
|
|
21
|
+
get tree() {
|
|
22
22
|
return this.segment.tree
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
get
|
|
25
|
+
get(index) {
|
|
26
26
|
return quickbit.get(this.bitfield, index)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
set
|
|
29
|
+
set(index, val) {
|
|
30
30
|
if (quickbit.set(this.bitfield, index, val)) {
|
|
31
31
|
this.tree.update(this.offset * 8 + index)
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
setRange
|
|
35
|
+
setRange(start, end, val) {
|
|
36
36
|
quickbit.fill(this.bitfield, val, start, end)
|
|
37
37
|
|
|
38
38
|
let i = Math.floor(start / 128)
|
|
@@ -41,26 +41,26 @@ class RemoteBitfieldPage {
|
|
|
41
41
|
while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
findFirst
|
|
44
|
+
findFirst(val, position) {
|
|
45
45
|
return quickbit.findFirst(this.bitfield, val, position)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
findLast
|
|
48
|
+
findLast(val, position) {
|
|
49
49
|
return quickbit.findLast(this.bitfield, val, position)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
insert
|
|
52
|
+
insert(start, bitfield) {
|
|
53
53
|
this.bitfield.set(bitfield, start / 32)
|
|
54
54
|
this.segment.refresh()
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
clear
|
|
57
|
+
clear(start, bitfield) {
|
|
58
58
|
quickbit.clear(this.bitfield, { field: bitfield, offset: start })
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
class RemoteBitfieldSegment {
|
|
63
|
-
constructor
|
|
63
|
+
constructor(index) {
|
|
64
64
|
this.index = index
|
|
65
65
|
this.offset = index * BYTES_PER_SEGMENT
|
|
66
66
|
this.tree = quickbit.Index.from([], BYTES_PER_SEGMENT)
|
|
@@ -68,15 +68,15 @@ class RemoteBitfieldSegment {
|
|
|
68
68
|
this.pagesLength = 0
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
get chunks
|
|
71
|
+
get chunks() {
|
|
72
72
|
return this.tree.chunks
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
refresh
|
|
75
|
+
refresh() {
|
|
76
76
|
this.tree = quickbit.Index.from(this.tree.chunks, BYTES_PER_SEGMENT)
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
add
|
|
79
|
+
add(page) {
|
|
80
80
|
const pageIndex = page.index - this.index * PAGES_PER_SEGMENT
|
|
81
81
|
if (pageIndex >= this.pagesLength) this.pagesLength = pageIndex + 1
|
|
82
82
|
|
|
@@ -94,7 +94,7 @@ class RemoteBitfieldSegment {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
findFirst
|
|
97
|
+
findFirst(val, position) {
|
|
98
98
|
position = this.tree.skipFirst(!val, position)
|
|
99
99
|
|
|
100
100
|
let j = position & (BITS_PER_PAGE - 1)
|
|
@@ -116,10 +116,10 @@ class RemoteBitfieldSegment {
|
|
|
116
116
|
i++
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
return
|
|
119
|
+
return val || this.pagesLength === PAGES_PER_SEGMENT ? -1 : this.pagesLength * BITS_PER_PAGE
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
findLast
|
|
122
|
+
findLast(val, position) {
|
|
123
123
|
position = this.tree.skipLast(!val, position)
|
|
124
124
|
|
|
125
125
|
let j = position & (BITS_PER_PAGE - 1)
|
|
@@ -148,13 +148,13 @@ class RemoteBitfieldSegment {
|
|
|
148
148
|
module.exports = class RemoteBitfield {
|
|
149
149
|
static BITS_PER_PAGE = BITS_PER_PAGE
|
|
150
150
|
|
|
151
|
-
constructor
|
|
151
|
+
constructor() {
|
|
152
152
|
this._pages = new BigSparseArray()
|
|
153
153
|
this._segments = new BigSparseArray()
|
|
154
154
|
this._maxSegments = 0
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
getBitfield
|
|
157
|
+
getBitfield(index) {
|
|
158
158
|
const j = index & (BITS_PER_PAGE - 1)
|
|
159
159
|
const i = (index - j) / BITS_PER_PAGE
|
|
160
160
|
|
|
@@ -162,7 +162,7 @@ module.exports = class RemoteBitfield {
|
|
|
162
162
|
return p || null
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
get
|
|
165
|
+
get(index) {
|
|
166
166
|
const j = index & (BITS_PER_PAGE - 1)
|
|
167
167
|
const i = (index - j) / BITS_PER_PAGE
|
|
168
168
|
|
|
@@ -171,7 +171,7 @@ module.exports = class RemoteBitfield {
|
|
|
171
171
|
return p ? p.get(j) : false
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
set
|
|
174
|
+
set(index, val) {
|
|
175
175
|
const j = index & (BITS_PER_PAGE - 1)
|
|
176
176
|
const i = (index - j) / BITS_PER_PAGE
|
|
177
177
|
|
|
@@ -188,7 +188,7 @@ module.exports = class RemoteBitfield {
|
|
|
188
188
|
if (p) p.set(j, val)
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
setRange
|
|
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
|
|
|
@@ -215,7 +215,7 @@ module.exports = class RemoteBitfield {
|
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
-
findFirst
|
|
218
|
+
findFirst(val, position) {
|
|
219
219
|
let j = position & (BITS_PER_SEGMENT - 1)
|
|
220
220
|
let i = (position - j) / BITS_PER_SEGMENT
|
|
221
221
|
|
|
@@ -235,20 +235,18 @@ module.exports = class RemoteBitfield {
|
|
|
235
235
|
|
|
236
236
|
// For the val === false case, we always return at least
|
|
237
237
|
// the 'position', also if nothing was found
|
|
238
|
-
return val
|
|
239
|
-
? -1
|
|
240
|
-
: Math.max(position, this._maxSegments * BITS_PER_SEGMENT)
|
|
238
|
+
return val ? -1 : Math.max(position, this._maxSegments * BITS_PER_SEGMENT)
|
|
241
239
|
}
|
|
242
240
|
|
|
243
|
-
firstSet
|
|
241
|
+
firstSet(position) {
|
|
244
242
|
return this.findFirst(true, position)
|
|
245
243
|
}
|
|
246
244
|
|
|
247
|
-
firstUnset
|
|
245
|
+
firstUnset(position) {
|
|
248
246
|
return this.findFirst(false, position)
|
|
249
247
|
}
|
|
250
248
|
|
|
251
|
-
findLast
|
|
249
|
+
findLast(val, position) {
|
|
252
250
|
let j = position & (BITS_PER_SEGMENT - 1)
|
|
253
251
|
let i = (position - j) / BITS_PER_SEGMENT
|
|
254
252
|
|
|
@@ -269,15 +267,15 @@ module.exports = class RemoteBitfield {
|
|
|
269
267
|
return -1
|
|
270
268
|
}
|
|
271
269
|
|
|
272
|
-
lastSet
|
|
270
|
+
lastSet(position) {
|
|
273
271
|
return this.findLast(true, position)
|
|
274
272
|
}
|
|
275
273
|
|
|
276
|
-
lastUnset
|
|
274
|
+
lastUnset(position) {
|
|
277
275
|
return this.findLast(false, position)
|
|
278
276
|
}
|
|
279
277
|
|
|
280
|
-
insert
|
|
278
|
+
insert(start, bitfield) {
|
|
281
279
|
if (start % 32 !== 0) return false
|
|
282
280
|
|
|
283
281
|
let length = bitfield.byteLength * 8
|
|
@@ -311,7 +309,7 @@ module.exports = class RemoteBitfield {
|
|
|
311
309
|
return true
|
|
312
310
|
}
|
|
313
311
|
|
|
314
|
-
clear
|
|
312
|
+
clear(start, bitfield) {
|
|
315
313
|
if (start % 32 !== 0) return false
|
|
316
314
|
|
|
317
315
|
let length = bitfield.byteLength * 8
|