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

"constants" expressions are expressions, not constants. Use "immutable" instead. #26

Open
code423n4 opened this issue Jan 4, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue

Comments

@code423n4
Copy link
Contributor

Handle

Dravee

Vulnerability details

Impact

Due to how constant variables are implemented, an expression assigned to a constant variable is recomputed each time that the variable is used, which wastes some gas.

If the variable was immutable instead: the calculation would only be done once at deploy time (in the constructor), and then the result would be saved and read directly at runtime rather than being recalculated.

See: ethereum/solidity#9232

Proof of Concept

Here, the exponentiation operation is computed everytime the _pointsMultiplier variable is used:

XDEFIDistribution.sol:17:    uint256 internal constant _pointsMultiplier = uint256(2**128);

Tools Used

VS Code

Recommended Mitigation Steps

Change expressions that are constant to immutable and implement the calculation in the constructor

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jan 4, 2022
@deluca-mike deluca-mike added the invalid This doesn't seem right label Jan 5, 2022
@deluca-mike
Copy link
Collaborator

deluca-mike commented Jan 5, 2022

I have tested this (and you can do by changing it and rerunning the hardhat tests and comparing the gas reports) and this seems to be fixed in solidity now, so it is not valid in this specific case. At least is not in 0.8.10. Thankfully, because uint256(2**128) is more readable.

@deluca-mike deluca-mike added sponsor disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue and removed invalid This doesn't seem right labels Jan 12, 2022
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 disputed Sponsor cannot duplicate the issue, or otherwise disagrees this is an issue
Projects
None yet
Development

No branches or pull requests

2 participants