From 83db44647efd6275939918943662d804c679e479 Mon Sep 17 00:00:00 2001 From: Holger Drewes Date: Sat, 17 Aug 2024 18:47:33 +0200 Subject: [PATCH] Add Verkle SM methods as optional methods to interface, replace VerkleSM imports and castings in VM --- packages/common/src/interfaces.ts | 10 ++++++- .../src/statelessVerkleStateManager.ts | 2 +- packages/vm/examples/test.ts | 6 ++++ packages/vm/src/runBlock.ts | 28 ++++++++----------- packages/vm/src/runTx.ts | 9 +++--- 5 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 packages/vm/examples/test.ts diff --git a/packages/common/src/interfaces.ts b/packages/common/src/interfaces.ts index f8c224e38f..c14abc2a49 100644 --- a/packages/common/src/interfaces.ts +++ b/packages/common/src/interfaces.ts @@ -2,7 +2,7 @@ * External Interfaces for other EthereumJS libraries */ -import type { Account, Address, PrefixedHexString } from '@ethereumjs/util' +import type { Account, Address, PrefixedHexString, VerkleExecutionWitness } from '@ethereumjs/util' export interface StorageDump { [key: string]: string @@ -161,6 +161,14 @@ export interface StateManagerInterface { } generateCanonicalGenesis?(initState: any): Promise // TODO make input more typesafe // only Verkle/EIP-6800 (experimental) + accessWitness?: AccessWitnessInterface + initVerkleExecutionWitness?( + blockNum: bigint, + executionWitness?: VerkleExecutionWitness | null, + accessWitness?: AccessWitnessInterface, + ): void + verifyVerkleProof?(stateRoot: Uint8Array): boolean + verifyPostState?(): boolean checkChunkWitnessPresent?(contract: Address, programCounter: number): Promise getAppliedKey?(address: Uint8Array): Uint8Array // only for preimages diff --git a/packages/statemanager/src/statelessVerkleStateManager.ts b/packages/statemanager/src/statelessVerkleStateManager.ts index d63733f996..392bc9a2ef 100644 --- a/packages/statemanager/src/statelessVerkleStateManager.ts +++ b/packages/statemanager/src/statelessVerkleStateManager.ts @@ -520,7 +520,7 @@ export class StatelessVerkleStateManager implements StateManagerInterface { * @param {Uint8Array} stateRoot - The stateRoot to verify the executionWitness against * @returns {boolean} - Returns true if the executionWitness matches the provided stateRoot, otherwise false */ - verifyProof(stateRoot: Uint8Array): boolean { + verifyVerkleProof(stateRoot: Uint8Array): boolean { if (this._executionWitness === undefined) { debug('Missing executionWitness') return false diff --git a/packages/vm/examples/test.ts b/packages/vm/examples/test.ts new file mode 100644 index 0000000000..21677ff3c5 --- /dev/null +++ b/packages/vm/examples/test.ts @@ -0,0 +1,6 @@ +import { StatelessVerkleStateManager } from '@ethereumjs/statemanager' +import { loadVerkleCrypto } from 'verkle-cryptography-wasm' + +const verkleCrypto = await loadVerkleCrypto() +const sm = new StatelessVerkleStateManager({ verkleCrypto }) +console.log(sm) diff --git a/packages/vm/src/runBlock.ts b/packages/vm/src/runBlock.ts index 3c637932a6..2be9373887 100644 --- a/packages/vm/src/runBlock.ts +++ b/packages/vm/src/runBlock.ts @@ -1,7 +1,6 @@ import { createBlock, genRequestsTrieRoot } from '@ethereumjs/block' import { ConsensusType, Hardfork } from '@ethereumjs/common' import { RLP } from '@ethereumjs/rlp' -import { StatelessVerkleStateManager } from '@ethereumjs/statemanager' import { Trie } from '@ethereumjs/trie' import { TransactionType } from '@ethereumjs/tx' import { @@ -134,7 +133,7 @@ export async function runBlock(vm: VM, opts: RunBlockOpts): Promise { let stateAccesses if (vm.common.isActivatedEIP(6800)) { - if (!(vm.stateManager instanceof StatelessVerkleStateManager)) { + if (typeof vm.stateManager.initVerkleExecutionWitness !== 'function') { throw Error(`StatelessVerkleStateManager needed for execution of verkle blocks`) } - stateAccesses = (vm.stateManager as StatelessVerkleStateManager).accessWitness + stateAccesses = vm.stateManager.accessWitness! } const txAccesses = stateAccesses?.shallowCopy() @@ -597,7 +596,7 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { let minerAccount = await state.getAccount(miner) if (minerAccount === undefined) { if (vm.common.isActivatedEIP(6800)) { - ;(state as StatelessVerkleStateManager).accessWitness!.touchAndChargeProofOfAbsence(miner) + state.accessWitness!.touchAndChargeProofOfAbsence(miner) } minerAccount = new Account() } @@ -609,7 +608,7 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { if (vm.common.isActivatedEIP(6800)) { // use vm utility to build access but the computed gas is not charged and hence free - ;(state as StatelessVerkleStateManager).accessWitness!.touchTxTargetAndComputeGas(miner, { + state.accessWitness!.touchTxTargetAndComputeGas(miner, { sendsValue: true, }) }