From dafd88780bc00b62bf4e4a59c651add7ef1caecc Mon Sep 17 00:00:00 2001 From: Aleksandr Tarelkin Date: Fri, 6 Dec 2024 20:45:57 +0300 Subject: [PATCH] hashConsensus gas optimization --- contracts/committees/HashConsensus.sol | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/contracts/committees/HashConsensus.sol b/contracts/committees/HashConsensus.sol index 2643c4f6..387616d7 100644 --- a/contracts/committees/HashConsensus.sol +++ b/contracts/committees/HashConsensus.sol @@ -250,7 +250,9 @@ abstract contract HashConsensus is Ownable { /// @param newMembers The array of addresses to be added as new members. /// @param executionQuorum The minimum number of members required for executing certain operations. function _addMembers(address[] memory newMembers, uint256 executionQuorum) internal { - for (uint256 i = 0; i < newMembers.length; ++i) { + uint256 membersCount = newMembers.length; + + for (uint256 i = 0; i < membersCount; ++i) { if (newMembers[i] == address(0)) { revert InvalidMemberAccount(newMembers[i]); } @@ -270,7 +272,9 @@ abstract contract HashConsensus is Ownable { /// @param membersToRemove The array of addresses to be removed from the members list. /// @param executionQuorum The updated minimum number of members required for executing certain operations. function _removeMembers(address[] memory membersToRemove, uint256 executionQuorum) internal { - for (uint256 i = 0; i < membersToRemove.length; ++i) { + uint256 membersCount = membersToRemove.length; + + for (uint256 i = 0; i < membersCount; ++i) { if (!_members.remove(membersToRemove[i])) { revert AccountIsNotMember(membersToRemove[i]); } @@ -285,7 +289,9 @@ abstract contract HashConsensus is Ownable { /// @param hash The hash to check /// @return support The number of votes in support of the hash function _getSupport(bytes32 hash) internal view returns (uint256 support) { - for (uint256 i = 0; i < _members.length(); ++i) { + uint256 membersCount = _members.length(); + + for (uint256 i = 0; i < membersCount; ++i) { if (approves[_members.at(i)][hash]) { support++; }