Gravita-Protocol / Gravita-SmartContracts

GNU General Public License v3.0
48 stars 31 forks source link

[SPL-01C] Optimization of Duplicate Entry Sanitization #381

Closed 0xfornax closed 1 year ago

0xfornax commented 1 year ago

SPL-01C: Optimization of Duplicate Entry Sanitization

Type Severity Location
Gas Optimization StabilityPool.sol:L590-L596

Description:

The referenced code block is meant to validate that no duplicates are present in the _assets array by iterating it in a nested loop.

Example:

unchecked {
    for (uint256 i = 0; i < assetsLen; i++) {
        for (uint256 j = i + 1; j < assetsLen; j++) {
            if (_assets[i] == _assets[j]) {
                revert StabilityPool__DuplicateElementOnArray();
            }
        }
    }
}

Recommendation:

We advise the code to instead follow a different approach, mandating that the address values within _assets are ordered in a strictly ascending / descending order.

Given that all address values are unique and they represent a "value", it is possible to ascertain that the _assets array contains no duplicates in one loop by ensuring that the caller of the function has supplied the assets in a strictly ascending / descending order.

0xfornax commented 1 year ago

Fixed.