Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: verkle adjustments #3775

Merged
merged 6 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@
}

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,
})

Check warning on line 143 in packages/vm/src/runBlock.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runBlock.ts#L139-L143

Added lines #L139 - L143 were not covered by tests

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`)
}

Check warning on line 147 in packages/vm/src/runBlock.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runBlock.ts#L145-L147

Added lines #L145 - L147 were not covered by tests

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

Check warning on line 154 in packages/vm/src/runBlock.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runBlock.ts#L149-L154

Added lines #L149 - L154 were not covered by tests

// 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)

Check warning on line 157 in packages/vm/src/runBlock.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runBlock.ts#L157

Added line #L157 was not covered by tests

if (
stateManager instanceof StatelessVerkleStateManager &&
verifyVerkleStateProof(stateManager) === false
) {
if (stateManager instanceof StatelessVerkleStateManager) {

Check warning on line 159 in packages/vm/src/runBlock.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runBlock.ts#L159

Added line #L159 was not covered by tests
// Update the stateRoot cache
await stateManager.setStateRoot(block.header.stateRoot)
if (verifyVerkleStateProof(stateManager) === true) {
if (vm.DEBUG) {
debug(`Verkle proof verification succeeded`)
}
} else {

Check warning on line 166 in packages/vm/src/runBlock.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runBlock.ts#L161-L166

Added lines #L161 - L166 were not covered by tests
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 @@
}
// 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

Check warning on line 328 in packages/vm/src/runBlock.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runBlock.ts#L328

Added line #L328 was not covered by tests
) {
throw new Error(`Verkle post state verification failed on block ${block.header.number}`)
}
Expand Down
Loading