Excessive gas consumption from Pot.sol
constructor called from ContestManager.sol::createContest
when deploying a new Pot.sol
with a large number of players.
Affected code - https://github.com/Cyfrin/2024-08-MyCut/blob/946231db0fe717039429a11706717be568d03b54/src/Pot.sol#L32-L34
The constructor takes in an array of players that are saved as a state variable, and It's length is used to itterate through the playersToRewards[i_players[i]] = i_rewards[i];
. With this looping condition it is calling the state variable each time and is using a lot of unnessesary gas.
Generate two arrays consisting of 100 and 1000 players
Calling creatingContest
with 100 players and 1000 players
Recording gas costs before and after
We observe that the gas cost has risen significantly
The unnecessary gas consumption in the Pot.sol
constructor increases deployment costs, making it financially prohibitive for creating contest with a substantial large numbers of players. This inefficiency poses scalability challenges, limiting the number of players that can be feasibly included in a contest and potentially affecting user engagement.
Manual Review and Foundry Test
Cache the lengths of the storage arrays if they are used and not modified in for loops.
It is more gas efficient to cache it in some local variable and use that variable instead, like in the following example:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.