code-423n4 / 2022-10-zksync-findings

3 stars 0 forks source link

Gas Optimizations #349

Closed code423n4 closed 1 year ago

code423n4 commented 1 year ago

[NAZ-G1] The Increment In For Loop Post Condition Can Be Made Unchecked

Context: Diamond.sol#L94, Diamond.sol#L132, Diamond.sol#L153, Diamond.sol#L173

Description: (This is only relevant if you are using the default solidity checked arithmetic). i++ involves checked arithmetic, which is not required. This is because the value of i is always strictly less than length <= 2256 - 1. Therefore, the theoretical maximum value of i to enter the for-loop body is `2256 - 2. This means that thei++` in the for loop can never overflow. Regardless, the overflow checks are performed by the compiler.

Unfortunately, the Solidity optimizer is not smart enough to detect this and remove the checks. One can manually do this by:

for (uint i = 0; i < length; ) {
    // do something that doesn't change the value of i
    unchecked {
        ++i;
    }
}

Recommendation: Consider doing the increment in the for loop post condition in an unchecked block.

[NAZ-G2] Rearanging Setters To Save gas

Context: Governance.sol#L54, Governance.sol#L69, Governance.sol#L94, Governance.sol#L104

Description: Moving the event before setting the new value can save gas EX:

contract UnOptimized {
    uint256 public value;

    event ValueUpdated(uint256 oldMinTarget, uint256 newMinTargetVotes);

    function updateValue(uint256 newValue) external {
        uint256 oldValue = value;
        value = newValue;

        emit ValueUpdated(oldValue, newValue);
    }
}

contract Optimized {
    uint256 public value;

    event ValueUpdated(uint256 oldValue, uint256 newValue);

    function updateValue(uint256 newValue) external {
        emit ValueUpdated(value, newValue);
        value = newValue;
    }
}

Recommendation: Consider Doing a similar code structure to save gas for setters.

GalloDaSballo commented 1 year ago

[NAZ-G1] The Increment In For Loop Post Condition Can Be Made Unchecked

20 each

80

[NAZ-G2] Rearanging Setters To Save gas

13 gas per instance 52

Good format but I think it's not enough savings

132

c4-judge commented 1 year ago

GalloDaSballo marked the issue as grade-c