Skip to content

Commit

Permalink
verkle: move static constructor to 'constructors.ts'
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Aug 27, 2024
1 parent 6061e05 commit e48336c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 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,17 +1,16 @@
import {
KeyEncoding,
Lock,
MapDB,
ValueEncoding,
bytesToHex,
equalsBytes,
intToHex,
matchingBytesLength,
zeros,
} from '@ethereumjs/util'
import debug from 'debug'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createVerkleTree } from './constructors.js'
import { CheckpointDB } from './db/checkpoint.js'
import { InternalNode } from './node/internalNode.js'
import { LeafNode } from './node/leafNode.js'
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
10 changes: 5 additions & 5 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 @@ -116,7 +116,7 @@ describe('Verkle tree', () => {
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0300000000000000000000000000000000000000000000000000000000000000',
]
const trie = await VerkleTree.create({
const trie = await createVerkleTree({
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
})
Expand Down Expand Up @@ -227,7 +227,7 @@ describe('Verkle tree', () => {
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0300000000000000000000000000000000000000000000000000000000000000',
]
const trie = await VerkleTree.create({
const trie = await createVerkleTree({
verkleCrypto,
db: new MapDB<Uint8Array, Uint8Array>(),
})
Expand All @@ -251,7 +251,7 @@ describe('Verkle tree', () => {
it('should put zeros in leaf node when del called with stem that was not in the trie before', async () => {
const keys = ['0x318dea512b6f3237a2d4763cf49bf26de3b617fb0cabe38a97807a5549df4d01']

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

0 comments on commit e48336c

Please sign in to comment.