How can a 'never-ending' loop that goes through a list of unknown size stop a smart contract function from working for everyone?
A smart contract is a program stored and executed on a blockchain, and its functions are specific pieces of code within it that can be called and run. Every operation performed by a smart contract function, such as reading data, writing data, or performing calculations, requires a computational fee known as "gas." Gas is the unit used to measure the computational effort needed for these operations. Each transaction, which is how a smart contract function is invoked, includes a "gas limit" set by the caller, representing the maximum gas they are willing to spend. Furthermore, there is a "block gas limit," which is the maximum total gas that can be consumed by all transactions collectively included in a single block on the blockchain.
When a smart contract function includes a loop designed to iterate through a list whose size is unknown or has grown to be excessively large, each individual operation within that loop consumes gas. For example, accessing or modifying a piece of data stored on the blockchain costs gas. As the loop continues to iterate over a vast number of items, the cumulative gas cost for processing the entire list can rapidly become prohibitively high. If the total gas required for the loop to complete its execution exceeds either the gas limit set for that specific transaction or, more significantly, the overall block gas limit, the transaction will "run out of gas."
When a transaction runs out of gas, it automatically "reverts." To revert means the entire transaction fails completely, and all changes to the smart contract's state that occurred during that transaction are undone, as if the transaction never happened. No data is permanently saved, and the function's intended operation is not fulfilled. Although the transaction fails and reverts, the gas consumed up to the point of failure is still charged to the caller, meaning the gas is spent without the desired outcome.
This mechanism stops the smart contract function from working for everyone because any subsequent attempt by any user to call that specific function will encounter the exact same problem: the loop will again attempt to process the excessively large list, consume more gas than allowed, hit the gas limit, and consequently revert. Since the underlying data structure, like the large list, often remains in a state that causes this gas exhaustion, the function becomes perpetually unusable and effectively broken for all future interactions by anyone trying to call it.