code-423n4 / 2022-02-concur-findings

2 stars 0 forks source link

Gas Optimizations #218

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Summary

During the code review, we found that few state variables that were declared as private could be marked as constant as they were not expected to be updated. Constant variables consume lesser gas than private variables. Also, we found a few functions that were declared as public but were never internally used. Such functions can always be marked as external instead of public as external functions consume lesser gas in comparison to public functions. It was also noticed that the ordering of variables in structs was in random order. Structs perform variable packing, and hence it is recommended to go from lower bytes to higher bytes as the lower bytes get packed together to save gas. The contract was also found to be using the safeMath library, which is redundant if version solidity compiler version 0.8.0 and above is used as it has enough built-in features to prevent buffer overflow attacks and perform safer arithmetic calculations. It is advised to remove the safeMath library as they are super expensive in terms of gas consumption.

Title:

State variables that could be declared constant

Description:

Constant state variables should be declared constant to save gas.

PoC:

The below variables concurPerBlock _concurShareMultiplier _perMille can be used as constants instead of private. https://github.com/code-423n4/2022-02-concur/blob/main/contracts/MasterChef.sol#L50 https://github.com/code-423n4/2022-02-concur/blob/main/contracts/MasterChef.sol#L56 https://github.com/code-423n4/2022-02-concur/blob/main/contracts/MasterChef.sol#L57

Suggested Fix:

concurPerBlock _concurShareMultiplier _perMille can be used as constants instead of private to save gas.

Title:

Functions that can be external instead of public

Description:

Public functions that are never called by a contract should be declared external in order to conserve gas.

Impact

Smart Contracts are required to have effective Gas usage as they cost real money, and each function should be monitored for the amount of gas it costs to make it gas efficient.

Public functions cost more Gas than external functions.

PoC:

The following functions can be declared external: https://github.com/code-423n4/2022-02-concur/blob/main/contracts/ConvexStakingWrapper.sol#L93-L118 https://github.com/code-423n4/2022-02-concur/blob/main/contracts/MasterChef.sol#L86-L101 https://github.com/code-423n4/2022-02-concur/blob/main/contracts/MasterChef.sol#L127-L132

Suggested Fix:

Use the external state visibility for functions that are never called from the contract.

Title:

Ordering of structs

Description:

Structs are packed, and arrangement matters for packing, which in turn affects the gas used. The ordering should be from lower to higher space consumption so packing can be done.

PoC:

Suggested Fix:

Use the ordering as below, switching depositFeeBP to 2nd place instead of last.

IERC20 depositToken; // Address of LP token contract. uint16 depositFeeBP; // Deposit fee in basis points uint allocPoint; // How many allocation points assigned to this pool. to distribute per block. uint lastRewardBlock; // Last block number that distribution occurs. uint accConcurPerShare; // Accumulated per share, times multiplier. See below.

Title:

Use of Safemath

Description:

Safemath module is considered very gas expensive. It was useful before solidity 0.8.0. After the 0.8.0 version, solidity auto handles buffer overflow, and hence safeMath can be avoided.

PoC:

The MasterChef.sol file is using safeMath which is https://github.com/code-423n4/2022-02-concur/blob/main/contracts/MasterChef.sol#L10

Suggested Fix:

Avoid using safeMath module to save gas

GalloDaSballo commented 2 years ago

State variables that could be declared constant

3 * 2100 = 6300

Functions that can be external instead of public

Will not save gas

Use the external state visibility for functions that are never called from the contract.

Will not save gas

Ordering of structs

Valid but no poc on gas saved so 0

Use of Safemath

No deets on how this save gas

Total Gas saved 6300

Would recommend the warden to check their report on a MD previewer to make it look slightly better