diff --git a/contracts/libraries/ClusterLib.sol b/contracts/libraries/ClusterLib.sol index 7fe492f6..6f4043d2 100644 --- a/contracts/libraries/ClusterLib.sol +++ b/contracts/libraries/ClusterLib.sol @@ -116,10 +116,8 @@ library ClusterLib { StorageData storage s, StorageProtocol storage sp ) internal { - (uint64 clusterIndex, uint64 burnRate) = OperatorLib.updateClusterOperators( + (uint64 clusterIndex, uint64 burnRate) = OperatorLib.updateClusterOperatorsOnRegistration( operatorIds, - true, - true, validatorCountDelta, s, sp diff --git a/contracts/libraries/OperatorLib.sol b/contracts/libraries/OperatorLib.sol index e0302356..17e4c3bd 100644 --- a/contracts/libraries/OperatorLib.sol +++ b/contracts/libraries/OperatorLib.sol @@ -34,10 +34,8 @@ library OperatorLib { if (operator.owner != msg.sender) revert ISSVNetworkCore.CallerNotOwner(); } - function updateClusterOperators( + function updateClusterOperatorsOnRegistration( uint64[] memory operatorIds, - bool isRegisteringValidator, - bool increaseValidatorCount, uint32 deltaValidatorCount, StorageData storage s, StorageProtocol storage sp @@ -47,50 +45,66 @@ library OperatorLib { for (uint256 i; i < operatorsLength; ) { uint64 operatorId = operatorIds[i]; - if (!isRegisteringValidator) { - ISSVNetworkCore.Operator storage operator = s.operators[operatorId]; - - if (operator.snapshot.block != 0) { - updateSnapshotSt(operator); - if (!increaseValidatorCount) { - operator.validatorCount -= deltaValidatorCount; - } else if ((operator.validatorCount += deltaValidatorCount) > sp.validatorsPerOperatorLimit) { - revert ISSVNetworkCore.ExceedValidatorLimit(); - } - - cumulativeFee += operator.fee; - } - cumulativeIndex += operator.snapshot.index; - } else { - if (i + 1 < operatorsLength) { - if (operatorId > operatorIds[i + 1]) { - revert ISSVNetworkCore.UnsortedOperatorsList(); - } else if (operatorId == operatorIds[i + 1]) { - revert ISSVNetworkCore.OperatorsListNotUnique(); - } + if (i + 1 < operatorsLength) { + if (operatorId > operatorIds[i + 1]) { + revert ISSVNetworkCore.UnsortedOperatorsList(); + } else if (operatorId == operatorIds[i + 1]) { + revert ISSVNetworkCore.OperatorsListNotUnique(); } - ISSVNetworkCore.Operator memory operator = s.operators[operatorId]; + } + ISSVNetworkCore.Operator memory operator = s.operators[operatorId]; - if (operator.snapshot.block == 0) { - revert ISSVNetworkCore.OperatorDoesNotExist(); - } - if (operator.whitelisted) { - address whitelisted = s.operatorsWhitelist[operatorId]; - if (whitelisted != address(0) && whitelisted != msg.sender) { - revert ISSVNetworkCore.CallerNotWhitelisted(); - } + if (operator.snapshot.block == 0) { + revert ISSVNetworkCore.OperatorDoesNotExist(); + } + if (operator.whitelisted) { + address whitelisted = s.operatorsWhitelist[operatorId]; + if (whitelisted != address(0) && whitelisted != msg.sender) { + revert ISSVNetworkCore.CallerNotWhitelisted(); } + } + + updateSnapshot(operator); + if ((operator.validatorCount += deltaValidatorCount) > sp.validatorsPerOperatorLimit) { + revert ISSVNetworkCore.ExceedValidatorLimit(); + } + + cumulativeFee += operator.fee; + cumulativeIndex += operator.snapshot.index; + + s.operators[operatorId] = operator; + + unchecked { + ++i; + } + } + } + + function updateClusterOperators( + uint64[] memory operatorIds, + bool increaseValidatorCount, + uint32 deltaValidatorCount, + StorageData storage s, + StorageProtocol storage sp + ) internal returns (uint64 cumulativeIndex, uint64 cumulativeFee) { + uint256 operatorsLength = operatorIds.length; + + for (uint256 i; i < operatorsLength; ) { + uint64 operatorId = operatorIds[i]; + + ISSVNetworkCore.Operator storage operator = s.operators[operatorId]; - updateSnapshot(operator); - if ((operator.validatorCount += deltaValidatorCount) > sp.validatorsPerOperatorLimit) { + if (operator.snapshot.block != 0) { + updateSnapshotSt(operator); + if (!increaseValidatorCount) { + operator.validatorCount -= deltaValidatorCount; + } else if ((operator.validatorCount += deltaValidatorCount) > sp.validatorsPerOperatorLimit) { revert ISSVNetworkCore.ExceedValidatorLimit(); } cumulativeFee += operator.fee; - cumulativeIndex += operator.snapshot.index; - - s.operators[operatorId] = operator; } + cumulativeIndex += operator.snapshot.index; unchecked { ++i; @@ -168,7 +182,7 @@ library OperatorLib { StorageData storage s ) internal { checkOwner(s.operators[operatorId]); - + address currentWhitelisted = s.operatorsWhitelist[operatorId]; // operator already whitelisted? EOA or generic contract diff --git a/contracts/modules/SSVClusters.sol b/contracts/modules/SSVClusters.sol index 8d60a5d8..f8053ecf 100644 --- a/contracts/modules/SSVClusters.sol +++ b/contracts/modules/SSVClusters.sol @@ -104,7 +104,7 @@ contract SSVClusters is ISSVClusters { if (cluster.active) { StorageProtocol storage sp = SSVStorageProtocol.load(); - (uint64 clusterIndex, ) = OperatorLib.updateClusterOperators(operatorIds, false, false, 1, s, sp); + (uint64 clusterIndex, ) = OperatorLib.updateClusterOperators(operatorIds, false, 1, s, sp); cluster.updateClusterData(clusterIndex, sp.currentNetworkFeeIndex()); @@ -154,7 +154,6 @@ contract SSVClusters is ISSVClusters { (uint64 clusterIndex, ) = OperatorLib.updateClusterOperators( operatorIds, false, - false, validatorsRemoved, s, sp @@ -185,7 +184,6 @@ contract SSVClusters is ISSVClusters { (uint64 clusterIndex, uint64 burnRate) = OperatorLib.updateClusterOperators( operatorIds, false, - false, cluster.validatorCount, s, sp @@ -236,7 +234,6 @@ contract SSVClusters is ISSVClusters { (uint64 clusterIndex, uint64 burnRate) = OperatorLib.updateClusterOperators( operatorIds, - false, true, cluster.validatorCount, s, diff --git a/test/helpers/gas-usage.ts b/test/helpers/gas-usage.ts index c2496afb..aac0eeff 100644 --- a/test/helpers/gas-usage.ts +++ b/test/helpers/gas-usage.ts @@ -140,7 +140,7 @@ const MAX_GAS_PER_GROUP: any = { [GasGroup.LIQUIDATE_CLUSTER_4]: 130500, [GasGroup.LIQUIDATE_CLUSTER_7]: 171000, [GasGroup.LIQUIDATE_CLUSTER_10]: 212000, - [GasGroup.LIQUIDATE_CLUSTER_13]: 252800, + [GasGroup.LIQUIDATE_CLUSTER_13]: 253000, [GasGroup.REACTIVATE_CLUSTER]: 121500, [GasGroup.NETWORK_FEE_CHANGE]: 45800,