EIPs Explained

EIP-2542 · Withdrawn · Core · 2020-02-29

EIP-2542: New opcodes TXGASLIMIT and CALLGASLIMIT

A withdrawn proposal for TXGASLIMIT and CALLGASLIMIT opcodes, which would have let contracts read the gas limit of the whole transaction and of the current call frame for exact gas accounting.

What problem does it solve?

Relayers, meta-transaction forwarders and account abstraction systems often need to know exactly how much gas a transaction has used so they can charge or reimburse the right amount. The EVM only exposes:

  • GASLIMIT (0x45) – the block gas limit, which contracts rarely care about.
  • GAS (0x5a, gasleft()) – the gas remaining, not the amount you started with.

So contracts approximate. A common trick is to call gasleft() on the first line and subtract later, but that misses intrinsic costs (the 21,000 base, calldata bytes) and whatever Solidity spent copying calldata before your first line ran. Another use case is enforcing a gas limit that a user signed in a meta-transaction: gasleft() is dynamic and cannot be compared with a signed value.

How it works

The proposal adds two opcodes, each costing 2 gas (the same as GASLIMIT):

Opcode Byte Pushes
TXGASLIMIT 0x5c The gas limit signed into the transaction (startgas)
CALLGASLIMIT 0x5d The gas given to the current frame, after the EIP-150 "all but one 64th" rule

It also suggests renaming GASLIMIT to BLOCKGASLIMIT to avoid confusion. Nothing else in the EVM changes, so existing contracts are unaffected.

The authors stress that these values should be used as information about the execution environment, not as the core of a security mechanism.

Sample code

Pseudo-Solidity from the proposal's rationale. msg.gasLimit and tx.gasLimit were never added to Solidity, so this does not compile:

// Illustrative only: would map to CALLGASLIMIT / TXGASLIMIT
function keepTrackOfGas() public {
    // ... work ...
    uint256 gasUsed = msg.gasLimit - gasleft();   // CALLGASLIMIT - GAS
}

function verifyGasLimit(uint256 desiredGasLimit /*, signature ... */) public {
    // the user signed desiredGasLimit; check the relayer honoured it
    require(tx.gasLimit == desiredGasLimit, "gas limit not authorized"); // TXGASLIMIT
}

What developers do today instead (approximate):

function relayed(bytes calldata data) external {
    uint256 start = gasleft();          // misses intrinsic + calldata cost
    // ... execute ...
    uint256 used = start - gasleft() + 21_000 + OVERHEAD_ESTIMATE;
}

Things to know

  • Status: Withdrawn. The withdrawal reason given is "Superseded by EIP-8141" (Frame Transaction), which is itself still a Draft.
  • Opcode bytes 0x5c and 0x5d were later assigned to other instructions (TLOAD/TSTORE in EIP-1153), so these numbers are no longer available.
  • Gas introspection is controversial: some core developers prefer that contracts not depend on gas values at all, so that gas costs can be repriced freely.
  • For relayed calls today, see ERC-2771 (trusted forwarders) and ERC-4337, which handle gas accounting at the bundler/EntryPoint level.

#evm #opcodes #gas #meta-transactions #core

Details

Authors
Alex Forshtat <[email protected]>
Status
Withdrawn
Type
Standards Track / Core
Created
2020-02-29

Machine-readable: Markdown · JSON