Concept lesson

EVM Architecture, Opcode Execution & Gas Metering

Stack-based Ethereum Virtual Machine mechanics, memory vs storage layouts, gas calculation rules, and ABI encoding.

lesson
Freshness: current16 min read
Mastery
not started · 0%

Learning outcomes

  • Understand core principles of EVM Architecture, Opcode Execution & Gas Metering
  • Apply production engineering patterns for EVM Architecture, Opcode Execution & Gas Metering

Mental model

The Ethereum Virtual Machine (EVM) is a quasi-Turing complete, 256-bit stack machine. Every opcode execution consumes gas to bound resource usage and prevent infinite loop Denial-of-Service (DoS) attacks on validator nodes.

Theory

EVM execution environment contains three distinct data locations:

  • Stack: 1024 256-bit word slots for active opcode evaluation (PUSH, POP, ADD, MUL).
  • Memory: Volatile, linear byte array erased after function execution. Gas cost scales quadratically with memory expansion.
  • Storage: Persistent 256-bit key-value mapping per contract. Reads (SLOAD) and writes (SSTORE) are the most expensive opcodes in gas cost.
solidity(12 lines)
1// EVM Data Locations Example
2function processOrder(uint256[] calldata itemIds) external {
3 // Read from calldata (cheapest)
4 uint256 len = itemIds.length;
5
6 // Allocate volatile memory array
7 uint256[] memory processed = new uint256[](len);
8
9 // SSTORE persistent state update (most expensive)
10 totalOrdersProcessed += len;
11}
Transaction Input Calldata Parsing
EVM Stack & Memory Allocation
Opcode Gas Consumption Check
Persistent Storage Trie Update
Conceptual teaching model synthesized from:Trustworthy Agents in Practice

Alternatives and trade-offs

  • Centralized Infrastructure: High performance and zero protocol overhead, but vulnerable to single-point-of-failure outages, vendor lock-in, and centralized censorship.
  • Decentralized Verifiable Infrastructure: Provides cryptographic guarantees, data immutability, and zero-trust execution, but introduces computational prover overhead and consensus latency.

Failure modes and misconceptions

  1. Semantic Truth vs Computational Integrity: Misinterpreting a ZK execution proof as proof that an AI model's output is real-world factually true (it proves execution integrity $M(X)=Y$, not semantic correctness).
  2. Unrestricted Private Key Delegation: Giving an autonomous AI agent direct access to un-constrained private keys without a Policy Engine or Smart Account rules.
Reflect before revealing the guide

Decision scenario

Adopt verifiable decentralized infrastructure when building autonomous financial agents, multi-party data mesh collaborations, or mission-critical AI systems where execution auditability, asset safety, and cryptographic provenance are mandatory.

Learning outcomes

  • Architect end-to-end blockchain transaction lifecycles from signature generation to state finality.
  • Implement smart contract security patterns to defend against reentrancy, oracle manipulation, and delegatecall risks.
  • Design verifiable AI agent pipelines leveraging ZK proofs, zkVMs, Account Abstraction, and Policy Engines.

Trade-offs

Verifiable blockchain infrastructure guarantees asset safety and execution integrity, but requires disciplined contract auditing, gas optimization, and policy-bounded agent sandboxing.

Evidence assessment

Theory and decision mastery

not-started · 0%
theory0%
decision0%
activityNot mapped
projectNot mapped
1. What is the fundamental difference between EVM `memory` and `storage` data locations?
2. Why does the EVM enforce gas metering during opcode execution?
3. Why is reading function parameters from `calldata` cheaper than `memory` in Solidity function execution?

Decision scenario

A smart contract developer needs to store a dynamic array of 10,000 user addresses and read them inside a loop during a transaction execution.

Which optimization strategy minimizes gas cost for state reads inside the execution loop?

Primary sources