Understanding Gas Limits: Exploring the 63/64 Rule
This text dives into the 63/64 rule in Ethereum, which determines the maximum amount of gas a caller can provide to a callee during a function call.
Last updated
This text dives into the 63/64 rule in Ethereum, which determines the maximum amount of gas a caller can provide to a callee during a function call.
Last updated
The caller of a contract specifies the amount of gas given to the callee when making a function call. However, there is a limit on how much gas can be provided. After the "Tangerine Whistle" hard fork (which introduced EIP-150), the maximum amount of gas that a caller can give to a callee is calculated as follows:
This means that the caller can only allocate a maximum of approximately 98.43% (63/64) of the total gas available to the callee. The remaining gas (approximately 1.56% or 1/64) is reserved for the caller's own use. This restriction is in place to prevent certain types of attacks (such as Call Stack Depth Attack) and to ensure that the caller always retains a small portion of the total gas for its own execution. This is because EIP-150 ensure the caller is left with at least 1/64 of the gas available, regardless of what happens to the call.
Understanding this limitation helps developers and users plan their gas allocation appropriately and consider the available gas when making function calls between contracts.
The introduction of the 63/64 rule in Ethereum was intended to address a specific issue that existed in previous implementations. Previously, the caller of a contract could send the entire amount of gas available to them when making a function call to a callee.
However, this created a potential problem. Since the gas cost of a call is relatively low, it allowed for a situation where contracts could call other contracts recursively, potentially leading to an infinite loop or excessive stack usage. This posed a risk of causing a Call Stack Depth Attack that led to a "stack too deep" issue in the implementation of Ethereum nodes, which could lead to instability and unexpected behavior.
A maximum depth limit of 1024 was imposed on the call stack. This means that after reaching a depth of 1024 calls, the last call would throw an exception, preventing further recursion. By introducing the 63/64 rule, the caller is now limited in how much gas they can provide to the callee, ensuring that a portion of the gas is reserved for the caller's own execution. This limitation helps prevent excessive recursion and mitigates the risk of encountering stack-related issues in Ethereum node implementations.
The solution involves reserving a portion of the available gas in the caller contract, specifically 1/64 of the total available gas. As the recursive calls continue, the amount of gas available diminishes rapidly, naturally limiting the depth of recursion. Although there is still a maximum call depth limit of 1024 in the node implementation, it is practically unreachable due to the diminishing gas.