Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: low
Invalid

Use a mapping to check for duplicates instead of nested for loops

Summary

You can use a mapping to check whether an address has already been entered instead of iterating through multiple loops.

Vulnerability Details

This is the nested for loops to check for duplicates

// 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");
}
}
emit RaffleEnter(newPlayers);
}

Impact

You're wasting gas iteraing through multiple loops. You are also rechecking things that have already been checked by checking the entire players array for dupes every time.

Tools Used

Manual review

Recommendations

Create a mapping variable addressesEntered (address entered to bool) and then remove the duplicates check portion of enteredRaffle and instead set the mapping for each address to true in the first loop in the enterRaffle function, as shown in the following changes:

Add mapping:

mapping(address => bool) private addressesEntered;

Remove the duplicates check in enterRaffle and instead add this to the first loop (the one that pushes addresses to the players array):

if(addressesEntered[newPlayers[i]] == true) {
revert PuppyRaffle__NoDuplicatesAllowed();}
else {addressesEntered[newPlayers[i]] = true;}
Updates

Lead Judging Commences

Hamiltonite Lead Judge about 2 years ago
Submission Judgement Published
Invalidated
Reason: Gas optimizations

Support

FAQs

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

Give us feedback!