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

Gas Optimizations #205

Open
code423n4 opened this issue May 24, 2022 · 1 comment
Open

Gas Optimizations #205

code423n4 opened this issue May 24, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

code423n4 commented May 24, 2022

Don't explicitly initialize variables with the default value

Uninitialized variables are assigned with the default value of their type, initializing a variable with its default value costs unnecessary gas.

Instances include :

mocks/balancer/MockFeeDistro.sol:19:        for (uint256 i = 0; i < _tokens.length; i++) {
mocks/balancer/MockFeeDistro.sol:38:        for (uint256 i = 0; i < tokens.length; i++) {
mocks/curve/MockCurveGauge.sol:36:        for (uint256 i = 0; i < reward_tokens.length; i++) {
AuraVestedEscrow.sol:99:        uint256 totalAmount = 0;
AuraVestedEscrow.sol:100:        for (uint256 i = 0; i < _recipient.length; i++) {
BalLiquidityProvider.sol:51:        for (uint256 i = 0; i < 2; i++) {
AuraLocker.sol:72:    uint256 public queuedCvxCrvRewards = 0;
AuraLocker.sol:174:            for (uint256 i = 0; i < rewardTokensLength; i++) {
AuraLocker.sol:381:        uint256 reward = 0;
AuraLocker.sol:485:        uint256 futureUnlocksSum = 0;
AuraLocker.sol:540:                    uint256 unlocksSinceLatestCkpt = 0;
AuraLocker.sol:630:        uint256 low = 0;
AuraLocker.sol:773:        for (uint256 i = 0; i < userRewardsLength; i++) {
AuraMerkleDrop.sol:29:    uint256 public pendingPenalty = 0;
ExtraRewardsDistributor.sol:231:        uint256 claimableTokens = 0;
AuraBalRewardPool.sol:35:    uint256 public pendingPenalty = 0;
AuraBalRewardPool.sol:38:    uint256 public periodFinish = 0;
AuraBalRewardPool.sol:39:    uint256 public rewardRate = 0;
AuraClaimZap.sol:143:        for (uint256 i = 0; i < rewardContracts.length; i++) {
AuraClaimZap.sol:147:        for (uint256 i = 0; i < extraRewardContracts.length; i++) {
AuraClaimZap.sol:151:        for (uint256 i = 0; i < tokenRewardContracts.length; i++) {
AuraVestedEscrow.sol:33:    bool public initialised = false;
AuraLocker.sol:114:    bool public isShutdown = false;

Recommendation

It is recommended to initialize variables without assigning them the default value, for example :

AuraVestedEscrow.sol:99:        uint256 totalAmount;
AuraVestedEscrow.sol:33:    bool public initialised;

Cache array length outside of for loop

Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop.

Instances include :

mocks/balancer/MockFeeDistro.sol:19:        for (uint256 i = 0; i < _tokens.length; i++) {
mocks/balancer/MockFeeDistro.sol:38:        for (uint256 i = 0; i < tokens.length; i++) {
mocks/curve/MockCurveGauge.sol:36:        for (uint256 i = 0; i < reward_tokens.length; i++) {
AuraVestedEscrow.sol:100:        for (uint256 i = 0; i < _recipient.length; i++) {
AuraLocker.sol:696:        for (uint256 i = nextUnlockIndex; i < locks.length; i++) {
AuraClaimZap.sol:143:        for (uint256 i = 0; i < rewardContracts.length; i++) {
AuraClaimZap.sol:147:        for (uint256 i = 0; i < extraRewardContracts.length; i++) {
AuraClaimZap.sol:151:        for (uint256 i = 0; i < tokenRewardContracts.length; i++) {

Recommendation

It is recommended to cache the array length on a variable before running the loop, then it doesn't need to read the length on every iteration, which cost gas, for example :

uint256 len = _tokens.length;
for (uint256 i = 0; i < len; i++) {

Use != 0 instead of > 0 when comparing unsigned integers

!= 0 will do the same as > 0 for unsigned integers, but != 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled.

Instances include :

mocks/curve/MockCurveVoteEscrow.sol:43:        require(amount > 0, "!amount");
mocks/curve/MockCurveVoteEscrow.sol:53:        require(lockAmounts[msg.sender] > 0, "Must have a lock");
mocks/curve/MockCurveVoteEscrow.sol:55:        require(amount > 0, "!amount");
mocks/curve/MockCurveVoteEscrow.sol:63:        require(lockAmounts[msg.sender] > 0, "Must have a lock");
AuraVestedEscrow.sol:118:        require(totalLocked[_recipient] > 0, "!funding");
BalLiquidityProvider.sol:57:            require(bal > 0 && bal == _request.maxAmountsIn[i], "!bal");
BalLiquidityProvider.sol:70:        require(balAfter > 0, "!mint");
AuraLocker.sol:210:        require(rewardData[_rewardsToken].lastUpdateTime > 0, "Reward does not exist");
AuraLocker.sol:259:        require(_amount > 0, "Cannot stake 0");
AuraLocker.sol:359:        require(amt > 0, "Nothing locked");
AuraLocker.sol:385:        require(length > 0, "no locks");
AuraLocker.sol:431:        require(locked > 0, "no exp locks");
AuraLocker.sol:471:        require(len > 0, "Nothing to delegate");
AuraLocker.sol:822:        require(_rewards > 0, "No reward");
AuraLocker.sol:851:        require(_reward > 0, "No reward");
AuraMerkleDrop.sol:122:        require(_amount > 0, "!amount");
Aura.sol:68:        require(_amount > 0, "Must mint something");
ExtraRewardsDistributor.sol:171:        require(_index > 0 && _index < rewardEpochs[_token].length - 1, "!past");
AuraBalRewardPool.sol:121:        require(_amount > 0, "RewardPool : Cannot stake 0");
AuraBalRewardPool.sol:139:        require(_amount > 0, "RewardPool : Cannot stake 0");
AuraBalRewardPool.sol:157:        require(amount > 0, "RewardPool : Cannot withdraw 0");
AuraBalRewardPool.sol:210:        require(rewardsAvailable > 0, "!balance");
AuraPenaltyForwarder.sol:52:        require(bal > 0, "!empty");

Recommendation

It is recommended to replace > 0 with != 0, as they do the same thing for unsigned integers, and '!= 0' costs less gas compared to > 0 in require statements with the optimizer enabled, also enable the optimizer.

For example :

mocks/curve/MockCurveVoteEscrow.sol:43:        require(amount != 0, "!amount");

If possible, use bitwise shift instead of division/multiplication

If the divisor/multiplier x is a power of 2, it can be calculated by shifting log2(x) to the right/left. Division with / cost more gas compared to bitwise shifting.

Instances include :

AuraMath.sol:36:        return (a / 2) + (b / 2) + (((a % 2) + (b % 2)) / 2);
AuraBalRewardPool.sol:183:                uint256 penalty = (reward * 2) / 10;

Recommendation

It is recommended to replace / and * with >> and << respectively and divisor/multiplier x to log2(x), for division/multiplication where the divisor/multiplier is a power of 2, for example :

AuraBalRewardPool.sol:183:                uint256 penalty = (reward << 1) / 10;

If possible, use prefix increment instead of postfix increment

Prefix increment ++i returns the updated value after it's incremented and postfix increment i++ returns the original value then increments it. Prefix increment costs less gas compared to postfix increment.

Instances includes :

mocks/balancer/MockFeeDistro.sol:19:        for (uint256 i = 0; i < _tokens.length; i++) {
mocks/balancer/MockFeeDistro.sol:38:        for (uint256 i = 0; i < tokens.length; i++) {
mocks/MockVoteStorage.sol:64:            len++;
mocks/curve/MockVoting.sol:23:            votesFor[voteId]++;
mocks/curve/MockVoting.sol:25:            votesAgainst[voteId]++;
mocks/curve/MockCurveGauge.sol:36:        for (uint256 i = 0; i < reward_tokens.length; i++) {
AuraVestedEscrow.sol:100:        for (uint256 i = 0; i < _recipient.length; i++) {
BalLiquidityProvider.sol:51:        for (uint256 i = 0; i < 2; i++) {
AuraLocker.sol:174:            for (uint256 i = 0; i < rewardTokensLength; i++) {
AuraLocker.sol:306:        for (uint256 i; i < rewardTokensLength; i++) {
AuraLocker.sol:410:            for (uint256 i = nextUnlockIndex; i < length; i++) {
AuraLocker.sol:426:                nextUnlockIndex++;
AuraLocker.sol:696:        for (uint256 i = nextUnlockIndex; i < locks.length; i++) {
AuraLocker.sol:702:                idx++;
AuraLocker.sol:773:        for (uint256 i = 0; i < userRewardsLength; i++) {
ExtraRewardsDistributor.sol:233:        for (uint256 i = epochIndex; i < tokenEpochs; i++) {
AuraClaimZap.sol:143:        for (uint256 i = 0; i < rewardContracts.length; i++) {
AuraClaimZap.sol:147:        for (uint256 i = 0; i < extraRewardContracts.length; i++) {
AuraClaimZap.sol:151:        for (uint256 i = 0; i < tokenRewardContracts.length; i++) {
AuraLocker.sol:497:                i--;
AuraLocker.sol:664:        for (uint256 i = locksLength; i > 0; i--) {
AuraLocker.sol:726:        for (uint256 i = epochIndex + 1; i > 0; i--) {

Recommendation

It is recommended to use prefix increment instead of postfix one when the return value is not needed, as both of them will give the same result and prefix increment costs less gas.

For example :

mocks/balancer/MockFeeDistro.sol:19:        for (uint256 i = 0; i < _tokens.length; ++i) {

Don't compare a boolean to a constant

Comparing a boolean to a constant costs more gas than directly checking the returned value of the boolean.

Instance include :

AuraMerkleDrop.sol:        require(hasClaimed[msg.sender] == false, "already claimed");

Recommendation

For example in an if statement, it is recommended to use if(directValue) instead of if(directValue == true) and if(!directValue) instead of if(directValue == false)

Changes :

AuraMerkleDrop.sol:        require(!hasClaimed[msg.sender], "already claimed");
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 24, 2022
code423n4 added a commit that referenced this issue May 24, 2022
@kartoonjoy
Copy link
Collaborator

Per warden, Kaiziron, the additional finding from Don't compare a boolean to a constant was to be appended to the gas report.

@0xMaharishi 0xMaharishi added the duplicate This issue or pull request already exists label May 27, 2022
@dmvt dmvt removed the duplicate This issue or pull request already exists label Jul 4, 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)
Projects
None yet
Development

No branches or pull requests

4 participants