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..7682a912f1 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,7 +8,6 @@ import { 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' @@ -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 { @@ -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..941b9b7f3d 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(), }) @@ -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(), }) @@ -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(), }) @@ -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(), })