EIPs Explained

EIP-170 · Final · Core · 2016-11-04

EIP-170: Contract code size limit

Capped deployed contract bytecode at 24,576 bytes, closing a denial-of-service vector where a constant-gas call could force clients to load and process arbitrarily large code.

What problem does it solve?

Calling a contract costs a fixed amount of base gas, no matter how big its code is. But the client still has to read the whole code from disk, prepare it for execution, and (for light clients) include it in proofs. So a call's real cost grows with code size while its gas price does not.

At 2016 gas limits this gap was tolerable. With block gas limits expected to rise, an attacker could deploy huge contracts and then call them cheaply to slow nodes down. EIP-170 closes the gap with a hard cap.

How it works

The rule is short:

  • MAX_CODE_SIZE = 0x6000 = 24,576 bytes (2**14 + 2**13, often called "24 KiB").
  • From block 2,675,000 on mainnet (the Spurious Dragon fork, EIP-607), if a contract's init code returns runtime code longer than MAX_CODE_SIZE, the creation fails with an out-of-gas error.

The limit applies to the runtime code that gets stored, not to the init code that runs during deployment. Exactly 24,576 bytes is still allowed. The value was chosen to sit just above what could fit under the gas limits of the time, so no realistic existing use case broke.

Sample code

Check how close a deployed contract is to the limit (ethers v6):

import { JsonRpcProvider, dataLength } from "ethers";

const MAX_CODE_SIZE = 0x6000; // 24,576 bytes (EIP-170)

const provider = new JsonRpcProvider(process.env.RPC_URL);
const code = await provider.getCode("0xYourContractAddress");
const size = dataLength(code);
console.log(`${size} / ${MAX_CODE_SIZE} bytes (${((size / MAX_CODE_SIZE) * 100).toFixed(1)}%)`);

Foundry and Hardhat can warn at build time. For example, forge build --sizes prints each contract's runtime size and margin.

Things to know

  • This proposal is Final and is still in force on Ethereum mainnet.
  • Large applications hit this limit often. Common workarounds are splitting logic into libraries or multiple contracts, using proxy or "diamond" patterns, turning on the optimizer, and shortening revert strings or switching to custom errors.
  • EIP-3860 later capped init code at twice this value (49,152 bytes) and charged gas per init-code word.
  • Raising the cap is under discussion. EIP-7954 proposes 65,536 bytes for code, and EIP-7907 proposes metering code size by gas instead of a hard cap. Neither was final at the time of writing.
  • Many other EVM chains copy this 24,576-byte limit, but some change it. Check the target chain.

#contracts #limits #dos #spurious-dragon #core

Details

Authors
Vitalik Buterin (@vbuterin)
Status
Final
Type
Standards Track / Core
Created
2016-11-04
Required by

Machine-readable: Markdown · JSON