Skip to main content

Skill Guide

EVM internals including storage layout, opcode behavior, and gas optimization

A deep understanding of the Ethereum Virtual Machine's execution model, including how smart contract state is stored, how operations are computed, and how to minimize computational cost.

This skill is critical for building secure, efficient, and cost-effective decentralized applications. It directly impacts operational expenditure (gas fees) and system security, which are primary determinants of a protocol's viability and user adoption.
1 Careers
1 Categories
9.2 Avg Demand
20% Avg AI Risk

How to Learn EVM internals including storage layout, opcode behavior, and gas optimization

1. **Solidity Memory Model**: Understand the difference between `storage`, `memory`, and `calldata` variables. 2. **Basic Opcodes**: Learn the function of key opcodes like `SLOAD`, `SSTORE`, `CALL`, and `MSTORE`. 3. **Gas Fundamentals**: Grasp the concept of gas units, gas price, and how simple operations (e.g., addition, storage writes) have fixed costs.
1. **Storage Layout Mastery**: Use `solc --storage-layout` to inspect contract storage slots. Practice packing structs and variables to use fewer storage slots. 2. **Assembly & Optimization**: Write and analyze inline assembly (Yul) for common patterns. Benchmark gas costs using Hardhat's gas reporter or Foundry's gas snapshots. 3. **Common Pitfalls**: Identify and avoid anti-patterns like unbounded loops over storage arrays, excessive use of `SSTORE` in loops, and inefficient data structure choices.
1. **Low-Level Security Analysis**: Analyze contracts for storage collision vulnerabilities and improper delegatecall usage. Understand the EVM's call depth limit and reentrancy mechanics at the opcode level. 2. **Custom Compiler Optimizations**: Implement custom optimizer passes in Solidity or Yul for highly specific use cases (e.g., assembly-heavy math libraries). 3. **EVM Equivalency & Forks**: Compare the EVM with other VMs (e.g., SVM, CosmWasm) and understand the implications of EVM-compatible L2s (e.g., minor gas cost differences on Optimism vs. Arbitrum).

Practice Projects

Beginner
Project

Gas-Efficient Storage Refactor

Scenario

You are given a poorly optimized ERC-20 token contract that tracks user balances and allowances in separate mappings and uses a struct for allowances with two separate storage slots.

How to Execute
1. Deploy the original contract and call its functions (transfer, approve) on a testnet. 2. Use a block explorer or Hardhat's gas reporter to record the gas costs. 3. Refactor the storage layout by packing the `allowance` struct into a single 256-bit slot. 4. Re-deploy and benchmark to demonstrate the gas savings on `approve` and `transferFrom`.
Intermediate
Project

Assembly-Optimized Math Library

Scenario

Develop a library of mathematical functions (e.g., `mulDiv`, `sqrt`) that are significantly more gas-efficient than their pure Solidity counterparts, which will be used in a high-volume DeFi protocol.

How to Execute
1. Write the pure Solidity version of the functions. 2. Rewrite them using inline Yul assembly, leveraging low-level opcodes (`MUL`, `DIV`, `ADDMOD`) and the stack. 3. Create a comprehensive test suite in Foundry that compares the assembly and Solidity outputs for a wide range of inputs, including edge cases (overflow, zero division). 4. Generate a gas benchmark report to quantify the savings per call.
Advanced
Project

EVM Opcode-Level Vulnerability Audit

Scenario

Conduct a security audit of a complex upgradeable proxy contract system that uses `delegatecall`. The goal is to identify storage layout inconsistencies between the proxy and implementation that could lead to critical vulnerabilities.

How to Execute
1. Manually trace the storage layout of both the proxy (EIP-1967 slots) and the implementation contract. 2. Use Foundry's `vm.load` cheatcode or a debugger to inspect storage at specific slots during a `delegatecall`. 3. Simulate an upgrade where the implementation's storage layout is shifted. 4. Craft a proof-of-concept exploit that demonstrates how an attacker could corrupt the proxy's admin slot or other critical variables through a malicious function call.

Tools & Frameworks

Development & Testing

Foundry (Forge, Cast, Anvil)Hardhatsolc (Solidity Compiler)

Foundry is the primary tool for gas benchmarking, writing tests in Solidity, and low-level debugging. Hardhat is used for deployment scripting and its gas reporter plugin. `solc` is used directly to inspect storage layouts and compile optimized bytecode.

Analysis & Debugging

TenderlyEVM Playgroundopcode.info

Tenderly provides transaction simulation and step-by-step opcode execution visualization. EVM Playground allows for raw bytecode and opcode experimentation. opcode.info is a quick reference for opcode gas costs and stack behavior.

Mental Models & Methodologies

Storage Slot PackingCold vs. Warm AccessMemory Expansion Cost

Storage packing minimizes `SSTORE` operations. The cold/warm access model (EIP-2929) dictates that the first access to a storage slot costs ~2100 gas, subsequent accesses cost 100 gas. Memory expansion cost is quadratic, making unbounded memory use prohibitively expensive.

Interview Questions

Answer Strategy

The candidate must demonstrate knowledge of EIP-2929's cold/warm access model. A strong answer will reference the specific cost values: a cold `SLOAD` costs 2100 gas, while a warm `SLOAD` (accessed previously in the same transaction) costs 100 gas. The answer should mention that this applies to both `SLOAD` and `SSTORE` opcodes.

Answer Strategy

This tests understanding of gas mechanics and security. The core competency is assessing compound risks. The answer should identify: 1) **Gas**: The loop performs multiple `SLOAD`s for the array length and elements, each with a potential cold access penalty. The `delegatecall` itself is expensive. This could lead to out-of-gas errors. 2) **Security**: The target of the `delegatecall` is read from storage. If an attacker can manipulate that storage array, they could redirect the `delegatecall` to a malicious contract, potentially compromising the caller's state.

Careers That Require EVM internals including storage layout, opcode behavior, and gas optimization

1 career found