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

1 stars 0 forks source link

Gas Optimizations #86

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. Title : Saving gas by removing = 0

This code can be saving more gas by removing = 0, it because If a variable was not set/initialized, it is assumed to have default value to 0

TOOLS USED

Manual Review

Mitigation Step

Remove = 0

  1. Title : using ++i than i++ for saving more gas

Using i++ instead ++i for all the loops, the variable i is incremented using i++. It is known that implementation by using ++i costs less gas per iteration than i++.

Tools Used

Manual Review

Occurances

Gravity.sol#L128 Gravity.sol#L233 Gravity.sol#L263 Gravity.sol#L453 Gravity.sol#L569 Gravity.sol#L579 Gravity.sol#L660

  1. Title : Title : change uint256 i = 0 into uint256 i for saving more gas

using this implementation can saving more gas for each loops.

Tool Used

Manual Review

Recommended Mitigation

Change it

Occurances

Gravity.sol#L128 Gravity.sol#L233 Gravity.sol#L263 Gravity.sol#L453 Gravity.sol#L569 Gravity.sol#L579 Gravity.sol#L660

  1. Title : Caching array length can saving more gas

This implementation can be saving more gas, since if caching the array length is more gas efficient. just because access to a local variable in solidity is more efficient.

Tool Used

Manual Review

Occurances

Gravity.sol#L128 Gravity.sol#L233 Gravity.sol#L263 Gravity.sol#L453 Gravity.sol#L569 Gravity.sol#L579 Gravity.sol#L660

  1. Title : Code can be shorter for saving more gas

This cumulativePowerImplementation can be used for saving more gas, instead of doube caching, it can be changed by using += instead.

POC

https://www.tutorialspoint.com/solidity/solidity_operators.htm

Tool Used

Manual Review, Remix

Recommended Mitigation

      cumulativePower = cumulativePower + _powers[i];

change to :

    cumulativePower += _powers[i];

Another Occurances

Gravity.sol#L244

  1. Title : Using short reason string can be used for saving more gas

Every reason string takes at least 32 bytes. Use short reason strings that fits in 32 bytes or it will become more expensive.

Tool Used

Manual Review

Occurances

Gravity.sol#L119 Gravity.sol#L256 Gravity.sol#L386 Gravity.sol#L492 Gravity.sol#L407 Gravity.sol#L496 Gravity.sol#L655 Gravity.sol#L668

  1. Value can be set as immutable for saving more gas

The linked variables assigned in the constructor can be declared as immutable. Immutable state variables can be assigned during contract creation but will remain constant throughout the lifetime of a deployed contract. A big advantage of immutable variables is that reading them is significantly cheaper than reading from regular state variables since they will not be stored in storage.

Tool Used

Manual Review