> cat ./x_thread.md
[terminal_mode]EVM Deep Dive: Understanding the Ethereum Virtual Machine Architecture
# EVM Deep Dive 🧵
Understanding the Ethereum Virtual Machine from the ground up:
**1. Architecture Overview:**
The EVM is: → Stack-based virtual machine → 256-bit word size → Deterministic execution → Gas-powered computation
``` ┌─────────────────────────────────────┐ │ EVM Runtime │ ├─────────────────────────────────────┤ │ Stack (1024 items max) │ │ Memory (byte-addressable) │ │ Storage (persistent key-value) │ │ Calldata (input data) │ │ Returndata (output data) │ └─────────────────────────────────────┘ ```
**2. Gas Mechanics:**
Every operation costs gas:
``` Simple Operations: PUSH1 → 3 gas ADD → 3 gas SLOAD → 2100 gas SSTORE → 20000/5000 gas
Complex Operations: CREATE → 32000 gas EXTCODECOPY → 700 gas DELEGATECALL → 700 gas ```
**3. Opcodes Categories:**
Arithmetic: → ADD, SUB, MUL, DIV, MOD, EXP
Storage: → SLOAD, SSTORE
Memory: → MLOAD, MSTORE, MSIZE
Flow Control: → JUMP, JUMPI, PC, GAS
Logging: → LOG0-LOG4
**4. Storage Patterns:**
Storage is expensive (SSTORE): → Cold access: 20,000 gas → Warm access: 5,000 gas → Zero to non-zero: 20,000 gas → Non-zero to zero: 5,000 gas (refund)
Optimization patterns: ```solidity // Expensive: Packed struct struct User { uint128 a; uint128 b; }
// Cheaper: Separate storage uint128 public userA; uint128 public userB; ```
**5. Memory Layout:**
Memory is cheap but grows in 32-byte words: → MSTORE: 3 gas → MLOAD: 3 gas → Memory expansion: quadratic cost
Best practices: ```solidity // Cache length uint256 length = array.length; for (uint256 i = 0; i < length; i++) { // Process } ```
**6. Call Data:**
Calldata is immutable and gas-efficient: → 4 gas per non-zero byte → 16 gas per zero byte
```solidity function transfer(address to, uint256 amount) external { // to and amount from calldata // No decoding cost like ABI } ```
**7. Bytecode Optimization:**
```assembly // Unoptimized: PUSH1 0x00 PUSH1 0x00 ADD
// Optimized: PUSH1 0x00 ADD // stack already has 0x00 ```
**8. EIP Standards:**
Important EIPs: → EIP-155: Replay protection → EIP-712: Typed data hashing → EIP-2612: Permit extension → EIP-3074: AUTH/AUTHCALL → EIP-4337: Account abstraction
**9. Alternative EVMs:**
EVM Compatible Chains: → Binance Smart Chain → Polygon → Avalanche (C-Chain) → Arbitrum → Optimism
Non-EVM: → Solana (Rust-based) → Cosmos (CosmWasm) → Polkadot (ink!)
**10. Tooling:**
Development: → Hardhat → Foundry → Remix
Analysis: → Slither → Mythril → Echidna
**11. Security Considerations:**
Reentrancy: ```solidity // UNSAFE function withdraw() external { msg.sender.call{value: balance}(""); balance[msg.sender] = 0; }
// SAFE - CEI pattern function withdraw() external { uint256 amount = balance[msg.sender]; balance[msg.sender] = 0; (bool success, ) = msg.sender.call{value: amount}(""); require(success); } ```
**12. Future of EVM:**
→ EIP-4844 (Proto-danksharding) → EIP-3074 (Auth abstraction) → EOF (EVM Object Format) → Verkle Trees
#Ethereum #EVM #Blockchain