From 4bf7a80b04860bc0e66097062fd78a6ca9bf388c Mon Sep 17 00:00:00 2001 From: Aleksandr Tarelkin Date: Fri, 6 Dec 2024 20:41:50 +0300 Subject: [PATCH] hashConsensus gas optimization --- contracts/committees/HashConsensus.sol | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/contracts/committees/HashConsensus.sol b/contracts/committees/HashConsensus.sol index 2643c4f6..ec370ca7 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,8 @@ 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 +288,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++; }