Tutorial·10 मिनट का पठन·Solingo द्वारा

EVM क्या है? — Ethereum Virtual Machine Complete Guide

Ethereum Virtual Machine (EVM) को depth में समझें: architecture, opcodes, gas mechanism और smart contracts कैसे execute होते हैं।

# EVM क्या है? — Ethereum Virtual Machine Complete Guide

Ethereum Virtual Machine (EVM) Ethereum ecosystem का heart है। यह deterministic virtual machine है जो smart contracts execute करता है decentralized तरीके से हजारों nodes पर simultaneously।

इस comprehensive guide में, हम EVM की architecture, execution model, gas mechanism और optimization techniques explore करेंगे।

EVM: Decentralized World Computer

EVM एक stack-based virtual machine है जो Ethereum protocol का core component है। इसके key characteristics:

  • Deterministic: Same input हमेशा same output produce करता है
  • Isolated: Contract execution sandboxed environment में होता है
  • Turing-complete: किसी भी computation को perform कर सकता है (gas limits के साथ)
  • Replicated: हर Ethereum node same EVM state maintain करता है

Why Virtual Machine?

Physical machine के बजाय virtual machine use करने के advantages:

  • Platform independence: Contract किसी भी OS पर run होते हैं
  • Security: Sandboxed execution malicious code को contain करता है
  • Consensus: Deterministic execution सभी nodes को same result पर agree करने देता है
  • Upgradeability: Protocol upgrades बिना hardware changes के possible हैं
  • EVM Architecture

    Memory Model

    EVM तीन areas में data store करता है:

  • Stack:
  • - 256-bit values के लिए

    - Maximum depth: 1024 items

    - LIFO (Last In First Out) structure

    - Operations: PUSH, POP, SWAP, DUP

  • Memory:
  • - Temporary volatile storage

    - Linear byte array

    - Transaction end पर cleared

    - Expensive (gas cost grows quadratically)

  • Storage:
  • - Persistent key-value store

    - 256-bit to 256-bit mapping

    - Blockchain में permanently stored

    - बहुत expensive (20,000 gas for new slot)

    Example Memory Usage:

    contract MemoryExample {
    

    uint256 public storedData; // Storage (persistent)

    function compute(uint256 x) public pure returns (uint256) {

    uint256 temp = x * 2; // Memory (temporary)

    return temp + 10; // Stack operations

    }

    function save(uint256 x) public {

    storedData = x; // Storage write (expensive)

    }

    }

    Opcodes: EVM की Language

    EVM bytecode opcodes (operation codes) की series है। हर opcode एक specific operation perform करता है।

    Common Opcodes:

    | Opcode | Name | Gas | Description |

    |--------|------|-----|-------------|

    | 0x00 | STOP | 0 | Execution halt करें |

    | 0x01 | ADD | 3 | Stack के top 2 values add करें |

    | 0x02 | MUL | 5 | Multiply करें |

    | 0x51 | MLOAD | 3 | Memory से load करें |

    | 0x52 | MSTORE | 3 | Memory में store करें |

    | 0x54 | SLOAD | 2100 | Storage से load करें |

    | 0x55 | SSTORE | 20000/5000 | Storage में store करें |

    Solidity to Bytecode:

    function add(uint256 a, uint256 b) public pure returns (uint256) {
    

    return a + b;

    }

    Compiled bytecode (simplified):

    PUSH1 0x04  // Load function selector
    

    CALLDATALOAD

    PUSH1 0x24

    CALLDATALOAD

    ADD // a + b

    PUSH1 0x00

    MSTORE

    PUSH1 0x20

    PUSH1 0x00

    RETURN

    Gas Mechanism

    Gas EVM का fuel है। यह computational resources measure करता है और spam prevent करता है।

    Gas Costs:

    • Simple operations: 3 gas (ADD, SUB)
    • Complex operations: 5 gas (MUL, DIV)
    • Memory expansion: Variable (quadratic growth)
    • Storage write: 20,000 gas (new), 5,000 gas (update)
    • Transaction base: 21,000 gas

    Gas Optimization Example:

    // ❌ Inefficient: Multiple storage reads
    

    function inefficient() public view returns (uint256) {

    return storedData + storedData + storedData;

    // 3 × 2100 gas = 6300 gas

    }

    // ✅ Efficient: Single storage read

    function efficient() public view returns (uint256) {

    uint256 temp = storedData; // 2100 gas

    return temp + temp + temp; // 9 gas

    // Total: 2109 gas (66% saving!)

    }

    Contract Execution Flow

  • Transaction submission: User wallet से RPC node को
  • Mempool: Pending transactions की pool
  • Block inclusion: Miner/validator transaction select करता है
  • EVM execution:
  • - State load (storage, balances)

    - Bytecode execute (opcode by opcode)

    - State update (storage writes, transfers)

    - Event emission (logs)

  • Consensus: सभी nodes same result verify करते हैं
  • State finalization: New state committed होता है
  • Call vs. Transaction:

    • Call: Read-only, free, local execution
    • Transaction: State-changing, costs gas, broadcasted
    contract Example {
    

    uint256 public data;

    function getData() public view returns (uint256) {

    return data; // Call: free, no state change

    }

    function setData(uint256 _data) public {

    data = _data; // Transaction: costs gas, changes state

    }

    }

    EVM Compatibility

    EVM compatibility ने multiple blockchain ecosystems enable किए:

    EVM-Compatible Chains:

    | Chain | Consensus | TPS | Avg Gas |

    |-------|-----------|-----|---------|

    | Ethereum | PoS | 15-30 | 15-50 gwei |

    | Polygon | PoS | 7000+ | <1 gwei |

    | Arbitrum | Optimistic | 4000+ | 0.1 gwei |

    | Optimism | Optimistic | 2000+ | 0.1 gwei |

    | BSC | PoSA | 100+ | 3 gwei |

    | Avalanche C-Chain | Avalanche | 4500+ | 25 nAVAX |

    इन chains पर same Solidity code deploy हो सकता है minimal modifications के साथ।

    Advanced Concepts

    Precompiled Contracts

    EVM में built-in contracts certain addresses पर (0x01 to 0x09):

    • 0x01: ecRecover (signature verification)
    • 0x02: SHA-256 hash
    • 0x03: RIPEMD-160 hash
    • 0x05: modexp (modular exponentiation)
    • 0x06-0x09: Elliptic curve operations

    ये contracts native code में optimized हैं और Solidity में callables हैं:

    function verifySignature(
    

    bytes32 hash,

    uint8 v,

    bytes32 r,

    bytes32 s

    ) public pure returns (address) {

    return ecrecover(hash, v, r, s); // Calls 0x01 precompile

    }

    CREATE vs CREATE2

    Contracts deploy करने के दो opcodes:

    • CREATE (0xF0): Address = hash(sender, nonce)
    • CREATE2 (0xF5): Address = hash(0xFF, sender, salt, bytecode)

    CREATE2 deterministic addresses enable करता है:

    function deploy(bytes32 salt) public returns (address) {
    

    return address(new Contract{salt: salt}());

    // Address deployment से पहले predictable है!

    }

    DELEGATECALL: Proxy Pattern

    DELEGATECALL opcode upgradeable contracts enable करता है:

    // Proxy contract
    

    contract Proxy {

    address public implementation;

    fallback() external payable {

    address impl = implementation;

    assembly {

    calldatacopy(0, 0, calldatasize())

    let result := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)

    returndatacopy(0, 0, returndatasize())

    switch result

    case 0 { revert(0, returndatasize()) }

    default { return(0, returndatasize()) }

    }

    }

    }

    EVM Limitations

  • 256-bit architecture: सभी math operations 256-bit में
  • No floating point: सिर्फ integers (decimals के लिए workarounds)
  • Stack depth limit: Maximum 1024 items
  • Block gas limit: Current ~30M gas per block
  • Determinism requirement: कोई random numbers नहीं (oracles needed)
  • Future: EVM Evolution

    EIP-4844 (Proto-Danksharding)

    • Blob transactions for L2 data
    • 10-100x cheaper L2 transactions

    EIP-3074 (Account Abstraction)

    • Native account abstraction
    • Better UX for users

    Verkle Trees (EIP-6800)

    • More efficient state proofs
    • Stateless clients possible

    निष्कर्ष

    EVM blockchain technology का marvel है — एक globally distributed computer जो हजारों nodes पर exact same computations replicate करता है। इसे समझना efficient, secure smart contracts लिखने के लिए crucial है।

    Key takeaways:

    • EVM stack-based VM है deterministic execution के साथ
    • Gas computational cost measure करता है
    • Storage expensive है, memory cheap
    • Opcodes bytecode की building blocks हैं
    • EVM compatibility cross-chain deployment enable करती है

    अगले कदम: Solingo पर EVM-specific challenges practice करें और gas optimization master करें!

    ---

    अतिरिक्त Resources:

    Practice में लगाने के लिए तैयार हैं?

    Solingo पर interactive exercises के साथ इन concepts को apply करें।

    मुफ्त में शुरू करें