The enterRaffle() function within PuppyRaffle.sol implements a nested for loop designed to intercept and filter out duplicate wallet entries during the lottery registration window. As the tracking storage array players[] scales up in size, the computational complexity required to validate entries grows quadratically at an footprint, causing execution gas costs to skyrocket.
The vulnerable routine is implemented as follows:
Because this mechanism checks every incoming entry linearly against the entire historical registry of active participants, each subsequent execution costs substantially more gas than the last. Since enterRaffle() has unrestricted visibility and accepts un-throttled batch inputs, a malicious actor can mount a Sybil attack by registering hundreds of unique, low-cost addresses to artificially bloat the internal storage vector.
Once the storage footprint expands past a critical threshold, the transaction gas required to pass the duplication check will cross Ethereum's maximum Block Gas Limit (currently pegged at ~30 million gas), causing the EVM to systematically revert all future registration attempts with Out-of-Gas errors.
The following Foundry unit test highlights the critical gas explosion vector:
Test Diagnostics:
Gas used (100 players): 6,523,122 gas
Gas used (200 players): 18,995,459 gas
Gas increase delta: 12,472,337 gas (A ~3x cost increase for a simple 2x scaling of participants)
The risk classification for this issue is set to High / Critical due to the immediate compromise of the smart contract's availability and fund integrity:
Irreversible Denial of Service (DoS): Any unprivileged address can permanently freeze the primary user entry point of the contract. Once the array is sufficiently bloated, the enterRaffle() sequence is rendered completely bricked for all future users.
Capital Lockup Vulnerability: If the entry sequence is jammed, dependent chronological sequences—such as selecting a winner via drawWinner() or executing clearing loops—can also experience sweeping cascade failures. This creates a high risk of freezing early deposits inside the contract vault permanently.
Complete Disruption of Value Proposition: The core business mechanics of the protocol break down entirely because it can no longer safely scale or clear operational cycles due to storage array un-scalability.
The projected risk matrix based on pool occupancy scales as follows:
| Active Players | Estimated Gas Footprint | System Status |
|---|---|---|
| 100 | ~6.5M Gas | ✅ Safe |
| 200 | ~19M Gas | ⚠️ Warning Threshold |
| 300 | ~45M Gas | ❌ Hard Near Block Gas Limit |
| 400 | ~80M Gas | ❌ Exceeds Block Gas Limit (Permanently Bricked) |
Strip out the nested for loop search model entirely and implement a lookup registry leveraging a state mapping to achieve a deterministic verification footprint:
## Description `enterRaffle` function uses gas inefficient duplicate check that causes leads to Denial of Service, making subsequent participants to spend much more gas than previous users to enter. ## Vulnerability Details In the `enterRaffle` function, to check duplicates, it loops through the `players` array. As the `player` array grows, it will make more checks, which leads the later user to pay more gas than the earlier one. More users in the Raffle, more checks a user have to make leads to pay more gas. ## Impact As the arrays grows significantly over time, it will make the function unusable due to block gas limit. This is not a fair approach and lead to bad user experience. ## POC In existing test suit, add this test to see the difference b/w gas for users. once added run `forge test --match-test testEnterRaffleIsGasInefficient -vvvvv` in terminal. you will be able to see logs in terminal. ```solidity function testEnterRaffleIsGasInefficient() public { vm.startPrank(owner); vm.txGasPrice(1); /// First we enter 100 participants uint256 firstBatch = 100; address[] memory firstBatchPlayers = new address[](firstBatch); for(uint256 i = 0; i < firstBatchPlayers; i++) { firstBatch[i] = address(i); } uint256 gasStart = gasleft(); puppyRaffle.enterRaffle{value: entranceFee * firstBatch}(firstBatchPlayers); uint256 gasEnd = gasleft(); uint256 gasUsedForFirstBatch = (gasStart - gasEnd) * txPrice; console.log("Gas cost of the first 100 partipants is:", gasUsedForFirstBatch); /// Now we enter 100 more participants uint256 secondBatch = 200; address[] memory secondBatchPlayers = new address[](secondBatch); for(uint256 i = 100; i < secondBatchPlayers; i++) { secondBatch[i] = address(i); } gasStart = gasleft(); puppyRaffle.enterRaffle{value: entranceFee * secondBatch}(secondBatchPlayers); gasEnd = gasleft(); uint256 gasUsedForSecondBatch = (gasStart - gasEnd) * txPrice; console.log("Gas cost of the next 100 participant is:", gasUsedForSecondBatch); vm.stopPrank(owner); } ``` ## Recommendations Here are some of recommendations, any one of that can be used to mitigate this risk. 1. User a mapping to check duplicates. For this approach you to declare a variable `uint256 raffleID`, that way each raffle will have unique id. Add a mapping from player address to raffle id to keep of users for particular round. ```diff + uint256 public raffleID; + mapping (address => uint256) public usersToRaffleId; . . function enterRaffle(address[] memory newPlayers) public payable { require(msg.value == entranceFee * newPlayers.length, "PuppyRaffle: Must send enough to enter raffle"); for (uint256 i = 0; i < newPlayers.length; i++) { players.push(newPlayers[i]); + usersToRaffleId[newPlayers[i]] = true; } // Check for duplicates + for (uint256 i = 0; i < newPlayers.length; i++){ + require(usersToRaffleId[i] != raffleID, "PuppyRaffle: Already a participant"); - for (uint256 i = 0; i < players.length - 1; i++) { - for (uint256 j = i + 1; j < players.length; j++) { - require(players[i] != players[j], "PuppyRaffle: Duplicate player"); - } } emit RaffleEnter(newPlayers); } . . . function selectWinner() external { //Existing code + raffleID = raffleID + 1; } ``` 2. Allow duplicates participants, As technically you can't stop people participants more than once. As players can use new address to enter. ```solidity function enterRaffle(address[] memory newPlayers) public payable { require(msg.value == entranceFee * newPlayers.length, "PuppyRaffle: Must send enough to enter raffle"); for (uint256 i = 0; i < newPlayers.length; i++) { players.push(newPlayers[i]); } emit RaffleEnter(newPlayers); } ```
The contest is live. Earn rewards by submitting a finding.
Submissions are being reviewed by our AI judge. Results will be available in a few minutes.
View all submissionsThe contest is complete and the rewards are being distributed.