Skip to content

Commit bde687d

Browse files
committed
test: fixes and refactor of weightOfOperators
1 parent 3d21250 commit bde687d

File tree

6 files changed

+73
-27
lines changed

6 files changed

+73
-27
lines changed

src/SlashingRegistryCoordinator.sol

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -731,17 +731,17 @@ contract SlashingRegistryCoordinator is
731731
IStakeRegistryTypes.StakeType stakeType,
732732
uint32 lookAheadPeriod
733733
) internal {
734-
// Increment the total quorum count. Fails if we're already at the max
735-
uint8 prevQuorumCount = quorumCount;
736-
require(prevQuorumCount < MAX_QUORUM_COUNT, MaxQuorumsReached());
737-
quorumCount = prevQuorumCount + 1;
738-
739-
// The previous count is the new quorum's number
740-
uint8 quorumNumber = prevQuorumCount;
734+
// The previous quorum count is the new quorum's number,
735+
// this is because quorum numbers begin from index 0.
736+
uint8 quorumNumber = quorumCount;
741737

742738
// Hook to allow for any pre-create quorum logic
743739
_beforeCreateQuorum(quorumNumber);
744740

741+
// Increment the total quorum count. Fails if we're already at the max
742+
require(quorumNumber < MAX_QUORUM_COUNT, MaxQuorumsReached());
743+
quorumCount += 1;
744+
745745
// Initialize the quorum here and in each registry
746746
_setOperatorSetParams(quorumNumber, operatorSetParams);
747747

src/StakeRegistry.sol

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,11 @@ contract StakeRegistry is StakeRegistryStorage {
534534
uint8 quorumNumber,
535535
address[] memory operators
536536
) internal view virtual returns (uint96[] memory, bool[] memory) {
537-
uint96[] memory weights;
538-
bool[] memory hasMinimumStakes;
537+
uint96[] memory weights = new uint96[](operators.length);
538+
bool[] memory hasMinimumStakes = new bool[](operators.length);
539539

540540
uint256 stratsLength = strategyParamsLength(quorumNumber);
541-
StrategyParams memory strategyAndMultiplier;
541+
StrategyParams[] memory stratsAndMultipliers = strategyParams[quorumNumber];
542542
uint256[][] memory strategyShares;
543543

544544
if (stakeTypePerQuorum[quorumNumber] == IStakeRegistryTypes.StakeType.TOTAL_SLASHABLE) {
@@ -550,12 +550,15 @@ contract StakeRegistry is StakeRegistryStorage {
550550
delegation.getOperatorsShares(operators, strategiesPerQuorum[quorumNumber]);
551551
}
552552

553-
for (uint256 stratIndex = 0; stratIndex < stratsLength; stratIndex++) {
554-
// accessing i'th StrategyParams struct for the quorumNumber
555-
strategyAndMultiplier = strategyParams[quorumNumber][stratIndex];
553+
// Calculate weight of each operator and whether they contain minimum stake for the quorum
554+
for (uint256 opIndex = 0; opIndex < operators.length; opIndex++) {
555+
// 1. For the given operator, loop through the strategies and calculate the operator's
556+
// weight for the quorum
557+
for (uint256 stratIndex = 0; stratIndex < stratsLength; stratIndex++) {
558+
// get multiplier for strategy
559+
StrategyParams memory strategyAndMultiplier = stratsAndMultipliers[stratIndex];
556560

557-
for (uint256 opIndex = 0; opIndex < operators.length; opIndex++) {
558-
// add the weight from the shares for this strategy to the total weight
561+
// calculate added weight for strategy and multiplier
559562
if (strategyShares[opIndex][stratIndex] > 0) {
560563
weights[opIndex] += uint96(
561564
strategyShares[opIndex][stratIndex] * strategyAndMultiplier.multiplier
@@ -564,9 +567,8 @@ contract StakeRegistry is StakeRegistryStorage {
564567
}
565568
}
566569

567-
// check if the operator meets the quorum's minimum stake
568-
hasMinimumStakes[stratIndex] =
569-
weights[stratIndex] >= minimumStakeForQuorum[quorumNumber];
570+
// 2. Check whether operator is above minimum stake threshold
571+
hasMinimumStakes[opIndex] = weights[opIndex] >= minimumStakeForQuorum[quorumNumber];
570572
}
571573

572574
return (weights, hasMinimumStakes);

test/harnesses/RegistryCoordinatorHarness.t.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ contract RegistryCoordinatorHarness is RegistryCoordinator, Test {
7979
) external {
8080
isM2QuorumRegistrationDisabled = disabled;
8181
}
82+
83+
function setM2QuorumBitmap(uint256 bitmap) external {
84+
_m2QuorumBitmap = bitmap;
85+
}
8286
}

test/mocks/DelegationMock.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,20 @@ contract DelegationMock is DelegationIntermediate {
282282
return shares;
283283
}
284284

285+
function getOperatorsShares(
286+
address[] memory operators,
287+
IStrategy[] memory strategies
288+
) external view override returns (uint256[][] memory) {
289+
uint256[][] memory operatorSharesArray = new uint256[][](operators.length);
290+
for (uint256 i = 0; i < operators.length; i++) {
291+
operatorSharesArray[i] = new uint256[](strategies.length);
292+
for (uint256 j = 0; j < strategies.length; j++) {
293+
operatorSharesArray[i][j] = _weightOf[operators[i]][strategies[j]];
294+
}
295+
}
296+
return operatorSharesArray;
297+
}
298+
285299
function minWithdrawalDelayBlocks() external view override returns (uint32) {
286300
return 10000;
287301
}

test/unit/RegistryCoordinatorUnit.t.sol

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {
88
ISlashingRegistryCoordinatorErrors
99
} from "../../src/interfaces/ISlashingRegistryCoordinator.sol";
1010

11+
import {
12+
IRegistryCoordinator,
13+
IRegistryCoordinatorTypes,
14+
IRegistryCoordinatorErrors
15+
} from "../../src/interfaces/IRegistryCoordinator.sol";
16+
1117
import {IBLSApkRegistryTypes} from "../../src/interfaces/IBLSApkRegistry.sol";
1218
import {QuorumBitmapHistoryLib} from "../../src/libraries/QuorumBitmapHistoryLib.sol";
1319
import {BitmapUtils} from "../../src/libraries/BitmapUtils.sol";
@@ -302,7 +308,7 @@ contract RegistryCoordinatorUnitTests_RegisterOperator is RegistryCoordinatorUni
302308

303309
quorumNumbersTooLarge[0] = 0xC0;
304310

305-
cheats.expectRevert(BitmapUtils.BitmapValueTooLarge.selector);
311+
cheats.expectRevert(IRegistryCoordinatorErrors.OnlyM2QuorumsAllowed.selector);
306312
cheats.prank(defaultOperator);
307313
registryCoordinator.registerOperator(
308314
quorumNumbersTooLarge, defaultSocket, pubkeyRegistrationParams, emptySig
@@ -317,7 +323,7 @@ contract RegistryCoordinatorUnitTests_RegisterOperator is RegistryCoordinatorUni
317323
quorumNumbersNotCreated[0] = 0x0B;
318324

319325
cheats.prank(defaultOperator);
320-
cheats.expectRevert(BitmapUtils.BitmapValueTooLarge.selector);
326+
cheats.expectRevert(IRegistryCoordinatorErrors.OnlyM2QuorumsAllowed.selector);
321327
registryCoordinator.registerOperator(
322328
quorumNumbersNotCreated, defaultSocket, pubkeyRegistrationParams, emptySig
323329
);
@@ -395,6 +401,8 @@ contract RegistryCoordinatorUnitTests_RegisterOperator is RegistryCoordinatorUni
395401

396402
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
397403
emit OperatorSocketUpdate(defaultOperatorId, defaultSocket);
404+
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
405+
emit OperatorRegistered(defaultOperator, defaultOperatorId);
398406
cheats.expectEmit(true, true, true, true, address(blsApkRegistry));
399407
emit OperatorAddedToQuorums(defaultOperator, defaultOperatorId, quorumNumbers);
400408
for (uint256 i = 0; i < quorumNumbers.length; i++) {
@@ -405,8 +413,6 @@ contract RegistryCoordinatorUnitTests_RegisterOperator is RegistryCoordinatorUni
405413
cheats.expectEmit(true, true, true, true, address(indexRegistry));
406414
emit QuorumIndexUpdate(defaultOperatorId, uint8(quorumNumbers[i]), 0);
407415
}
408-
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
409-
emit OperatorRegistered(defaultOperator, defaultOperatorId);
410416

411417
uint256 gasBefore = gasleft();
412418
cheats.prank(defaultOperator);
@@ -636,6 +642,8 @@ contract RegistryCoordinatorUnitTests_RegisterOperator is RegistryCoordinatorUni
636642

637643
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
638644
emit OperatorSocketUpdate(defaultOperatorId, defaultSocket);
645+
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
646+
emit OperatorRegistered(defaultOperator, defaultOperatorId);
639647
cheats.expectEmit(true, true, true, true, address(blsApkRegistry));
640648
emit OperatorAddedToQuorums(defaultOperator, defaultOperatorId, quorumNumbers);
641649
cheats.expectEmit(true, true, true, true, address(stakeRegistry));
@@ -1707,6 +1715,9 @@ contract RegistryCoordinatorUnitTests_RegisterOperatorWithChurn is RegistryCoord
17071715
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
17081716
emit OperatorSocketUpdate(operatorToRegisterId, defaultSocket);
17091717

1718+
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
1719+
emit OperatorRegistered(operatorToRegister, operatorToRegisterId);
1720+
17101721
cheats.expectEmit(true, true, true, true, address(blsApkRegistry));
17111722
emit OperatorAddedToQuorums(operatorToRegister, operatorToRegisterId, quorumNumbers);
17121723
cheats.expectEmit(true, true, true, false, address(stakeRegistry));
@@ -1725,8 +1736,6 @@ contract RegistryCoordinatorUnitTests_RegisterOperatorWithChurn is RegistryCoord
17251736
emit OperatorStakeUpdate(operatorToKickId, defaultQuorumNumber, 0);
17261737
cheats.expectEmit(true, true, true, true, address(indexRegistry));
17271738
emit QuorumIndexUpdate(operatorToRegisterId, defaultQuorumNumber, numOperators - 1);
1728-
cheats.expectEmit(true, true, true, true, address(registryCoordinator));
1729-
emit OperatorRegistered(operatorToRegister, operatorToRegisterId);
17301739

17311740
{
17321741
ISignatureUtils.SignatureWithSaltAndExpiry memory emptyAVSRegSig;
@@ -2394,12 +2403,26 @@ contract RegistryCoordinatorUnitTests_BeforeMigration is RegistryCoordinatorUnit
23942403
});
23952404
uint32 lookAheadPeriod = 100;
23962405

2406+
assertEq(
2407+
registryCoordinator.quorumCount(),
2408+
0,
2409+
"No quorums should exist before"
2410+
);
2411+
23972412
// Attempt to create quorum with slashable stake type before enabling operator sets
23982413
cheats.prank(registryCoordinatorOwner);
2399-
cheats.expectRevert();
24002414
registryCoordinator.createSlashableStakeQuorum(
24012415
operatorSetParams, minimumStake, strategyParams, lookAheadPeriod
24022416
);
2417+
assertEq(
2418+
registryCoordinator.quorumCount(),
2419+
1,
2420+
"New quorum 0 should be created"
2421+
);
2422+
assertFalse(
2423+
registryCoordinator.isM2Quorum(0),
2424+
"Quorum created should not be an M2 quorum"
2425+
);
24032426
}
24042427
}
24052428

@@ -2744,8 +2767,6 @@ contract RegistryCoordinatorUnitTests_AfterMigration is RegistryCoordinatorUnitT
27442767
function test_deregisterHook_Reverts_WhenNotALM() public {
27452768
_deployMockEigenLayerAndAVS(0);
27462769

2747-
assertTrue(registryCoordinator.operatorSetsEnabled(), "operatorSetsEnabled should be true");
2748-
27492770
// Create quorum params
27502771
ISlashingRegistryCoordinatorTypes.OperatorSetParam memory operatorSetParams =
27512772
ISlashingRegistryCoordinatorTypes.OperatorSetParam({
@@ -2764,6 +2785,9 @@ contract RegistryCoordinatorUnitTests_AfterMigration is RegistryCoordinatorUnitT
27642785
cheats.prank(registryCoordinatorOwner);
27652786
registryCoordinator.createTotalDelegatedStakeQuorum(operatorSetParams, 0, strategyParams);
27662787

2788+
// operator sets should be enabled after creating a new quorum
2789+
assertTrue(registryCoordinator.operatorSetsEnabled(), "operatorSetsEnabled should be true");
2790+
27672791
// Prank as allocation manager and call register hook
27682792
uint32[] memory operatorSetIds = new uint32[](1);
27692793
operatorSetIds[0] = 0;

test/utils/MockAVSDeployer.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ contract MockAVSDeployer is Test {
357357

358358
operatorStateRetriever = new OperatorStateRetriever();
359359

360+
// Set RegistryCoordinator as M2 state with existing quorums
361+
registryCoordinator.setM2QuorumBitmap(0);
360362
registryCoordinator.setOperatorSetsEnabled(false);
361363
registryCoordinator.setM2QuorumRegistrationDisabled(false);
362364
}

0 commit comments

Comments
 (0)