Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache and read storage variables from the stack can save gas #175

Open
code423n4 opened this issue Nov 17, 2021 · 1 comment
Open

Cache and read storage variables from the stack can save gas #175

code423n4 opened this issue Nov 17, 2021 · 1 comment
Assignees
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

For the storage variables that will be accessed multiple times, cache and read from the stack can save ~100 gas from each extra read (SLOAD after Berlin).

For example:

https://github.com/code-423n4/2021-11-nested/blob/f646002b692ca5fa3631acfff87dda897541cf41/contracts/FeeSplitter.sol#L152-L162

function sendFeesWithRoyalties(
    address _royaltiesTarget,
    IERC20 _token,
    uint256 _amount
) external nonReentrant {
    require(_royaltiesTarget != address(0), "FeeSplitter: INVALID_ROYALTIES_TARGET_ADDRESS");

    _sendFees(_token, _amount, totalWeights);
    _addShares(_royaltiesTarget, _computeShareCount(_amount, royaltiesWeight, totalWeights), address(_token));
}

Recommendation

Change to:

function sendFeesWithRoyalties(
    address _royaltiesTarget,
    IERC20 _token,
    uint256 _amount
) external nonReentrant {
    require(_royaltiesTarget != address(0), "FeeSplitter: INVALID_ROYALTIES_TARGET_ADDRESS");

    uint256 _totalWeights = totalWeights;

    _sendFees(_token, _amount, _totalWeights);
    _addShares(_royaltiesTarget, _computeShareCount(_amount, royaltiesWeight, _totalWeights), address(_token));
}
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 17, 2021
code423n4 added a commit that referenced this issue Nov 17, 2021
@adrien-supizet
Copy link
Collaborator


Before:
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
| FeeSplitter · sendFeesWithRoyalties · 74018 · 159518 · 121043 · 4 · - │

After:
| FeeSplitter · sendFeesWithRoyalties · 73923 · 159423 · 120948 · 4 · - │

@adrien-supizet adrien-supizet added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Nov 22, 2021
@maximebrugel maximebrugel self-assigned this Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants