Skip to content

Commit b3ff2bc

Browse files
authored
trie: rename trie helpers to mpt (#3718)
* trie: rename trie to mpt * trie: fix example * trie: rename trie helpers to mpt * verkle: fix type issues
1 parent 53a293e commit b3ff2bc

35 files changed

+344
-340
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MerkleStateManager } from '@ethereumjs/statemanager'
2-
import { verifyTrieRangeProof } from '@ethereumjs/trie'
2+
import { verifyMPTRangeProof } from '@ethereumjs/trie'
33
import {
44
BIGINT_0,
55
BIGINT_1,
@@ -324,7 +324,7 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
324324
const keys = accounts.map((acc: any) => acc.hash)
325325
const values = accounts.map((acc: any) => accountBodyToRLP(acc.body))
326326
// convert the request to the right values
327-
return verifyTrieRangeProof(stateRoot, origin, keys[keys.length - 1], keys, values, proof, {
327+
return verifyMPTRangeProof(stateRoot, origin, keys[keys.length - 1], keys, values, proof, {
328328
common: this.config.chainCommon,
329329
useKeyHashingFunction: this.config.chainCommon?.customCrypto?.keccak256 ?? keccak256,
330330
})
@@ -405,7 +405,7 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
405405
// check zero-element proof
406406
if (rangeResult.proof.length > 0) {
407407
try {
408-
const isMissingRightRange = await verifyTrieRangeProof(
408+
const isMissingRightRange = await verifyMPTRangeProof(
409409
this.root,
410410
origin,
411411
null,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MerkleStateManager } from '@ethereumjs/statemanager'
2-
import { verifyTrieRangeProof } from '@ethereumjs/trie'
2+
import { verifyMPTRangeProof } from '@ethereumjs/trie'
33
import {
44
BIGINT_0,
55
BIGINT_1,
@@ -128,7 +128,7 @@ export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageDat
128128
)
129129
const keys = slots.map((slot: any) => slot.hash)
130130
const values = slots.map((slot: any) => slot.body)
131-
return await verifyTrieRangeProof(
131+
return await verifyMPTRangeProof(
132132
stateRoot,
133133
origin,
134134
keys[keys.length - 1],
@@ -280,7 +280,7 @@ export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageDat
280280
// zero-element proof
281281
if (rangeResult.proof.length > 0) {
282282
try {
283-
const isMissingRightRange = await verifyTrieRangeProof(
283+
const isMissingRightRange = await verifyMPTRangeProof(
284284
task.storageRequests[0].storageRoot,
285285
origin,
286286
null,
@@ -337,7 +337,7 @@ export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageDat
337337
const proof = i === rangeResult.slots.length - 1 ? rangeResult.proof : undefined
338338
if (proof === undefined || proof.length === 0) {
339339
// all-elements proof verification
340-
await verifyTrieRangeProof(
340+
await verifyMPTRangeProof(
341341
root,
342342
null,
343343
null,

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { MerkleStateManager } from '@ethereumjs/statemanager'
22
import {
3-
BranchNode,
4-
ExtensionNode,
5-
LeafNode,
3+
BranchMPTNode,
4+
ExtensionMPTNode,
5+
LeafMPTNode,
66
MerklePatriciaTrie,
77
decodeNode,
88
mergeAndFormatKeyPaths,
@@ -226,8 +226,8 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
226226
let hasStorageComponent = false
227227

228228
// get all children of received node
229-
if (node instanceof BranchNode) {
230-
const children = (node as BranchNode).getChildren()
229+
if (node instanceof BranchMPTNode) {
230+
const children = (node as BranchMPTNode).getChildren()
231231
for (const [i, embeddedNode] of children) {
232232
if (embeddedNode !== null) {
233233
const newStoragePath = nodePath.concat(bytesToHex(Uint8Array.from([i])))
@@ -240,7 +240,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
240240
})
241241
}
242242
}
243-
} else if (node instanceof ExtensionNode) {
243+
} else if (node instanceof ExtensionMPTNode) {
244244
this.DEBUG && this.debug('extension node found')
245245
const stringPath = bytesToHex(pathToHexKey(nodePath, node.key(), 'hex'))
246246
const syncPath =
@@ -341,7 +341,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
341341

342342
// add account node data to account trie
343343
const node = decodeNode(nodeData)
344-
if (node instanceof LeafNode) {
344+
if (node instanceof LeafMPTNode) {
345345
const key = bytesToHex(pathToHexKey(path, node.key(), 'keybyte'))
346346
ops.push({
347347
type: 'put',
@@ -359,7 +359,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
359359
if (pathToStorageNode !== undefined && pathToStorageNode.size > 0) {
360360
for (const [path, data] of pathToStorageNode) {
361361
const storageNode = decodeNode(data)
362-
if (storageNode instanceof LeafNode) {
362+
if (storageNode instanceof LeafMPTNode) {
363363
const storageKey = bytesToHex(pathToHexKey(path, storageNode.key(), 'keybyte'))
364364
storageTrieOps.push({
365365
type: 'put',

packages/client/test/net/protocol/snapprotocol.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RLP } from '@ethereumjs/rlp'
2-
import { decodeNode, verifyTrieRangeProof } from '@ethereumjs/trie'
2+
import { decodeNode, verifyMPTRangeProof } from '@ethereumjs/trie'
33
import {
44
KECCAK256_NULL,
55
KECCAK256_RLP,
@@ -191,7 +191,7 @@ describe('[SnapProtocol]', () => {
191191
try {
192192
const keys = accounts.map((acc: any) => acc.hash)
193193
const values = accounts.map((acc: any) => accountBodyToRLP(acc.body))
194-
await verifyTrieRangeProof(stateRoot, keys[0], keys[keys.length - 1], keys, values, proof, {
194+
await verifyMPTRangeProof(stateRoot, keys[0], keys[keys.length - 1], keys, values, proof, {
195195
useKeyHashingFunction: keccak256,
196196
})
197197
} catch (e) {
@@ -324,7 +324,7 @@ describe('[SnapProtocol]', () => {
324324
try {
325325
const keys = lastAccountSlots.map((acc: any) => acc.hash)
326326
const values = lastAccountSlots.map((acc: any) => acc.body)
327-
await verifyTrieRangeProof(
327+
await verifyMPTRangeProof(
328328
lastAccountStorageRoot,
329329
keys[0],
330330
keys[keys.length - 1],

packages/client/test/sync/fetcher/trienodefetcher.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Config } from '../../../src/config.js'
99
import { SnapProtocol } from '../../../src/net/protocol/index.js'
1010
import { wait } from '../../integration/util.js'
1111

12-
import type { BranchNode } from '@ethereumjs/trie'
12+
import type { BranchMPTNode } from '@ethereumjs/trie'
1313

1414
// Collected from Sepolia:
1515
// getTrieNodes({
@@ -165,7 +165,7 @@ describe('[TrieNodeFetcher]', async () => {
165165

166166
await fetcher.store(requestResult)
167167

168-
const rootNode = decodeNode(nodes[0] as unknown as Uint8Array) as BranchNode
168+
const rootNode = decodeNode(nodes[0] as unknown as Uint8Array) as BranchMPTNode
169169
const children = rootNode.getChildren()
170170
assert.equal(
171171
children.length,

packages/statemanager/src/proofs/merkle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
createMerkleProof,
44
createTrieFromProof,
55
updateTrieFromMerkleProof,
6-
verifyTrieProof,
6+
verifyMPTProof,
77
} from '@ethereumjs/trie'
88
import {
99
KECCAK256_NULL,
@@ -194,7 +194,7 @@ export async function verifyMerkleStateProof(
194194

195195
// This returns the account if the proof is valid.
196196
// Verify that it matches the reported account.
197-
const value = await verifyTrieProof(key, accountProof, {
197+
const value = await verifyMPTProof(key, accountProof, {
198198
useKeyHashing: true,
199199
})
200200

@@ -240,7 +240,7 @@ export async function verifyMerkleStateProof(
240240
const storageProof = stProof.proof.map((value: PrefixedHexString) => hexToBytes(value))
241241
const storageValue = setLengthLeft(hexToBytes(stProof.value), 32)
242242
const storageKey = hexToBytes(stProof.key)
243-
const proofValue = await verifyTrieProof(storageKey, storageProof, {
243+
const proofValue = await verifyMPTProof(storageKey, storageProof, {
244244
useKeyHashing: true,
245245
})
246246
const reportedValue = setLengthLeft(

packages/statemanager/test/rpcStateManager.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createBlockFromJSONRPCProvider, createBlockFromRPC } from '@ethereumjs/block'
22
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
33
import { type EVMRunCallOpts, createEVM } from '@ethereumjs/evm'
4-
import { verifyTrieProof } from '@ethereumjs/trie'
4+
import { verifyMPTProof } from '@ethereumjs/trie'
55
import { createFeeMarket1559Tx, createTxFromRPC } from '@ethereumjs/tx'
66
import {
77
Address,
@@ -88,7 +88,7 @@ describe('RPC State Manager API tests', () => {
8888
const address = createAddressFromString('0xccAfdD642118E5536024675e776d32413728DD07')
8989
const proof = await getRPCStateProof(state, address)
9090
const proofBuf = proof.accountProof.map((proofNode) => hexToBytes(proofNode))
91-
const doesThisAccountExist = await verifyTrieProof(address.bytes, proofBuf, {
91+
const doesThisAccountExist = await verifyMPTProof(address.bytes, proofBuf, {
9292
useKeyHashing: true,
9393
})
9494
assert.ok(!doesThisAccountExist, 'getAccount returns undefined for non-existent account')

packages/trie/scripts/view.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77
utf8ToBytes,
88
} from '@ethereumjs/util'
99

10-
import { BranchNode, ExtensionNode, LeafNode } from '../node/index.js'
10+
import { BranchMPTNode, ExtensionMPTNode, LeafMPTNode } from '../node/index.js'
1111
import { MerklePatriciaTrie } from '../trie.js'
1212

1313
import { _walkTrie } from './asyncWalk.js'
1414

15-
import type { TrieNode } from '../types.js'
15+
import type { MPTNode } from '../types.js'
1616
import type { Debugger } from 'debug'
1717

1818
const debug = _debug('trieview') // cspell:disable-line
@@ -45,17 +45,17 @@ const debugN = (type: TNode, d?: Debugger) => {
4545
}
4646
const debugT = debug.extend('Trie')
4747

48-
function getNodeType(node: TrieNode): TNode {
49-
return node instanceof BranchNode
48+
function getNodeType(node: MPTNode): TNode {
49+
return node instanceof BranchMPTNode
5050
? 'br'
51-
: node instanceof ExtensionNode
51+
: node instanceof ExtensionMPTNode
5252
? 'ex'
53-
: node instanceof LeafNode
53+
: node instanceof LeafMPTNode
5454
? 'lf'
5555
: 'nl'
5656
}
5757

58-
function logNode(trie: MerklePatriciaTrie, node: TrieNode, currentKey: number[]): void {
58+
function logNode(trie: MerklePatriciaTrie, node: MPTNode, currentKey: number[]): void {
5959
delimiter(3)
6060
const type = getNodeType(node)
6161
if (equalsBytes(trie.hash(node.serialize()), trie.root())) {

packages/trie/src/constructors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { concatBytes } from 'ethereum-cryptography/utils'
99

1010
import { MerklePatriciaTrie, ROOT_DB_KEY, updateTrieFromMerkleProof } from './index.js'
1111

12-
import type { Proof, TrieOpts } from './index.js'
12+
import type { MPTOpts, Proof } from './index.js'
1313

14-
export async function createTrie(opts?: TrieOpts) {
14+
export async function createTrie(opts?: MPTOpts) {
1515
const keccakFunction =
1616
opts?.common?.customCrypto.keccak256 ?? opts?.useKeyHashingFunction ?? keccak256
1717
let key = ROOT_DB_KEY
@@ -60,7 +60,7 @@ export async function createTrie(opts?: TrieOpts) {
6060
* @param trieOpts trie opts to be applied to returned trie
6161
* @returns new trie created from given proof
6262
*/
63-
export async function createTrieFromProof(proof: Proof, trieOpts?: TrieOpts) {
63+
export async function createTrieFromProof(proof: Proof, trieOpts?: MPTOpts) {
6464
const shouldVerifyRoot = trieOpts?.root !== undefined
6565
const trie = new MerklePatriciaTrie(trieOpts)
6666
const root = await updateTrieFromMerkleProof(trie, proof, shouldVerifyRoot)

packages/trie/src/node/branch.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { RLP } from '@ethereumjs/rlp'
22

3-
import type { BranchNodeBranchValue, NodeReferenceOrRawNode } from '../types.js'
3+
import type { BranchMPTNodeBranchValue, NodeReferenceOrRawNode } from '../types.js'
44

5-
export class BranchNode {
6-
_branches: BranchNodeBranchValue[]
5+
export class BranchMPTNode {
6+
_branches: BranchMPTNodeBranchValue[]
77
_value: Uint8Array | null
88

99
constructor() {
1010
this._branches = new Array(16).fill(null)
1111
this._value = null
1212
}
1313

14-
static fromArray(arr: Uint8Array[]): BranchNode {
15-
const node = new BranchNode()
14+
static fromArray(arr: Uint8Array[]): BranchMPTNode {
15+
const node = new BranchMPTNode()
1616
node._branches = arr.slice(0, 16)
1717
node._value = arr[16]
1818
return node
@@ -26,19 +26,19 @@ export class BranchNode {
2626
return this._value && this._value.length > 0 ? this._value : null
2727
}
2828

29-
setBranch(i: number, v: BranchNodeBranchValue) {
29+
setBranch(i: number, v: BranchMPTNodeBranchValue) {
3030
this._branches[i] = v
3131
}
3232

33-
raw(): BranchNodeBranchValue[] {
33+
raw(): BranchMPTNodeBranchValue[] {
3434
return [...this._branches, this._value]
3535
}
3636

3737
serialize(): Uint8Array {
3838
return RLP.encode(this.raw())
3939
}
4040

41-
getBranch(i: number): BranchNodeBranchValue {
41+
getBranch(i: number): BranchMPTNodeBranchValue {
4242
const b = this._branches[i]
4343
if (b !== null && b.length > 0) {
4444
return b

0 commit comments

Comments
 (0)