---
eip: 3332
label: EIP-3332
title: MEDGASPRICE Opcode
status: Withdrawn
type: Standards Track
category: Core
created: 2021-03-05
official: https://eips.ethereum.org/EIPS/eip-3332
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3332.md
canonical: https://eips.huatuo.xyz/zh/eip-3332/
lang: zh
tldr: 一项已撤回的提案：新增 MEDGASPRICE 操作码，返回父区块的 gas 价格中位数，让合约可以为传入调用的 gas 价格设上限，削弱抢跑。
tags: [evm, opcode, gas, mev, withdrawn]
---

# EIP-3332: MEDGASPRICE Opcode

> An opcode for getting the median gas price of the parent block.

**TL;DR:** 一项已撤回的提案：新增 MEDGASPRICE 操作码，返回父区块的 gas 价格中位数，让合约可以为传入调用的 gas 价格设上限，削弱抢跑。

## 它解决什么问题？

有些操作会向最先完成的人支付奖励，例如向 optimistic rollup 提交欺诈证明。监视内存池的机器人可以复制这样的交易，附上高得多的 gas 价格，从而抢走奖励。这会打击诚实参与者去做真正验证工作的积极性。

一种自然的防御方式是拒绝为 gas 支付"过高"价格的调用，这样模仿者就无法简单地出更高价。但合约需要一个参考价格来设定这个上限。可选方案要么是硬编码的常量（很快就会过时），要么是由管理员更新（需要信任，还要额外的交易）。EIP-3332 提议让 EVM 直接报告市场价格。

## 它如何运作

- 新操作码 `MEDGASPRICE`，位于 `0x46`，0 个输入，1 个输出，成本 8 gas。
- 它压入**父区块的 gas 价格中位数**。之所以看父区块，是因为当前区块仍在构建中。
- 在 [EIP-1559](/eip-1559/) 之后，它本意是返回上一个区块 `effective_gas_price` 的中位数。结合 [EIP-3198](/eip-3198/) 的 `BASEFEE`，合约可以减去基础费用，转而为优先费设上限。

规范中设想的用法：

```solidity
function submitFraudProof(bytes calldata proof) public {
    require(tx.gasprice <= maxGasPrice());
    // verify and reward
}

function maxGasPrice() public view returns (uint) {
    return 3 * block.medgasprice; // hypothetical binding for MEDGASPRICE
}
```

## 示例代码

`MEDGASPRICE` 从未存在过。用如今的操作码，合约可以通过相对于基础费用限制**优先费**来实现同样的想法：

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

contract CappedReward {
    uint256 public constant MAX_TIP = 2 gwei;

    function submitFraudProof(bytes calldata proof) external {
        // tx.gasprice is the effective price paid; block.basefee comes from EIP-3198.
        require(tx.gasprice - block.basefee <= MAX_TIP, "tip too high");
        // ... verify proof and pay reward ...
    }
}
```

## 需要注意

- 状态：**Withdrawn**。它从未被采纳。
- 操作码 `0x46` 已经被 [EIP-1344](/eip-1344/)（Istanbul）分配给 `CHAINID`，因此所提议的编号按原样根本无法使用。
- 规范本身列出了 gas 价格上限的局限：只有区块生产者公平地排列同价交易时它才有用，而且仅靠重新排序就能使其失效。快速变化的中位数还可能让诚实的交易失败，或再次变得易受攻击。
- 如今大多数抢跑通过私有订单流和区块构建者进行，gas 价格上限几乎起不到保护作用。承诺-揭示（commit-reveal）方案，或把奖励与提交者的身份绑定，更加稳健。


---

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