TwentyOne

First Flight #29
Beginner FriendlyGameFiFoundrySolidity
100 EXP
View results
Submission Details
Severity: low
Invalid

[L-1] Using long loop in `TwentyOne::initializeDeck` might lead to revert `Out of gas` in some cases (due to heavy network traffic or gas-intensive operations)

Summary

In TwentyOne::initializeDeck function there's the loop with 52 iterations that performs storage operations, which are some of the most costly operations in Solidity.

Vulnerability Details

function initializeDeck(address player) internal {
require(
availableCards[player].length == 0,
"Player's deck is already initialized"
);
@> for (uint256 i = 1; i <= 52; i++) {
@> availableCards[player].push(i);
}
}

Impact

The gas already consumed up to the point of the revert is non-refundable.

Tools Used

Manual, Foundry

Recommendations

There's a way to make gas consumption more efficient due to heavy network traffic or gas-intensive operations:

function initializeDeck(address player) internal {
require(
availableCards[player].length == 0,
"Player's deck is already initialized"
);
- for (uint256 i = 1; i <= 52; i++) {
- availableCards[player].push(i);
- }
+ uint256[52] memory deck;
+ for (uint256 i = 0; i < 52; i++) {
+ deck[i] = i + 1;
+ }
+ availableCards[player] = deck;
}
Updates

Lead Judging Commences

inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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