Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: medium
Valid

Nested Loop Over participants Array in enterRaffle() Causes Denial of Service (DoS) via Block Gas Limit Exhaustion

Description

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:

// Check for duplicates
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");
}
}

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.

Proof of Code (PoC)

The following Foundry unit test highlights the critical gas explosion vector:

function test_DoS_enterRaffle() public {
// Register the initial 100 players
address[] memory players100 = new address[](100);
for (uint256 i = 0; i < 100; i++) {
players100[i] = address(uint160(i + 1));
}
uint256 gasBefore = gasleft();
puppyRaffle.enterRaffle{value: entranceFee * 100}(players100);
uint256 gasUsed100 = gasBefore - gasleft();
// Register the next 100 players (Totaling 200 active slots)
address[] memory players200 = new address[](100);
for (uint256 i = 0; i < 100; i++) {
players200[i] = address(uint160(i + 101));
}
gasBefore = gasleft();
puppyRaffle.enterRaffle{value: entranceFee * 100}(players200);
uint256 gasUsed200 = gasBefore - gasleft();
// Assert that the gas requirements scale abnormally
assert(gasUsed200 > gasUsed100);
}

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)


Risk

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)

Recommended Mitigation

Strip out the nested for loop search model entirely and implement a lookup registry leveraging a state mapping to achieve a deterministic verification footprint:

// 1. Declare a tracking state mapping to monitor active registration statuses
mapping(address => bool) public hasEntered;
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++) {
// 2. Perform an O(1) instantaneous check — cost remains static regardless of pool size
require(!hasEntered[newPlayers[i]], "PuppyRaffle: Duplicate player");
hasEntered[newPlayers[i]] = true;
players.push(newPlayers[i]);
}
emit RaffleEnter(newPlayers);
}
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 6 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[M-01] `PuppyRaffle: enterRaffle` Use of gas extensive duplicate check leads to Denial of Service, making subsequent participants to spend much more gas than prev ones to enter

## 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); } ```

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!