EIP-2542 Adding opcodes to query the gas limit (New opcodes TXGASLIMIT and CALLGASLIMIT)
No call decision · EVM & execution · EIP status Withdrawn · created 2020-02-29
A proposal to add two opcodes, TXGASLIMIT and CALLGASLIMIT, letting smart contracts read directly from the EVM the gas limit signed for the entire transaction and the gas limit allocated to the current call.
Why it is needed
Currently the EVM only provides ways to read the gas limit for the entire block (GASLIMIT) and the remaining gas (GAS); there is no way at the EVM level to know the original gas limit signed into the transaction (startgas) or the gas limit actually passed to the current call frame after applying the EIP-150 63/64 rule. As relaying, meta-transactions, and account abstraction, where a third party pays gas on someone else's behalf, become more common, contracts increasingly need to accurately compute the actual gas consumed (gasUsed) or verify that the gas limit approved by the signer matches the limit in effect during execution. gasleft() alone cannot account for dynamically changing costs such as the number of zero and non-zero bytes in calldata or opcode pricing, and since it is not a fixed value, it cannot be included in a signature either.
What changes
Today
- gasleft() only reveals the remaining gas; the original gas limit signed into the transaction (startgas) cannot be read from the EVM
- There is also no direct way for the EVM to read the gas limit actually passed to the current call frame (the value after applying the 63/64 rule)
- In meta-transactions, it is difficult to verify on-chain whether the gas limit approved by the signer matches the limit in effect during execution
After EIP-2542
- TXGASLIMIT (0x5c) pushes the gas limit signed for the entire transaction directly onto the stack
- CALLGASLIMIT (0x5d) pushes the gas limit allocated to the current execution frame directly onto the stack
- Both opcodes have a gas cost of 2, the same as the existing GASLIMIT
- A proposal to rename the existing 0x45 GASLIMIT to BLOCKGASLIMIT is also considered alongside this
Who is affected
- Smart contract developers: Enables writing exact gasUsed calculations and gas limit verification logic instead of approximating with gasleft()
- Wallet and relayer developers: Lets contracts directly check whether the signed gas limit matches the actual limit in meta-transactions and account abstraction
- Client teams: Must handle implementing the two new opcodes together with the question of renaming the existing GASLIMIT
- Ordinary users: No direct impact, though reliability may improve for contracts using this feature
In depth
TXGASLIMIT (0x5c)
Pushes the gas limit for the entire transaction onto the stack. This is the value of the 'startgas' parameter signed by the externally owned account (EOA). The gas cost is 2, the same as GASLIMIT.
CALLGASLIMIT (0x5d)
Pushes the gas limit of the current execution frame onto the stack. This is the 'callGas' value obtained by applying EIP-150's 'all but one 64th' rule. The gas cost is 2, the same as GASLIMIT.
When it applies
The specification states that these two opcodes are added once block.number reaches or exceeds a block number designated as TBD (to be determined). The original text does not specify a concrete fork block number.
Considering renaming the existing GASLIMIT
The original text also proposes renaming the existing 0x45 GASLIMIT opcode (the block gas limit) to BLOCKGASLIMIT, to avoid confusion with the newly added transaction- and call-level gas limit opcodes.
Use case: computing gasUsed and verifying a signed limit
The Rationale section presents two use cases. First, accurately computing gasUsed, which is currently inaccurate, by subtracting gasleft() from msg.gasLimit; it also mentions the GTXDATANONZERO issue, where cost varies with the ratio of zero to non-zero bytes in calldata. Second, verifying together with ecrecover, in a meta-transaction, whether the desiredGasLimit approved by the signer matches tx.gasLimit. It explains that gasleft() cannot be used for this purpose because it is dynamic and cannot be included in a signature.
Forward compatibility: relationship to the UNGAS proposal
EIP-2489 (deprecation of the GAS opcode, also known as UNGAS) arose from concern that 'gas introspection' by contracts creates a dependency on the current opcode pricing scheme. The author explains that, separately from critiquing that concern, if a change breaks the behavior of the existing 0x5a GAS opcode, the same consideration simply applies equally to the newly proposed opcodes, and this adds no additional constraint to EVM evolution. It is also considered not to conflict with Stateless Ethereum, since it does not expose the gas schedule either.
Security considerations
Existing smart contracts are unaffected by this change. It states that contracts using the new opcodes should use them only to provide information about the execution environment, not as core logic for a security feature.
Current status: Withdrawn
The status of this EIP is Withdrawn, and the withdrawal-reason in the metadata states it was superseded by EIP-8141. In other words, this proposal was never actually adopted, and a successor proposal addressing the same goal, EIP-8141, is being discussed instead.