|
1 |
| -import { StatelessVerkleStateManager } from '@ethereumjs/statemanager' |
| 1 | +import { StatefulVerkleStateManager } from '@ethereumjs/statemanager' |
| 2 | +import { |
| 3 | + bytesToBigInt, |
| 4 | + bytesToHex, |
| 5 | + createAddressFromString, |
| 6 | + equalsBytes, |
| 7 | + hexToBytes, |
| 8 | +} from '@ethereumjs/util' |
| 9 | +import { createVerkleTree } from '@ethereumjs/verkle' |
2 | 10 |
|
3 | 11 | import { createEVM } from '../constructors.js'
|
4 | 12 | import { EvmErrorResult, OOGResult } from '../evm.js'
|
5 | 13 | import { ERROR, EvmError } from '../exceptions.js'
|
| 14 | +import { VerkleAccessWitness } from '../verkleAccessWitness.js' |
6 | 15 |
|
7 | 16 | import { gasLimitCheck } from './util.js'
|
8 | 17 |
|
9 | 18 | import { getPrecompileName } from './index.js'
|
10 | 19 |
|
| 20 | +import type { EVM } from '../evm.js' |
11 | 21 | import type { ExecResult } from '../types.js'
|
12 | 22 | import type { PrecompileInput } from './types.js'
|
| 23 | +import type { VerkleExecutionWitness } from '@ethereumjs/util' |
13 | 24 |
|
14 |
| -export function precompile12(opts: PrecompileInput): ExecResult { |
| 25 | +export async function precompile12(opts: PrecompileInput): Promise<ExecResult> { |
15 | 26 | const pName = getPrecompileName('12')
|
16 | 27 | const data = opts.data
|
17 |
| - |
| 28 | + const evm = opts._EVM as EVM |
18 | 29 | const gasUsed = opts.common.param('executeGasCost')
|
19 | 30 | if (!gasLimitCheck(opts, gasUsed, pName)) {
|
20 | 31 | return OOGResult(opts.gasLimit)
|
21 | 32 | }
|
22 | 33 |
|
23 |
| - if (data.length < 128) { |
| 34 | + if (data.length !== 128) { |
24 | 35 | return EvmErrorResult(new EvmError(ERROR.INVALID_INPUT_LENGTH), opts.gasLimit)
|
25 | 36 | }
|
26 | 37 |
|
27 |
| - const preStateRoot = data.subarray(0, 32) // prestateroot for L2 state |
| 38 | + const _preStateRoot = data.subarray(0, 32) // prestateroot for L2 state |
28 | 39 | const postStateRoot = data.subarray(32, 64) // post state root for L2 state
|
29 |
| - const trace = data.subarray(64, 96) // reference to state access and |
30 |
| - const executeGasUsed = data.subarray(96) |
| 40 | + const traceBlob = evm['executionBlobs'].get(bytesToHex(data.subarray(64, 96))) // reference to state access and transactions |
| 41 | + if (traceBlob === undefined) { |
| 42 | + opts._debug?.(`${pName} error - trace not found`) |
| 43 | + return EvmErrorResult(new EvmError(ERROR.REVERT), opts.gasLimit) |
| 44 | + } |
31 | 45 |
|
| 46 | + const decodedTrace = JSON.parse(new TextDecoder().decode(traceBlob)) |
| 47 | + |
| 48 | + if (decodedTrace.txs === undefined || decodedTrace.witness === undefined) { |
| 49 | + opts._debug?.(`${pName} error - trace is invalid`) |
| 50 | + return EvmErrorResult(new EvmError(ERROR.REVERT), opts.gasLimit) |
| 51 | + } |
| 52 | + const executeGasUsed = bytesToBigInt(data.subarray(96)) |
| 53 | + |
| 54 | + const witness = decodedTrace.witness as VerkleExecutionWitness |
| 55 | + const tree = await createVerkleTree({ verkleCrypto: opts.common.customCrypto.verkle }) |
| 56 | + |
| 57 | + // Populate the L2 state trie with the prestate |
| 58 | + for (const stateDiff of witness.stateDiff) { |
| 59 | + const suffixes: number[] = [] |
| 60 | + const values: Uint8Array[] = [] |
| 61 | + for (const diff of stateDiff.suffixDiffs) { |
| 62 | + if (diff.currentValue !== null) { |
| 63 | + suffixes.push(Number(diff.suffix)) |
| 64 | + values.push(hexToBytes(diff.currentValue)) |
| 65 | + } |
| 66 | + } |
| 67 | + const stem = hexToBytes(stateDiff.stem) |
| 68 | + await tree.put(stem, suffixes, values) |
| 69 | + } |
32 | 70 | const executionResult = true
|
33 | 71 |
|
34 |
| - const stateManager = new StatelessVerkleStateManager({ common: opts.common }) |
35 |
| - const evm = createEVM({ stateManager, common: opts.common }) |
| 72 | + const stateManager = new StatefulVerkleStateManager({ common: opts.common, trie: tree }) |
| 73 | + const l2EVM = await createEVM({ stateManager, common: opts.common }) |
| 74 | + |
| 75 | + l2EVM.verkleAccessWitness = new VerkleAccessWitness({ |
| 76 | + verkleCrypto: opts.common.customCrypto.verkle!, |
| 77 | + }) |
| 78 | + l2EVM.systemVerkleAccessWitness = new VerkleAccessWitness({ |
| 79 | + verkleCrypto: opts.common.customCrypto.verkle!, |
| 80 | + }) |
| 81 | + let computedGasUsed = 0n |
| 82 | + |
| 83 | + // Run each transaction in the trace |
| 84 | + for (const tx of decodedTrace.txs) { |
| 85 | + const res = await l2EVM.runCall({ |
| 86 | + to: createAddressFromString(tx.to), |
| 87 | + caller: createAddressFromString(tx.from), |
| 88 | + gasLimit: BigInt(tx.gasLimit), |
| 89 | + gasPrice: BigInt(tx.gasPrice), |
| 90 | + value: BigInt(tx.value), |
| 91 | + data: tx.data !== undefined ? hexToBytes(tx.data) : undefined, |
| 92 | + }) |
| 93 | + computedGasUsed += res.execResult.executionGasUsed |
| 94 | + } |
| 95 | + |
| 96 | + if (computedGasUsed !== executeGasUsed) { |
| 97 | + opts._debug?.(`${pName} gas used mismatch: ${computedGasUsed} !== ${executeGasUsed}`) |
| 98 | + return EvmErrorResult(new EvmError(ERROR.REVERT), opts.gasLimit) |
| 99 | + } |
| 100 | + |
| 101 | + if (!equalsBytes(postStateRoot, tree.root())) { |
| 102 | + opts._debug?.(`${pName} post state root mismatch`) |
| 103 | + return EvmErrorResult(new EvmError(ERROR.REVERT), opts.gasLimit) |
| 104 | + } |
36 | 105 |
|
37 | 106 | opts._debug?.(`${pName} trace executed successfully=${executionResult}`)
|
38 | 107 |
|
|
0 commit comments