EIP-3332 The MEDGASPRICE opcode (MEDGASPRICE Opcode)
No call decision · Fees & gas · EIP status Withdrawn · created 2021-03-05
Adds the MEDGASPRICE (0x46) opcode, letting a smart contract directly query the median gas price of the previous block, so gas price cap logic can be written without on-chain storage.
Why it is needed
Optimistic rollups reward whoever submits a fraud proof, which makes it possible for bots watching the mempool to front-run by copying the same proof with a higher gas price and intercepting the reward. Such an attacker collects the reward without doing any verification themselves, and this ultimately reduces the incentive for others to perform verification. This is also why contracts that want to preserve a first-come-first-serve order want to impose a gas price cap. Right now, the only options are hardcoding a constant cap or having someone adjust it manually; a more sophisticated approach requires storing gas price information on chain, which adds extra transactions and gas costs.
What changes
Today
- To impose a gas price cap, a contract must hardcode a constant or have an admin update it manually
- Using past gas price information on chain requires recording it in a separate transaction, which costs extra gas
- Transactions with a reward attached, such as fraud proof submissions, are directly exposed to front-running
After EIP-3332
- A single call to MEDGASPRICE immediately reads the median gas price of the previous block
- That value can be used to dynamically compute a gas price cap within the contract
- After EIP-1559, it is defined to return the median effective_gas_price of the previous block
Who is affected
- Optimistic rollup fraud proof submitters: They can add cap logic to the contract to block front-running bots that use high gas prices.
- Smart contract developers: When writing first-come-first-serve processing logic, they can use a recent gas price benchmark directly without on-chain storage.
- Client (node) implementation teams: They must implement the new opcode 0x46 with a gas cost of 8.
- The post-EIP-1559 ecosystem: Must be used together with BASEFEE (EIP-3198) to compute the pure inclusion fee after subtracting the base fee.
In depth
Opcode specification
Adds the new opcode MEDGASPRICE (0x46) once block.number is at or above a TBD (undetermined) activation block. It takes 0 inputs and produces 1 output, pushing the median gas price of the previous block onto the stack, with a gas cost of 8.
Motivation: fraud proof front-running
Because optimistic rollups pay a monetary reward to whoever submits a fraud proof, it is possible to watch the mempool and front-run by copying the same proof at a higher gas price to intercept the reward. Such an attacker collects the reward without performing any verification themselves, which in turn reduces other participants' incentive to verify. Capping the transaction's gas price is proposed as a defense against this attack.
Example of a first-come-first-serve contract
A first-come-first-serve mechanism must overcome a market structure in which higher gas price transactions get processed first. Currently the only options are fixing the gas price cap as a constant or having a specific group adjust it manually; a more sophisticated approach requires storing gas price information on chain, which increases the number of transactions and the ether cost. With MEDGASPRICE, a function like maxGasPrice() can set a dynamic cap based on the previous block, such as 3 * block.medgasprice.
Compatibility with EIP-1559
After EIP-1559, the base fee is burned and the inclusion fee remains as a portion of it. Whether the block is full or has room to spare, an attacker can still raise the inclusion fee to front-run, so this EIP remains meaningful. Accordingly, after 1559 MEDGASPRICE must return the median effective_gas_price of the previous block, and BASEFEE (EIP-3198) is needed to subtract base_fee_per_gas from effective_gas_price to derive the pure inclusion fee and implement the cap.
Naming rationale
Since the network's median gas price can only be calculated from the most recently finalized block, a transaction being executed should expect a median gas price based on the previous block. This is why it was named MEDGASPRICE.
Security considerations
The gas price cap strategy has two limitations. First, it relies on the assumption that miners are fair; this can be defeated simply by reordering transactions with the same gas price. Second, the MEDGASPRICE value can fluctuate rapidly from block to block, so a transaction that is not included immediately may fail because the gas price has dropped, or may be exposed to front-running again because the price has risen.
Current status
This EIP needs to be read in comparison with EIP-1559, EIP-3198, and other EIPs that were later proposed and actually adopted. Its status is Withdrawn, and the block.number threshold that triggers activation remains TBD, so it was never actually implemented.