---
eip: 7620
label: EIP-7620
title: EOF Contract Creation
status: Stagnant
type: Standards Track
category: Core
created: 2024-02-12
requires: 170, 684, 2929, 3540, 3541, 3670
official: https://eips.ethereum.org/EIPS/eip-7620
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7620.md
canonical: https://eips.huatuo.xyz/zh/eip-7620/
lang: zh
tldr: EVM 对象格式（EOF）升级的一部分：新增 EOFCREATE 和 RETURNCODE，使 EOF 工厂合约可以从内嵌的子容器部署子合约，取代 EOF 代码中的 CREATE/CREATE2。
tags: [evm, eof, opcodes, contract-creation, core]
---

# EIP-7620: EOF Contract Creation

> Introduce `EOFCREATE` and `RETURNCODE` instructions

**TL;DR:** EVM 对象格式（EOF）升级的一部分：新增 EOFCREATE 和 RETURNCODE，使 EOF 工厂合约可以从内嵌的子容器部署子合约，取代 EOF 代码中的 CREATE/CREATE2。

## 它解决什么问题？

EVM 对象格式（EOF，[EIP-3540](/eip-3540/)）是一种面向合约字节码的结构化容器提案，分为独立的代码段和数据段，并在部署时进行验证。EOF 的一个核心目标是**代码不可观测性**：合约不应能够把代码当作数据来读取或操控。这就排除了传统的创建路径，即 `CREATE`/`CREATE2` 从内存中取出任意字节（往往由 `CODECOPY` 构造）并作为 initcode 执行。

不过，工厂仍然需要部署合约。EIP-7620 为 EOF 提供了它自己的创建机制：要部署的代码作为已验证的**子容器**内嵌在工厂中，新指令按索引选择它。

## 它如何运作

两个新的 opcode，仅在 EOF 代码中有效：

**`EOFCREATE`（`0xec`）**，带 1 字节立即数 `initcontainer_index`：

- 弹出 `salt, input_offset, input_size, value`（顺序与 `EXTCALL` 一致，见 [EIP-7069](/eip-7069/)）。
- 收取 32,000 gas，在静态上下文中失败，检查调用深度和余额。
- 把子容器 `initcontainer_index` 作为 initcode 运行，内存切片作为其 calldata。
- 新地址为 `keccak256(0xff || sender32 || salt)[12:]`。与 [CREATE2](/eip-1014/) 不同，initcode 哈希**不**包含在内，因此地址只取决于部署者和 salt。
- 成功时压入新地址，失败时压入 `0`；每个已部署字节收取 200 gas。

**`RETURNCODE`（`0xee`）**，带 1 字节立即数 `deploy_container_index`：

- 结束 initcode 帧，并指明哪个子容器成为运行时代码。
- 弹出 `aux_data_offset, aux_data_size`；该内存会被**追加到已部署容器的数据段**。构造函数参数和 immutable 就是这样在无需修改代码的情况下被固化进去的。

验证规则确保容器索引存在，子容器要么作为 initcode 要么作为运行时代码使用而不能两者兼具，并且 initcode 以 `RETURNCODE` 而不是 `RETURN`/`STOP` 结尾。

在 EOF 代码中，`CREATE` 和 `CREATE2` 会被拒绝。在传统代码中，initcode 以 `0xEF00` 开头的 `CREATE`/`CREATE2` 会提前失败，且不消耗 initcode 的 gas。通过交易进行的顶层部署由 [EIP-7873](/eip-7873/) 单独处理。

## 示例代码

`EOFCREATE` 核心逻辑的伪代码：

```python
def eofcreate(frame, initcontainer_index):
    charge_gas(frame, 32000)
    assert not frame.is_static
    salt, in_off, in_size, value = frame.stack.pop_n(4)
    expand_memory(frame, in_off, in_size)

    init = frame.container.subcontainers[initcontainer_index]
    if frame.depth >= 1024 or frame.balance < value:
        frame.stack.push(0); return

    result = execute(init, calldata=frame.memory[in_off:in_off + in_size], value=value)
    frame.sender.nonce += 1
    new_address = keccak256(b"\xff" + frame.address.rjust(32, b"\0") + salt)[12:]

    if not result.success:
        frame.stack.push(0); return
    deployed = result.deploy_container.with_appended_data(result.aux_data)
    assert len(deployed) <= 24576
    set_code(new_address, deployed)
    charge_gas(frame, 200 * len(deployed))
    frame.stack.push(new_address)
```

## 需要注意

- 状态为 **Stagnant**。EOF 整体（元 EIP [EIP-7692](/eip-7692/)）尚未在以太坊主网激活，相关的 EOF EIP 也都是 Stagnant。
- 想要锁定确切子合约代码的工厂，可以自行把其哈希折叠进 `salt`，因为地址公式中没有包含它。
- 传统字节码中的 `EOFCREATE` 和 `RETURNCODE` 无效并会中止执行，因此对非 EOF 合约没有任何影响。
- 主要的安全审查负担落在 [EIP-7873](/eip-7873/) 的 initcode 交易上，因为新的未经验证的代码会在那里进入状态。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7620
