code-423n4 / 2022-02-redacted-cartel-findings

0 stars 0 forks source link

Gas Optimizations #100

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

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

  1. The contest repository contracts contain floating pragma ^0.8.0. The contracts pragma version can be updated to 0.8.4 for the gas optimization.

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

  1. Navigate to the following contracts.
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/RewardDistributor.sol#L82

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.


https://github.com/code-423n4/2022-02-redacted-cartel/blob/main/contracts/TokemakBribe.sol#L40

https://github.com/code-423n4/2022-02-redacted-cartel/blob/main/contracts/BribeVault.sol#L27

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

  1. Navigate to the following smart contract line.

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.

drahrealm commented 2 years ago

Duplicate with previous similar gas optimization tips

GalloDaSballo commented 2 years ago

003 is not correct, this "myth" emerged from old contests and we busted it

GalloDaSballo commented 2 years ago

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

GalloDaSballo commented 2 years ago

2/10