From 38f2922827335e191cce2b544333becf2624471c Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:13:11 +0200 Subject: [PATCH] no get/set --- lib/merkle-tree.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/merkle-tree.js b/lib/merkle-tree.js index 1078487f..fcd60d2f 100644 --- a/lib/merkle-tree.js +++ b/lib/merkle-tree.js @@ -183,6 +183,7 @@ class MerkleTreeBatch { } appendRoot (node, ite) { + unslabNodes([node]) this.hashCached = null this.upgraded = true this.length += ite.factor / 2 @@ -255,7 +256,9 @@ class MerkleTreeBatch { if (this.tree.flushing !== null) truncateMap(this.tree.flushing, this.ancestors) } + unslabNodes(this.roots) this.tree.roots = this.roots + this.tree.length = this.length this.tree.byteLength = this.byteLength this.tree.fork = this.fork @@ -426,7 +429,10 @@ module.exports = class MerkleTree { constructor (storage, roots, fork, signature, prologue) { this.crypto = crypto this.fork = fork + + unslabNodes(roots) this.roots = roots + this.length = roots.length ? totalSpan(roots) / 2 : 0 this.byteLength = totalSize(roots) this.signature = signature @@ -440,22 +446,6 @@ module.exports = class MerkleTree { this.truncateTo = 0 } - get roots () { - return this._roots - } - - set roots (roots) { - if (roots) { // 1 slab for all roots - const hashes = roots.map(r => r.hash) - const unslabbed = unslab.all(hashes) - for (let i = 0; i < roots.length; i++) { - roots[i].hash = unslabbed[i] - } - } - - this._roots = roots - } - addNode (node) { if (node.size === 0 && b4a.equals(node.hash, BLANK_HASH)) node = blankNode(node.index) this.unflushed.set(node.index, node) @@ -1404,3 +1394,16 @@ async function generateProof (tree, block, hash, seek, upgrade) { function getUnpaddedSize (node, padding, ite) { return padding === 0 ? node.size : node.size - padding * (ite ? ite.countLeaves() : flat.countLeaves(node.index)) } + +function unslabNodes (nodes) { + const hashes = [] + for (const node of nodes) { + hashes.push(node.hash) + } + + const unslabbed = unslab.all(hashes) + + for (let i = 0; i < nodes.length; i++) { + nodes[i].hash = unslabbed[i] + } +}