Skip to content

Commit

Permalink
no get/set
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Jul 26, 2024
1 parent efa3736 commit 38f2922
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/merkle-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class MerkleTreeBatch {
}

appendRoot (node, ite) {
unslabNodes([node])
this.hashCached = null
this.upgraded = true
this.length += ite.factor / 2
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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]
}
}

0 comments on commit 38f2922

Please sign in to comment.