EIP-7784 The GETCONTRACT Opcode (GETCONTRACT opcode)
No call decision · EVM & execution · EIP status Review · created 2024-10-07
Adds a new EVM instruction, GETCONTRACT (0x4f), that looks up the address of a contract holding a given code by its code hash. This makes it possible to find a contract based on the code itself rather than its address.
Why it is needed
Today, the only way to point to a contract on Ethereum is by address. But an address is a non-deterministic value that depends on how the contract was deployed, and if a proxy (a middle contract that forwards calls to another contract) is inserted, the address stays the same while the code actually executed can change. Things change if a contract can be found by the hash of the code itself (a hash being a unique fingerprint of data). A security auditor can verify integrity based on the bytecode alone, without having to consider the deployment environment or storage contents. A developer can get pre-approval before deployment using only the code hash, without disclosing the code, or can save on gas costs by referencing code that is already deployed instead of redeploying the same code. This proposal provides content addressing, a technique commonly used in database design, as a single EVM instruction.
What changes
Today
- Contracts can only be found by address, and addresses depend on how they were deployed
- If the actual code changes behind a proxy, looking at the address alone gives no indication
- Using the same code requires redeploying it every time
After EIP-7784
- Supplying the keccak256 hash of the code returns the address of the contract holding that code
- A matching hash guarantees matching code, making it possible to build a code-based chain of trust
- Already-deployed code can be referenced by hash and reused without redeployment
Who is affected
- Dapp developers: Applications that reuse the same code as a dependency extensively can reduce gas costs by referencing existing code instead of redeploying it.
- Security auditors: Can guarantee code integrity based on the bytecode hash, without considering deployment parameters or storage contents.
- Client teams: Alongside implementing the new opcode, logic must be added to index every deployed contract into the state trie keyed by its code hash.
- Wallet and Account Abstraction Developers: Can serve as the basis for referencing code by its hash, instead of a mutable address, when setting code on an EOA via EIP-7702.
In depth
Instruction definition
GETCONTRACT uses opcode value 0x4f. It pops a single 32-byte codehash from the stack, and pushes the address of the corresponding contract if that hash exists in state, or 0 otherwise. The stack effect is 1 pop, 1 push. The gas cost is 150. If the codehash is invalid or an error occurs during the bytecode lookup, the instruction reverts.
State Trie Indexing Rules
Every contract stored in the EVM MUST be added to the state trie keyed by the keccak256 hash of its bytecode. This is subject to three conditions: the same hash must not already be registered, the contract must never have called the SELFDESTRUCT opcode, and the EXTCODEHASH return value must not be 0xeadcdba66a79ab5dce91622d1d75c8cff5cff0b96944c3bf1072cd08ce018329, i.e. keccak256(0xef01). The last condition is a mechanism to filter out accounts that have delegated code via EIP-7702 from the index.
The Rationale for Prohibiting Re-indexing and for the Gas Cost
The probability of a hash collision attack is small but non-zero. Making an already-indexed entry non-updatable blocks the path by which different code producing the same hash later could swap out the existing entry. The reason bytecode is used as the basis instead of address is that bytecode is deterministic and verifiable on-chain, whereas an address is opaque and mutable. As for the gas cost, the rationale cited is that while this operation is more complex than a simple data read, hash-based database lookups are generally low in complexity.
Security considerations
The index only tells you where code is located; it does not guarantee that code's safety or functionality. Users MUST review an indexed contract themselves before interacting with it. Because the index points only to bytecode and does not include storage contents, a contract's state can change over time. Code temporarily delegated via EIP-7702 can disappear later, so it is explicitly prohibited from being indexed. A contract that has called SELFDESTRUCT leaves no codehash in the state trie, so it is not indexed.
Connection to EIP-7702
The source explains that this instruction could be used as a dependency when building new core standards. It cites EIP-7702 as an example, noting that a user setting one-time code on an EOA could reference the code via GETCONTRACT instead of a mutable address-based reference. Conversely, as noted above, code delegated under 7702 is itself prohibited from being registered in the index, so the relationship between the two proposals is that one can use the other while being excluded from its registration target.
Current status
This proposal's status is Review. Details such as the opcode value, gas cost, and indexing conditions may still change during discussion.