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

Expensive operations inside loops

Summary

Functionality relying on complex array loop operations

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

Checking duplicates is 0(n^2) complexity

function getActivePlayerIndex(address player) external view returns (uint256) {
for (uint256 i = 0; i < players.length; i++) {
if (players[i] == player) {
return i;
}
}
return 0;
}

Getting active player index if players index is so large functions like getActivePlayerIndex(...) become too expensive or will always run out of gas and never work

function _isActivePlayer() internal view returns (bool) {
for (uint256 i = 0; i < players.length; i++) {
if (players[i] == msg.sender) {
return true;
}
}
return false;
}

Above function also uses expensive loop operations but could have just used a mapping

Impact

Can lead to gas intensive functions eg when entering raffle, can lead to out of gas for various function and even a DOS if that function runs out of gas and is relied on other functions

Tools Used

Manual Analysis

Recommendations

  1. For checking duplicates this must be done offline or make use better looping using mapping to avoid the current complexity 0(n^2) complexity

  2. For getActivePlayerIndex(...) instead of looping through players array make use a mapping that has index value if player is active e.g mapping(address => index) addressToIndex; this variable is saved when enterRAffle is called for each address in address[] memory newPlayers

  3. Fro _isActivePlayer() instead of looping through players array make use a mapping that has boolean value if player is active e.g mapping(address => bool) isActive; this variable is activated when enterRAffle is called for each address in address[] memory newPlayers

Updates

Lead Judging Commences

patrickalphac Lead Judge
over 1 year ago
Hamiltonite Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Other

Support

FAQs

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