---
eip: 2488
label: EIP-2488
title: Deprecate the CALLCODE opcode
status: Stagnant
type: Standards Track
category: Core
created: 2019-12-20
requires: 7
official: https://eips.ethereum.org/EIPS/eip-2488
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2488.md
canonical: https://eips.huatuo.xyz/zh/eip-2488/
lang: zh
tldr: 提议让遗留的 CALLCODE 操作码始终返回失败，从而将其关闭，移除这个在 2016 年已被 DELEGATECALL 取代的功能。
tags: [evm, opcode, deprecation, core]
---

# EIP-2488: Deprecate the CALLCODE opcode

> Deprecate CALLCODE in a somewhat backwards compatible way, by making it always return failure.

**TL;DR:** 提议让遗留的 CALLCODE 操作码始终返回失败，从而将其关闭，移除这个在 2016 年已被 DELEGATECALL 取代的功能。

## 它解决什么问题？

`CALLCODE`（`0xf2`）随 Frontier 发布。它本意是让一个合约针对自己的存储运行另一个合约的代码，类似库调用。实际上它有一处设计错误：在借来的代码内部，`msg.sender` 和 `msg.value` 没有从原始调用中保留下来，导致很难安全地使用。

[EIP-7](/eip-7/) 在 Homestead（2016 年初）中通过加入 `DELEGATECALL` 修复了这一问题，它确实保留了调用者上下文。从那以后，`CALLCODE` 实际上无人使用，但每个 EVM 实现仍然必须支持它。ERC-2488 提议将其退役。

## 它如何运作

规范只有一行。在分叉区块之后：

- 执行 `CALLCODE` 始终向栈中压入 `0`，表示调用失败。

该操作码并未被移除，也不会中止执行。这是有意为之：通过返回失败，仍在调用 `CALLCODE` 的合约有机会发现并恢复，而不是直接回滚。

规范指出，这对从创世区块开始同步的客户端没有帮助（它们仍须执行历史上的 `CALLCODE`），但对轻客户端和从较晚状态开始的客户端有帮助。

## 示例代码

对所提议规则的示意性伪代码：

```python
def op_callcode(evm, gas, addr, value, in_off, in_len, out_off, out_len):
    if evm.block_number >= FORK_BLOCK:
        evm.stack.push(0)   # always "call failed"
        return
    legacy_callcode(evm, gas, addr, value, in_off, in_len, out_off, out_len)
```

合约应改用的方案，即通过 Solidity 使用 `DELEGATECALL`：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Caller {
    uint256 public x;

    // Runs `lib`'s code against this contract's storage, keeping msg.sender
    function useLib(address lib, bytes calldata data) external {
        (bool ok, ) = lib.delegatecall(data);
        require(ok, "delegatecall failed");
    }
}
```

## 需要注意

- 状态为 **Stagnant**。它尚未排期，`CALLCODE` 目前在以太坊主网上仍然可用。
- 这是一项破坏性变更。作者预计不会影响任何有价值的合约，但规范本身也标明这一说法尚未验证，安全性考虑、测试和实现均留作"TBA"。
- 现代 Solidity 不再暴露 `callcode`；请使用 `delegatecall`。
- EVM 对象格式（[EIP-3540](/eip-3540/)）走了另一条路：[EIP-3670](/eip-3670/) 的代码验证会拒绝新的 EOF 合约中的 `CALLCODE`，同时不触动遗留合约。


---

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