Skip to content

Commit

Permalink
vm: verkle adjustments (#3775)
Browse files Browse the repository at this point in the history
* initialize witness

* fix typo in postState verification check

* Reorganize verkle checks

* fix code chunking size calculation

* remove extraneous debug log

* clean up logging
  • Loading branch information
gabrocheleau authored Oct 29, 2024
1 parent 88ef815 commit fac5169
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
5 changes: 2 additions & 3 deletions packages/statemanager/src/statefulVerkleStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ export class StatefulVerkleStateManager implements StateManagerInterface {
// Determine code ending byte (if we're on the last chunk)
let sliceEnd = 32
if (x === chunks.length - 1) {
sliceEnd = (codeSize % VERKLE_CODE_CHUNK_SIZE) + 1
// On the last chunk, the end of the slice is either codeSize (if only one chunk) or codeSize % chunkSize
sliceEnd = (x === 0 ? codeSize : codeSize % VERKLE_CODE_CHUNK_SIZE) + 1
}

code.set(chunks[x]!.slice(1, sliceEnd), code.byteOffset + x * VERKLE_CODE_CHUNK_SIZE)
}
this._caches?.code?.put(address, code)
Expand Down Expand Up @@ -538,7 +538,6 @@ export class StatefulVerkleStateManager implements StateManagerInterface {
return bytesToHex(basicDataBytes)
} else {
const encodedAccount = this._caches?.account?.get(address)?.accountRLP
this._debug(`we have encoded account ${encodedAccount}`)
if (encodedAccount === undefined) {
return null
}
Expand Down
56 changes: 27 additions & 29 deletions packages/vm/src/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import type {
import type { VM } from './vm.js'
import type { Block } from '@ethereumjs/block'
import type { Common } from '@ethereumjs/common'
import type { StatefulVerkleStateManager } from '@ethereumjs/statemanager'
import type { CLRequest, CLRequestType, PrefixedHexString } from '@ethereumjs/util'

const debug = debugDefault('vm:block')
Expand Down Expand Up @@ -134,40 +135,37 @@ export async function runBlock(vm: VM, opts: RunBlockOpts): Promise<RunBlockResu
}

if (vm.common.isActivatedEIP(6800)) {
// We only do these checks if operating statelessly since the execution witness is
// constructed during stateful execution
if (stateManager instanceof StatelessVerkleStateManager) {
if (typeof stateManager.initVerkleExecutionWitness !== 'function') {
throw Error(`StatelessVerkleStateManager needed for execution of verkle blocks`)
}
// Initialize the access witness
if ((stateManager as any)['verkleCrypto'] === undefined)
throw Error('verkleCrypto required when EIP-6800 is active')
vm.evm.verkleAccessWitness = new VerkleAccessWitness({
verkleCrypto: (stateManager as StatefulVerkleStateManager).verkleCrypto,
})

if (vm.DEBUG) {
debug(`Initializing StatelessVerkleStateManager executionWitness`)
}
if (clearCache) {
stateManager.clearCaches()
}
if (typeof stateManager.initVerkleExecutionWitness !== 'function') {
throw Error(`VerkleStateManager needed for execution of verkle blocks`)
}

// Update the stateRoot cache
await stateManager.setStateRoot(block.header.stateRoot)
if (vm.DEBUG) {
debug(`Initializing executionWitness`)
}
if (clearCache) {
stateManager.clearCaches()
}

// Initialize the access witness
vm.evm.verkleAccessWitness = new VerkleAccessWitness({
verkleCrypto: stateManager.verkleCrypto,
})
// Populate the execution witness
stateManager.initVerkleExecutionWitness!(block.header.number, block.executionWitness)
// Populate the execution witness
stateManager.initVerkleExecutionWitness!(block.header.number, block.executionWitness)

if (
stateManager instanceof StatelessVerkleStateManager &&
verifyVerkleStateProof(stateManager) === false
) {
if (stateManager instanceof StatelessVerkleStateManager) {
// Update the stateRoot cache
await stateManager.setStateRoot(block.header.stateRoot)
if (verifyVerkleStateProof(stateManager) === true) {
if (vm.DEBUG) {
debug(`Verkle proof verification succeeded`)
}
} else {
throw Error(`Verkle proof verification failed`)
}

if (vm.DEBUG) {
debug(`Verkle proof verification succeeded`)
}
}
} else {
if (typeof stateManager.initVerkleExecutionWitness === 'function') {
Expand Down Expand Up @@ -327,7 +325,7 @@ export async function runBlock(vm: VM, opts: RunBlockOpts): Promise<RunBlockResu
}
// If verkle is activated and executing statelessly, only validate the post-state
if (
!(await vm['_opts'].stateManager!.verifyPostState!(vm.evm.verkleAccessWitness)) === false
(await vm['_opts'].stateManager!.verifyPostState!(vm.evm.verkleAccessWitness)) === false
) {
throw new Error(`Verkle post state verification failed on block ${block.header.number}`)
}
Expand Down

0 comments on commit fac5169

Please sign in to comment.