Platform: Code4rena
Start Date: 04/01/2022
Pot Size: $25,000 USDC
Total HM: 3
Participants: 40
Period: 3 days
Judge: Ivo Georgiev
Total Solo HM: 1
Id: 75
League: ETH
Rank: 15/40
Findings: 1
Award: $258.94
🌟 Selected for report: 3
🚀 Solo Findings: 0
27.0627 USDC - $27.06
defsec
!= 0
is a cheaper operation compared to > 0
, when dealing with uint.
https://github.com/XDeFi-tech/xdefi-distribution/blob/master/contracts/XDEFIDistribution.sol#L145
Code Review
Use "!=0" instead of ">0" for the gas optimization.
#0 - deluca-mike
2022-01-05T19:28:19Z
This is not always true, but still valid point that it is worth testing and checking. In any case, require
s wil be converted into if-revert
s with custom error messages to that will now become: if (totalUnitsCached == uint256(0)) revert NoUnitSupply();
#1 - deluca-mike
2022-01-14T05:04:38Z
For what it's worth, there are no more such issues in the release candidate contracts.
🌟 Selected for report: Dravee
Also found by: Czar102, TomFrenchBlockchain, defsec
18.2673 USDC - $18.27
defsec
In some cases, having function arguments in calldata instead of memory is more optimal.
Consider the following generic example:
contract C { function add(uint[] memory arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }
In the above example, 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. Consider the following snippet instead:
contract C { function add(uint[] calldata arr) external returns (uint sum) { uint length = arr.length; for (uint i = 0; i < arr.length; i++) { sum += arr[i]; } } }
In the above snippet, instead of going via memory, the value is directly read from calldata using calldataload. That is, there are no intermediate memory operations that carries this value.
Gas savings: In the former example, the ABI decoding begins with copying value from calldata to memory in a for loop. Each iteration would cost at least 60 gas. In the latter example, this can be completely avoided. This will also reduce the number of instructions and therefore reduces the deploy time cost of the contract.
In short, use calldata instead of memory if the function argument is only read.
Note that in older Solidity versions, changing some function arguments from memory to calldata may cause "unimplemented feature error". This can be avoided by using a newer (0.8.*) Solidity compiler.
Examples Note: The following pattern is prevalent in the codebase:
function f(bytes memory data) external { (...) = abi.decode(data, (..., types, ...)); }
Here, changing to bytes calldata will decrease the gas. The total savings for this change across all such uses would be quite significant.
Examples:
https://github.com/XDeFi-tech/xdefi-distribution/blob/master/contracts/XDEFIDistribution.sol#L325
None
Change memory definition with calldata.
#0 - deluca-mike
2022-01-08T00:05:40Z
Yup, good catch. Will do.
#1 - deluca-mike
2022-01-09T11:05:34Z
Duplicate #29
🌟 Selected for report: defsec
100.2321 USDC - $100.23
defsec
For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
https://github.com/XDeFi-tech/xdefi-distribution/blob/master/contracts/XDEFIDistribution.sol#L274
None
Consider applying unchecked arithmetic where overflow/underflow is not possible.
#0 - deluca-mike
2022-01-08T01:44:41Z
Agreed, we'll do unchecked
where possible.
#1 - deluca-mike
2022-01-14T04:26:11Z
In the release candidate XDEFIDistribution
contract and release candidate XDEFIDistributionHelper
contract, unchecked math is now used throughout.
Specifically for this issue, expiry: uint32(block.timestamp + duration_)
is now done in an unchecked block.
#2 - Ivshti
2022-01-16T06:30:23Z
valid finding & resolved
🌟 Selected for report: defsec
100.2321 USDC - $100.23
defsec
Lower than uint256 size storage instance variables are actually less gas efficient. E.g. using uint32 does not give any efficiency, actually, it is the opposite as EVM operates on default of 256-bit values so uint32 is more expensive in this case as it needs a conversion. It only gives improvements in cases where you can pack variables together, e.g. structs.
https://github.com/XDeFi-tech/xdefi-distribution/blob/master/contracts/XDEFIDistribution.sol#L301
None
Consider to review all uint types. Change them with uint256 If the integer is not necessary to present with uint32.
#0 - deluca-mike
2022-01-06T06:26:45Z
Good catch. Will stop using integers with less than 256 bits where possible, throughout contract.
#1 - deluca-mike
2022-01-14T03:35:57Z
You can see that the release candidate contract only uses uint256
now, aside from in structs, where compaction matters.
#2 - Ivshti
2022-01-16T06:31:31Z
valid finding & resolved
13.1525 USDC - $13.15
defsec
There is a function declared as public that are never called internally within the contract. It is best practice to mark such functions as external instead, as this saves gas (especially in the case where the function takes arguments, as external functions can read arguments directly from calldata instead of having to allocate memory).
https://github.com/XDeFi-tech/xdefi-distribution/blob/6021c9a89bad4adf17eaba4ea6b3697793d9d7c6/contracts/XDEFIDistributionHelper.sol#L11 https://github.com/XDeFi-tech/xdefi-distribution/blob/6021c9a89bad4adf17eaba4ea6b3697793d9d7c6/contracts/XDEFIDistributionHelper.sol#L20
Code Review
All of the public functions in the contract are not called internally, so access can be changed to external to reduce gas.
#0 - deluca-mike
2022-01-05T19:30:10Z
Incorrect with respect to line 11 (getAllTokensForAccount
) since it is used by getAllLockedPositionsForAccount
, but correct about line 20 (getAllLockedPositionsForAccount
). Will fix.
#1 - deluca-mike
2022-01-09T10:36:38Z
Duplicate #6