Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EIP: GETCONTRACT opcode #8935

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
77 changes: 77 additions & 0 deletions EIPS/eip-xxxx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
eip: 7784

Check failure on line 2 in EIPS/eip-xxxx.md

View workflow job for this annotation

GitHub Actions / EIP Walidator

file name must reflect the preamble header `eip`

error[preamble-file-name]: file name must reflect the preamble header `eip` --> EIPS/eip-xxxx.md:2:5 | 2 | eip: 7784 | ^^^^^ this value | = help: this file's name should be `eip-7784.md` = help: see https://ethereum.github.io/eipw/preamble-file-name/
peersky marked this conversation as resolved.
Show resolved Hide resolved
title: GETCONTRACT opcode
description: Global byte code accessing by it's hash
author: Tim Pechersky (@peersky)
discussions-to: https://ethereum-magicians.org/t/erc-7744-code-index/20569
peersky marked this conversation as resolved.
Show resolved Hide resolved
status: Draft
type: Standards Track
category: Core
created: 2024-10-07
---

## Abstract

Initially proposed in erc track, this EIP is now moved to core track, discussion kept in the same thread as they are related and can be seen as exclusive.

This is a proposal to add a new opcode, `GETCONTRACT`. The `GETCONTRACT` opcode would return the address containing the bytecode by it's hash.

## Motivation

Content addressing by it's hash is a common pattern in database design. It allows to store and retrieve data by it's unique footprint in the storage. This pattern is widely used in the industry and it allows to abstract from actual storage location and allows to reuse the same bytecode in multiple contracts.

Today, existing contract discovery relies on addresses, which are non-deterministic and can be obfuscated through proxies. Indexing by bytecode hash provides a deterministic and tamper-proof way to identify and verify contract code, enhancing security and trust in the Ethereum ecosystem.

Consider a security auditor who wants to attest to the integrity of a contract’s code. By referencing bytecode hashes, auditors can focus their audit on the bytecode itself, without needing to assess deployment parameters or storage contents. This method verifies the integrity of a contract’s codebase without auditing the entire contract state.

Additionally, bytecode referencing allows whitelist contracts before deployment, allowing developers to get pre-approval for their codebase without disclosing the code itself, or even pre-setup infrastructure that will change it behavior upon adding some determined functionality on chain.

For developers relying on extensive code reuse, bytecode referencing protects against malicious changes that can occur with address-based referencing through proxies. This builds long-term trust chains extending to end-user applications.

For decentralized application (dApp) developers, a code index can save gas costs by allowing them to reference existing codebase instead of redeploying them, optimizing resource usage. This can be useful for dApps that rely on extensive re-use of same codebase as own dependencies.

This also allows to build new core standards more conveniently, for example, [EIP-7702](./eip-7702) can use it as dependency, to allow users who want to set up one-time code on their EOAs by referring to `GETCONTRACT` instead of uploading the code itself or having to figure out where was the required code deployed.

## Specification

### Opcode Definition

* **Mnemonic:** `GETCONTRACT`
* **Opcode Value:** `0xfe`
* **Input:**
* `codehash`: A single 32-byte code hash from the stack.
* **Output:**
* `address`: If the `codehash` exists in the state, pushes the corresponding contract address onto the stack. Otherwise, pushes 0.
* **Gas Cost:** ?? (TBD)
* **Stack Effects:** Pops 1 item, pushes 1 item.
* **Error Handling:** If the `codehash` is invalid or the bytecode retrieval encounters an error, the instruction will revert.

Contract MUST be added to the state trie with the key being the keccak256 hash of the contract's bytecode if it is not already present and if the contract dit not call `SELFDESTRUCT` opcode.


### Example Usage

```solidity
function getContractAddress(bytes32 codehash) public view returns (address) {
address contractAddress = GETCONTRACT(codehash);
return contractAddress;
}
```

## Rationale

**Bytecode over Addresses**: Bytecode is deterministic and can be verified on-chain, while addresses are opaque and mutable.

**EIP not ERC**: This EIP is proposed as a core standard, as it enables global index by default, abstracts developers from need to maintain the index, and can be used as a dependency for other EIPs.

**Do not re-index**: There is small, yet non-zero probability of hash collision attack. Disallowing updates to indexed location of bytecode coupes with this.

## Security Considerations

**Malicious Code**: The index does NOT guarantee the safety or functionality of indexed contracts. Users MUST exercise caution and perform their own due diligence before interacting with indexed contracts.

**Storage contents of registered contracts**: The index only refers to the bytecode of the contract, not the storage contents. This means that the contract state is not indexed and may change over time.

## Copyright

Copyright and related rights waived via [CC0](../LICENSE.md).
Loading