Blog

Infinite loops and dealing with Out-of-Gas Situations

Unlike loops with a predetermined number of iterations, such as those controlled by constants or input parameters with known bounds, loops that depend on storage values can vary significantly in their execution time. This variability poses a risk because transactions on blockchains are constrained by the block gas limit. Each block has a maximum amount of gas that can be consumed by all the transactions it contains. If a single transaction consumes too much gas due to an unpredictable loop, it can exceed this limit and fail.

This not only leads to wasted computational resources but also to financial losses for the user initiating the transaction and even worse to a DoS.

One key approach is to implement gas-efficient programming practices. This involves avoiding loops that depend on variables with potentially unbounded sizes, such as user inputs or external data sources. These types of loops can easily lead to scenarios where the gas required to complete the transaction surpasses the block gas limit. Instead, developers should consider breaking down large tasks into smaller, more manageable sub-tasks that can be executed across multiple transactions. This technique, often referred to as "loop splitting" or "function splitting," ensures that each transaction only requires a limited amount of gas, reducing the risk of running out of gas.




In the same vein, using `gasleft()` to monitor the remaining gas in a function allows you to check how much gas is still available during execution proactively. This helps in making decisions to avoid exceeding the gas limit. For example, a function can be designed to stop execution if the remaining gas is too low: