Description
Denial of Service vulnerabilities allow an attacker to make a contract unusable. Common vectors include: unbounded loops over user-controlled arrays, external calls in loops that can be made to fail, block gas limit exploitation, and griefing via unexpected reverts.
The enterRaffle() function at PuppyRaffle.sol:86-90 uses nested loops O(n^2) to check for duplicate players across the entire players array. Gas costs grow quadratically — first 100 players cost ~6.5M gas, next 100 players (200 total) cost ~19M gas (2.9x ratio). At ~500 players, gas exceeds block gas limit (30M), making it impossible for anyone to enter. An attacker can exploit this by entering with many addresses early (refundable), blocking all legitimate players.
| Players | Comparisons | Approximate Gas |
|---|---|---|
| 100 | ~5,000 | ~6.5M |
| 200 | ~20,000 | ~19M |
| 500 | ~125,000 | >30M (exceeds block limit) |
Likelihood:
Can occur naturally with popular raffles (300+ legitimate players)
Can be deliberately triggered by a griefing attacker (cost is refundable)
Impact:
Raffle becomes unusable after ~300-500 players
Earlier entrants pay significantly less gas than later entrants (unfairness)
Attacker griefing cost is refundable via refund()
How the attack works:
An attacker enters the raffle with a large batch of addresses (e.g., 300 unique addresses), paying entranceFee * 300
Gas cost for subsequent enterRaffle() calls grows quadratically because the nested loop now iterates over 300+ existing entries for every new entrant
At ~500 total players, the duplicate check exceeds the 30M block gas limit, making enterRaffle() impossible to execute for any new participant
The attacker can then call refund() for all their addresses to recover their entrance fees, having effectively griefed the raffle at zero net cost
PoC code:
Expected outcome: Gas cost for enterRaffle() grows quadratically, eventually exceeding the block gas limit (~500 players), making the raffle permanently unusable for new entrants.
Replace the O(n^2) nested loop with a mapping(address => uint256) for O(1) duplicate detection:
## 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.