BriVault

First Flight #52
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: medium
Valid

Gas optimization for function "_getWinnerShares"

Root + Impact

Description

  • Function "_getWinnerShares" can be optimized to save gas. It can use memory array to read user's addresses instead getting it from storage in a loop.


@> for (uint256 i = 0; i < usersAddress.length; ++i){
address user = usersAddress[i];

Risk

Likelihood:

  • Reason 1: When setWinner function is called


Impact:

  • Impact 1: Higher gas consumption

Proof of Concept

Run gas report with following results:

Average aas usage before:
setWinner | 174408
Average aas usage after optimization:
setWinner | 173991

Recommended Mitigation

Mitigation is to use memory variable in a loop

- for (uint256 i = 0; i < usersAddress.length; ++i){
- address user = usersAddress[i];
+ address[] memory _usersAddress = usersAddress;
+ for (uint256 i = 0; i < _usersAddress.length; ++i) {
+ address user = _usersAddress[i];
Updates

Appeal created

bryanconquer Lead Judge 19 days ago
Submission Judgement Published
Validated
Assigned finding tags:

Unbounded Loop in _getWinnerShares Causes Denial of Service

The _getWinnerShares() function is intended to iterate through all users and sum their shares for the winning country, returning the total.

Support

FAQs

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

Give us feedback!