This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x45Introduced: Frontier (EVM genesis)GASLIMIT retrieves the maximum amount of gas that can be consumed by all transactions in the current block. This limit is dynamically adjusted by validators/miners based on network demand and consensus rules.
Specification
Stack Input:
(none)
Stack Output:
gas_limit (u256)
Gas Cost: 2 (GasQuickStep)Operation:
stack.push(block.gasLimit)
Behavior
GASLIMIT pushes the block gas limit onto the stack as a 256-bit unsigned integer:
Cost: 2 gas (GasQuickStep)GASLIMIT is one of the cheapest EVM operations.Comparison:
GASLIMIT: 2 gas
NUMBER, TIMESTAMP, COINBASE: 2 gas
GAS (0x5A): 2 gas
GASPRICE (0x3A): 2 gas
Common Usage
Gas-Aware Operations
contract GasAware { function checkBlockCapacity() public view returns (bool) { // Check if expensive operation could fit uint256 estimatedGas = 5_000_000; return estimatedGas <= block.gaslimit; }}
Dynamic Batch Sizing
contract BatchProcessor { uint256 constant GAS_PER_ITEM = 50_000; function maxBatchSize() public view returns (uint256) { // Calculate max items based on block gas limit return block.gaslimit / GAS_PER_ITEM; } function processBatch(bytes[] memory data) external { uint256 maxItems = maxBatchSize(); require(data.length <= maxItems, "Batch too large"); for (uint i = 0; i < data.length; i++) { // Process each item } }}
Gas Target Validation
contract GasLimitValidator { uint256 public constant MIN_GAS_LIMIT = 15_000_000; function isNetworkHealthy() public view returns (bool) { return block.gaslimit >= MIN_GAS_LIMIT; }}
Network Congestion Detection
contract CongestionDetector { uint256 public constant TARGET_GAS_LIMIT = 30_000_000; uint256 public constant TOLERANCE = 5_000_000; function isCongested() public view returns (bool) { // If gas limit dropping, network congestion possible return block.gaslimit < TARGET_GAS_LIMIT - TOLERANCE; } function adjustStrategy() external view returns (string memory) { if (block.gaslimit < 20_000_000) { return "High congestion - delay non-urgent txs"; } else if (block.gaslimit < 25_000_000) { return "Moderate congestion - increase gas price"; } else { return "Normal operation"; } }}
Transaction Splitting Logic
contract SmartBatcher { function shouldSplit(uint256 totalGas) public view returns (bool) { // Split if would consume >50% of block gas limit return totalGas > (block.gaslimit / 2); } function calculateChunks(uint256 totalGas) public view returns (uint256) { uint256 safeLimit = (block.gaslimit * 80) / 100; // 80% safety margin return (totalGas + safeLimit - 1) / safeLimit; // Ceiling division }}
Security Considerations
Gas Limit Manipulation
Validators can gradually adjust gas limit (±1/1024 per block):
// Be aware: Gas limit can change over timecontract GasDependent { uint256 public deploymentGasLimit; constructor() { deploymentGasLimit = block.gaslimit; } function checkGasLimitChange() public view returns (int256) { return int256(block.gaslimit) - int256(deploymentGasLimit); }}
DoS via Gas Limit Assumptions
Don’t assume gas limit won’t change:
// VULNERABLE: Assumes constant gas limitcontract Vulnerable { function massUpdate(uint256[] memory data) external { // Could fail if gas limit decreases for (uint i = 0; i < data.length; i++) { // Operations consuming gas } }}// SAFE: Dynamic sizingcontract Safe { function safeUpdate(uint256[] memory data) external { uint256 maxItems = block.gaslimit / 100_000; // Dynamic limit require(data.length <= maxItems, "Too many items"); for (uint i = 0; i < data.length; i++) { // Operations consuming gas } }}
Block Gas Limit vs Transaction Gas Limit
contract GasLimitAware { // block.gaslimit: Max gas for entire block // gasleft(): Remaining gas in current transaction function expensiveOperation() external { // Check transaction gas, not block gas require(gasleft() >= 100_000, "Insufficient gas in tx"); // Heavy computation }}
EIP-1559 Considerations
Post-London (EIP-1559), gas limit still applies:
contract EIP1559Aware { // Block gas limit: Hard cap // Base fee: Adjusts based on block fullness // Target: 50% of gas limit (15M if limit is 30M) function gasMetrics() public view returns ( uint256 limit, uint256 target, uint256 baseFee ) { limit = block.gaslimit; target = block.gaslimit / 2; // EIP-1559 target baseFee = block.basefee; }}
Implementation
TypeScript
/** * GASLIMIT opcode (0x45) - Get block gas limit */export function gaslimit(frame: FrameType): EvmError | null { // Consume gas (GasQuickStep = 2) frame.gasRemaining -= 2n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Push gas limit to stack if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(BigInt(frame.evm.blockContext.block_gas_limit)); frame.pc += 1; return null;}
// Gas limit can adjust by ±1/1024 per blockuint256 maxIncrease = currentGasLimit / 1024;uint256 maxDecrease = currentGasLimit / 1024;// Validators vote by setting gas target in block header// Network gradually converges to consensus