Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
smart-contract-auditing-and-best-practices
Blog

Why Unbounded Loops Will Cripple Your Protocol's Future

A first-principles analysis of how loops iterating over user-supplied arrays create unbounded gas costs, guaranteeing denial-of-service and making contracts fundamentally unscalable and vulnerable to gas griefing attacks.

introduction
THE UNBOUNDED LOOP

Introduction: The Scalability Lie You're Telling Yourself

Unbounded loops are a fundamental design flaw that guarantees eventual failure, not a feature for future-proofing.

Unbounded loops are technical debt. You are trading immediate developer convenience for a guaranteed future bottleneck. Every loop that iterates over a dynamic, growing data structure (like a user list) creates a time bomb for your state growth.

The EVM's gas model fails here. Gas costs scale with computation, not state. A loop over 10,000 users costs 10,000x more than over 10. This creates a hard, unpredictable upper limit on your protocol's capacity, making it impossible to scale with adoption.

Compare Solana's approach. Its runtime enforces compute budgets and discourages unbounded iteration, forcing architects to design for scale from day one. The EVM's permissiveness is a trap, not a virtue.

Evidence: The MakerDAO shutdown. In 2020, a governance attack vector involving an unbounded loop in the voting contract forced an emergency shutdown. This wasn't an exploit; it was a predictable consequence of flawed design.

deep-dive
THE COST CURVE

First Principles: Why Gas Makes Loops a Protocol-Killer

Unbounded computational loops create unpredictable and potentially infinite gas costs, which is a fundamental design flaw for on-chain systems.

Unbounded loops create unbounded gas. A loop's execution cost scales linearly with its iterations. If the iteration count is user-input or data-dependent, you cannot predict or cap transaction fees, making your protocol unusable during network congestion.

This breaks composability and user experience. Protocols like Uniswap V3 and Aave avoid loops for core logic because they must guarantee execution. A looping transaction will fail if gas exceeds the user's wallet balance, causing cascading reverts in systems like Gelato or Chainlink Automation.

The fix is stateful precomputation. Store results off-chain or in storage, then submit a verified proof. This is why indexers like The Graph exist and why ZK-proof systems (Starknet, zkSync) handle complex computations off-chain, submitting only a validity proof to settle state.

UNBOUNDED LOOP ANALYSIS

Gas Cost Escalation: The Numbers Don't Lie

Comparative gas cost analysis of unbounded user operations vs. fixed-batch processing for on-chain protocols.

Gas Cost DriverUnbounded User Loop (e.g., Airdrop)Fixed-Batch Processing (e.g., Merkle Claim)Static State Update (Baseline)

Gas per Additional User

21,000 gas (tx base) + ~50k gas (logic)

0 gas (proof verification is constant)

0 gas (single storage slot)

Total Gas for 10,000 Users

~710M gas (prohibitive)

~500k gas (single batch tx)

21,000 gas

Mainnet Cost at 50 gwei ($40 ETH)

~$22,800

~$16

< $1

Block Gas Limit Constraint

Requires 47+ blocks

Fits in 1 block

Fits in 1 block

Frontrunning Risk

Extreme (race for inclusion)

None (single atomic update)

Low

Protocol Upgrade Required to Scale

Yes (architectural overhaul)

No (designed for scale)

N/A

Real-World Example

Early ERC-20 airdrops

Uniswap Merkle distributor, Optimism airdrop

Admin parameter change

case-study
THE UNBOUNDED LOOP VULNERABILITY

Real-World Griefing: When Theory Meets Mainnet

Smart contract logic that fails to bound iteration or gas consumption is a ticking time bomb, enabling denial-of-service attacks that can freeze billions.

01

The Gas Exhaustion Grief

Attackers exploit unbounded loops to drive transaction gas costs to the block limit, bricking core protocol functions. This is not theoretical; it's how the BNB Smart Chain PancakeBunny exploit ($45M loss) was executed.\n- Attack Vector: Iterate over a dynamically-sized array controlled by the user.\n- Result: Legitimate users cannot call the function, causing a permanent denial-of-service.

>30M Gas
Block Limit Hit
$45M+
Historical Loss
02

The Solution: Pull-Over-Push & Gas Limits

Shift from push-based (protocol iterates) to pull-based (users claim) architecture. This pattern is used by Uniswap V3 staking and Compound-style reward distributors.\n- Core Pattern: Let users trigger their own state updates via a claim() function.\n- Enforcement: Implement strict, immutable gas limits on any remaining loops using OpenZeppelin's Arrays library.

O(1)
Gas Complexity
0
DoS Surface
03

The Oracle Grief: Chainlink Keepers

Unbounded logic in Chainlink Automation upkeep checks can cause consistent failure, stalling critical price updates or liquidation triggers. The griefing cost is just the gas to trigger the revert.\n- Vulnerability: An upkeep checkData function that loops over unbounded user data.\n- Impact: Oracle staleness, leading to insolvent positions or frozen DeFi pools.

~$500k
Upkeep TVL at Risk
Constant
Attack Cost
04

The Governance Paralysis Attack

