Skip to content

Commit 8bbf0e2

Browse files
GarmashAlexam1r021acolytec3
authored
docs(evm): update internal structure section in README (#4045)
* docs(evm): update internal structure section in README * Update packages/evm/README.md Co-authored-by: am1r021 <[email protected]> * Update README.md --------- Co-authored-by: am1r021 <[email protected]> Co-authored-by: acolytec3 <[email protected]>
1 parent e74a502 commit 8bbf0e2

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

packages/evm/README.md

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -422,28 +422,59 @@ Additional log selections can be added with a comma separated list (no spaces).
422422

423423
### Internal Structure
424424

425-
The EVM processes state changes at many levels.
426-
427-
- **runCall**
428-
- checkpoint state
429-
- transfer value
430-
- load code
431-
- runCode
432-
- materialize created contracts
433-
- revert or commit checkpoint
434-
- **runCode**
435-
- iterate over code
436-
- run op codes
437-
- track gas usage
438-
- **OpFns**
439-
- run individual op code
440-
- modify stack
441-
- modify memory
442-
- calculate fee
443-
444-
The opFns for `CREATE`, `CALL`, and `CALLCODE` call back up to `runCall`.
445-
446-
TODO: this section likely needs an update.
425+
The EVM processes state changes through a hierarchical flow of execution:
426+
427+
#### Top Level: Message Execution (`runCall`)
428+
The `runCall` method handles the execution of messages, which can be either contract calls or contract creations:
429+
- Creates a checkpoint in the state
430+
- Sets up the execution environment (block context, transaction origin, etc.)
431+
- Manages account nonce updates
432+
- Handles value transfers between accounts
433+
- Delegates to either `_executeCall` or `_executeCreate` based on whether the message has a `to` address
434+
- **Both `_executeCall` and `_executeCreate` call into `runInterpreter` to actually execute the bytecode**
435+
- Processes any errors or exceptions
436+
- Manages selfdestruct sets and created contract addresses
437+
- Commits or reverts state changes based on execution result
438+
- Triggers events (`beforeMessage`, `afterMessage`)
439+
440+
#### Code Execution (`runCode` / `runInterpreter`)
441+
The `runCode` method is a helper for directly running EVM bytecode (e.g., for testing or utility purposes) without the full message/transaction context:
442+
- Sets up a minimal message context for code execution
443+
- **Directly calls `runInterpreter` to execute the provided bytecode**
444+
- Does not go through the full message handling logic of `runCall`
445+
446+
The `runInterpreter` method is used by both `runCall` (via `_executeCall`/`_executeCreate`) and `runCode` to process the actual bytecode.
447+
448+
#### Bytecode Processing (Interpreter)
449+
The Interpreter class is the core bytecode processor:
450+
- Manages execution state (program counter, stack, memory, gas)
451+
- Executes a loop that:
452+
- Analyzes jump destinations
453+
- Fetches the next opcode
454+
- Calculates gas costs (static and dynamic)
455+
- Executes the opcode handler
456+
- Updates the program counter
457+
- Emits step events for debugging/tracing
458+
- Handles stack, memory, and storage operations
459+
- Processes call and creation operations by delegating back to the EVM
460+
461+
#### Opcode Functions
462+
Each opcode has an associated handler function that:
463+
- Validates inputs
464+
- Calculates dynamic gas costs
465+
- Performs the opcode's logic (stack operations, memory operations, etc.)
466+
- Updates the EVM state
467+
- The program counter is incremented in between the execution of the gas handler and opcode logic handler functions, this should be considered e.g. if parsing immediate input parameters
468+
- Special opcodes like `CALL`, `CREATE`, `DELEGATECALL` create a new message and call back to the EVM's `runCall` method
469+
470+
#### Journal and State Management
471+
- State changes are tracked in a journal system
472+
- The journal supports checkpointing and reversion
473+
- Transient storage (EIP-1153) has its own checkpoint mechanism
474+
- When a message completes successfully, changes are committed to the state
475+
- On failure (exceptions), changes are reverted
476+
477+
This layered architecture provides separation of concerns while allowing for the complex interactions needed to execute smart contracts on the Ethereum platform.
447478

448479
## Profiling the EVM
449480

0 commit comments

Comments
 (0)