EIPs Explained

EIP-150 · Final · Core · 2016-09-24

EIP-150: Gas cost changes for IO-heavy operations

Raised gas costs for state-reading opcodes after the 2016 DoS attacks and added the "all but one 64th" rule that caps gas forwarded to sub-calls.

What problem does it solve?

In autumn 2016, attackers spammed Ethereum with transactions that ran opcodes like EXTCODESIZE and BALANCE over and over. These opcodes read state from disk, which is slow, but they cost only 20 gas each. Blocks became extremely slow to process and the network degraded.

The spec says the underlying problem would not go away with client optimizations alone: state-reading opcodes were underpriced compared with how expensive they are to execute. EIP-150 reprices them. It also fixes a related weakness, the call-stack depth limit.

How it works

From block 2,463,000 on mainnet (the Tangerine Whistle fork):

Opcode Old gas New gas
EXTCODESIZE 20 700
EXTCODECOPY (base) 20 700
BALANCE 20 400
SLOAD 50 200
CALL, CALLCODE, DELEGATECALL 40 700
SELFDESTRUCT 0 5000 (+25000 if it creates a new account)

The spec derives these numbers from a target of at most about 8 MB of disk reads per block, plus a flat 300 gas to cover loading contract code.

The 63/64 rule. Before this change, a call that requested more gas than was available failed with out-of-gas. Now the call is not rejected. It receives at most "all but one 64th" of the remaining gas, N - floor(N / 64). CREATE follows the same rule.

This has two effects:

  • Old contracts that forwarded msg.gas - 40 (assuming a call costs 40) kept working after the call price changed.
  • Every nested call keeps 1/64 of its gas back, so gas shrinks with each level. The spec says this limits practical call depth to about 340. The call-stack depth attack, where an attacker made a victim's sub-call fail at depth 1024, stopped being a practical threat.

The recommended block gas limit target was raised to 5.5 million to keep throughput roughly the same.

Sample code

The new gas-forwarding logic, from the spec:

def max_call_gas(gas):
    return gas - (gas // 64)

extra_gas = (not account_exists(to)) * GCALLNEWACCOUNT + (value > 0) * GCALLVALUETRANSFER
if available_gas < extra_gas:
    raise OutOfGas
if available_gas < requested_gas + extra_gas:
    requested_gas = min(requested_gas, max_call_gas(available_gas - extra_gas))
sub_call_gas = requested_gas + GSTIPEND * (value > 0)

In Solidity, the parent always keeps some gas back, even when you forward "everything":

(bool ok, ) = target.call{gas: gasleft()}(data); // callee gets at most 63/64 of what remains

Things to know

  • The hard fork that shipped this change is described in EIP-608 (Tangerine Whistle).
  • Because of the 63/64 rule, a contract cannot assume a sub-call got exactly the gas it asked for. Code that checks gasleft() after a failed call should take this into account.
  • Most of these prices were raised again in Berlin by EIP-2929, which split state access into "cold" (first touch) and "warm" costs.

#gas #dos #evm #core #tangerine-whistle

Details

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

Machine-readable: Markdown · JSON