Eggstravaganza

First Flight #37
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: high
Invalid

EggFindThreshold should not ever be lower than 10

Summary

In searchForEgg() "random" is compared to "eggFindThreshold" in order to determine if a egg was found or not.
"random" has to be lower than "eggFindThreshold" in order for an egg to be found
EggFindThreshold can not take a value lower than 10(in one digit 0-9), because the value of random will always have two digits(%100)

Vulnerability Details

Eggs will never be found, calling searchForEgg() will always be unsuccessful

Impact

Game will be unplayable, players will not be able to play the game

Tools Used

Forge Test, Remix, Manual review

Recommendations

Add a dev note in the declaration of eggFindThreshold

/// @notice Chance (in percent) to find an egg on each search attempt.
/// @dev Value should never be set below 10 => innability to find eggs
uint256 public eggFindThreshold = 90; // Default is a 20% chance

Add a new condition to guard for values below 10 in setEggFindThreshold()

/// @notice Allows the owner to adjust the egg-finding chance.
function setEggFindThreshold(uint256 newThreshold) external onlyOwner {
require(newThreshold <= 100, "Threshold must be <= 100");
require(10 <= newThreshold, "Threshold must be >= 10"); // Values lower than 10 not allowed
eggFindThreshold = newThreshold;
}

Add a new check to guard for values below 10 in searchForEgg() in case the contract is deployed with a eggFindThreshold below 10

/// @notice Participants call this function to search for an egg.
/// A pseudo-random number is generated and, if below the threshold, an egg is found.
function searchForEgg() external returns (string memory unsuccessful) {
require(gameActive, "Game not active");
require(block.timestamp >= startTime, "Game not started yet");
require(block.timestamp <= endTime, "Game ended");
require(10 <= eggFindThreshold, "Threshold must be >= 10"); // Values lower than 10 not allowed
// Pseudo-random number generation (for demonstration purposes only)
uint256 random =
uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, msg.sender, eggCounter))) % 100;
if (random < eggFindThreshold) {
eggCounter++;
eggsFound[msg.sender] += 1;
eggNFT.mintEgg(msg.sender, eggCounter);
emit EggFound(msg.sender, eggCounter, eggsFound[msg.sender]);
}
}
Updates

Lead Judging Commences

m3dython Lead Judge 5 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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