---
eip: 2970
label: EIP-2970
title: IS_STATIC opcode
status: Stagnant
type: Standards Track
category: Core
created: 2020-09-13
official: https://eips.ethereum.org/EIPS/eip-2970
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2970.md
canonical: https://eips.huatuo.xyz/zh/eip-2970/
lang: zh
tldr: 提出了 IS_STATIC 操作码，让合约知道自己是否运行在 STATICCALL 之内，从而在允许只读调用的同时阻止状态变更。
tags: [evm, opcode, staticcall, account-abstraction, core]
---

# EIP-2970: IS_STATIC opcode

> Add a ISSTATIC (0x4A) opcode that pushes 1 if the current context is static (ie. the execution is in a STATICCALL or a descendant thereof, so state-changing operations are not possible), and 0 if it is not.

**TL;DR:** 提出了 IS_STATIC 操作码，让合约知道自己是否运行在 STATICCALL 之内，从而在允许只读调用的同时阻止状态变更。

## 它解决什么问题？

[EIP-214](/eip-214/) 添加了 `STATICCALL`，它在一个任何状态变更（存储写入、日志、转账、合约创建）都会导致失败的上下文中运行代码。然而，运行在其中的代码没有直接的办法询问"我现在是静态的吗？"。

这对账户抽象很重要。在 [EIP-2938](/eip-2938/) 之下，合约账户可能希望允许外部对其发起无害的只读调用，同时拒绝任何会改变状态的调用。要做到这一点，它需要知道当前上下文是否为静态。

## 它如何运作

该提案新增一个操作码：

- 位于 `0x4A` 的 `IS_STATIC`：如果当前执行位于 `STATICCALL`（或由其调用的任何东西）之内，则压入 `1`，否则压入 `0`。

如今唯一的办法是一种 hack：向一段会发出 `LOG` 然后退出的代码发起一次小的 `CALL`。在静态上下文中 `LOG` 会失败，因此该调用返回 0。规范认为这种做法很脆弱（因为依赖 gas 成本）且浪费。

## 示例代码

仅作说明。`IS_STATIC` 从未被激活，因此这是对所提议行为的伪代码：

```python
def op_is_static(evm):
    evm.stack.push(1 if evm.message.is_static else 0)
```

该 EIP 想要取代的变通方案，用 Solidity 写成：

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

contract StaticProbe {
    function emitLog() external {
        require(msg.sender == address(this));
        emit Probe();
    }
    event Probe();

    // Returns true if called inside a STATICCALL. Costs gas and relies on
    // LOG being forbidden in static context.
    function isStatic() public returns (bool) {
        (bool ok, ) = address(this).call{gas: 5000}(abi.encodeCall(this.emitLog, ()));
        return !ok;
    }
}
```

## 需要注意

- 状态为 **Stagnant**。摘要、向后兼容性和安全性部分从未填写。
- 操作码 `0x4A` 后来被 [EIP-7516](/eip-7516/) 分配给了 `BLOBBASEFEE`，所以如果重启这个想法，就需要另一个操作码编号。
- 推动它的使用场景 [EIP-2938](/eip-2938/) 账户抽象同样没有上线；后来的账户抽象工作走了其他路线。
- 上面的探测式变通方案对 gas 计价表的变化很敏感；请把它当作演示，而不是生产代码。


---

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