Platform: Code4rena
Start Date: 04/05/2022
Pot Size: $50,000 DAI
Total HM: 24
Participants: 71
Period: 5 days
Judge: Justin Goro
Total Solo HM: 14
Id: 119
League: ETH
Rank: 58/71
Findings: 1
Award: $59.77
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: IllIllI
Also found by: 0x1f8b, 0xNazgul, 0xYamiDancho, 0xf15ers, 0xkatana, ACai, CertoraInc, Dravee, Funen, GimelSec, Hawkeye, PPrieditis, Picodes, Ruhum, TerrierLover, Tomio, VAD37, Waze, csanuragjain, defsec, delfin454000, eccentricexit, ellahi, fatherOfBlocks, gzeon, hansfriese, horsefacts, ilan, joestakey, juicy, minhquanym, oyc_109, rajatbeladiya, reassor, rfa, robee, samruna, simon135, z3s
59.7678 DAI - $59.77
++index
instead of index++
to increment a loop counterContext: PermissionlessBasicPoolFactory.sol#L92-L132
, PermissionlessBasicPoolFactory.sol#L137-L149
, PermissionlessBasicPoolFactory.sol#L156-L173
, PermissionlessBasicPoolFactory.sol#L209-L237
, PermissionlessBasicPoolFactory.sol#L242-L256
, PermissionlessBasicPoolFactory.sol#L261-L272
Description:
Due to reduced stack operations, using ++index
saves 5 gas per iteration.
Recommendation:
Use ++index
to increment a loop counter.
Context: PermissionlessBasicPoolFactory.sol#L92-L132
, PermissionlessBasicPoolFactory.sol#L137-L149
, PermissionlessBasicPoolFactory.sol#L156-L173
, PermissionlessBasicPoolFactory.sol#L209-L237
, PermissionlessBasicPoolFactory.sol#L242-L256
, PermissionlessBasicPoolFactory.sol#L261-L272
, MerkleLib.sol#L17-L29
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: PermissionlessBasicPoolFactory.sol#L137-L149
, PermissionlessBasicPoolFactory.sol#L209-L237 (For L225, L226 and L229)
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: MerkleResistor.sol#L169-L206
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: PermissionlessBasicPoolFactory.sol#L92-L132
, MerkleLib.sol#L17-L29
, MerkleDropFactory.sol#L88-L109
, MerkleIdentity.sol#L116-L135
, MerkleIdentity.sol#L152-L155
, MerkleIdentity.sol#L163-L166
, MerkleEligibility.sol#L70-L78
, MerkleEligibility.sol#L85-L94
, VoterID.sol#L122-L144
, VoterID.sol#L215-L218
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: PermissionlessBasicPoolFactory.sol
, MerkleIdentity.sol
, MerkleEligibility.sol
, VoterID.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.
#0 - illuzen
2022-05-10T05:44:10Z
Good to know, but this is an incredibly small amount of gas.
#1 - illuzen
2022-05-12T08:23:00Z
all duplicates