hypercore 10.37.11 → 10.37.13
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 +7 -1
- package/lib/merkle-tree.js +18 -6
- package/lib/messages.js +2 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const z32 = require('z32')
|
|
|
11
11
|
const id = require('hypercore-id-encoding')
|
|
12
12
|
const safetyCatch = require('safety-catch')
|
|
13
13
|
const { createTracer } = require('hypertrace')
|
|
14
|
+
const unslab = require('unslab')
|
|
14
15
|
|
|
15
16
|
const Replicator = require('./lib/replicator')
|
|
16
17
|
const Core = require('./lib/core')
|
|
@@ -911,7 +912,12 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
911
912
|
}
|
|
912
913
|
|
|
913
914
|
async _cacheOnResolve (index, req, fork) {
|
|
914
|
-
const
|
|
915
|
+
const resolved = await req
|
|
916
|
+
|
|
917
|
+
// Unslab only when it takes up less then half the slab
|
|
918
|
+
const block = resolved !== null && 2 * resolved.byteLength < resolved.buffer.byteLength
|
|
919
|
+
? unslab(resolved)
|
|
920
|
+
: resolved
|
|
915
921
|
|
|
916
922
|
if (this.cache && fork === this.core.tree.fork) {
|
|
917
923
|
this.cache.set(index, Promise.resolve(block))
|
package/lib/merkle-tree.js
CHANGED
|
@@ -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()
|
|
@@ -259,7 +260,7 @@ class MerkleTreeBatch {
|
|
|
259
260
|
this.tree.length = this.length
|
|
260
261
|
this.tree.byteLength = this.byteLength
|
|
261
262
|
this.tree.fork = this.fork
|
|
262
|
-
this.tree.signature = this.signature
|
|
263
|
+
this.tree.signature = this.signature !== null ? unslab(this.signature) : null
|
|
263
264
|
}
|
|
264
265
|
|
|
265
266
|
seek (bytes, padding) {
|
|
@@ -429,7 +430,7 @@ module.exports = class MerkleTree {
|
|
|
429
430
|
this.roots = roots
|
|
430
431
|
this.length = roots.length ? totalSpan(roots) / 2 : 0
|
|
431
432
|
this.byteLength = totalSize(roots)
|
|
432
|
-
this.signature = signature
|
|
433
|
+
this.signature = signature !== null ? unslab(signature) : null
|
|
433
434
|
this.prologue = prologue
|
|
434
435
|
|
|
435
436
|
this.storage = storage
|
|
@@ -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
|
|
@@ -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)
|
|
@@ -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/messages.js
CHANGED
|
@@ -2,6 +2,7 @@ const c = require('compact-encoding')
|
|
|
2
2
|
const b4a = require('b4a')
|
|
3
3
|
const { DEFAULT_NAMESPACE } = require('./caps')
|
|
4
4
|
const { INVALID_OPLOG_VERSION } = require('hypercore-errors')
|
|
5
|
+
const unslab = require('unslab')
|
|
5
6
|
|
|
6
7
|
const EMPTY = b4a.alloc(0)
|
|
7
8
|
|
|
@@ -244,7 +245,7 @@ wire.handshake = {
|
|
|
244
245
|
const flags = c.uint.decode(state)
|
|
245
246
|
return {
|
|
246
247
|
seeks: (flags & 1) !== 0,
|
|
247
|
-
capability: c.fixed32.decode(state)
|
|
248
|
+
capability: unslab(c.fixed32.decode(state))
|
|
248
249
|
}
|
|
249
250
|
}
|
|
250
251
|
}
|