Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Impact: low
Likelihood: low
Invalid

Storage variables in a loop should be cached

[G-2] Storage variables in a loop should be cached

Description: Every time you call players.length you read from storage. This is very expensive in a loop because in each loop cost more gas than caching it in memory.

// @audit-gas uint256 playersLength = players.length;
@> 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");
}
}

Recommendation: Create a new variable to store players.length so it is only read once instead of on every iteration.

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

Lead Judging Commences

ai-first-flight-judge Lead Judge about 2 hours ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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

Give us feedback!