code-423n4 / 2022-05-velodrome-findings

0 stars 0 forks source link

Gas Optimizations #39

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Use ++index instead of index++ to increment a loop counter

Context: VotingEscrow.sol#L1139-L1151, VotingEscrow.sol#L1184-L1199, VotingEscrow.sol#L1209-L1258 (For both loops), VotingEscrow.sol#L1278-L1333 (For all 3), Pair.sol#L254-L261, Pair.sol#L388-L410, Router.sol#L86-L96, Router.sol#L315-L325, VelodromeLibrary.sol#L23-L45, RewardsDistributor.sol#L64-L95, RewardsDistributor.sol#L102-L116, RewardsDistributor.sol#L118-L132, RewardsDistributor.sol#L148-L163, RewardsDistributor.sol#L169-L224, RewardsDistributor.sol#L226-L275, RewardsDistributor.sol#L294-L315, Minter.sol#L49-L62, Gauge.sol#L173-L186, Gauge.sol#L347-L372, Gauge.sol#L390-L417, Gauge.sol#L424-L430, Gauge.sol#L432-L469, Gauge.sol#L472-L505, Voter.sol#L72-L80, Voter.sol#L98-L121, Voter.sol#L123-L133, Voter.sol#L135-L170 (For Both), Voter.sol#L265-L269, Voter.sol#L271-L275, Voter.sol#L303-L307, Voter.sol#L309-L313, Voter.sol#L339-L343, Voter.sol#L345-L349

Description: Due to reduced stack operations, using ++index saves 5 gas per iteration.

Recommendation: Use ++indexto increment a loop counter.

Catching The Array Length Prior To Loop

Context: VotingEscrow.sol#L1139-L1151, VotingEscrow.sol#L1184-L1199, VotingEscrow.sol#L1209-L1258 (For both loops), VotingEscrow.sol#L1278-L1333 (For L1295 && L1320), Pair.sol#L254-L261, Router.sol#L86-L96, Router.sol#L315-L325, RewardsDistributor.sol#L294-L315, Minter.sol#L49-L62, Gauge.sol#L347-L372, Voter.sol#L72-L80, Voter.sol#L265-L269, Voter.sol#L303-L307, Voter.sol#L309-L313, Voter.sol#L345-L349

Description: One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables.

Recommendation: Simply do something like so before the for loop: uint length = variable.length. Then add length in place of variable.length in the for loop.

The Increment In For Loop Post Condition Can Be Made Unchecked

Context: VotingEscrow.sol#L1139-L1151, VotingEscrow.sol#L1184-L1199, VotingEscrow.sol#L1209-L1258 (For both loops), VotingEscrow.sol#L1278-L1333 (For all 3), Pair.sol#L254-L261, Pair.sol#L388-L410, Router.sol#L86-L96, Router.sol#L315-L325, VelodromeLibrary.sol#L23-L45, RewardsDistributor.sol#L64-L95, RewardsDistributor.sol#L102-L116, RewardsDistributor.sol#L118-L132, RewardsDistributor.sol#L148-L163, RewardsDistributor.sol#L169-L224, RewardsDistributor.sol#L226-L275, RewardsDistributor.sol#L294-L315, Minter.sol#L49-L62, Gauge.sol#L173-L186, Gauge.sol#L347-L372, Gauge.sol#L390-L417, Gauge.sol#L424-L430, Gauge.sol#L432-L469, Gauge.sol#L472-L505, Voter.sol#L72-L80, Voter.sol#L98-L121, Voter.sol#L123-L133, Voter.sol#L135-L170 (For Both), Voter.sol#L265-L269, Voter.sol#L271-L275, Voter.sol#L303-L307, Voter.sol#L309-L313, Voter.sol#L339-L343, Voter.sol#L345-L349

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; i = unchecked_inc(i)) {
    // do something that doesn't change the value of i
}

function unchecked_inc(uint i) returns (uint) {
    unchecked {
        return i + 1;
    }
}

Note that it’s important that the call to unchecked_inc is inlined. This is only possible for solidity versions starting from 0.8.2.

Recommendation: The increment in the for loop post condition can be made unchecked.

In require(), Use != 0 Instead of > 0 With Uint Values

Context: VotingEscrow.sol#L769-L776 (For L772 && L773), VotingEscrow.sol#L769-L776 (For L785), VotingEscrow.sol#L814-L824 (For L820), VotingEscrow.sol#L828-L840 (For L835), Pair.sol#L289-L308 (For L303), Pair.sol#L312-L331 (For L322), Pair.sol#L334-L366 (For L336 && L353), Pair.sol#L521-L526 (For L522), PairFees.sol#L19-L24 (For L20), Router.sol#L57-L61 (For L58 && L59), Router.sol#L409-L414 (For L410), Router.sol#L415-L421 (For L417), Gauge.sol#L511-L542 (For L512), Gauge.sol#L590-L624 (For L592 && L613), Gauge.sol#L664-L669 (For L665), Gauge.sol#L671-L676 (For L672), Gauge.sol#L678-L683 (For L679), Bribe.sol#L41-L60 (For L42), Bribe.sol#L92-L97 (For L93), Bribe.sol#L99-L104 (For L100), Voter.sol#L351-L356 (For L352)

Description: In a require, when checking a uint, using != 0 instead of > 0 saves 6 gas. This will jump over or avoid an extra ISZERO opcode.

Recommendation: Use != 0 instead of > 0 with uint values but only in require() statements.

Setting The Constructor To Payable

Context: All Contracts

Description: You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0 and saves 21 gas on deployment with no security risks.

Recommendation: Set the constructor to payable.

Function Ordering via Method ID

Context: All Contracts

Description: Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.

Recommendation: Find a lower method ID name for the most called functions for example mostCalled() vs. mostCalled_41q() is cheaper by 44 gas.

GalloDaSballo commented 2 years ago

Function Ordering via Method ID

This finding doesn't help unless you gas golf and provide a recommended solution for the sponsor to implement

Setting The Constructor To Payable

It is valid but won't save gas for end users

In require(), Use != 0 Instead of > 0 With Uint Values

No longer valid (>= 0.8.12)

The Increment In For Loop Post Condition Can Be Made Unchecked

Valid and saves 20 gas per instance

Catching The Array Length Prior To Loop

Saves 6 gas per instance

17 * 6 = 102

Use ++index instead of index++ to increment a loop counter

Saves 5 gas per instance

37 * 5 = 185

All in all a pretty decent format, the report is missing those juicy storage savings that give way bigger savings

Only note of advice is I'd list the count of the instances to make it easy to know how many you found, also adding an estimate of the gas saved will help make the report easier to scan.

Total Gas Saved: 307