From 81fdf8358f6fe7679cc4ddaf1e10a58944e310ec Mon Sep 17 00:00:00 2001 From: daveroga Date: Thu, 9 Jan 2025 17:31:45 +0100 Subject: [PATCH] fix error cyclomatic-complexity from solhint --- contracts/verifiers/Verifier.sol | 50 +++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/contracts/verifiers/Verifier.sol b/contracts/verifiers/Verifier.sol index 679e3c32..6ea8ed1b 100644 --- a/contracts/verifiers/Verifier.sol +++ b/contracts/verifiers/Verifier.sol @@ -249,27 +249,23 @@ abstract contract Verifier is IVerifier, ContextUpgradeable { revert GroupIdAlreadyExists(groupID); } - bool existingGroupID = false; - for (uint256 j = 0; j < newGroupsCount; j++) { - if (newGroupsGroupID[j] == groupID) { - newGroupsRequestCount[j]++; - existingGroupID = true; - break; - } - } - if (!existingGroupID) { + (bool exists, uint256 groupIDIndex) = _getGroupIDIndex( + groupID, + newGroupsGroupID, + newGroupsCount + ); + + if (!exists) { newGroupsGroupID[newGroupsCount] = groupID; newGroupsRequestCount[newGroupsCount]++; newGroupsCount++; + } else { + newGroupsRequestCount[groupIDIndex]++; } } } - for (uint256 i = 0; i < newGroupsCount; i++) { - if (newGroupsRequestCount[i] < 2) { - revert GroupMustHaveAtLeastTwoRequests(newGroupsGroupID[i]); - } - } + _checkGroupsRequestsCount(newGroupsGroupID, newGroupsRequestCount, newGroupsCount); // 2. Set requests checking groups for (uint256 i = 0; i < requests.length; i++) { @@ -290,6 +286,32 @@ abstract contract Verifier is IVerifier, ContextUpgradeable { } } + function _getGroupIDIndex( + uint256 groupID, + uint256[] memory groupList, + uint256 listCount + ) internal pure returns (bool, uint256) { + for (uint256 j = 0; j < listCount; j++) { + if (groupList[j] == groupID) { + return (true, j); + } + } + + return (false, 0); + } + + function _checkGroupsRequestsCount( + uint256[] memory groupList, + uint256[] memory groupRequestsList, + uint256 groupsCount + ) internal pure { + for (uint256 i = 0; i < groupsCount; i++) { + if (groupRequestsList[i] < 2) { + revert GroupMustHaveAtLeastTwoRequests(groupList[i]); + } + } + } + /** * @dev Gets a specific request by ID * @param requestId The ID of the request