-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathSlashingRegistryCoordinator.sol
1082 lines (960 loc) · 42.7 KB
/
SlashingRegistryCoordinator.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.27;
import {IPauserRegistry} from "eigenlayer-contracts/src/contracts/interfaces/IPauserRegistry.sol";
import {ISignatureUtils} from "eigenlayer-contracts/src/contracts/interfaces/ISignatureUtils.sol";
import {IStrategy} from "eigenlayer-contracts/src/contracts/interfaces/IStrategy.sol";
import {
IAllocationManager,
OperatorSet,
IAllocationManagerTypes
} from "eigenlayer-contracts/src/contracts/interfaces/IAllocationManager.sol";
import {IBLSApkRegistry, IBLSApkRegistryTypes} from "./interfaces/IBLSApkRegistry.sol";
import {IStakeRegistry, IStakeRegistryTypes} from "./interfaces/IStakeRegistry.sol";
import {IIndexRegistry} from "./interfaces/IIndexRegistry.sol";
import {ISlashingRegistryCoordinator} from "./interfaces/ISlashingRegistryCoordinator.sol";
import {ISocketRegistry} from "./interfaces/ISocketRegistry.sol";
import {BitmapUtils} from "./libraries/BitmapUtils.sol";
import {BN254} from "./libraries/BN254.sol";
import {SignatureCheckerLib} from "./libraries/SignatureCheckerLib.sol";
import {QuorumBitmapHistoryLib} from "./libraries/QuorumBitmapHistoryLib.sol";
import {OwnableUpgradeable} from "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol";
import {Initializable} from "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
import {Pausable} from "eigenlayer-contracts/src/contracts/permissions/Pausable.sol";
import {SlashingRegistryCoordinatorStorage} from "./SlashingRegistryCoordinatorStorage.sol";
/**
* @title A `RegistryCoordinator` that has four registries:
* 1) a `StakeRegistry` that keeps track of operators' stakes
* 2) a `BLSApkRegistry` that keeps track of operators' BLS public keys and aggregate BLS public keys for each quorum
* 3) an `IndexRegistry` that keeps track of an ordered list of operators for each quorum
* 4) a `SocketRegistry` that keeps track of operators' sockets (arbitrary strings)
*
* @author Layr Labs, Inc.
*/
contract SlashingRegistryCoordinator is
EIP712,
Initializable,
Pausable,
OwnableUpgradeable,
SlashingRegistryCoordinatorStorage,
ISignatureUtils
{
using BitmapUtils for *;
using BN254 for BN254.G1Point;
modifier onlyAllocationManager() {
_checkAllocationManager();
_;
}
modifier onlyEjector() {
_checkEjector();
_;
}
/// @dev Checks that `quorumNumber` corresponds to a quorum that has been created
/// via `initialize` or `createQuorum`
modifier quorumExists(
uint8 quorumNumber
) {
_checkQuorumExists(quorumNumber);
_;
}
constructor(
IStakeRegistry _stakeRegistry,
IBLSApkRegistry _blsApkRegistry,
IIndexRegistry _indexRegistry,
ISocketRegistry _socketRegistry,
IAllocationManager _allocationManager,
IPauserRegistry _pauserRegistry
)
SlashingRegistryCoordinatorStorage(
_stakeRegistry,
_blsApkRegistry,
_indexRegistry,
_socketRegistry,
_allocationManager
)
EIP712("AVSRegistryCoordinator", "v0.0.1")
Pausable(_pauserRegistry)
{
_disableInitializers();
}
/**
*
* EXTERNAL FUNCTIONS
*
*/
function initialize(
address _initialOwner,
address _churnApprover,
address _ejector,
uint256 _initialPausedStatus,
address _accountIdentifier
) external initializer {
_transferOwnership(_initialOwner);
_setChurnApprover(_churnApprover);
_setPausedStatus(_initialPausedStatus);
_setEjector(_ejector);
_setAccountIdentifier(_accountIdentifier);
// Add registry contracts to the registries array
registries.push(address(stakeRegistry));
registries.push(address(blsApkRegistry));
registries.push(address(indexRegistry));
registries.push(address(socketRegistry));
}
/// @inheritdoc ISlashingRegistryCoordinator
function createTotalDelegatedStakeQuorum(
OperatorSetParam memory operatorSetParams,
uint96 minimumStake,
IStakeRegistryTypes.StrategyParams[] memory strategyParams
) external virtual onlyOwner {
_createQuorum(
operatorSetParams,
minimumStake,
strategyParams,
IStakeRegistryTypes.StakeType.TOTAL_DELEGATED,
0
);
}
/// @inheritdoc ISlashingRegistryCoordinator
function createSlashableStakeQuorum(
OperatorSetParam memory operatorSetParams,
uint96 minimumStake,
IStakeRegistryTypes.StrategyParams[] memory strategyParams,
uint32 lookAheadPeriod
) external virtual onlyOwner {
_createQuorum(
operatorSetParams,
minimumStake,
strategyParams,
IStakeRegistryTypes.StakeType.TOTAL_SLASHABLE,
lookAheadPeriod
);
}
/// @inheritdoc ISlashingRegistryCoordinator
function registerOperator(
address operator,
uint32[] memory operatorSetIds,
bytes calldata data
) external override onlyAllocationManager onlyWhenNotPaused(PAUSED_REGISTER_OPERATOR) {
bytes memory quorumNumbers = _getQuorumNumbers(operatorSetIds);
(
RegistrationType registrationType,
string memory socket,
IBLSApkRegistryTypes.PubkeyRegistrationParams memory params
) = abi.decode(
data, (RegistrationType, string, IBLSApkRegistryTypes.PubkeyRegistrationParams)
);
/**
* If the operator has NEVER registered a pubkey before, use `params` to register
* their pubkey in blsApkRegistry
*
* If the operator HAS registered a pubkey, `params` is ignored and the pubkey hash
* (operatorId) is fetched instead
*/
bytes32 operatorId = _getOrCreateOperatorId(operator, params);
if (registrationType == RegistrationType.NORMAL) {
uint32[] memory numOperatorsPerQuorum = _registerOperator({
operator: operator,
operatorId: operatorId,
quorumNumbers: quorumNumbers,
socket: socket,
checkMaxOperatorCount: true
}).numOperatorsPerQuorum;
// For each quorum, validate that the new operator count does not exceed the maximum
// (If it does, an operator needs to be replaced -- see `registerOperatorWithChurn`)
for (uint256 i = 0; i < quorumNumbers.length; i++) {
uint8 quorumNumber = uint8(quorumNumbers[i]);
require(
numOperatorsPerQuorum[i] <= _quorumParams[quorumNumber].maxOperatorCount,
MaxQuorumsReached()
);
}
} else if (registrationType == RegistrationType.CHURN) {
// Decode registration data from bytes
(
,
,
,
OperatorKickParam[] memory operatorKickParams,
SignatureWithSaltAndExpiry memory churnApproverSignature
) = abi.decode(
data,
(
RegistrationType,
string,
IBLSApkRegistryTypes.PubkeyRegistrationParams,
OperatorKickParam[],
SignatureWithSaltAndExpiry
)
);
_registerOperatorWithChurn({
operator: operator,
operatorId: operatorId,
quorumNumbers: quorumNumbers,
socket: socket,
operatorKickParams: operatorKickParams,
churnApproverSignature: churnApproverSignature
});
} else {
revert InvalidRegistrationType();
}
}
/// @inheritdoc ISlashingRegistryCoordinator
function deregisterOperator(
address operator,
uint32[] memory operatorSetIds
) external override onlyAllocationManager onlyWhenNotPaused(PAUSED_DEREGISTER_OPERATOR) {
bytes memory quorumNumbers = _getQuorumNumbers(operatorSetIds);
_deregisterOperator(operator, quorumNumbers);
}
/// @inheritdoc ISlashingRegistryCoordinator
function updateOperators(
address[] memory operators
) external override onlyWhenNotPaused(PAUSED_UPDATE_OPERATOR) {
for (uint256 i = 0; i < operators.length; i++) {
// create single-element arrays for the operator and operatorId
address[] memory singleOperator = new address[](1);
singleOperator[0] = operators[i];
bytes32[] memory singleOperatorId = new bytes32[](1);
singleOperatorId[0] = _operatorInfo[operators[i]].operatorId;
uint192 currentBitmap = _currentOperatorBitmap(singleOperatorId[0]);
bytes memory quorumNumbers = currentBitmap.bitmapToBytesArray();
for (uint256 j = 0; j < quorumNumbers.length; j++) {
// update the operator's stake for each quorum
uint8 quorumNumber = uint8(quorumNumbers[j]);
bool[] memory shouldBeDeregistered = stakeRegistry.updateOperatorsStake(
singleOperator, singleOperatorId, quorumNumber
);
if (shouldBeDeregistered[0]) {
bytes memory singleQuorumNumber = new bytes(1);
singleQuorumNumber[0] = quorumNumbers[j];
_deregisterOperator(operators[i], singleQuorumNumber);
}
}
}
}
/// @inheritdoc ISlashingRegistryCoordinator
function updateOperatorsForQuorum(
address[][] memory operatorsPerQuorum,
bytes calldata quorumNumbers
) external onlyWhenNotPaused(PAUSED_UPDATE_OPERATOR) {
// Input validation
// - all quorums should exist (checked against `quorumCount` in orderedBytesArrayToBitmap)
// - there should be no duplicates in `quorumNumbers`
// - there should be one list of operators per quorum
BitmapUtils.orderedBytesArrayToBitmap(quorumNumbers, quorumCount);
require(operatorsPerQuorum.length == quorumNumbers.length, InputLengthMismatch());
// For each quorum, update ALL registered operators
for (uint256 i = 0; i < quorumNumbers.length; ++i) {
uint8 quorumNumber = uint8(quorumNumbers[i]);
// Ensure we've passed in the correct number of operators for this quorum
address[] memory currQuorumOperators = operatorsPerQuorum[i];
require(
currQuorumOperators.length == indexRegistry.totalOperatorsForQuorum(quorumNumber),
QuorumOperatorCountMismatch()
);
bytes32[] memory operatorIds = new bytes32[](currQuorumOperators.length);
address prevOperatorAddress = address(0);
// For each operator:
// - check that they are registered for this quorum
// - check that their address is strictly greater than the last operator
// ... then, update their stakes
for (uint256 j = 0; j < currQuorumOperators.length; ++j) {
address operator = currQuorumOperators[j];
operatorIds[j] = _operatorInfo[operator].operatorId;
{
uint192 currentBitmap = _currentOperatorBitmap(operatorIds[j]);
// Check that the operator is registered
require(
BitmapUtils.isSet(currentBitmap, quorumNumber), NotRegisteredForQuorum()
);
// Prevent duplicate operators
require(operator > prevOperatorAddress, NotSorted());
}
prevOperatorAddress = operator;
}
bool[] memory shouldBeDeregistered =
stakeRegistry.updateOperatorsStake(currQuorumOperators, operatorIds, quorumNumber);
for (uint256 j = 0; j < currQuorumOperators.length; ++j) {
if (shouldBeDeregistered[j]) {
_deregisterOperator(currQuorumOperators[j], quorumNumbers[i:i + 1]);
}
}
// Update timestamp that all operators in quorum have been updated all at once
quorumUpdateBlockNumber[quorumNumber] = block.number;
emit QuorumBlockNumberUpdated(quorumNumber, block.number);
}
}
/// @inheritdoc ISlashingRegistryCoordinator
function updateSocket(
string memory socket
) external {
require(_operatorInfo[msg.sender].status == OperatorStatus.REGISTERED, NotRegistered());
_setOperatorSocket(_operatorInfo[msg.sender].operatorId, socket);
}
/**
*
* EXTERNAL FUNCTIONS - EJECTOR
*
*/
/// @inheritdoc ISlashingRegistryCoordinator
function ejectOperator(address operator, bytes memory quorumNumbers) external onlyEjector {
lastEjectionTimestamp[operator] = block.timestamp;
OperatorInfo storage operatorInfo = _operatorInfo[operator];
bytes32 operatorId = operatorInfo.operatorId;
uint192 quorumsToRemove =
uint192(BitmapUtils.orderedBytesArrayToBitmap(quorumNumbers, quorumCount));
uint192 currentBitmap = _currentOperatorBitmap(operatorId);
if (
operatorInfo.status == OperatorStatus.REGISTERED && !quorumsToRemove.isEmpty()
&& quorumsToRemove.isSubsetOf(currentBitmap)
) {
_deregisterOperator({operator: operator, quorumNumbers: quorumNumbers});
}
}
/**
*
* EXTERNAL FUNCTIONS - OWNER
*
*/
/// @inheritdoc ISlashingRegistryCoordinator
function setOperatorSetParams(
uint8 quorumNumber,
OperatorSetParam memory operatorSetParams
) external onlyOwner quorumExists(quorumNumber) {
_setOperatorSetParams(quorumNumber, operatorSetParams);
}
/**
* @notice Sets the churnApprover, which approves operator registration with churn
* (see `registerOperatorWithChurn`)
* @param _churnApprover the new churn approver
* @dev only callable by the owner
*/
function setChurnApprover(
address _churnApprover
) external onlyOwner {
_setChurnApprover(_churnApprover);
}
/// @inheritdoc ISlashingRegistryCoordinator
function setEjector(
address _ejector
) external onlyOwner {
_setEjector(_ejector);
}
/// @inheritdoc ISlashingRegistryCoordinator
function setAccountIdentifier(
address _accountIdentifier
) external onlyOwner {
_setAccountIdentifier(_accountIdentifier);
}
/// @inheritdoc ISlashingRegistryCoordinator
function setEjectionCooldown(
uint256 _ejectionCooldown
) external onlyOwner {
ejectionCooldown = _ejectionCooldown;
}
/**
*
* INTERNAL FUNCTIONS
*
*/
/**
* @notice Register the operator for one or more quorums. This method updates the
* operator's quorum bitmap, socket, and status, then registers them with each registry.
*/
function _registerOperator(
address operator,
bytes32 operatorId,
bytes memory quorumNumbers,
string memory socket,
bool checkMaxOperatorCount
) internal virtual returns (RegisterResults memory results) {
/**
* Get bitmap of quorums to register for and operator's current bitmap. Validate that:
* - we're trying to register for at least 1 quorum
* - the quorums we're registering for exist (checked against `quorumCount` in orderedBytesArrayToBitmap)
* - the operator is not currently registered for any quorums we're registering for
* Then, calculate the operator's new bitmap after registration
*/
uint192 quorumsToAdd =
uint192(BitmapUtils.orderedBytesArrayToBitmap(quorumNumbers, quorumCount));
uint192 currentBitmap = _currentOperatorBitmap(operatorId);
// call hook to allow for any pre-register logic
_beforeRegisterOperator(operator, operatorId, quorumNumbers, currentBitmap);
require(!quorumsToAdd.isEmpty(), BitmapEmpty());
require(quorumsToAdd.noBitsInCommon(currentBitmap), AlreadyRegisteredForQuorums());
uint192 newBitmap = uint192(currentBitmap.plus(quorumsToAdd));
// Check that the operator can reregister if ejected
require(
lastEjectionTimestamp[operator] + ejectionCooldown < block.timestamp,
CannotReregisterYet()
);
/**
* Update operator's bitmap, socket, and status. Only update operatorInfo if needed:
* if we're `REGISTERED`, the operatorId and status are already correct.
*/
_updateOperatorBitmap({operatorId: operatorId, newBitmap: newBitmap});
_setOperatorSocket(operatorId, socket);
// If the operator wasn't registered for any quorums, update their status
// and register them with this AVS in EigenLayer core (DelegationManager)
if (_operatorInfo[operator].status != OperatorStatus.REGISTERED) {
_operatorInfo[operator] = OperatorInfo(operatorId, OperatorStatus.REGISTERED);
emit OperatorRegistered(operator, operatorId);
}
// Register the operator with the BLSApkRegistry, StakeRegistry, and IndexRegistry
blsApkRegistry.registerOperator(operator, quorumNumbers);
(results.operatorStakes, results.totalStakes) =
stakeRegistry.registerOperator(operator, operatorId, quorumNumbers);
results.numOperatorsPerQuorum = indexRegistry.registerOperator(operatorId, quorumNumbers);
if (checkMaxOperatorCount) {
for (uint256 i = 0; i < quorumNumbers.length; i++) {
OperatorSetParam memory operatorSetParams = _quorumParams[uint8(quorumNumbers[i])];
require(
results.numOperatorsPerQuorum[i] <= operatorSetParams.maxOperatorCount,
MaxQuorumsReached()
);
}
}
// call hook to allow for any post-register logic
_afterRegisterOperator(operator, operatorId, quorumNumbers, newBitmap);
return results;
}
function _registerOperatorWithChurn(
address operator,
bytes32 operatorId,
bytes memory quorumNumbers,
string memory socket,
OperatorKickParam[] memory operatorKickParams,
SignatureWithSaltAndExpiry memory churnApproverSignature
) internal virtual {
require(operatorKickParams.length == quorumNumbers.length, InputLengthMismatch());
// Verify the churn approver's signature for the registering operator and kick params
_verifyChurnApproverSignature({
registeringOperator: operator,
registeringOperatorId: operatorId,
operatorKickParams: operatorKickParams,
churnApproverSignature: churnApproverSignature
});
// Register the operator in each of the registry contracts and update the operator's
// quorum bitmap and registration status
RegisterResults memory results = _registerOperator({
operator: operator,
operatorId: operatorId,
quorumNumbers: quorumNumbers,
socket: socket,
checkMaxOperatorCount: false
});
// Check that each quorum's operator count is below the configured maximum. If the max
// is exceeded, use `operatorKickParams` to deregister an existing operator to make space
for (uint256 i = 0; i < quorumNumbers.length; i++) {
OperatorSetParam memory operatorSetParams = _quorumParams[uint8(quorumNumbers[i])];
/**
* If the new operator count for any quorum exceeds the maximum, validate
* that churn can be performed, then deregister the specified operator
*/
if (results.numOperatorsPerQuorum[i] > operatorSetParams.maxOperatorCount) {
_validateChurn({
quorumNumber: uint8(quorumNumbers[i]),
totalQuorumStake: results.totalStakes[i],
newOperator: operator,
newOperatorStake: results.operatorStakes[i],
kickParams: operatorKickParams[i],
setParams: operatorSetParams
});
bytes memory singleQuorumNumber = new bytes(1);
singleQuorumNumber[0] = quorumNumbers[i];
_deregisterOperator(operatorKickParams[i].operator, singleQuorumNumber);
}
}
}
/**
* @dev Deregister the operator from one or more quorums
* This method updates the operator's quorum bitmap and status, then deregisters
* the operator with the BLSApkRegistry, IndexRegistry, and StakeRegistry
*/
function _deregisterOperator(address operator, bytes memory quorumNumbers) internal virtual {
// Fetch the operator's info and ensure they are registered
OperatorInfo storage operatorInfo = _operatorInfo[operator];
bytes32 operatorId = operatorInfo.operatorId;
uint192 currentBitmap = _currentOperatorBitmap(operatorId);
// call hook to allow for any pre-deregister logic
_beforeDeregisterOperator(operator, operatorId, quorumNumbers, currentBitmap);
require(operatorInfo.status == OperatorStatus.REGISTERED, NotRegistered());
/**
* Get bitmap of quorums to deregister from and operator's current bitmap. Validate that:
* - we're trying to deregister from at least 1 quorum
* - the quorums we're deregistering from exist (checked against `quorumCount` in orderedBytesArrayToBitmap)
* - the operator is currently registered for any quorums we're trying to deregister from
* Then, calculate the operator's new bitmap after deregistration
*/
uint192 quorumsToRemove =
uint192(BitmapUtils.orderedBytesArrayToBitmap(quorumNumbers, quorumCount));
require(!quorumsToRemove.isEmpty(), BitmapCannotBeZero());
require(quorumsToRemove.isSubsetOf(currentBitmap), NotRegisteredForQuorum());
uint192 newBitmap = uint192(currentBitmap.minus(quorumsToRemove));
// Update operator's bitmap and status
_updateOperatorBitmap({operatorId: operatorId, newBitmap: newBitmap});
// If the operator is no longer registered for any quorums, update their status and deregister
// them from the AVS via the EigenLayer core contracts
if (newBitmap.isEmpty()) {
_operatorInfo[operator].status = OperatorStatus.DEREGISTERED;
emit OperatorDeregistered(operator, operatorId);
}
// Deregister operator with each of the registry contracts
blsApkRegistry.deregisterOperator(operator, quorumNumbers);
stakeRegistry.deregisterOperator(operatorId, quorumNumbers);
indexRegistry.deregisterOperator(operatorId, quorumNumbers);
// If the caller is not the allocationManager, then this is a force deregistration not consented by the operator
if (msg.sender != address(allocationManager)) {
_forceDeregisterOperator(operator, quorumNumbers);
}
// call hook to allow for any post-deregister logic
_afterDeregisterOperator(operator, operatorId, quorumNumbers, newBitmap);
}
/**
* @notice Helper function to handle operator set deregistration for OperatorSets quorums. This is used
* when an operator is force-deregistered from a set of quorums.
* @param operator The operator to deregister
* @param quorumNumbers The quorum numbers the operator is force-deregistered from
*/
function _forceDeregisterOperator(
address operator,
bytes memory quorumNumbers
) internal virtual {
allocationManager.deregisterFromOperatorSets(
IAllocationManagerTypes.DeregisterParams({
operator: operator,
avs: accountIdentifier,
operatorSetIds: _getOperatorSetIds(quorumNumbers)
})
);
}
/**
* @notice Checks if the caller is the ejector
* @dev Reverts if the caller is not the ejector
*/
function _checkEjector() internal view {
require(msg.sender == ejector, OnlyEjector());
}
function _checkAllocationManager() internal view {
require(msg.sender == address(allocationManager), OnlyAllocationManager());
}
/**
* @notice Checks if a quorum exists
* @param quorumNumber The quorum number to check
* @dev Reverts if the quorum does not exist
*/
function _checkQuorumExists(
uint8 quorumNumber
) internal view {
require(quorumNumber < quorumCount, QuorumDoesNotExist());
}
/**
* @notice Fetches an operator's pubkey hash from the BLSApkRegistry. If the
* operator has not registered a pubkey, attempts to register a pubkey using
* `params`
* @param operator the operator whose pubkey to query from the BLSApkRegistry
* @param params contains the G1 & G2 public keys of the operator, and a signature proving their ownership
* @dev `params` can be empty if the operator has already registered a pubkey in the BLSApkRegistry
*/
function _getOrCreateOperatorId(
address operator,
IBLSApkRegistryTypes.PubkeyRegistrationParams memory params
) internal returns (bytes32 operatorId) {
operatorId = blsApkRegistry.getOperatorId(operator);
if (operatorId == 0) {
operatorId = blsApkRegistry.registerBLSPublicKey(
operator, params, pubkeyRegistrationMessageHash(operator)
);
}
return operatorId;
}
/**
* @notice Validates that an incoming operator is eligible to replace an existing
* operator based on the stake of both
* @dev In order to churn, the incoming operator needs to have more stake than the
* existing operator by a proportion given by `kickBIPsOfOperatorStake`
* @dev In order to be churned out, the existing operator needs to have a proportion
* of the total quorum stake less than `kickBIPsOfTotalStake`
* @param quorumNumber `newOperator` is trying to replace an operator in this quorum
* @param totalQuorumStake the total stake of all operators in the quorum, after the
* `newOperator` registers
* @param newOperator the incoming operator
* @param newOperatorStake the incoming operator's stake
* @param kickParams the quorum number and existing operator to replace
* @dev the existing operator's registration to this quorum isn't checked here, but
* if we attempt to deregister them, this will be checked in `_deregisterOperator`
* @param setParams config for this quorum containing `kickBIPsX` stake proportions
* mentioned above
*/
function _validateChurn(
uint8 quorumNumber,
uint96 totalQuorumStake,
address newOperator,
uint96 newOperatorStake,
OperatorKickParam memory kickParams,
OperatorSetParam memory setParams
) internal view {
address operatorToKick = kickParams.operator;
bytes32 idToKick = _operatorInfo[operatorToKick].operatorId;
require(newOperator != operatorToKick, CannotChurnSelf());
require(kickParams.quorumNumber == quorumNumber, QuorumOperatorCountMismatch());
// Get the target operator's stake and check that it is below the kick thresholds
uint96 operatorToKickStake = stakeRegistry.getCurrentStake(idToKick, quorumNumber);
require(
newOperatorStake > _individualKickThreshold(operatorToKickStake, setParams),
InsufficientStakeForChurn()
);
require(
operatorToKickStake < _totalKickThreshold(totalQuorumStake, setParams),
CannotKickOperatorAboveThreshold()
);
}
/**
* @notice Returns the stake threshold required for an incoming operator to replace an existing operator
* The incoming operator must have more stake than the return value.
*/
function _individualKickThreshold(
uint96 operatorStake,
OperatorSetParam memory setParams
) internal pure returns (uint96) {
return operatorStake * setParams.kickBIPsOfOperatorStake / BIPS_DENOMINATOR;
}
/**
* @notice Returns the total stake threshold required for an operator to remain in a quorum.
* The operator must have at least the returned stake amount to keep their position.
*/
function _totalKickThreshold(
uint96 totalStake,
OperatorSetParam memory setParams
) internal pure returns (uint96) {
return totalStake * setParams.kickBIPsOfTotalStake / BIPS_DENOMINATOR;
}
/**
* @notice Updates an operator's socket address in the SocketRegistry
* @param operatorId The unique identifier of the operator
* @param socket The new socket address to set for the operator
* @dev Emits an OperatorSocketUpdate event after updating
*/
function _setOperatorSocket(bytes32 operatorId, string memory socket) internal {
socketRegistry.setOperatorSocket(operatorId, socket);
emit OperatorSocketUpdate(operatorId, socket);
}
/// @notice verifies churnApprover's signature on operator churn approval and increments the churnApprover nonce
function _verifyChurnApproverSignature(
address registeringOperator,
bytes32 registeringOperatorId,
OperatorKickParam[] memory operatorKickParams,
SignatureWithSaltAndExpiry memory churnApproverSignature
) internal {
// make sure the salt hasn't been used already
require(!isChurnApproverSaltUsed[churnApproverSignature.salt], ChurnApproverSaltUsed());
require(churnApproverSignature.expiry >= block.timestamp, SignatureExpired());
// set salt used to true
isChurnApproverSaltUsed[churnApproverSignature.salt] = true;
// check the churnApprover's signature
SignatureCheckerLib.isValidSignature(
churnApprover,
calculateOperatorChurnApprovalDigestHash(
registeringOperator,
registeringOperatorId,
operatorKickParams,
churnApproverSignature.salt,
churnApproverSignature.expiry
),
churnApproverSignature.signature
);
}
/**
* @notice Creates a quorum and initializes it in each registry contract
* @param operatorSetParams configures the quorum's max operator count and churn parameters
* @param minimumStake sets the minimum stake required for an operator to register or remain
* registered
* @param strategyParams a list of strategies and multipliers used by the StakeRegistry to
* calculate an operator's stake weight for the quorum
*/
function _createQuorum(
OperatorSetParam memory operatorSetParams,
uint96 minimumStake,
IStakeRegistryTypes.StrategyParams[] memory strategyParams,
IStakeRegistryTypes.StakeType stakeType,
uint32 lookAheadPeriod
) internal {
// The previous quorum count is the new quorum's number,
// this is because quorum numbers begin from index 0.
uint8 quorumNumber = quorumCount;
// Hook to allow for any pre-create quorum logic
_beforeCreateQuorum(quorumNumber);
// Increment the total quorum count. Fails if we're already at the max
require(quorumNumber < MAX_QUORUM_COUNT, MaxQuorumsReached());
quorumCount += 1;
// Initialize the quorum here and in each registry
_setOperatorSetParams(quorumNumber, operatorSetParams);
// Create array of CreateSetParams for the new quorum
IAllocationManagerTypes.CreateSetParams[] memory createSetParams =
new IAllocationManagerTypes.CreateSetParams[](1);
// Extract strategies from strategyParams
IStrategy[] memory strategies = new IStrategy[](strategyParams.length);
for (uint256 i = 0; i < strategyParams.length; i++) {
strategies[i] = strategyParams[i].strategy;
}
// Initialize CreateSetParams with quorumNumber as operatorSetId
createSetParams[0] = IAllocationManagerTypes.CreateSetParams({
operatorSetId: quorumNumber,
strategies: strategies
});
allocationManager.createOperatorSets({avs: accountIdentifier, params: createSetParams});
// Initialize stake registry based on stake type
if (stakeType == IStakeRegistryTypes.StakeType.TOTAL_DELEGATED) {
stakeRegistry.initializeDelegatedStakeQuorum(quorumNumber, minimumStake, strategyParams);
} else if (stakeType == IStakeRegistryTypes.StakeType.TOTAL_SLASHABLE) {
stakeRegistry.initializeSlashableStakeQuorum(
quorumNumber, minimumStake, lookAheadPeriod, strategyParams
);
}
indexRegistry.initializeQuorum(quorumNumber);
blsApkRegistry.initializeQuorum(quorumNumber);
// Hook to allow for any post-create quorum logic
_afterCreateQuorum(quorumNumber);
}
/**
* @notice Record an update to an operator's quorum bitmap.
* @param newBitmap is the most up-to-date set of bitmaps the operator is registered for
*/
function _updateOperatorBitmap(bytes32 operatorId, uint192 newBitmap) internal {
QuorumBitmapHistoryLib.updateOperatorBitmap(_operatorBitmapHistory, operatorId, newBitmap);
}
/// @notice Get the most recent bitmap for the operator, returning an empty bitmap if
/// the operator is not registered.
function _currentOperatorBitmap(
bytes32 operatorId
) internal view returns (uint192) {
return QuorumBitmapHistoryLib.currentOperatorBitmap(_operatorBitmapHistory, operatorId);
}
/**
* @notice Returns the index of the quorumBitmap for the provided `operatorId` at the given `blockNumber`
* @dev Reverts if the operator had not yet (ever) registered at `blockNumber`
* @dev This function is designed to find proper inputs to the `getQuorumBitmapAtBlockNumberByIndex` function
*/
function _getQuorumBitmapIndexAtBlockNumber(
uint32 blockNumber,
bytes32 operatorId
) internal view returns (uint32 index) {
return QuorumBitmapHistoryLib.getQuorumBitmapIndexAtBlockNumber(
_operatorBitmapHistory, blockNumber, operatorId
);
}
/// @notice Returns the quorum numbers for the provided `OperatorSetIds`
/// OperatorSetIds are used in the AllocationManager to identify operator sets for a given AVS
function _getQuorumNumbers(
uint32[] memory operatorSetIds
) internal pure returns (bytes memory) {
bytes memory quorumNumbers = new bytes(operatorSetIds.length);
for (uint256 i = 0; i < operatorSetIds.length; i++) {
quorumNumbers[i] = bytes1(uint8(operatorSetIds[i]));
}
return quorumNumbers;
}
function _getOperatorSetIds(
bytes memory quorumNumbers
) internal pure returns (uint32[] memory) {
uint32[] memory operatorSetIds = new uint32[](quorumNumbers.length);
for (uint256 i = 0; i < quorumNumbers.length; i++) {
operatorSetIds[i] = uint32(uint8(quorumNumbers[i]));
}
return operatorSetIds;
}
function _setOperatorSetParams(
uint8 quorumNumber,
OperatorSetParam memory operatorSetParams
) internal {
_quorumParams[quorumNumber] = operatorSetParams;
emit OperatorSetParamsUpdated(quorumNumber, operatorSetParams);
}
function _setChurnApprover(
address newChurnApprover
) internal {
emit ChurnApproverUpdated(churnApprover, newChurnApprover);
churnApprover = newChurnApprover;
}
function _setEjector(
address newEjector
) internal {
emit EjectorUpdated(ejector, newEjector);
ejector = newEjector;
}
function _setAccountIdentifier(
address _accountIdentifier
) internal {
accountIdentifier = _accountIdentifier;
}
/// @dev Hook to allow for any pre-create quorum logic
function _beforeCreateQuorum(
uint8 quorumNumber
) internal virtual {}
/// @dev Hook to allow for any post-create quorum logic
function _afterCreateQuorum(
uint8 quorumNumber
) internal virtual {}
/// @dev Hook to allow for any pre-register logic in `_registerOperator`
function _beforeRegisterOperator(
address operator,
bytes32 operatorId,
bytes memory quorumNumbers,
uint192 currentBitmap
) internal virtual {}
/// @dev Hook to allow for any post-register logic in `_registerOperator`
function _afterRegisterOperator(
address operator,
bytes32 operatorId,
bytes memory quorumNumbers,
uint192 newBitmap
) internal virtual {}
/// @dev Hook to allow for any pre-deregister logic in `_deregisterOperator`
function _beforeDeregisterOperator(
address operator,
bytes32 operatorId,
bytes memory quorumNumbers,
uint192 currentBitmap
) internal virtual {}
/// @dev Hook to allow for any post-deregister logic in `_deregisterOperator`
function _afterDeregisterOperator(
address operator,
bytes32 operatorId,
bytes memory quorumNumbers,
uint192 newBitmap
) internal virtual {}
/**
*
* VIEW FUNCTIONS
*
*/
/// @notice Returns the operator set params for the given `quorumNumber`
function getOperatorSetParams(
uint8 quorumNumber
) external view returns (OperatorSetParam memory) {
return _quorumParams[quorumNumber];
}
/// @notice Returns the operator struct for the given `operator`
function getOperator(
address operator
) external view returns (OperatorInfo memory) {
return _operatorInfo[operator];
}
/// @notice Returns the operatorId for the given `operator`
function getOperatorId(
address operator
) external view returns (bytes32) {
return _operatorInfo[operator].operatorId;
}
/// @notice Returns the operator address for the given `operatorId`
function getOperatorFromId(
bytes32 operatorId
) external view returns (address) {
return blsApkRegistry.getOperatorFromPubkeyHash(operatorId);
}
/// @notice Returns the status for the given `operator`
function getOperatorStatus(
address operator
) external view returns (ISlashingRegistryCoordinator.OperatorStatus) {
return _operatorInfo[operator].status;
}
/**
* @notice Returns the indices of the quorumBitmaps for the provided `operatorIds` at the given `blockNumber`
* @dev Reverts if any of the `operatorIds` was not (yet) registered at `blockNumber`
* @dev This function is designed to find proper inputs to the `getQuorumBitmapAtBlockNumberByIndex` function
*/
function getQuorumBitmapIndicesAtBlockNumber(
uint32 blockNumber,
bytes32[] memory operatorIds
) external view returns (uint32[] memory) {
return QuorumBitmapHistoryLib.getQuorumBitmapIndicesAtBlockNumber(
_operatorBitmapHistory, blockNumber, operatorIds
);
}
/**
* @notice Returns the quorum bitmap for the given `operatorId` at the given `blockNumber` via the `index`,
* reverting if `index` is incorrect
* @dev This function is meant to be used in concert with `getQuorumBitmapIndicesAtBlockNumber`, which
* helps off-chain processes to fetch the correct `index` input
*/
function getQuorumBitmapAtBlockNumberByIndex(
bytes32 operatorId,
uint32 blockNumber,
uint256 index
) external view returns (uint192) {
return QuorumBitmapHistoryLib.getQuorumBitmapAtBlockNumberByIndex(