Skip to content

Commit

Permalink
Update test logic to use findPath result in updating trie
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Jun 24, 2024
1 parent b408105 commit eb68730
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/verkle/test/verkle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
InternalNode,
LeafNode,
ROOT_DB_KEY,
VerkleNodeType,
decodeNode,
matchingBytesLength,
} from '../src/index.js'
Expand Down Expand Up @@ -188,6 +189,11 @@ describe('findPath validation', () => {

// Find path to closest node in tree
const foundPath = await trie.findPath(stem2)

// Confirm node with stem2 doesn't exist in trie
assert.equal(foundPath.node, null)

// Create new leaf node
const leafNode2 = await LeafNode.create(
stem2,
new Array(256).fill(new Uint8Array(32)),
Expand All @@ -196,18 +202,23 @@ describe('findPath validation', () => {
leafNode2.setValue(hexToBytes(keys[2])[31], hexToBytes(values[2]))
putStack.push([leafNode2.hash(), leafNode2])

// Create new internal node
const internalNode1 = InternalNode.create(verkleCrypto)
const nearestNode = foundPath.stack.pop()![0]
// Verify that another leaf node is "nearest" node
assert.equal(nearestNode.type, VerkleNodeType.Leaf)
assert.deepEqual(nearestNode, leafNode1)

// Compute the portion of stem1 and stem2 that match
// Compute the portion of stem1 and stem2 that match (i.e. the partial path closest to stem2)
// Note: We subtract 1 since we are using 0-indexed arrays
const partialMatchingStemIndex = matchingBytesLength(stem1, stem2) - 1
// Find the path to the new internal node (the matching portion of stem1 and stem2)
const internalNode1Path = stem1.slice(0, partialMatchingStemIndex)
// Create new internal node
const internalNode1 = InternalNode.create(verkleCrypto)

// Update the child references for leafNode1 and leafNode 2
internalNode1.setChild(stem1[partialMatchingStemIndex], {
commitment: leafNode1.commitment,
path: stem1,
commitment: nearestNode.commitment,
path: (nearestNode as LeafNode).stem,
})
internalNode1.setChild(stem2[partialMatchingStemIndex], {
commitment: leafNode2.commitment,
Expand All @@ -216,12 +227,16 @@ describe('findPath validation', () => {

putStack.push([internalNode1.hash(), internalNode1])
// Update rootNode child reference for internal node 1
rootNode.setChild(internalNode1Path[0], {

const rootNodeFromPath = foundPath.stack.pop()![0] as InternalNode
// Confirm node from findPath matches root
assert.deepEqual(rootNodeFromPath, rootNode)
rootNodeFromPath.setChild(internalNode1Path[0], {
commitment: internalNode1.commitment,
path: internalNode1Path,
})
trie.root(verkleCrypto.serializeCommitment(rootNode.commitment))
putStack.push([trie.root(), rootNode])
trie.root(verkleCrypto.serializeCommitment(rootNodeFromPath.commitment))
putStack.push([trie.root(), rootNodeFromPath])
await trie.saveStack(putStack)
let res2 = await trie.findPath(stem1)

Expand Down

0 comments on commit eb68730

Please sign in to comment.