Skip to content

Commit

Permalink
unslab nodes when they enter the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Jul 26, 2024
1 parent f512d67 commit d7b8462
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
22 changes: 14 additions & 8 deletions lib/merkle-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class MerkleTreeBatch {
}

appendRoot (node, ite) {
unslabNodes([node])
node = unslabNode(node)
this.hashCached = null
this.upgraded = true
this.length += ite.factor / 2
Expand All @@ -201,7 +201,7 @@ class MerkleTreeBatch {
break
}

const node = parentNode(this.tree.crypto, ite.parent(), a, b)
const node = unslabNode(parentNode(this.tree.crypto, ite.parent(), a, b))
this.nodes.push(node)
this.roots.pop()
this.roots.pop()
Expand Down Expand Up @@ -429,7 +429,6 @@ module.exports = class MerkleTree {
this.crypto = crypto
this.fork = fork

unslabNodes(roots)
this.roots = roots

this.length = roots.length ? totalSpan(roots) / 2 : 0
Expand Down Expand Up @@ -458,8 +457,7 @@ module.exports = class MerkleTree {
const batch = new MerkleTreeBatch(this)
if (length === this.length) return batch

const roots = await this.getRoots(length)
unslabNodes(roots)
const roots = unslabNodes(await this.getRoots(length))

batch.roots = roots
batch.length = length
Expand Down Expand Up @@ -676,8 +674,7 @@ module.exports = class MerkleTree {
if (i < batch.roots.length && batch.roots[i].index === root) continue

while (batch.roots.length > i) batch.roots.pop()
const node = await this.get(root)
unslabNodes([node])
const node = unslabNode(await this.get(root))
batch.roots.push(node)
}

Expand Down Expand Up @@ -826,7 +823,7 @@ module.exports = class MerkleTree {

const roots = []
for (const index of flat.fullRoots(2 * length)) {
roots.push(await getStoredNode(storage, index, null, true))
roots.push(unslabNode(await getStoredNode(storage, index, null, true)))
}

return new MerkleTree(storage, roots, opts.fork || 0, opts.signature || null, opts.prologue || null)
Expand Down Expand Up @@ -1408,4 +1405,13 @@ function unslabNodes (nodes) {
for (let i = 0; i < nodes.length; i++) {
nodes[i].hash = unslabbed[i]
}

return nodes
}

function unslabNode (node) {
if (node === null) return node

node.hash = unslab(node.hash)
return node
}
24 changes: 13 additions & 11 deletions test/merkle-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ test('checkout nodes in a batch', async t => {
t.alike(nodes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28])
})

test('roots get unslabbed in one slab', async function (t) {
test('roots get unslabbed', async function (t) {
const tree = await create()

const b = tree.batch()
Expand All @@ -616,21 +616,23 @@ test('roots get unslabbed in one slab', async function (t) {
t.is(tree.roots.length > 1, true, 'sanity check')

const rootByteLength = 32
const nrRoots = 3
const buffer = tree.roots[0].hash.buffer

t.is(
buffer.byteLength,
nrRoots * rootByteLength,
'unslabbed the roots, and put them all in the same slab'
rootByteLength,
'unslabbed the first root'
)
t.is(
tree.roots[1].hash.buffer.byteLength,
rootByteLength,
'unslabbed the second root'
)
t.is(
tree.roots[2].hash.buffer.byteLength,
rootByteLength,
'unslabbed the third root'
)

// Sanity check
for (const root of tree.roots) {
if (root.hash.buffer !== buffer) {
t.fail('a root lives on a different slab')
}
}
})

test('buffer of cached nodes is copied to small slab', async function (t) {
Expand Down

0 comments on commit d7b8462

Please sign in to comment.