TangibleTNFT / baskets-foundry

baskets-foundry
1 stars 0 forks source link

[BMR-03C] Inefficient On-Chain Sorting of Features #51

Closed chasebrownn closed 7 months ago

chasebrownn commented 7 months ago

BMR-03C: Inefficient On-Chain Sorting of Features

Type Severity Location
Gas Optimization BasketManager.sol:L337

Description:

The BasketManager::createHash function will sort the _features array on-chain which incurs a significant gas cost for the caller.

Example:

/**
 * @notice This method is used to create a unique hash given a category and list of sub categories.
 * @param _tnftType Category identifier.
 * @param _location ISO Code for country.
 * @param _features List of subcategories.
 */
function createHash(uint256 _tnftType, uint16 _location, uint256[] memory _features) public pure returns (bytes32 hashedFeatures) {
    hashedFeatures = keccak256(abi.encodePacked(_tnftType, _location, _features.sort()));
}

Recommendation:

As is standard across the DeFi space, we advise the code to instead expect the _features to already be sorted and to validate their sort order which is gas optimal and would not perform any mutations on the array.

chasebrownn commented 7 months ago

Fixed