hypercore 10.37.12 → 10.37.14
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 +12 -1
- package/lib/merkle-tree.js +19 -7
- package/lib/verifier.js +6 -3
- package/package.json +1 -1
package/lib/core.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const hypercoreCrypto = require('hypercore-crypto')
|
|
2
2
|
const b4a = require('b4a')
|
|
3
|
+
const unslab = require('unslab')
|
|
3
4
|
const Oplog = require('./oplog')
|
|
4
5
|
const BigHeader = require('./big-header')
|
|
5
6
|
const Mutex = require('./mutex')
|
|
@@ -125,6 +126,11 @@ module.exports = class Core {
|
|
|
125
126
|
header = await bigHeader.load(header.external)
|
|
126
127
|
}
|
|
127
128
|
|
|
129
|
+
// unslab the long lived buffers to avoid keeping the slab alive
|
|
130
|
+
if (header.key !== null) header.key = unslab(header.key)
|
|
131
|
+
if (header.tree.rootHash !== null) header.tree.rootHash = unslab(header.tree.rootHash)
|
|
132
|
+
if (header.tree.signature !== null) header.tree.signature = unslab(header.tree.signature)
|
|
133
|
+
|
|
128
134
|
if (opts.manifest) {
|
|
129
135
|
// if we provide a manifest and no key, verify that the stored key is the same
|
|
130
136
|
if (!opts.key && !Verifier.isValidManifest(header.key, Verifier.createManifest(opts.manifest))) {
|
|
@@ -163,6 +169,9 @@ module.exports = class Core {
|
|
|
163
169
|
while (bitfield.get(header.hints.contiguousLength)) header.hints.contiguousLength++
|
|
164
170
|
}
|
|
165
171
|
|
|
172
|
+
// to unslab
|
|
173
|
+
if (header.manifest) header.manifest = Verifier.createManifest(header.manifest)
|
|
174
|
+
|
|
166
175
|
const verifier = header.manifest ? new Verifier(header.key, header.manifest, { crypto, legacy }) : null
|
|
167
176
|
|
|
168
177
|
for (const e of entries) {
|
|
@@ -184,7 +193,7 @@ module.exports = class Core {
|
|
|
184
193
|
if (e.treeUpgrade) {
|
|
185
194
|
const batch = await tree.truncate(e.treeUpgrade.length, e.treeUpgrade.fork)
|
|
186
195
|
batch.ancestors = e.treeUpgrade.ancestors
|
|
187
|
-
batch.signature = e.treeUpgrade.signature
|
|
196
|
+
batch.signature = e.treeUpgrade.signature === null ? null : unslab(e.treeUpgrade.signature)
|
|
188
197
|
addReorgHint(header.hints.reorgs, tree, batch)
|
|
189
198
|
batch.commit()
|
|
190
199
|
|
|
@@ -697,6 +706,8 @@ module.exports = class Core {
|
|
|
697
706
|
}
|
|
698
707
|
}
|
|
699
708
|
|
|
709
|
+
manifest = Verifier.createManifest(manifest) // To unslab
|
|
710
|
+
|
|
700
711
|
const verifier = this.verifier || new Verifier(this.header.key, manifest, { crypto: this.crypto, legacy: this._legacy })
|
|
701
712
|
|
|
702
713
|
if (!verifier.verify(batch, batch.signature)) {
|
package/lib/merkle-tree.js
CHANGED
|
@@ -138,7 +138,7 @@ class MerkleTreeBatch {
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
hash () {
|
|
141
|
-
if (this.hashCached === null) this.hashCached = this.tree.crypto.tree(this.roots)
|
|
141
|
+
if (this.hashCached === null) this.hashCached = unslab(this.tree.crypto.tree(this.roots))
|
|
142
142
|
return this.hashCached
|
|
143
143
|
}
|
|
144
144
|
|
|
@@ -183,6 +183,7 @@ class MerkleTreeBatch {
|
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
appendRoot (node, ite) {
|
|
186
|
+
node = unslabNode(node)
|
|
186
187
|
this.hashCached = null
|
|
187
188
|
this.upgraded = true
|
|
188
189
|
this.length += ite.factor / 2
|
|
@@ -200,7 +201,7 @@ class MerkleTreeBatch {
|
|
|
200
201
|
break
|
|
201
202
|
}
|
|
202
203
|
|
|
203
|
-
const node = parentNode(this.tree.crypto, ite.parent(), a, b)
|
|
204
|
+
const node = unslabNode(parentNode(this.tree.crypto, ite.parent(), a, b))
|
|
204
205
|
this.nodes.push(node)
|
|
205
206
|
this.roots.pop()
|
|
206
207
|
this.roots.pop()
|
|
@@ -453,7 +454,7 @@ module.exports = class MerkleTree {
|
|
|
453
454
|
const batch = new MerkleTreeBatch(this)
|
|
454
455
|
if (length === this.length) return batch
|
|
455
456
|
|
|
456
|
-
const roots = await this.getRoots(length)
|
|
457
|
+
const roots = unslabNodes(await this.getRoots(length))
|
|
457
458
|
|
|
458
459
|
batch.roots = roots
|
|
459
460
|
batch.length = length
|
|
@@ -470,7 +471,7 @@ module.exports = class MerkleTree {
|
|
|
470
471
|
}
|
|
471
472
|
|
|
472
473
|
hash () {
|
|
473
|
-
return this.crypto.tree(this.roots)
|
|
474
|
+
return unslab(this.crypto.tree(this.roots))
|
|
474
475
|
}
|
|
475
476
|
|
|
476
477
|
signable (namespace) {
|
|
@@ -670,7 +671,7 @@ module.exports = class MerkleTree {
|
|
|
670
671
|
if (i < batch.roots.length && batch.roots[i].index === root) continue
|
|
671
672
|
|
|
672
673
|
while (batch.roots.length > i) batch.roots.pop()
|
|
673
|
-
batch.roots.push(await this.get(root))
|
|
674
|
+
batch.roots.push(unslabNode(await this.get(root)))
|
|
674
675
|
}
|
|
675
676
|
|
|
676
677
|
while (batch.roots.length > fullRoots.length) {
|
|
@@ -818,7 +819,7 @@ module.exports = class MerkleTree {
|
|
|
818
819
|
|
|
819
820
|
const roots = []
|
|
820
821
|
for (const index of flat.fullRoots(2 * length)) {
|
|
821
|
-
roots.push(await getStoredNode(storage, index, null, true))
|
|
822
|
+
roots.push(unslabNode(await getStoredNode(storage, index, null, true)))
|
|
822
823
|
}
|
|
823
824
|
|
|
824
825
|
return new MerkleTree(storage, roots, opts.fork || 0, opts.signature || null, opts.prologue || null)
|
|
@@ -981,7 +982,7 @@ function verifyUpgrade ({ fork, upgrade }, blockRoot, batch) {
|
|
|
981
982
|
ite.sibling()
|
|
982
983
|
}
|
|
983
984
|
|
|
984
|
-
batch.signature = upgrade.signature
|
|
985
|
+
batch.signature = upgrade.signature !== null ? unslab(upgrade.signature) : null
|
|
985
986
|
batch.fork = fork
|
|
986
987
|
|
|
987
988
|
return q.extra === null
|
|
@@ -1388,3 +1389,14 @@ async function generateProof (tree, block, hash, seek, upgrade) {
|
|
|
1388
1389
|
function getUnpaddedSize (node, padding, ite) {
|
|
1389
1390
|
return padding === 0 ? node.size : node.size - padding * (ite ? ite.countLeaves() : flat.countLeaves(node.index))
|
|
1390
1391
|
}
|
|
1392
|
+
|
|
1393
|
+
function unslabNodes (nodes) {
|
|
1394
|
+
for (const node of nodes) unslabNode(node)
|
|
1395
|
+
return nodes
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
function unslabNode (node) {
|
|
1399
|
+
if (node === null) return node
|
|
1400
|
+
node.hash = unslab(node.hash)
|
|
1401
|
+
return node
|
|
1402
|
+
}
|
package/lib/verifier.js
CHANGED
|
@@ -3,6 +3,7 @@ const b4a = require('b4a')
|
|
|
3
3
|
const c = require('compact-encoding')
|
|
4
4
|
const flat = require('flat-tree')
|
|
5
5
|
const { BAD_ARGUMENT } = require('hypercore-errors')
|
|
6
|
+
const unslab = require('unslab')
|
|
6
7
|
|
|
7
8
|
const m = require('./messages')
|
|
8
9
|
const multisig = require('./multisig')
|
|
@@ -201,7 +202,9 @@ module.exports = class Verifier {
|
|
|
201
202
|
if (!(b4a.isBuffer(inp.prologue.hash) && inp.prologue.hash.byteLength === 32) || !(inp.prologue.length >= 0)) {
|
|
202
203
|
throw BAD_ARGUMENT('Invalid prologue')
|
|
203
204
|
}
|
|
205
|
+
|
|
204
206
|
manifest.prologue = inp.prologue
|
|
207
|
+
manifest.prologue.hash = unslab(manifest.prologue.hash)
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
return manifest
|
|
@@ -237,7 +240,7 @@ function defaultQuorum (man) {
|
|
|
237
240
|
}
|
|
238
241
|
|
|
239
242
|
function generateUpgrade (patch, start, length) {
|
|
240
|
-
const upgrade = { start, length, nodes: null, additionalNodes: [] }
|
|
243
|
+
const upgrade = { start, length, nodes: null, additionalNodes: [], signature: null }
|
|
241
244
|
|
|
242
245
|
const from = start * 2
|
|
243
246
|
const to = from + length * 2
|
|
@@ -274,8 +277,8 @@ function parseSigner (signer) {
|
|
|
274
277
|
validateSigner(signer)
|
|
275
278
|
return {
|
|
276
279
|
signature: 'ed25519',
|
|
277
|
-
namespace: signer.namespace || caps.DEFAULT_NAMESPACE,
|
|
278
|
-
publicKey: signer.publicKey
|
|
280
|
+
namespace: unslab(signer.namespace || caps.DEFAULT_NAMESPACE),
|
|
281
|
+
publicKey: unslab(signer.publicKey)
|
|
279
282
|
}
|
|
280
283
|
}
|
|
281
284
|
|