Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verkle: extract static constructor #3616

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/verkle/src/constructors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { KeyEncoding, MapDB, ValueEncoding } from '@ethereumjs/util'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'

import { ROOT_DB_KEY } from './types.js'
import { VerkleTree } from './verkleTree.js'

import type { VerkleTreeOpts } from './types.js'

export async function createVerkleTree(opts?: VerkleTreeOpts) {
const key = ROOT_DB_KEY

if (opts?.db !== undefined && opts?.useRootPersistence === true) {
if (opts?.root === undefined) {
opts.root = await opts?.db.get(key, {
keyEncoding: KeyEncoding.Bytes,
valueEncoding: ValueEncoding.Bytes,
})
} else {
await opts?.db.put(key, opts.root, {
keyEncoding: KeyEncoding.Bytes,
valueEncoding: ValueEncoding.Bytes,
})
}
}

if (opts?.verkleCrypto === undefined) {
const verkleCrypto = await loadVerkleCrypto()
if (opts === undefined)
opts = {
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
}
else {
opts.verkleCrypto = verkleCrypto
}
}

const trie = new VerkleTree(opts)
await trie['_createRootNode']()
return trie
}
1 change: 1 addition & 0 deletions packages/verkle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './constructors.js'
export * from './db/index.js'
export * from './node/index.js'
export * from './types.js'
Expand Down
41 changes: 3 additions & 38 deletions packages/verkle/src/verkleTree.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {
KeyEncoding,
Lock,
MapDB,
ValueEncoding,
bytesToHex,
equalsBytes,
intToHex,
matchingBytesLength,
zeros,
} from '@ethereumjs/util'
import debug from 'debug'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'

import { CheckpointDB } from './db/checkpoint.js'
import { InternalNode } from './node/internalNode.js'
Expand All @@ -24,6 +21,8 @@ import {
type VerkleTreeOptsWithDefaults,
} from './types.js'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { createVerkleTree } from './constructors.js' // Imported so intellisense can display docs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, I stumbled over this as well at some point. It would really be good if this can be solved on a more global scale (all such imports auto-added or so), this problem is all over the place and it would be good if we could use this @link feature from the docs more reliably.

(just as some PR-unrelated side note)

Copy link
Contributor

@acolytec3 acolytec3 Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, have done some research on this but haven't quite solved it yet. We need to somehow link via something like {@link module#referencedType } but I haven't figured out specifically how to implement yet.

import type { DB, PutBatch, VerkleCrypto } from '@ethereumjs/util'
import type { Debugger } from 'debug'
interface Path {
Expand Down Expand Up @@ -62,7 +61,7 @@ export class VerkleTree {
* Creates a new verkle tree.
* @param opts Options for instantiating the verkle tree
*
* Note: in most cases, the static {@link VerkleTree.create} constructor should be used. It uses the same API but provides sensible defaults
* Note: in most cases, the static {@link createVerkleTree} constructor should be used. It uses the same API but provides sensible defaults
*/
constructor(opts?: VerkleTreeOpts) {
if (opts !== undefined) {
Expand Down Expand Up @@ -105,40 +104,6 @@ export class VerkleTree {
|| ----------------`)
}

static async create(opts?: VerkleTreeOpts) {
const key = ROOT_DB_KEY

if (opts?.db !== undefined && opts?.useRootPersistence === true) {
if (opts?.root === undefined) {
opts.root = await opts?.db.get(key, {
keyEncoding: KeyEncoding.Bytes,
valueEncoding: ValueEncoding.Bytes,
})
} else {
await opts?.db.put(key, opts.root, {
keyEncoding: KeyEncoding.Bytes,
valueEncoding: ValueEncoding.Bytes,
})
}
}

if (opts?.verkleCrypto === undefined) {
const verkleCrypto = await loadVerkleCrypto()
if (opts === undefined)
opts = {
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
}
else {
opts.verkleCrypto = verkleCrypto
}
}

const trie = new VerkleTree(opts)
await trie._createRootNode()
return trie
}

database(db?: DB<Uint8Array, Uint8Array>) {
if (db !== undefined) {
if (db instanceof CheckpointDB) {
Expand Down
22 changes: 12 additions & 10 deletions packages/verkle/test/verkle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
LeafNode,
VerkleLeafNodeValue,
VerkleNodeType,
createVerkleTree,
decodeNode,
} from '../src/index.js'
import { VerkleTree } from '../src/verkleTree.js'

import type { VerkleNode } from '../src/index.js'
import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('Verkle tree', () => {
'0x318dea512b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d04',
].map((key) => hexToBytes(key as PrefixedHexString))

const tree = await VerkleTree.create({
const tree = await createVerkleTree({
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
})
Expand Down Expand Up @@ -109,14 +109,14 @@ describe('Verkle tree', () => {
'0x318dfa512b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d02',
// A key with a partially matching stem 0x318dfa51 to above key
'0x318dfa513b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d02',
]
] as PrefixedHexString[]
const values = [
'0x320122e8584be00d000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0300000000000000000000000000000000000000000000000000000000000000',
]
const trie = await VerkleTree.create({
] as PrefixedHexString[]
const trie = await createVerkleTree({
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
})
Expand Down Expand Up @@ -220,14 +220,14 @@ describe('Verkle tree', () => {
'0x318dfa512b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d02',
// A key with a partially matching stem 0x318dfa51 to above key
'0x318dfa513b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d02',
]
] as PrefixedHexString[]
const values = [
'0x320122e8584be00d000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0300000000000000000000000000000000000000000000000000000000000000',
]
const trie = await VerkleTree.create({
] as PrefixedHexString[]
const trie = await createVerkleTree({
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
})
Expand All @@ -249,9 +249,11 @@ describe('Verkle tree', () => {
assert.deepEqual(await trie.get(hexToBytes(keys[0])), hexToBytes(values[0]))
})
it('should put zeros in leaf node when del called with stem that was not in the trie before', async () => {
const keys = ['0x318dea512b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d01']
const keys = [
'0x318dea512b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d01',
] as PrefixedHexString[]

const trie = await VerkleTree.create({
const trie = await createVerkleTree({
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
})
Expand Down
Loading