Platform: Code4rena
Start Date: 03/05/2022
Pot Size: $50,000 USDC
Total HM: 4
Participants: 46
Period: 5 days
Judge: gzeon
Total Solo HM: 2
Id: 117
League: ETH
Rank: 13/46
Findings: 2
Award: $383.36
🌟 Selected for report: 0
🚀 Solo Findings: 0
The IChainlinkAggregator
implementation utilized by the codebase is outdated and as such is not properly sanitized.
While the interface
itself needs to be updated in its dedicated finding, the code utilizing it should also be updated to properly sanitize the values returned by the latestRoundData
call. Namely, the answeredInRound
value should be within a project-specified delta threshold from roundID
to ensure data is not stale.
#0 - bunkerfinance-dev
2022-05-07T21:58:56Z
Duplicate of #1
🌟 Selected for report: BowTiedWardens
Also found by: 0v3rf10w, 0x1f8b, 0x4non, 0xDjango, 0xNazgul, 0xkatana, Cityscape, Fitraldys, Funen, GimelSec, IllIllI, MaratCerby, Picodes, TerrierLover, Tomio, delfin454000, ellahi, fatherOfBlocks, hansfriese, ilan, joestakey, oyc_109, rfa, robee, samruna, simon135, slywaters, throttle
84.784 USDC - $84.78
++index
instead of index++
to increment a loop counterContext: CEther.sol#L170-L189
, Comptroller.sol#L115-L126
, Comptroller.sol#L167-L217
, Comptroller.sol#L580-L647
, Comptroller.sol#L927-L932
, Comptroller.sol#L1222-L1243 (For all 4)
,
Description:
Due to reduced stack operations, using ++index
saves 5 gas per iteration.
Recommendation:
Use ++index
to increment a loop counter.
Context: ERC1155Enumerable.sol#L35-L70
, CEther.sol#L170-L189
, Comptroller.sol#L580-L647
, Comptroller.sol#L927-L932
, Comptroller.sol#L927-L932
, Comptroller.sol#L1222-L1243 (For all 4)
, UniswapV2PriceOracle.sol#L40-L48
, CNftPriceOracle.sol#L58-L74
,
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.
arr[i] = arr[i] + 1
is cheaper than arr[i] += 1
Context: CNft.sol#L36-L81
, CNft.sol#L84-L109
, CNft.sol#L112-L158
, CNft.sol#L166-L188
,
Description: Due to stack operations this is 25 gas cheaper when dealing with arrays in storage, and 4 gas cheaper for memory arrays.
Recommendation:
Use arr[i] = arr[i] + 1
instead of arr[i] += 1
when dealing with arrays
require()
, Use != 0
Instead of > 0
With Uint ValuesContext: UniswapV2PriceOracle.sol#L52-L75
, UniswapV2PriceOracle.sol#L78-L99
, UniswapV2PriceOracle.sol#L102-L123
, UniswapV2PriceOracle.sol#L126-L144
, UniswapV2PriceOracle.sol#L147-L165
, CNftPriceOracle.sol#L58-L74
,
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.
calldata
Instead of memory
For Function ParametersContext: Comptroller.sol#L115-L126
, Comptroller.sol#L1209-L1213
, Comptroller.sol#L1222-L1243 (For both)
,
Description: The dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient.
Recommendation:
Use calldata
instead of memory
for function parameters to avoid using memory with array values whena function is getting called externally.
Context: PriceOracleImplementation.sol
, CEther.sol
, Comptroller.sol
, CNftPriceOracle.sol
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.
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.
Context: All Contracts
Description: Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks for free!
The advantages of versions =0.8.*= over =<0.8.0= are:
Low level inliner
from =0.8.2=, leads to cheaper runtime gas.
Especially relevant when the contract has small functions. For
example, OpenZeppelin libraries typically have a lot of small
helper functions and if they are not inlined, they cost an
additional 20 to 40 gas because of 2 extra =jump= instructions and
additional stack operations needed for function calls.Optimizer improvements in packed structs
: Before =0.8.3=, storing
packed structs, in some cases used an additional storage read
operation. After EIP-2929
, if the slot was already cold, this
means unnecessary stack operations and extra deploy time costs.
However, if the slot was already warm, this means additional cost
of =100= gas alongside the same unnecessary stack operations and
extra deploy time costs.Custom errors
from =0.8.4=, leads to cheaper deploy time cost and
run time cost. Note: the run time cost is only relevant when the
revert condition is met. In short, replace revert strings by
custom errors.Recommendation: Upgrade to at least 0.8.4 for the additional benefits.