EIP-7784 · Review · Core · 2024-10-07
EIP-7784: GETCONTRACT opcode
Proposes a GETCONTRACT opcode that looks up a deployed contract's address from the hash of its bytecode, so code can be referenced by content instead of by address.
What problem does it solve?
Contracts find each other by address. An address says nothing about what code is behind it, and with proxies the code can change while the address stays the same. If you want to depend on "this exact audited code", you have to trust whoever controls the address.
Content addressing, looking things up by the hash of their content, is a common fix in databases and storage systems. EIP-7784 proposes the same for the EVM: given a bytecode hash, return an address where that exact code lives. The spec's motivations include auditors attesting to bytecode hashes instead of deployments, allow-listing code before it is deployed, reusing existing deployments instead of redeploying identical code, and letting standards such as EIP-7702 delegation refer to immutable code by hash.
How it works
- Opcode:
GETCONTRACT, value0x4f. - Stack: pops one 32-byte
codehash, pushes one item: the address holding that bytecode, or0if none is indexed. - Gas: 150.
- Errors: if the code hash is invalid or retrieval fails, the instruction reverts.
To make lookups possible, every contract stored in the EVM must be added to the state trie under the key keccak256(bytecode), but only if:
- the hash is not already indexed (the first deployment wins and is never re-indexed, which also blunts hash-collision tricks);
- the contract has not executed
SELFDESTRUCT; - its code hash is not
keccak256(0xef01), i.e. it is not an EIP-7702 delegation designator, since that code is temporary.
The hash used is the same value EIP-1052 EXTCODEHASH returns for an account, so you can get the key from any existing deployment.
Sample code
Hypothetical usage, mirroring the spec's example. No compiler supports GETCONTRACT today, so treat the assembly line as pseudocode:
// Illustrative only: no Solidity version supports GETCONTRACT today.
function implementationFor(bytes32 codehash) public view returns (address impl) {
assembly {
// GETCONTRACT (0x4f): codehash -> address or 0
impl := getcontract(codehash)
}
require(impl != address(0), "code not indexed");
}
// Getting the key from a known deployment works today:
function hashOf(address a) public view returns (bytes32) {
return a.codehash; // EXTCODEHASH
}
Things to know
- Status is Review and the opcode is not scheduled for any network upgrade.
- The index only says the bytecode matches. It says nothing about the contract's storage, which can differ between deployments and change over time, and nothing about whether the code is safe.
- The same source compiled with different settings, or deployed with different immutable values, yields different runtime bytecode and therefore a different hash.
- Indexing every contract adds entries to the state trie, and the proposed 150 gas price would need validation by client teams.