TwentyOne

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

Using Loop in `TwentyOne::initializeDeck()` each time the game start can cause higher cos of gas

Description:
The initializeDeck() function in the TwentyOne contract uses a loop to populate a player's deck with all cards from a standard deck (1 to 52). This approach incurs significant gas costs because:

  1. Storage writes (e.g., availableCards[player].push(i)) are among the most expensive operations in Solidity.

  2. Each iteration of the loop performs a write operation, which is executed 52 times, leading to excessive gas consumption.

for (uint256 i = 1; i <= 52; i++) {
availableCards[player].push(i);
}

Impact:

  1. Increased Gas Costs for Users:
    Players or game operators will pay significantly more gas every time the deck is initialized, reducing user satisfaction and scalability.

Proof of Concept:

Recommended Mitigation:

Consider using immutable deck variable instead of using list each initializations :

uint256[52] public constant DECK = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52
];
availableCards[player] = DECK
Updates

Lead Judging Commences

inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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