Skip to content

Commit 934e20d

Browse files
acolytec3holgerd77
andauthored
Convert StatelessVerkleStateManager usage to type in vm (#4021)
* Make Verkle State Managers types in vm * Add clarifying comments * address feedback * verifyPostState -> verifyVerklePostState --------- Co-authored-by: Holger Drewes <[email protected]>
1 parent 3d94933 commit 934e20d

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

packages/statemanager/src/statefulVerkleStateManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ export class StatefulVerkleStateManager implements StateManagerInterface {
622622
}
623623

624624
// Verifies that the witness post-state matches the computed post-state
625+
// NOTE: Do not rename this method as we check for this function name in the VM to verify we're using a StatefulVerkleStateManager
625626
async verifyVerklePostState(accessWitness: VerkleAccessWitnessInterface): Promise<boolean> {
626627
// track what all chunks were accessed so as to compare in the end if any chunks were missed
627628
// in access while comparing against the provided poststate in the execution witness
@@ -710,7 +711,7 @@ export class StatefulVerkleStateManager implements StateManagerInterface {
710711

711712
const verifyPassed = postFailures === 0
712713
this.DEBUG &&
713-
this._debug(`verifyPostState verifyPassed=${verifyPassed} postFailures=${postFailures}`)
714+
this._debug(`verifyVerklePostState verifyPassed=${verifyPassed} postFailures=${postFailures}`)
714715

715716
return verifyPassed
716717
}

packages/statemanager/src/statelessVerkleStateManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ export class StatelessVerkleStateManager implements StateManagerInterface {
499499
}
500500

501501
// Verifies that the witness post-state matches the computed post-state
502+
// NOTE: Do not rename this method as we check for this function name in the VM to verify we're using a StatelessVerkleStateManager
502503
verifyVerklePostState(accessWitness: VerkleAccessWitnessInterface): Promise<boolean> {
503504
// track what all chunks were accessed so as to compare in the end if any chunks were missed
504505
// in access while comparing against the provided poststate in the execution witness
@@ -571,7 +572,7 @@ export class StatelessVerkleStateManager implements StateManagerInterface {
571572

572573
const verifyPassed = postFailures === 0
573574
this.DEBUG &&
574-
this._debug(`verifyPostState verifyPassed=${verifyPassed} postFailures=${postFailures}`)
575+
this._debug(`verifyVerklePostState verifyPassed=${verifyPassed} postFailures=${postFailures}`)
575576

576577
// This is async so the stateful variant can use the same interface method
577578
return Promise.resolve(verifyPassed)

packages/vm/src/runBlock.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConsensusType, Hardfork } from '@ethereumjs/common'
33
import { type EVM, type EVMInterface, VerkleAccessWitness } from '@ethereumjs/evm'
44
import { MerklePatriciaTrie } from '@ethereumjs/mpt'
55
import { RLP } from '@ethereumjs/rlp'
6-
import { StatelessVerkleStateManager, verifyVerkleStateProof } from '@ethereumjs/statemanager'
6+
import { type StatelessVerkleStateManager, verifyVerkleStateProof } from '@ethereumjs/statemanager'
77
import { TransactionType } from '@ethereumjs/tx'
88
import {
99
Account,
@@ -160,10 +160,11 @@ export async function runBlock(vm: VM, opts: RunBlockOpts): Promise<RunBlockResu
160160
// Populate the execution witness
161161
stateManager.initVerkleExecutionWitness!(block.header.number, block.executionWitness)
162162

163-
if (stateManager instanceof StatelessVerkleStateManager) {
163+
// Check if statemanager is a Verkle State Manager (stateless and stateful both have verifyVerklePostState)
164+
if ('verifyVerklePostState' in stateManager) {
164165
// Update the stateRoot cache
165166
await stateManager.setStateRoot(block.header.stateRoot)
166-
if (verifyVerkleStateProof(stateManager) === true) {
167+
if (verifyVerkleStateProof(stateManager as StatelessVerkleStateManager) === true) {
167168
if (vm.DEBUG) {
168169
debug(`Verkle proof verification succeeded`)
169170
}
@@ -273,7 +274,8 @@ export async function runBlock(vm: VM, opts: RunBlockOpts): Promise<RunBlockResu
273274
}
274275
}
275276

276-
if (!(vm.stateManager instanceof StatelessVerkleStateManager)) {
277+
// Check if statemanager is a StatelessVerkleStateManager by checking for a method only on StatelessVerkleStateManager API
278+
if (!('verifyVerklePostState' in vm.stateManager)) {
277279
// Only validate the following headers if Stateless isn't activated
278280
if (equalsBytes(result.receiptsRoot, block.header.receiptTrie) === false) {
279281
if (vm.DEBUG) {

packages/vm/src/runTx.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { cliqueSigner, createBlockHeader } from '@ethereumjs/block'
22
import { ConsensusType, Hardfork } from '@ethereumjs/common'
33
import { BinaryTreeAccessWitness, type EVM, VerkleAccessWitness } from '@ethereumjs/evm'
44
import {
5-
StatefulBinaryTreeStateManager,
6-
StatefulVerkleStateManager,
7-
StatelessVerkleStateManager,
5+
type StatefulVerkleStateManager,
6+
type StatelessVerkleStateManager,
87
} from '@ethereumjs/statemanager'
98
import { Capability, isBlob4844Tx, recoverAuthority } from '@ethereumjs/tx'
109
import {
@@ -205,20 +204,22 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise<RunTxResult> {
205204
throw Error(`Verkle access witness needed for execution of verkle blocks`)
206205
}
207206

208-
if (
209-
!(vm.stateManager instanceof StatefulVerkleStateManager) &&
210-
!(vm.stateManager instanceof StatelessVerkleStateManager)
211-
) {
207+
// Check if statemanager is a Verkle State Manager (stateless and stateful both have verifyVerklePostState)
208+
if (!('verifyVerklePostState' in vm.stateManager)) {
212209
throw EthereumJSErrorWithoutCode(`Verkle State Manager needed for execution of verkle blocks`)
213210
}
214211
stateAccesses = vm.evm.verkleAccessWitness
215-
txAccesses = new VerkleAccessWitness({ verkleCrypto: vm.stateManager.verkleCrypto })
212+
txAccesses = new VerkleAccessWitness({
213+
verkleCrypto: (vm.stateManager as StatelessVerkleStateManager | StatefulVerkleStateManager)
214+
.verkleCrypto,
215+
})
216216
} else if (vm.common.isActivatedEIP(7864)) {
217217
if (vm.evm.binaryTreeAccessWitness === undefined) {
218218
throw Error(`Binary tree access witness needed for execution of binary tree blocks`)
219219
}
220220

221-
if (!(vm.stateManager instanceof StatefulBinaryTreeStateManager)) {
221+
// Check if statemanager is a BinaryTreeStateManager by checking for a method only on BinaryTreeStateManager API
222+
if (!('verifyBinaryPostState' in vm.stateManager)) {
222223
throw EthereumJSErrorWithoutCode(
223224
`Binary tree State Manager needed for execution of binary tree blocks`,
224225
)

0 commit comments

Comments
 (0)