Skip to content

Commit

Permalink
refactor operators update on cluster registration
Browse files Browse the repository at this point in the history
  • Loading branch information
mtabasco committed Apr 19, 2024
1 parent f74c429 commit 03e1937
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 47 deletions.
4 changes: 1 addition & 3 deletions contracts/libraries/ClusterLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 53 additions & 39 deletions contracts/libraries/OperatorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions contracts/modules/SSVClusters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -154,7 +154,6 @@ contract SSVClusters is ISSVClusters {
(uint64 clusterIndex, ) = OperatorLib.updateClusterOperators(
operatorIds,
false,
false,
validatorsRemoved,
s,
sp
Expand Down Expand Up @@ -185,7 +184,6 @@ contract SSVClusters is ISSVClusters {
(uint64 clusterIndex, uint64 burnRate) = OperatorLib.updateClusterOperators(
operatorIds,
false,
false,
cluster.validatorCount,
s,
sp
Expand Down Expand Up @@ -236,7 +234,6 @@ contract SSVClusters is ISSVClusters {

(uint64 clusterIndex, uint64 burnRate) = OperatorLib.updateClusterOperators(
operatorIds,
false,
true,
cluster.validatorCount,
s,
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/gas-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 03e1937

Please sign in to comment.