@@ -18,7 +18,6 @@ import {
1818import { verifyRangeProof } from './verifyRangeProof'
1919// eslint-disable-next-line implicit-dependencies/no-implicit
2020import type { LevelUp } from 'levelup'
21- const assert = require ( 'assert' )
2221
2322export type Proof = Buffer [ ]
2423
@@ -35,6 +34,24 @@ export type FoundNodeFunction = (
3534 walkController : WalkController
3635) => void
3736
37+ export interface TrieOpts {
38+ /**
39+ * A [levelup](https://github.com/Level/levelup) instance.
40+ * By default (if the db is `null` or left undefined) creates an
41+ * in-memory [memdown](https://github.com/Level/memdown) instance.
42+ */
43+ db ?: LevelUp | null
44+ /**
45+ * A `Buffer` for the root of a previously stored trie
46+ */
47+ root ?: Buffer
48+ /**
49+ * Delete nodes from DB on delete operations (disallows switching to an older state root)
50+ * Default: `false`
51+ */
52+ deleteFromDB ?: boolean
53+ }
54+
3855/**
3956 * The basic trie interface, use with `import { BaseTrie as Trie } from 'merkle-patricia-tree'`.
4057 * In Ethereum applications stick with the {@link SecureTrie} overlay.
@@ -51,22 +68,19 @@ export class Trie {
5168 private _deleteFromDB : boolean
5269
5370 /**
54- * test
55- * @param db - A [levelup](https://github.com/Level/levelup) instance. By default (if the db is `null` or
56- * left undefined) creates an in-memory [memdown](https://github.com/Level/memdown) instance.
57- * @param root - A `Buffer` for the root of a previously stored trie
58- * @param deleteFromDB - Delete nodes from DB on delete operations (disallows switching to an older state root) (default: `false`)
71+ * Create a new trie
72+ * @param opts Options for instantiating the trie
5973 */
60- constructor ( db ?: LevelUp | null , root ?: Buffer , deleteFromDB : boolean = false ) {
74+ constructor ( opts : TrieOpts = { } ) {
6175 this . EMPTY_TRIE_ROOT = KECCAK256_RLP
6276 this . lock = new Semaphore ( 1 )
6377
64- this . db = db ? new DB ( db ) : new DB ( )
78+ this . db = opts . db ? new DB ( opts . db ) : new DB ( )
6579 this . _root = this . EMPTY_TRIE_ROOT
66- this . _deleteFromDB = deleteFromDB
80+ this . _deleteFromDB = opts . deleteFromDB ?? false
6781
68- if ( root ) {
69- this . root = root
82+ if ( opts . root ) {
83+ this . root = opts . root
7084 }
7185 }
7286
@@ -77,7 +91,7 @@ export class Trie {
7791 if ( ! value ) {
7892 value = this . EMPTY_TRIE_ROOT
7993 }
80- assert ( value . length === 32 , 'Invalid root length. Roots are 32 bytes' )
94+ if ( value . length !== 32 ) throw new Error ( 'Invalid root length. Roots are 32 bytes' )
8195 this . _root = value
8296 }
8397
@@ -88,17 +102,6 @@ export class Trie {
88102 return this . _root
89103 }
90104
91- /**
92- * This method is deprecated.
93- * Please use {@link Trie.root} instead.
94- *
95- * @param value
96- * @deprecated
97- */
98- setRoot ( value ?: Buffer ) {
99- this . root = value ?? this . EMPTY_TRIE_ROOT
100- }
101-
102105 /**
103106 * Checks if a given root exists.
104107 */
@@ -458,7 +461,7 @@ export class Trie {
458461 }
459462
460463 let lastNode = stack . pop ( ) as TrieNode
461- assert ( lastNode )
464+ if ( ! lastNode ) throw new Error ( 'missing last node' )
462465 let parentNode = stack . pop ( )
463466 const opStack : BatchDBOp [ ] = [ ]
464467
@@ -683,7 +686,7 @@ export class Trie {
683686 * @returns The value from the key, or null if valid proof of non-existence.
684687 */
685688 static async verifyProof ( rootHash : Buffer , key : Buffer , proof : Proof ) : Promise < Buffer | null > {
686- let proofTrie = new Trie ( null , rootHash )
689+ let proofTrie = new Trie ( { root : rootHash } )
687690 try {
688691 proofTrie = await Trie . fromProof ( proof , proofTrie )
689692 } catch ( e : any ) {
@@ -735,7 +738,7 @@ export class Trie {
735738 */
736739 copy ( ) : Trie {
737740 const db = this . db . copy ( )
738- return new Trie ( db . _leveldb , this . root )
741+ return new Trie ( { db : db . _leveldb , root : this . root , deleteFromDB : this . _deleteFromDB } )
739742 }
740743
741744 /**
0 commit comments