diff --git a/packages/verkle/src/constructors.ts b/packages/verkle/src/constructors.ts new file mode 100644 index 0000000000..4e9bb71e63 --- /dev/null +++ b/packages/verkle/src/constructors.ts @@ -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(), + } + else { + opts.verkleCrypto = verkleCrypto + } + } + + const trie = new VerkleTree(opts) + await trie['_createRootNode']() + return trie +} diff --git a/packages/verkle/src/index.ts b/packages/verkle/src/index.ts index af73aaea16..c1adab8a4d 100644 --- a/packages/verkle/src/index.ts +++ b/packages/verkle/src/index.ts @@ -1,3 +1,4 @@ +export * from './constructors.js' export * from './db/index.js' export * from './node/index.js' export * from './types.js' diff --git a/packages/verkle/src/verkleTree.ts b/packages/verkle/src/verkleTree.ts index 47c768c7f8..8d7bca3320 100644 --- a/packages/verkle/src/verkleTree.ts +++ b/packages/verkle/src/verkleTree.ts @@ -1,8 +1,6 @@ import { - KeyEncoding, Lock, MapDB, - ValueEncoding, bytesToHex, equalsBytes, intToHex, @@ -10,8 +8,9 @@ import { 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' @@ -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) { @@ -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(), - } - else { - opts.verkleCrypto = verkleCrypto - } - } - - const trie = new VerkleTree(opts) - await trie._createRootNode() - return trie - } - database(db?: DB) { if (db !== undefined) { if (db instanceof CheckpointDB) { diff --git a/packages/verkle/test/verkle.spec.ts b/packages/verkle/test/verkle.spec.ts index 17c11b5f69..4531915baf 100644 --- a/packages/verkle/test/verkle.spec.ts +++ b/packages/verkle/test/verkle.spec.ts @@ -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' @@ -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(), }) @@ -116,7 +116,7 @@ describe('Verkle tree', () => { '0x0000000000000000000000000000000000000000000000000000000000000000', '0x0300000000000000000000000000000000000000000000000000000000000000', ] - const trie = await VerkleTree.create({ + const trie = await createVerkleTree({ verkleCrypto, db: new MapDB(), }) @@ -227,7 +227,7 @@ describe('Verkle tree', () => { '0x0000000000000000000000000000000000000000000000000000000000000000', '0x0300000000000000000000000000000000000000000000000000000000000000', ] - const trie = await VerkleTree.create({ + const trie = await createVerkleTree({ verkleCrypto, db: new MapDB(), }) @@ -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(), })