Skip to content

Commit

Permalink
verkle: extract static constructor (#3616)
Browse files Browse the repository at this point in the history
* verkle: move static constructor to 'constructors.ts'

* nits

---------

Co-authored-by: acolytec3 <[email protected]>
  • Loading branch information
ScottyPoi and acolytec3 authored Aug 29, 2024
1 parent 24b0dc2 commit 0cf6399
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
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
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

0 comments on commit 0cf6399

Please sign in to comment.