Skip to content

Commit 53a293e

Browse files
authored
trie: rename trie class to MerklePatriciaTrie (#3717)
* trie: rename trie to mpt * trie: fix example
1 parent 477dc76 commit 53a293e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+296
-259
lines changed

packages/block/src/block/block.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ConsensusType } from '@ethereumjs/common'
22
import { RLP } from '@ethereumjs/rlp'
3-
import { Trie } from '@ethereumjs/trie'
3+
import { MerklePatriciaTrie } from '@ethereumjs/trie'
44
import { Blob4844Tx, Capability } from '@ethereumjs/tx'
55
import {
66
BIGINT_0,
@@ -226,7 +226,10 @@ export class Block {
226226
* Generates transaction trie for validation.
227227
*/
228228
async genTxTrie(): Promise<Uint8Array> {
229-
return genTransactionsTrieRoot(this.transactions, new Trie({ common: this.common }))
229+
return genTransactionsTrieRoot(
230+
this.transactions,
231+
new MerklePatriciaTrie({ common: this.common }),
232+
)
230233
}
231234

232235
/**
@@ -471,7 +474,7 @@ export class Block {
471474
if (this.cache.withdrawalsTrieRoot === undefined) {
472475
this.cache.withdrawalsTrieRoot = await genWithdrawalsTrieRoot(
473476
this.withdrawals!,
474-
new Trie({ common: this.common }),
477+
new MerklePatriciaTrie({ common: this.common }),
475478
)
476479
}
477480
result = equalsBytes(this.cache.withdrawalsTrieRoot, this.header.withdrawalsRoot!)

packages/block/src/block/constructors.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RLP } from '@ethereumjs/rlp'
2-
import { Trie } from '@ethereumjs/trie'
2+
import { MerklePatriciaTrie } from '@ethereumjs/trie'
33
import {
44
type TxOptions,
55
createTx,
@@ -401,10 +401,13 @@ export async function createBlockFromExecutionPayload(
401401
}
402402
}
403403

404-
const transactionsTrie = await genTransactionsTrieRoot(txs, new Trie({ common: opts?.common }))
404+
const transactionsTrie = await genTransactionsTrieRoot(
405+
txs,
406+
new MerklePatriciaTrie({ common: opts?.common }),
407+
)
405408
const withdrawals = withdrawalsData?.map((wData) => createWithdrawal(wData))
406409
const withdrawalsRoot = withdrawals
407-
? await genWithdrawalsTrieRoot(withdrawals, new Trie({ common: opts?.common }))
410+
? await genWithdrawalsTrieRoot(withdrawals, new MerklePatriciaTrie({ common: opts?.common }))
408411
: undefined
409412

410413
const hasDepositRequests = depositRequests !== undefined && depositRequests !== null
@@ -434,7 +437,7 @@ export async function createBlockFromExecutionPayload(
434437
}
435438

436439
const requestsRoot = requests
437-
? await genRequestsTrieRoot(requests, new Trie({ common: opts?.common }))
440+
? await genRequestsTrieRoot(requests, new MerklePatriciaTrie({ common: opts?.common }))
438441
: undefined
439442

440443
const header: HeaderData = {

packages/block/src/helpers.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RLP } from '@ethereumjs/rlp'
2-
import { Trie } from '@ethereumjs/trie'
2+
import { MerklePatriciaTrie } from '@ethereumjs/trie'
33
import { Blob4844Tx } from '@ethereumjs/tx'
44
import { BIGINT_0, BIGINT_1, TypeOutput, isHexString, toType } from '@ethereumjs/util'
55

@@ -124,8 +124,8 @@ export const fakeExponential = (factor: bigint, numerator: bigint, denominator:
124124
* @param wts array of Withdrawal to compute the root of
125125
* @param optional emptyTrie to use to generate the root
126126
*/
127-
export async function genWithdrawalsTrieRoot(wts: Withdrawal[], emptyTrie?: Trie) {
128-
const trie = emptyTrie ?? new Trie()
127+
export async function genWithdrawalsTrieRoot(wts: Withdrawal[], emptyTrie?: MerklePatriciaTrie) {
128+
const trie = emptyTrie ?? new MerklePatriciaTrie()
129129
for (const [i, wt] of wts.entries()) {
130130
await trie.put(RLP.encode(i), RLP.encode(wt.raw()))
131131
}
@@ -137,8 +137,11 @@ export async function genWithdrawalsTrieRoot(wts: Withdrawal[], emptyTrie?: Trie
137137
* @param txs array of TypedTransaction to compute the root of
138138
* @param optional emptyTrie to use to generate the root
139139
*/
140-
export async function genTransactionsTrieRoot(txs: TypedTransaction[], emptyTrie?: Trie) {
141-
const trie = emptyTrie ?? new Trie()
140+
export async function genTransactionsTrieRoot(
141+
txs: TypedTransaction[],
142+
emptyTrie?: MerklePatriciaTrie,
143+
) {
144+
const trie = emptyTrie ?? new MerklePatriciaTrie()
142145
for (const [i, tx] of txs.entries()) {
143146
await trie.put(RLP.encode(i), tx.serialize())
144147
}
@@ -151,7 +154,10 @@ export async function genTransactionsTrieRoot(txs: TypedTransaction[], emptyTrie
151154
* @param emptyTrie optional empty trie used to generate the root
152155
* @returns a 32 byte Uint8Array representing the requests trie root
153156
*/
154-
export async function genRequestsTrieRoot(requests: CLRequest<CLRequestType>[], emptyTrie?: Trie) {
157+
export async function genRequestsTrieRoot(
158+
requests: CLRequest<CLRequestType>[],
159+
emptyTrie?: MerklePatriciaTrie,
160+
) {
155161
// Requests should be sorted in monotonically ascending order based on type
156162
// and whatever internal sorting logic is defined by each request type
157163
if (requests.length > 1) {
@@ -160,7 +166,7 @@ export async function genRequestsTrieRoot(requests: CLRequest<CLRequestType>[],
160166
throw new Error('requests are not sorted in ascending order')
161167
}
162168
}
163-
const trie = emptyTrie ?? new Trie()
169+
const trie = emptyTrie ?? new MerklePatriciaTrie()
164170
for (const [i, req] of requests.entries()) {
165171
await trie.put(RLP.encode(i), req.serialize())
166172
}

packages/client/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface EthereumClientOptions {
3030
* Database to store the state.
3131
* Should be an abstract-leveldown compliant store.
3232
*
33-
* Default: Database created by the Trie class
33+
* Default: Database created by the MerklePatriciaTrie class
3434
*/
3535
stateDB?: AbstractLevel<string | Uint8Array, string | Uint8Array, string | Uint8Array>
3636

packages/client/src/sync/fetcher/accountfetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type { AccountData } from '../../net/protocol/snapprotocol.js'
3333
import type { FetcherOptions } from './fetcher.js'
3434
import type { StorageRequest } from './storagefetcher.js'
3535
import type { Job, SnapFetcherDoneFlags } from './types.js'
36-
import type { Trie } from '@ethereumjs/trie'
36+
import type { MerklePatriciaTrie } from '@ethereumjs/trie'
3737
import type { Debugger } from 'debug'
3838

3939
type AccountDataResponse = AccountData[] & { completed?: boolean }
@@ -70,7 +70,7 @@ export type JobTask = {
7070
export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData> {
7171
protected debug: Debugger
7272
stateManager: MerkleStateManager
73-
accountTrie: Trie
73+
accountTrie: MerklePatriciaTrie
7474

7575
root: Uint8Array
7676
highestKnownHash: Uint8Array | undefined

packages/client/src/sync/fetcher/trienodefetcher.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
BranchNode,
44
ExtensionNode,
55
LeafNode,
6-
Trie,
6+
MerklePatriciaTrie,
77
decodeNode,
88
mergeAndFormatKeyPaths,
99
pathToHexKey,
@@ -37,7 +37,7 @@ type TrieNodesResponse = Uint8Array[] & { completed?: boolean }
3737
*/
3838
export interface TrieNodeFetcherOptions extends FetcherOptions {
3939
root: Uint8Array
40-
accountToStorageTrie?: Map<String, Trie>
40+
accountToStorageTrie?: Map<String, MerklePatriciaTrie>
4141
stateManager?: MerkleStateManager
4242

4343
/** Destroy fetcher once all tasks are done */
@@ -71,7 +71,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
7171

7272
stateManager: MerkleStateManager
7373
fetcherDoneFlags: SnapFetcherDoneFlags
74-
accountTrie: Trie
74+
accountTrie: MerklePatriciaTrie
7575
codeDB: DB
7676

7777
/**
@@ -351,7 +351,10 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
351351

352352
// add storage data for account if it has fetched nodes
353353
// TODO figure out what the key should be for mapping accounts to storage tries
354-
const storageTrie = new Trie({ useKeyHashing: true, common: this.config.chainCommon })
354+
const storageTrie = new MerklePatriciaTrie({
355+
useKeyHashing: true,
356+
common: this.config.chainCommon,
357+
})
355358
const storageTrieOps: BatchDBOp[] = []
356359
if (pathToStorageNode !== undefined && pathToStorageNode.size > 0) {
357360
for (const [path, data] of pathToStorageNode) {

packages/client/src/util/debug.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { Level } from 'level';
2929
import { Common } from '@ethereumjs/common'
3030
import { Block } from '@ethereumjs/block'
3131
import { VM, runBlock, createVM } from './src'
32-
import { Trie } from '@ethereumjs/trie'
32+
import { MerklePatriciaTrie } from '@ethereumjs/trie'
3333
import { MerkleStateManager } from './src/state'
3434
import { Blockchain } from '@ethereumjs/blockchain'
3535
@@ -40,7 +40,7 @@ const main = async () => {
4040
const block = createBlockFromRLP(hexToBytes('${bytesToHex(block.serialize())}'), { common })
4141
4242
const stateDB = new Level('${execution.config.getDataDirectory(DataDirectory.State)}')
43-
const trie = new Trie({ db: stateDB, useKeyHashing: true })
43+
const trie = new MerklePatriciaTrie({ db: stateDB, useKeyHashing: true })
4444
const stateManager = new MerkleStateManager({ trie, common })
4545
// Ensure we run on the right root
4646
stateManager.setStateRoot(hexToBytes('${bytesToHex(

packages/client/test/rpc/engine/withdrawals.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { genWithdrawalsTrieRoot } from '@ethereumjs/block'
2-
import { Trie } from '@ethereumjs/trie'
2+
import { MerklePatriciaTrie } from '@ethereumjs/trie'
33
import { bigIntToHex, bytesToHex, createWithdrawal, intToHex } from '@ethereumjs/util'
44
import { assert, it } from 'vitest'
55

@@ -105,7 +105,7 @@ for (const { name, withdrawals, withdrawalsRoot, gethBlockRlp } of testCases) {
105105
it(name, async () => {
106106
// check withdrawals root computation
107107
const computedWithdrawalsRoot = bytesToHex(
108-
await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal), new Trie()),
108+
await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal), new MerklePatriciaTrie()),
109109
)
110110
assert.equal(
111111
withdrawalsRoot,

packages/statemanager/src/merkleStateManager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Common, Mainnet } from '@ethereumjs/common'
22
import { RLP } from '@ethereumjs/rlp'
3-
import { Trie } from '@ethereumjs/trie'
3+
import { MerklePatriciaTrie } from '@ethereumjs/trie'
44
import {
55
Account,
66
bytesToUnprefixedHex,
@@ -65,8 +65,8 @@ export class MerkleStateManager implements StateManagerInterface {
6565

6666
originalStorageCache: OriginalStorageCache
6767

68-
protected _trie: Trie
69-
protected _storageTries: { [key: string]: Trie }
68+
protected _trie: MerklePatriciaTrie
69+
protected _storageTries: { [key: string]: MerklePatriciaTrie }
7070

7171
protected readonly _prefixCodeHashes: boolean
7272
protected readonly _prefixStorageTrieKeys: boolean
@@ -102,7 +102,7 @@ export class MerkleStateManager implements StateManagerInterface {
102102

103103
this._checkpointCount = 0
104104

105-
this._trie = opts.trie ?? new Trie({ useKeyHashing: true, common: this.common })
105+
this._trie = opts.trie ?? new MerklePatriciaTrie({ useKeyHashing: true, common: this.common })
106106
this._storageTries = {}
107107

108108
this.keccakFunction = opts.common?.customCrypto.keccak256 ?? keccak256
@@ -263,14 +263,14 @@ export class MerkleStateManager implements StateManagerInterface {
263263
* @param rootAccount (Optional) Account object whose 'storageRoot' is to be used as
264264
* the root of the new storageTrie returned when there is no pre-existing trie.
265265
* If left undefined, the EMPTY_TRIE_ROOT will be used as the root instead.
266-
* @returns storage Trie object
266+
* @returns storage MerklePatriciaTrie object
267267
* @private
268268
*/
269269
// TODO PR: have a better interface for hashed address pull?
270270
protected _getStorageTrie(
271271
addressOrHash: Address | { bytes: Uint8Array } | Uint8Array,
272272
rootAccount?: Account,
273-
): Trie {
273+
): MerklePatriciaTrie {
274274
// use hashed key for lookup from storage cache
275275
const addressBytes: Uint8Array =
276276
addressOrHash instanceof Uint8Array ? addressOrHash : this.keccakFunction(addressOrHash.bytes)
@@ -295,7 +295,7 @@ export class MerkleStateManager implements StateManagerInterface {
295295
* cache or does a lookup.
296296
* @private
297297
*/
298-
protected _getAccountTrie(): Trie {
298+
protected _getAccountTrie(): MerklePatriciaTrie {
299299
return this._trie
300300
}
301301

@@ -347,7 +347,7 @@ export class MerkleStateManager implements StateManagerInterface {
347347
protected async _modifyContractStorage(
348348
address: Address,
349349
account: Account,
350-
modifyTrie: (storageTrie: Trie, done: Function) => void,
350+
modifyTrie: (storageTrie: MerklePatriciaTrie, done: Function) => void,
351351
): Promise<void> {
352352
// eslint-disable-next-line no-async-promise-executor
353353
return new Promise(async (resolve) => {

packages/statemanager/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type PrefixedHexString } from '@ethereumjs/util'
22

33
import type { AccessWitness, Caches } from './index.js'
44
import type { Common } from '@ethereumjs/common'
5-
import type { Trie } from '@ethereumjs/trie'
5+
import type { MerklePatriciaTrie } from '@ethereumjs/trie'
66
import type { VerkleCrypto } from '@ethereumjs/util'
77
import type { VerkleTree } from '@ethereumjs/verkle'
88
/**
@@ -32,9 +32,9 @@ export interface RPCStateManagerOpts extends BaseStateManagerOpts {
3232
*/
3333
export interface MerkleStateManagerOpts extends BaseStateManagerOpts {
3434
/**
35-
* A {@link Trie} instance
35+
* A {@link MerklePatriciaTrie} instance
3636
*/
37-
trie?: Trie
37+
trie?: MerklePatriciaTrie
3838
/**
3939
* Option to prefix codehashes in the database. This defaults to `true`.
4040
* If this is disabled, note that it is possible to corrupt the trie, by deploying code

0 commit comments

Comments
 (0)