Open code423n4 opened 2 years ago
Duplicate with previous similar gas optimization tips
003 is not correct, this "myth" emerged from old contests and we busted it
Very basic submission, I fell like these findings are a little too generic and compared to other warden's work there was more to be found
2/10
C4-001 : Upgrade pragma to at least 0.8.4
Impact - Gas Optimization
The advantages of versions 0.8.* over <0.8.0 are:
Proof of Concept
All Contracts
Tools Used
None
Recommended Mitigation Steps
Consider to upgrade pragma to at least 0.8.4.
C4-002 : # ++i is more gas efficient than i++ in loops forwarding
Impact
++i is more gas efficient than i++ in loops forwarding.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
It is recommend to use unchecked{++i} and change i declaration to uint256.
C4-003 : # Use of constant keccak variables results in extra hashing (and so gas).
Impact
That would Increase gas costs on all privileged operations.
Proof of Concept
The following role variables are marked as constant.
This results in the keccak operation being performed whenever the variable is used, increasing gas costs relative to just storing the output hash. Changing to immutable will only perform hashing on contract deployment which will save gas.
See: ethereum/solidity#9232 (https://github.com/ethereum/solidity/issues/9232#issuecomment-646131646)
Tools Used
Code Review
Recommended Mitigation Steps
Consider to change the variable to be immutable rather than constant.
C4-004 : # Cache array length in for loops can save gas
Impact
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Proof of Concept
https://github.com/code-423n4/2022-02-redacted-cartel/blob/main/contracts/BribeVault.sol#L269
https://github.com/code-423n4/2022-02-redacted-cartel/blob/main/contracts/BribeVault.sol#L269
Tools Used
Code Review
Recommended Mitigation Steps
Consider caching array call data elements in the for loop.