EIPs Explained

EIP-6913 · Withdrawn · Core · 2023-04-20

EIP-6913: SETCODE instruction

A withdrawn proposal for a SETCODE opcode that would let a contract replace its own code in place, keeping its address, balance and storage.

What problem does it solve?

Many contracts need to be upgradeable without moving to a new address. In 2023 there were three common approaches, each with drawbacks:

  • CALL to a separate logic contract – every future version must be able to modify the shared state.
  • DELEGATECALL proxies – flexible, but add indirection and an extra call on every interaction.
  • SELFDESTRUCT + CREATE2 redeploy ("metamorphic" contracts) – replaces code at the same address, but wipes all storage and leaves the contract unusable until the redeploy completes. With SELFDESTRUCT being deprecated, this route was closing (see EIP-6780).

EIP-6913 proposed a direct primitive instead: an opcode that swaps the running account's code for new bytecode.

How it works

SETCODE (proposed opcode 0xfc) takes offset and length from the stack, pointing to the new code in memory.

  • It aborts in a read-only context (e.g. under STATICCALL) and whenever the executing code is not the account's own code, such as inside DELEGATECALL or CREATE. So only a contract's own code can replace itself.
  • The new code goes through the same validation as CREATE / CREATE2 output.
  • After the call, EXTCODESIZE, EXTCODECOPY and new message calls see the new code. Frames already running the old code keep running it, and their CODESIZE / CODECOPY still refer to it.
  • The change is reverted if the current or any parent frame reverts, like SSTORE.
  • Balance, nonce and storage are untouched.
  • Gas: Gselfdestruct + Gcodedeposit × len(newCode).

A useful property: code without the SETCODE byte could be assumed immutable, making it easy to flag upgradeable contracts.

Sample code

The spec's own test contract, a self-replacing contract that installs whatever calldata it receives:

36        CALLDATASIZE      ; len
5f        PUSH0             ; memory offset 0
5f        PUSH0             ; calldata offset 0
37        CALLDATACOPY      ; mem[0:len] = calldata
36        CALLDATASIZE      ; length
5f        PUSH0             ; offset
fc        SETCODE           ; code = mem[0:len]
00        STOP

Bytecode 0x365f5f37365ffc00. Called with calldata 0x00, its code becomes 0x00, per the spec's test table.

Things to know

  • Status: Withdrawn. The stated reason is that the author prefers EIP-7702, which lets an EOA set delegated code and shipped in the Pectra upgrade.
  • SELFDESTRUCT remains deprecated (EIP-6049) and was neutered by EIP-6780, so in-place code replacement on today's Ethereum is done with proxy patterns, not opcodes.
  • The security notes apply to any upgrade mechanism: restrict who can upgrade, test thoroughly, and have wallets warn users before approving tokens to upgradeable accounts.

#evm #opcode #upgradeability #withdrawn

Details

Authors
William Morriss (@wjmelements)
Status
Withdrawn
Type
Standards Track / Core
Created
2023-04-20

Machine-readable: Markdown · JSON