Skip to content

Commit d21d716

Browse files
ryanioholgerd77
authored andcommitted
trie: convert constructor to options interface, remove deprecated setRoot (#1874)
* trie: add constructor options interface * remove assert use, remove deprecated setRoot
1 parent c5acee2 commit d21d716

File tree

10 files changed

+56
-49
lines changed

10 files changed

+56
-49
lines changed

packages/client/lib/execution/vmexecution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class VMExecution extends Execution {
3636
super(options)
3737

3838
if (!this.config.vm) {
39-
const trie = new Trie(this.stateDB)
39+
const trie = new Trie({ db: this.stateDB })
4040

4141
const stateManager = new DefaultStateManager({
4242
common: this.config.execCommon,

packages/client/lib/util/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const main = async () => {
3939
.toString('hex')}', 'hex'), { common })
4040
4141
const stateDB = level('${execution.config.getDataDirectory(DataDirectory.State)}')
42-
const trie = new Trie(stateDB)
42+
const trie = new Trie({ db: stateDB })
4343
const stateManager = new DefaultStateManager({ trie, common })
4444
// Ensure we run on the right root
4545
stateManager.setStateRoot(Buffer.from('${(

packages/trie/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import level from 'level'
2929
import { BaseTrie as Trie } from 'merkle-patricia-tree'
3030

3131
const db = level('./testdb')
32-
const trie = new Trie(db)
32+
const trie = new Trie({ db })
3333

3434
async function test() {
3535
await trie.put(Buffer.from('test'), Buffer.from('one'))
@@ -120,7 +120,7 @@ const stateRoot = '0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580
120120
// Convert the state root to a Buffer (strip the 0x prefix)
121121
const stateRootBuffer = Buffer.from(stateRoot.slice(2), 'hex')
122122
// Initialize trie
123-
const trie = new Trie(db, stateRootBuffer)
123+
const trie = new Trie({ db, root: stateRootBuffer })
124124

125125
trie
126126
.createReadStream()
@@ -140,7 +140,7 @@ import { SecureTrie as Trie } from 'merkle-patricia-tree'
140140
const stateRoot = 'STATE_ROOT_OF_A_BLOCK'
141141

142142
const db = level('YOUR_PATH_TO_THE_GETH_CHAINDATA_FOLDER')
143-
const trie = new Trie(db, stateRoot)
143+
const trie = new Trie({ db, root: stateRoot })
144144

145145
const address = 'AN_ETHEREUM_ACCOUNT_ADDRESS'
146146

packages/trie/src/baseTrie.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import { verifyRangeProof } from './verifyRangeProof'
1919
// eslint-disable-next-line implicit-dependencies/no-implicit
2020
import type { LevelUp } from 'levelup'
21-
const assert = require('assert')
2221

2322
export 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
/**

packages/trie/src/checkpointDb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class CheckpointDB extends DB {
2121
* defaults to an [in-memory store](https://github.com/Level/memdown).
2222
* @param leveldb - An abstract-leveldown compliant store
2323
*/
24-
constructor(leveldb?: LevelUp) {
24+
constructor(leveldb?: LevelUp | null) {
2525
super(leveldb)
2626
// Roots of trie at the moment of checkpoint
2727
this.checkpoints = []

packages/trie/src/checkpointTrie.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Trie as BaseTrie } from './baseTrie'
1+
import { Trie as BaseTrie, TrieOpts } from './baseTrie'
22
import { CheckpointDB } from './checkpointDb'
33

44
/**
@@ -7,9 +7,9 @@ import { CheckpointDB } from './checkpointDb'
77
export class CheckpointTrie extends BaseTrie {
88
db: CheckpointDB
99

10-
constructor(...args: any) {
11-
super(...args)
12-
this.db = new CheckpointDB(...args)
10+
constructor(opts: TrieOpts = {}) {
11+
super(opts)
12+
this.db = new CheckpointDB(opts.db)
1313
}
1414

1515
/**
@@ -63,7 +63,11 @@ export class CheckpointTrie extends BaseTrie {
6363
*/
6464
copy(includeCheckpoints = true): CheckpointTrie {
6565
const db = this.db.copy()
66-
const trie = new CheckpointTrie(db._leveldb, this.root)
66+
const trie = new CheckpointTrie({
67+
db: db._leveldb,
68+
root: this.root,
69+
deleteFromDB: (this as any)._deleteFromDB,
70+
})
6771
if (includeCheckpoints && this.isCheckpoint) {
6872
trie.db.checkpoints = [...this.db.checkpoints]
6973
}

packages/trie/src/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class DB {
2727
* defaults to an [in-memory store](https://github.com/Level/memdown).
2828
* @param leveldb - An abstract-leveldown compliant store
2929
*/
30-
constructor(leveldb?: LevelUp) {
30+
constructor(leveldb?: LevelUp | null) {
3131
this._leveldb = leveldb ?? level()
3232
}
3333

packages/trie/src/secure.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import { Proof } from './baseTrie'
1111
* @public
1212
*/
1313
export class SecureTrie extends CheckpointTrie {
14-
constructor(...args: any) {
15-
super(...args)
16-
}
17-
1814
/**
1915
* Gets a value given a `key`
2016
* @param key - the key to search for
@@ -110,7 +106,11 @@ export class SecureTrie extends CheckpointTrie {
110106
*/
111107
copy(includeCheckpoints = true): SecureTrie {
112108
const db = this.db.copy()
113-
const secureTrie = new SecureTrie(db._leveldb, this.root)
109+
const secureTrie = new SecureTrie({
110+
db: db._leveldb,
111+
root: this.root,
112+
deleteFromDB: (this as any)._deleteFromDB,
113+
})
114114
if (includeCheckpoints && this.isCheckpoint) {
115115
secureTrie.db.checkpoints = [...this.db.checkpoints]
116116
}

packages/trie/src/verifyRangeProof.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ async function verifyProof(
317317
key: Buffer,
318318
proof: Buffer[]
319319
): Promise<{ value: Buffer | null; trie: Trie }> {
320-
let proofTrie = new Trie(null, rootHash)
320+
let proofTrie = new Trie({ root: rootHash })
321321
try {
322322
proofTrie = await Trie.fromProof(proof, proofTrie)
323323
} catch (e) {
@@ -483,7 +483,7 @@ export async function verifyRangeProof(
483483
)
484484
}
485485

486-
let trie = new Trie(null, rootHash)
486+
let trie = new Trie({ root: rootHash })
487487
trie = await Trie.fromProof(proof, trie)
488488

489489
// Remove all nodes between two edge proofs

packages/trie/test/index.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tape('simple save and retrieve', function (tester) {
1414
'3f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d',
1515
'hex'
1616
)
17-
const trie = new CheckpointTrie(null, root)
17+
const trie = new CheckpointTrie({ root })
1818
const value = await trie.get(Buffer.from('test'))
1919
t.equal(value, null)
2020
t.end()
@@ -165,7 +165,7 @@ tape('testing deletion cases', function (tester) {
165165
msg: 'without DB delete',
166166
}
167167
const trieSetupWithDBDelete = {
168-
trie: new CheckpointTrie(undefined, undefined, true),
168+
trie: new CheckpointTrie({ deleteFromDB: true }),
169169
msg: 'with DB delete',
170170
}
171171
const trieSetups = [trieSetupWithoutDBDelete, trieSetupWithDBDelete]
@@ -311,12 +311,12 @@ tape('setting back state root (deleteFromDB)', async (t) => {
311311

312312
const trieSetups = [
313313
{
314-
trie: new BaseTrie(undefined, undefined, false),
314+
trie: new BaseTrie({ deleteFromDB: false }),
315315
expected: v1,
316316
msg: 'should return v1 when setting back the state root when deleteFromDB=false',
317317
},
318318
{
319-
trie: new BaseTrie(undefined, undefined, true),
319+
trie: new BaseTrie({ deleteFromDB: true }),
320320
expected: null,
321321
msg: 'should return null when setting back the state root when deleteFromDB=true',
322322
},

0 commit comments

Comments
 (0)