---
eip: 1930
label: EIP-1930
title: CALLs with strict gas semantic. Revert if not enough gas available.
status: Stagnant
type: Standards Track
category: Core
created: 2019-04-10
official: https://eips.ethereum.org/EIPS/eip-1930
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1930.md
canonical: https://eips.huatuo.xyz/zh/eip-1930/
lang: zh
tldr: 提议新增 CALL 变体，要么给被调用方恰好所请求的 gas，要么让调用方 revert，以堵住 63/64 规则的漏洞，该漏洞会让中继者故意使元交易和 ERC-165 检查失败。
tags: [evm, gas, opcode, meta-transactions, core]
---

# EIP-1930: CALLs with strict gas semantic. Revert if not enough gas available.

> Add the ability for smart contract to execute calls with a specific amount of gas. If this is not possible the execution should revert.

**TL;DR:** 提议新增 CALL 变体，要么给被调用方恰好所请求的 gas，要么让调用方 revert，以堵住 63/64 规则的漏洞，该漏洞会让中继者故意使元交易和 ERC-165 检查失败。

## 它解决什么问题？

`CALL`、`DELEGATECALL` 和 `STATICCALL` 的 gas 参数只是一个*上限*。自 [EIP-150](/eip-150/) 起，一次调用最多转发剩余 gas `G` 中的 `G - floor(G/64)`，其中 1/64 留给调用方。如果调用方请求的超过这个数，被调用方会悄悄得到更少的 gas，而调用方继续运行。

当被调用方的失败必须反映其真实行为，而不是缺少 gas 时，这就成了问题：

- **元交易和智能合约钱包。** 中继者提交由用户签名、并指定了 gas 数量的调用。通过提供刚好不够的 gas，恶意中继者可以让内部调用失败，而外层交易仍然成功（例如 nonce 被用掉或手续费被支付）。
- **[ERC-165](/eip-165/) 检查。** `supportsInterface` 以 30,000 gas 被调用。如果实际转发的更少，它可能失败，调用方就会错误地得出该接口不受支持的结论。

开发者可以用手动的 `gasleft()` 检查来变通，但这依赖于精确的 gas 定价，而且很容易写错。

## 它如何运作

该 EIP 提供了两种备选方案：

- **方案 a：** 添加 `CALL`、`DELEGATECALL` 和 `STATICCALL` 的严格变体。如果（扣除调用自身成本后）剩余的 gas 不足以转发所请求的数量，当前帧就会 revert。
- **方案 b：** 复用现有操作码。如果 gas 参数的最高位被置位，其余位就表示严格的 gas 数量；否则行为不变。这依赖于没有任何现有合约传入如此巨大的 gas 值。

在两种情况下，严格规则都是：除非 `G >= I * 64/63`，否则 revert，其中 `G` 是调用时的剩余 gas，`I` 是所请求的数量。用 Geth 的话说，原本悄悄限制 gas 的那个分支，会改为返回 out-of-gas 错误。

## 示例代码

如今合约所使用的变通写法，而这个 EIP 会让它变得不再必要：

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

contract Forwarder {
    /// Call `to` with exactly `txGas`, or revert.
    function execute(address to, bytes calldata data, uint256 txGas)
        external returns (bool success, bytes memory ret)
    {
        (success, ret) = to.call{gas: txGas}(data);

        // Post-call check from the EIP: if the callee was starved,
        // less than txGas/63 can remain, so revert the whole thing.
        if (gasleft() <= txGas / 63) {
            assembly { invalid() } // consume all gas, like a real out-of-gas
        }
    }
}
```

调用后的检查仍然存在漏洞：如果被调用方在得到过少的 gas 后提前返回，剩余的 gas 可能仍足以通过该检查。这就是作者主张在 EVM 层面修复的原因。

## 需要注意

- 状态为 **Stagnant**。主网上不存在严格 gas 的调用操作码，因此上述变通方法仍然是必要的。
- 像 OpenZeppelin 的元交易转发器这样的库包含这类 gas 检查；请优先使用经过审计的实现，而不是手写代码。
- 方案 a 提到 [EIP-1702](/eip-1702/)（账户版本控制）作为淘汰旧操作码的一种方式；该提案同样已停滞。
- 严格 gas 操作码还能让这类合约与 EIP-150 的精确转发规则解耦，这样规则以后即使改变，也不会破坏它们。


---

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