EIP-7620 · Stagnant(停滞) · Core(核心) · 2024-02-12
EIP-7620: EOF Contract Creation
EVM 对象格式(EOF)升级的一部分:新增 EOFCREATE 和 RETURNCODE,使 EOF 工厂合约可以从内嵌的子容器部署子合约,取代 EOF 代码中的 CREATE/CREATE2。
它解决什么问题?
EVM 对象格式(EOF,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)。 - 收取 32,000 gas,在静态上下文中失败,检查调用深度和余额。
- 把子容器
initcontainer_index作为 initcode 运行,内存切片作为其 calldata。 - 新地址为
keccak256(0xff || sender32 || salt)[12:]。与 CREATE2 不同,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 单独处理。
示例代码
EOFCREATE 核心逻辑的伪代码:
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)