Skip to main content

> cat ./x_thread.md

[terminal_mode]

EVM Deep Dive: Understanding the Ethereum Virtual Machine Architecture

DATE:2018-06-15|FORMAT:x_twitter_thread
Nelson Lourenço's avatar
Nelson Lourenço@nelsonstr
42 tweets in this thread
📅 2018-06-15
tweet #11/42

# EVM Deep Dive 🧵

❤️513🔁45💬54📅2018-06-15
tweet #22/42

Understanding the Ethereum Virtual Machine from the ground up:

❤️513🔁45💬54
tweet #33/42

**1. Architecture Overview:**

❤️513🔁45💬54
tweet #44/42

The EVM is: → Stack-based virtual machine → 256-bit word size → Deterministic execution → Gas-powered computation

❤️513🔁45💬54
tweet #55/42

``` ┌─────────────────────────────────────┐ │ EVM Runtime │ ├─────────────────────────────────────┤ │ Stack (1024 items max) │ │ Memory (byte-addressable) │ │ Storage (persistent key-value) │ │ Calldata (input data) │ │ Returndata (output data) │ └─────────────────────────────────────┘ ```

❤️513🔁45💬54
tweet #66/42

**2. Gas Mechanics:**

❤️513🔁45💬54
tweet #77/42

Every operation costs gas:

❤️513🔁45💬54
tweet #88/42

``` Simple Operations: PUSH1 → 3 gas ADD → 3 gas SLOAD → 2100 gas SSTORE → 20000/5000 gas

❤️513🔁45💬54
tweet #99/42

Complex Operations: CREATE → 32000 gas EXTCODECOPY → 700 gas DELEGATECALL → 700 gas ```

❤️513🔁45💬54
tweet #1010/42

**3. Opcodes Categories:**

❤️513🔁45💬54
tweet #1111/42

Arithmetic: → ADD, SUB, MUL, DIV, MOD, EXP

❤️513🔁45💬54
tweet #1212/42

Storage: → SLOAD, SSTORE

❤️513🔁45💬54
tweet #1313/42

Memory: → MLOAD, MSTORE, MSIZE

❤️513🔁45💬54
tweet #1414/42

Flow Control: → JUMP, JUMPI, PC, GAS

❤️513🔁45💬54
tweet #1515/42

Logging: → LOG0-LOG4

❤️513🔁45💬54
tweet #1616/42

**4. Storage Patterns:**

❤️513🔁45💬54
tweet #1717/42

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)

❤️513🔁45💬54
tweet #1818/42

Optimization patterns: ```solidity // Expensive: Packed struct struct User { uint128 a; uint128 b; }

❤️513🔁45💬54
tweet #1919/42

// Cheaper: Separate storage uint128 public userA; uint128 public userB; ```

❤️513🔁45💬54
tweet #2020/42

**5. Memory Layout:**

❤️513🔁45💬54
tweet #2121/42

Memory is cheap but grows in 32-byte words: → MSTORE: 3 gas → MLOAD: 3 gas → Memory expansion: quadratic cost

❤️513🔁45💬54
tweet #2222/42

Best practices: ```solidity // Cache length uint256 length = array.length; for (uint256 i = 0; i < length; i++) { // Process } ```

❤️513🔁45💬54
tweet #2323/42

**6. Call Data:**

❤️513🔁45💬54
tweet #2424/42

Calldata is immutable and gas-efficient: → 4 gas per non-zero byte → 16 gas per zero byte

❤️513🔁45💬54
tweet #2525/42

```solidity function transfer(address to, uint256 amount) external { // to and amount from calldata // No decoding cost like ABI } ```

❤️513🔁45💬54
tweet #2626/42

**7. Bytecode Optimization:**

❤️513🔁45💬54
tweet #2727/42

```assembly // Unoptimized: PUSH1 0x00 PUSH1 0x00 ADD

❤️513🔁45💬54
tweet #2828/42

// Optimized: PUSH1 0x00 ADD // stack already has 0x00 ```

❤️513🔁45💬54
tweet #2929/42

**8. EIP Standards:**

❤️513🔁45💬54
tweet #3030/42

Important EIPs: → EIP-155: Replay protection → EIP-712: Typed data hashing → EIP-2612: Permit extension → EIP-3074: AUTH/AUTHCALL → EIP-4337: Account abstraction

❤️513🔁45💬54
tweet #3131/42

**9. Alternative EVMs:**

❤️513🔁45💬54
tweet #3232/42

EVM Compatible Chains: → Binance Smart Chain → Polygon → Avalanche (C-Chain) → Arbitrum → Optimism

❤️513🔁45💬54
tweet #3333/42

Non-EVM: → Solana (Rust-based) → Cosmos (CosmWasm) → Polkadot (ink!)

❤️513🔁45💬54
tweet #3434/42

**10. Tooling:**

❤️513🔁45💬54
tweet #3535/42

Development: → Hardhat → Foundry → Remix

❤️513🔁45💬54
tweet #3636/42

Analysis: → Slither → Mythril → Echidna

❤️513🔁45💬54
tweet #3737/42

**11. Security Considerations:**

❤️513🔁45💬54
tweet #3838/42

Reentrancy: ```solidity // UNSAFE function withdraw() external { msg.sender.call{value: balance}(""); balance[msg.sender] = 0; }

❤️513🔁45💬54
tweet #3939/42

// SAFE - CEI pattern function withdraw() external { uint256 amount = balance[msg.sender]; balance[msg.sender] = 0; (bool success, ) = msg.sender.call{value: amount}(""); require(success); } ```

❤️513🔁45💬54
tweet #4040/42

**12. Future of EVM:**

❤️513🔁45💬54
tweet #4141/42

→ EIP-4844 (Proto-danksharding) → EIP-3074 (Auth abstraction) → EOF (EVM Object Format) → Verkle Trees

❤️513🔁45💬54
tweet #4242/42

#Ethereum #EVM #Blockchain

#Ethereum#EVM#Blockchain
❤️513🔁45💬54
❤️513 likes🔁45 retweets💬54 replies
View on X →