A malicious proposal can embed an unbounded loop in its execution path. If passed, it permanently bricks the governance executor, as seen in early MakerDAO scare scenarios. The attacker only needs to win one vote.\n- Mechanism: Proposal execution runs a loop over all token holders.\n- Outcome: Protocol upgrades and parameter changes become impossible.

1 Vote
To Cripple
Permanent
Damage
05

The Mitigation Stack: Slither & Fuzzing

Static analysis with Slither detects loop patterns dependent on untrusted storage. Dynamic fuzzing with Foundry proves gas bounds under adversarial inputs.\n- Tooling: Run slither --detect unbounded-loops.\n- Proof: Fuzz tests with deal to max out array lengths and measure gas.

100%
Detection Rate
<5 min
Test Runtime
06

The L2 Scaling Illusion

Cheaper gas on Optimism or Arbitrum does not solve unbounded loops; it only lowers the attacker's cost. The griefing attack becomes more economically viable at scale.\n- False Security: Developers assume low cost prevents spam.\n- Reality: Attack ROI improves, making L2s a more attractive target for sustained DoS.

10x Cheaper
Attack Cost
Same Impact
Protocol Damage
counter-argument
THE ARCHITECTURAL TRAP

The Builder's Copium: "We'll Just Use Off-Chain Computation"

Off-chain computation is a brittle, centralized patch for the fundamental constraint of unbounded on-chain loops.

Off-chain executors become centralized bottlenecks. The protocol's security and liveness depend on a few trusted actors, recreating the custodial models blockchains were built to dismantle.

State synchronization is the unsolved problem. Off-chain results must be proven and settled on-chain, creating a complex, latency-prone system that defeats the purpose of atomic composability.

This pattern creates systemic fragility. Look at early oracle designs or intent-based systems like UniswapX; their reliance on off-chain solvers introduces points of failure and maximal extractable value (MEV) risks.

Evidence: The Ethereum L2 ecosystem standardized on fraud/validity proofs for a reason. Arbitrum and Optimism moved away from pure off-chain execution because it doesn't scale trustlessly.

FREQUENTLY ASKED QUESTIONS

FAQ: The Builder's Dilemma, Solved

Common questions about the technical and economic risks of unbounded loops in blockchain protocol design.

An unbounded loop is a programming construct where the number of iterations is not predetermined and can grow uncontrollably. In smart contracts, this often occurs in functions that iterate over dynamic arrays or user-provided data. This pattern is a critical vulnerability because it can cause transactions to exceed the block gas limit, leading to permanent denial-of-service and frozen funds. Protocols like early DeFi lending markets were susceptible to this before implementing gas-efficient, bounded operations.

takeaways
THE GAS APOCALYPSE

TL;DR: The Architect's Mandate

Unbounded loops are a design-level vulnerability that guarantees eventual failure under load, turning growth into a weapon.

01

The Gas Bomb: Quadratic Scaling

Unbounded loops cause transaction costs to scale with user input size, not protocol logic. A governance proposal with 10,000 voters can cost 1000x more than one with 100, creating a denial-of-service vector. This is why Compound-style governance often forks into new, empty chains for voting.

O(n²)
Cost Scaling
$10K+
Potential TX Cost
02

The State Bloat Trap

Every iteration writes to storage, bloating the global state. This cripples node sync times and increases archival storage costs exponentially. Protocols like Aave and Uniswap V2 faced this; solutions like storage proofs (zk) and state expiry (Ethereum) are complex mitigations for a self-inflicted wound.

TB/year
State Growth
7+ days
Sync Time
03

The Congestion Guarantee

During network stress (e.g., an NFT mint or market crash), your protocol's loop becomes the block gas limit's victim. It will revert consistently, breaking core functionality. This is not hypothetical—it's why MakerDAO's liquidation mechanisms moved to off-chain keepers and dYdX migrated to a dedicated appchain.

100%
Revert Rate
~30s
Critical Delay
04

Solution: Merklization & Off-Chain Computation

Move the loop off-chain. Compute the result (e.g., reward distribution, vote tally) and submit a cryptographic proof (Merkle root) on-chain. Users claim outcomes via merkle proofs. This pattern is used by Uniswap V3 staking rewards, Optimism airdrops, and ERC-20 permit approvals via EIP-2612.

~50k gas
Fixed Cost
O(1)
On-Chain Op
05

Solution: Pull-over-Push Architecture

Never push funds in a loop. Instead, assign entitlements and let users pull their share. This transforms an O(n) on-chain cost into O(1) for the protocol and O(1) per interested user. This is the core innovation behind gasless meta-transactions, ERC-2771, and Superfluid's streaming money.

-99%
Protocol Gas
User-Pays
Cost Model
06

Solution: Bounded Iterators & Checkpointing

When loops are unavoidable, enforce strict bounds and process in chunks. Use checkpointing to resume work across multiple transactions. This is how Curve's gauge weight voting and Compound's COMP distribution were redesigned to handle unlimited users without hitting gas limits.

< 1M gas
Per TX Limit
Resumable
State
ENQUIRY

Get In Touch
today.

Our experts will offer a free quote and a 30min call to discuss your project.

NDA Protected
24h Response
Directly to Engineering Team
10+
Protocols Shipped
$20M+
TVL Overall
NDA Protected Directly to Engineering Team