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

Needless recheck for duplicates of players existing in the players array when a new person calls enterRaffle

Summary

Every time a player calls enterRaffle, enterRaffle iterates through the entire players array checking for duplicates, rather than just comparing the new entries against the existing entries. For example, let's say 2 users A and B have called enterRaffle, each entering with their own address. When B called enterRaffle, it checked that A and B were unique. Now user C comes along and makes one entry with his address. enterRaffle will check C against A and C against B, but it will also check A against B even though that is not necessary since it has already been done.

Vulnerability Details

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

Impact

You are wasting gas iterating through people who you have already checked for duplicates. Loops are very expensive.

Tools Used

Manual review

Recommendations

You could fix this by saving the length of the players array before pushing the new players into a variable and then having the loop start with at that index as follows:
Add this at the beginning of the function:

numberOfExistingPlayers = players.length;

Then in the check for duplicates have the inner loop start from the index numberOfExistingPlayers (since the length will be one longer than the length so players[numberOfExistingPlayers] is equal to the index of the first new player):

for (uint256 i = 0; i < players.length - 1; i++) {
for (uint256 j = numberOfExistingPlayers; j < players.length; j++) {
require(players[i] != players[j], "PuppyRaffle: Duplicate player");
}

But I think an even better way to check for duplicates is using a mapping as I suggested in a different finding

Updates

Lead Judging Commences

Hamiltonite Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Gas optimizations

Support

FAQs

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