Skip to content

Commit

Permalink
Add trie walking helper
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Oct 29, 2024
1 parent 5543970 commit f29f458
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/verkle/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { bytesToHex, concatBytes, equalsBytes } from '@ethereumjs/util'

import { LeafVerkleNode, LeafVerkleNodeValue, decodeVerkleNode } from './node/index.js'

import type { VerkleTree } from './verkleTree.js'
import type { PrefixedHexString } from '@ethereumjs/util'

export const dumpLeafValues = async (
tree: VerkleTree,
startingNode: Uint8Array,
): Promise<[PrefixedHexString, PrefixedHexString][] | undefined> => {
if (equalsBytes(startingNode, tree.EMPTY_TREE_ROOT) === true) return
// Retrieve starting node from DB
const rawNode = await tree['_db'].get(startingNode)
if (rawNode === undefined) return
const node = decodeVerkleNode(rawNode, tree['verkleCrypto'])
const entries: [PrefixedHexString, PrefixedHexString][] = []
if (node instanceof LeafVerkleNode) {
for (const [idx, val] of node.values.entries()) {
if (val !== LeafVerkleNodeValue.Untouched) {
entries.push([
bytesToHex(concatBytes(node.stem, Uint8Array.from([idx]))),
bytesToHex(val === LeafVerkleNodeValue.Deleted ? new Uint8Array(32) : val),
])
}
}
return entries
} else {
const childPaths = node.children
.filter((value) => value !== null)
.map((value) => dumpLeafValues(tree, tree['verkleCrypto'].hashCommitment(value.commitment)))

const res = (await Promise.all(childPaths)).filter((val) => val !== undefined)
return res.flat(1)
}
}
67 changes: 66 additions & 1 deletion packages/verkle/test/verkle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MapDB, equalsBytes, hexToBytes, matchingBytesLength } from '@ethereumjs/util'
import { MapDB, bytesToHex, equalsBytes, hexToBytes, matchingBytesLength } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import { assert, beforeAll, describe, it } from 'vitest'

Expand All @@ -10,6 +10,7 @@ import {
createVerkleTree,
decodeVerkleNode,
} from '../src/index.js'
import { dumpLeafValues } from '../src/util.js'

import type { VerkleNode } from '../src/index.js'
import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'
Expand Down Expand Up @@ -279,3 +280,67 @@ describe('Verkle tree', () => {
assert.deepEqual((res.node as LeafVerkleNode).values[keys[0][31]], LeafVerkleNodeValue.Deleted)
})
})

describe('dump leaf values', () => {
// Values taken from verkle fixtures
const values = [
[
'0x0365b079a274a1808d56484ce5bd97914629907d75767f51439102e22cd50d00',
'0x00000000000000000000000000000001000000000000003635c9adc5de9ccbaa',
],
[
'0x0365b079a274a1808d56484ce5bd97914629907d75767f51439102e22cd50d01',
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
],
[
'0x5b5fdfedd6a0e932da408ac7d772a36513d1eee9b9926e52620c43a433aad700',
'0x0000000000000036000000000000000100000000000000000000000000000000',
],
[
'0x5b5fdfedd6a0e932da408ac7d772a36513d1eee9b9926e52620c43a433aad701',
'0xdf61faef43babbb1ebde8fd82ab9cb4cb74c240d0025138521477e073f72080a',
],
[
'0x5b5fdfedd6a0e932da408ac7d772a36513d1eee9b9926e52620c43a433aad740',
'0x00b2e892fbf04dcdbb33d71633d7cea0722aed27f8a9d0cf9912f97b34f9dadd',
],
[
'0x5b5fdfedd6a0e932da408ac7d772a36513d1eee9b9926e52620c43a433aad780',
'0x0060203611603157600143035f35116029575f35612000014311602957612000',
],
[
'0x5b5fdfedd6a0e932da408ac7d772a36513d1eee9b9926e52620c43a433aad781',
'0x005f3506545f5260205ff35b5f5f5260205ff35b5f5ffd000000000000000000',
],
[
'0x914ec5f0e0c27fe094862fbd89a6abe684939af6940434d8bf218cedb2d62400',
'0x000000000000000000000000000000000000000000000000000000000000f618',
],
[
'0x914ec5f0e0c27fe094862fbd89a6abe684939af6940434d8bf218cedb2d62401',
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
],
[
'0xecb505156708480caf702cd85124f67f3ed78ae4bc890a6dcb62574ba9a90c00',
'0x0000000000000000000000000000000000000000000000000000000000000006',
],
[
'0xecb505156708480caf702cd85124f67f3ed78ae4bc890a6dcb62574ba9a90c01',
'0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
],
]
it('should produce a correct state root', async () => {
const trie = await createVerkleTree()
for (const [key, val] of values) {
const treeKey = hexToBytes(key)
const value = hexToBytes(val)
await trie.put(treeKey.slice(0, 31), [treeKey[31]], [value])
}
assert.equal(
bytesToHex(trie.root()),
'0x3a36cd08db733a2c1ef113142c754bd88bfea541cb56361ed1825d5883378e71',
)
const entries = await dumpLeafValues(trie, trie.root())
assert.deepEqual(entries, values)
}, 10000)
})

0 comments on commit f29f458

Please sign in to comment.