-
Notifications
You must be signed in to change notification settings - Fork 346
/
AllocationManager.sol
839 lines (696 loc) · 36 KB
/
AllocationManager.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.27;
import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol";
import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol";
import "@openzeppelin-upgrades/contracts/security/ReentrancyGuardUpgradeable.sol";
import "../mixins/PermissionControllerMixin.sol";
import "../permissions/Pausable.sol";
import "../libraries/SlashingLib.sol";
import "../libraries/OperatorSetLib.sol";
import "./AllocationManagerStorage.sol";
contract AllocationManager is
Initializable,
OwnableUpgradeable,
Pausable,
AllocationManagerStorage,
ReentrancyGuardUpgradeable,
PermissionControllerMixin
{
using DoubleEndedQueue for DoubleEndedQueue.Bytes32Deque;
using EnumerableSet for *;
using Snapshots for Snapshots.DefaultWadHistory;
using OperatorSetLib for OperatorSet;
using SlashingLib for uint256;
/**
*
* INITIALIZING FUNCTIONS
*
*/
/**
* @dev Initializes the DelegationManager address, the deallocation delay, and the allocation configuration delay.
*/
constructor(
IDelegationManager _delegation,
IPauserRegistry _pauserRegistry,
IPermissionController _permissionController,
uint32 _DEALLOCATION_DELAY,
uint32 _ALLOCATION_CONFIGURATION_DELAY
)
AllocationManagerStorage(_delegation, _DEALLOCATION_DELAY, _ALLOCATION_CONFIGURATION_DELAY)
Pausable(_pauserRegistry)
PermissionControllerMixin(_permissionController)
{
_disableInitializers();
}
/// @inheritdoc IAllocationManager
function initialize(address initialOwner, uint256 initialPausedStatus) external initializer {
_setPausedStatus(initialPausedStatus);
_transferOwnership(initialOwner);
}
/// @inheritdoc IAllocationManager
function slashOperator(
address avs,
SlashingParams calldata params
) external onlyWhenNotPaused(PAUSED_OPERATOR_SLASHING) checkCanCall(avs) {
// Check that the operator set exists and the operator is registered to it
OperatorSet memory operatorSet = OperatorSet(avs, params.operatorSetId);
bool isOperatorSlashable = _isOperatorSlashable(params.operator, operatorSet);
require(params.strategies.length == params.wadsToSlash.length, InputArrayLengthMismatch());
require(_operatorSets[operatorSet.avs].contains(operatorSet.id), InvalidOperatorSet());
require(isOperatorSlashable, OperatorNotSlashable());
uint256[] memory wadSlashed = new uint256[](params.strategies.length);
// For each strategy in the operator set, slash any existing allocation
for (uint256 i = 0; i < params.strategies.length; i++) {
// Check that `strategies` is in ascending order.
require(
i == 0 || uint160(address(params.strategies[i])) > uint160(address(params.strategies[i - 1])),
StrategiesMustBeInAscendingOrder()
);
// Check that `wadToSlash` is within acceptable bounds.
require(0 < params.wadsToSlash[i] && params.wadsToSlash[i] <= WAD, InvalidWadToSlash());
// Check that the operator set contains the strategy.
require(
_operatorSetStrategies[operatorSet.key()].contains(address(params.strategies[i])),
StrategyNotInOperatorSet()
);
// 1. Get the operator's allocation info for the strategy and operator set
(StrategyInfo memory info, Allocation memory allocation) =
_getUpdatedAllocation(params.operator, operatorSet.key(), params.strategies[i]);
// 2. Skip if the operator does not have a slashable allocation
// NOTE: this "if" is equivalent to: `if (!_isAllocationSlashable)`, because the other
// conditions in this method are already true (isOperatorSlashable + operatorSetStrategies.contains)
if (allocation.currentMagnitude == 0) {
continue;
}
// 3. Calculate the amount of magnitude being slashed, and subtract from
// the operator's currently-allocated magnitude, as well as the strategy's
// max and encumbered magnitudes
uint64 slashedMagnitude = uint64(uint256(allocation.currentMagnitude).mulWadRoundUp(params.wadsToSlash[i]));
uint64 prevMaxMagnitude = info.maxMagnitude;
wadSlashed[i] = uint256(slashedMagnitude).divWad(info.maxMagnitude);
allocation.currentMagnitude -= slashedMagnitude;
info.maxMagnitude -= slashedMagnitude;
info.encumberedMagnitude -= slashedMagnitude;
// 4. If there is a pending deallocation, reduce the pending deallocation proportionally.
// This ensures that when the deallocation is completed, less magnitude is freed.
if (allocation.pendingDiff < 0) {
uint64 slashedPending =
uint64(uint256(uint128(-allocation.pendingDiff)).mulWadRoundUp(params.wadsToSlash[i]));
allocation.pendingDiff += int128(uint128(slashedPending));
emit AllocationUpdated(
params.operator,
operatorSet,
params.strategies[i],
_addInt128(allocation.currentMagnitude, allocation.pendingDiff),
allocation.effectBlock
);
}
// 5. Update state
_updateAllocationInfo(params.operator, operatorSet.key(), params.strategies[i], info, allocation);
// Emit an event for the updated allocation
emit AllocationUpdated(
params.operator, operatorSet, params.strategies[i], allocation.currentMagnitude, uint32(block.number)
);
_updateMaxMagnitude(params.operator, params.strategies[i], info.maxMagnitude);
// 6. Decrease and burn operators shares in the DelegationManager
delegation.burnOperatorShares({
operator: params.operator,
strategy: params.strategies[i],
prevMaxMagnitude: prevMaxMagnitude,
newMaxMagnitude: info.maxMagnitude
});
}
emit OperatorSlashed(params.operator, operatorSet, params.strategies, wadSlashed, params.description);
}
/// @inheritdoc IAllocationManager
function modifyAllocations(
address operator,
AllocateParams[] memory params
) external onlyWhenNotPaused(PAUSED_MODIFY_ALLOCATIONS) {
// Check that the caller is allowed to modify allocations on behalf of the operator
// We do not use a modifier to avoid `stack too deep` errors
require(_checkCanCall(operator), InvalidCaller());
// Check that the operator exists and has configured an allocation delay
uint32 operatorAllocationDelay;
{
(bool isSet, uint32 delay) = getAllocationDelay(operator);
require(isSet, UninitializedAllocationDelay());
operatorAllocationDelay = delay;
}
for (uint256 i = 0; i < params.length; i++) {
require(params[i].strategies.length == params[i].newMagnitudes.length, InputArrayLengthMismatch());
// Check that the operator set exists and get the operator's registration status
// Operators do not need to be registered for an operator set in order to allocate
// slashable magnitude to the set. In fact, it is expected that operators will
// allocate magnitude before registering, as AVS's will likely only accept
// registrations from operators that are already slashable.
OperatorSet memory operatorSet = params[i].operatorSet;
require(_operatorSets[operatorSet.avs].contains(operatorSet.id), InvalidOperatorSet());
bool isOperatorSlashable = _isOperatorSlashable(operator, operatorSet);
for (uint256 j = 0; j < params[i].strategies.length; j++) {
IStrategy strategy = params[i].strategies[j];
// 1. If the operator has any pending deallocations for this strategy, clear them
// to free up magnitude for allocation. Fetch the operator's up to date allocation
// info and ensure there is no remaining pending modification.
_clearDeallocationQueue(operator, strategy, type(uint16).max);
(StrategyInfo memory info, Allocation memory allocation) =
_getUpdatedAllocation(operator, operatorSet.key(), strategy);
require(allocation.pendingDiff == 0, ModificationAlreadyPending());
// 2. Check whether the operator's allocation is slashable. If not, we allow instant
// deallocation.
bool isSlashable = _isAllocationSlashable(operatorSet, strategy, allocation, isOperatorSlashable);
// 3. Calculate the change in magnitude
allocation.pendingDiff = _calcDelta(allocation.currentMagnitude, params[i].newMagnitudes[j]);
require(allocation.pendingDiff != 0, SameMagnitude());
// 4. Handle deallocation/allocation
if (allocation.pendingDiff < 0) {
if (isSlashable) {
// If the operator is slashable, deallocated magnitude will be freed after
// the deallocation delay. This magnitude remains slashable until then.
deallocationQueue[operator][strategy].pushBack(operatorSet.key());
// deallocations are slashable in the window [block.number, block.number + deallocationDelay]
// therefore, the effectBlock is set to the block right after the slashable window
allocation.effectBlock = uint32(block.number) + DEALLOCATION_DELAY + 1;
} else {
// Deallocation immediately updates/frees magnitude if the operator is not slashable
info.encumberedMagnitude = _addInt128(info.encumberedMagnitude, allocation.pendingDiff);
allocation.currentMagnitude = params[i].newMagnitudes[j];
allocation.pendingDiff = 0;
allocation.effectBlock = uint32(block.number);
}
} else if (allocation.pendingDiff > 0) {
// Allocation immediately consumes available magnitude, but the additional
// magnitude does not become slashable until after the allocation delay
info.encumberedMagnitude = _addInt128(info.encumberedMagnitude, allocation.pendingDiff);
require(info.encumberedMagnitude <= info.maxMagnitude, InsufficientMagnitude());
allocation.effectBlock = uint32(block.number) + operatorAllocationDelay;
}
// 5. Update state
_updateAllocationInfo(operator, operatorSet.key(), strategy, info, allocation);
// 6. Emit an event for the updated allocation
emit AllocationUpdated(
operator,
OperatorSetLib.decode(operatorSet.key()),
strategy,
_addInt128(allocation.currentMagnitude, allocation.pendingDiff),
allocation.effectBlock
);
}
}
}
/// @inheritdoc IAllocationManager
function clearDeallocationQueue(
address operator,
IStrategy[] calldata strategies,
uint16[] calldata numToClear
) external onlyWhenNotPaused(PAUSED_MODIFY_ALLOCATIONS) {
require(strategies.length == numToClear.length, InputArrayLengthMismatch());
for (uint256 i = 0; i < strategies.length; ++i) {
_clearDeallocationQueue({operator: operator, strategy: strategies[i], numToClear: numToClear[i]});
}
}
/// @inheritdoc IAllocationManager
function registerForOperatorSets(
address operator,
RegisterParams calldata params
) external onlyWhenNotPaused(PAUSED_OPERATOR_SET_REGISTRATION_AND_DEREGISTRATION) checkCanCall(operator) {
// Check that the operator exists
require(delegation.isOperator(operator), InvalidOperator());
for (uint256 i = 0; i < params.operatorSetIds.length; i++) {
// Check the operator set exists and the operator is not currently registered to it
OperatorSet memory operatorSet = OperatorSet(params.avs, params.operatorSetIds[i]);
require(_operatorSets[operatorSet.avs].contains(operatorSet.id), InvalidOperatorSet());
require(!_isOperatorSlashable(operator, operatorSet), AlreadyMemberOfSet());
// Add operator to operator set
registeredSets[operator].add(operatorSet.key());
_operatorSetMembers[operatorSet.key()].add(operator);
emit OperatorAddedToOperatorSet(operator, operatorSet);
// Mark the operator registered
registrationStatus[operator][operatorSet.key()].registered = true;
}
// Call the AVS to complete registration. If the AVS reverts, registration will fail.
getAVSRegistrar(params.avs).registerOperator(operator, params.operatorSetIds, params.data);
}
/// @inheritdoc IAllocationManager
function deregisterFromOperatorSets(
DeregisterParams calldata params
) external onlyWhenNotPaused(PAUSED_OPERATOR_SET_REGISTRATION_AND_DEREGISTRATION) {
// Check that the caller is either authorized on behalf of the operator or AVS
require(_checkCanCall(params.operator) || _checkCanCall(params.avs), InvalidCaller());
for (uint256 i = 0; i < params.operatorSetIds.length; i++) {
// Check the operator set exists and the operator is registered to it
OperatorSet memory operatorSet = OperatorSet(params.avs, params.operatorSetIds[i]);
require(_operatorSets[params.avs].contains(operatorSet.id), InvalidOperatorSet());
require(registrationStatus[params.operator][operatorSet.key()].registered, NotMemberOfSet());
// Remove operator from operator set
registeredSets[params.operator].remove(operatorSet.key());
_operatorSetMembers[operatorSet.key()].remove(params.operator);
emit OperatorRemovedFromOperatorSet(params.operator, operatorSet);
// Mark operator deregistered until the DEALLOCATION_DELAY passes
// forgefmt: disable-next-item
registrationStatus[params.operator][operatorSet.key()] = RegistrationStatus({
registered: false,
slashableUntil: uint32(block.number) + DEALLOCATION_DELAY
});
}
// Call the AVS to complete deregistration. Even if the AVS reverts, the operator is
// considered deregistered
try getAVSRegistrar(params.avs).deregisterOperator(params.operator, params.operatorSetIds) {} catch {}
}
/// @inheritdoc IAllocationManager
function setAllocationDelay(address operator, uint32 delay) external {
if (msg.sender != address(delegation)) {
require(_checkCanCall(operator), InvalidCaller());
require(delegation.isOperator(operator), InvalidOperator());
}
_setAllocationDelay(operator, delay);
}
/// @inheritdoc IAllocationManager
function setAVSRegistrar(address avs, IAVSRegistrar registrar) external checkCanCall(avs) {
_avsRegistrar[avs] = registrar;
emit AVSRegistrarSet(avs, getAVSRegistrar(avs));
}
/// @inheritdoc IAllocationManager
function updateAVSMetadataURI(address avs, string calldata metadataURI) external checkCanCall(avs) {
emit AVSMetadataURIUpdated(avs, metadataURI);
}
/// @inheritdoc IAllocationManager
function createOperatorSets(address avs, CreateSetParams[] calldata params) external checkCanCall(avs) {
for (uint256 i = 0; i < params.length; i++) {
OperatorSet memory operatorSet = OperatorSet(avs, params[i].operatorSetId);
// Create the operator set, ensuring it does not already exist
require(_operatorSets[avs].add(operatorSet.id), InvalidOperatorSet());
emit OperatorSetCreated(OperatorSet(avs, operatorSet.id));
// Add strategies to the operator set
bytes32 operatorSetKey = operatorSet.key();
for (uint256 j = 0; j < params[i].strategies.length; j++) {
_operatorSetStrategies[operatorSetKey].add(address(params[i].strategies[j]));
emit StrategyAddedToOperatorSet(operatorSet, params[i].strategies[j]);
}
}
}
/// @inheritdoc IAllocationManager
function addStrategiesToOperatorSet(
address avs,
uint32 operatorSetId,
IStrategy[] calldata strategies
) external checkCanCall(avs) {
OperatorSet memory operatorSet = OperatorSet(avs, operatorSetId);
bytes32 operatorSetKey = operatorSet.key();
require(_operatorSets[avs].contains(operatorSet.id), InvalidOperatorSet());
for (uint256 i = 0; i < strategies.length; i++) {
require(_operatorSetStrategies[operatorSetKey].add(address(strategies[i])), StrategyAlreadyInOperatorSet());
emit StrategyAddedToOperatorSet(operatorSet, strategies[i]);
}
}
/// @inheritdoc IAllocationManager
function removeStrategiesFromOperatorSet(
address avs,
uint32 operatorSetId,
IStrategy[] calldata strategies
) external checkCanCall(avs) {
OperatorSet memory operatorSet = OperatorSet(avs, operatorSetId);
require(_operatorSets[avs].contains(operatorSet.id), InvalidOperatorSet());
bytes32 operatorSetKey = operatorSet.key();
for (uint256 i = 0; i < strategies.length; i++) {
require(_operatorSetStrategies[operatorSetKey].remove(address(strategies[i])), StrategyNotInOperatorSet());
emit StrategyRemovedFromOperatorSet(operatorSet, strategies[i]);
}
}
/**
*
* INTERNAL FUNCTIONS
*
*/
/**
* @dev Clear one or more pending deallocations to a strategy's allocated magnitude
* @param operator the operator whose pending deallocations will be cleared
* @param strategy the strategy to update
* @param numToClear the number of pending deallocations to clear
*/
function _clearDeallocationQueue(address operator, IStrategy strategy, uint16 numToClear) internal {
uint256 numCleared;
uint256 length = deallocationQueue[operator][strategy].length();
while (length > 0 && numCleared < numToClear) {
bytes32 operatorSetKey = deallocationQueue[operator][strategy].front();
(StrategyInfo memory info, Allocation memory allocation) =
_getUpdatedAllocation(operator, operatorSetKey, strategy);
// If we've reached a pending deallocation that isn't completable yet,
// we can stop. Any subsequent deallocation will also be uncompletable.
if (block.number < allocation.effectBlock) {
break;
}
// Update state. This completes the deallocation, because `_getUpdatedAllocation`
// gave us strategy/allocation info as if the deallocation was already completed.
_updateAllocationInfo(operator, operatorSetKey, strategy, info, allocation);
// Remove the deallocation from the queue
deallocationQueue[operator][strategy].popFront();
++numCleared;
--length;
}
}
/**
* @dev Sets the operator's allocation delay. This is the number of blocks between an operator
* allocating magnitude to an operator set, and the magnitude becoming slashable.
* @param operator The operator to set the delay on behalf of.
* @param delay The allocation delay in blocks.
*/
function _setAllocationDelay(address operator, uint32 delay) internal {
AllocationDelayInfo memory info = _allocationDelayInfo[operator];
// If there is a pending delay that can be applied now, set it
if (info.effectBlock != 0 && block.number >= info.effectBlock) {
info.delay = info.pendingDelay;
info.isSet = true;
}
info.pendingDelay = delay;
info.effectBlock = uint32(block.number) + ALLOCATION_CONFIGURATION_DELAY;
_allocationDelayInfo[operator] = info;
emit AllocationDelaySet(operator, delay, info.effectBlock);
}
/// @notice returns whether the operator is slashable in the given operator set
function _isOperatorSlashable(address operator, OperatorSet memory operatorSet) internal view returns (bool) {
RegistrationStatus memory status = registrationStatus[operator][operatorSet.key()];
// slashableUntil returns the last block the operator is slashable in so we check for
// less than or equal to
return status.registered || block.number <= status.slashableUntil;
}
/// @notice returns whether the operator's allocation is slashable in the given operator set
function _isAllocationSlashable(
OperatorSet memory operatorSet,
IStrategy strategy,
Allocation memory allocation,
bool isOperatorSlashable
) internal view returns (bool) {
/// forgefmt: disable-next-item
return
// If the operator set does not use this strategy, any allocation from it is not slashable
_operatorSetStrategies[operatorSet.key()].contains(address(strategy)) &&
// If the operator is not slashable by the operatorSet, any allocation is not slashable
isOperatorSlashable &&
// If there is nothing allocated, the allocation is not slashable
allocation.currentMagnitude != 0;
}
/**
* @dev For an operator set, get the operator's effective allocated magnitude.
* If the operator set has a pending deallocation that can be completed at the
* current block number, this method returns a view of the allocation as if the deallocation
* was completed.
* @return info the effective allocated and pending magnitude for the operator set, and
* the effective encumbered magnitude for all operator sets belonging to this strategy
*/
function _getUpdatedAllocation(
address operator,
bytes32 operatorSetKey,
IStrategy strategy
) internal view returns (StrategyInfo memory, Allocation memory) {
StrategyInfo memory info = StrategyInfo({
maxMagnitude: _maxMagnitudeHistory[operator][strategy].latest(),
encumberedMagnitude: encumberedMagnitude[operator][strategy]
});
Allocation memory allocation = allocations[operator][operatorSetKey][strategy];
// If the pending change can't be completed yet, return as-is
if (block.number < allocation.effectBlock) {
return (info, allocation);
}
// Otherwise, complete the pending change and return updated info
allocation.currentMagnitude = _addInt128(allocation.currentMagnitude, allocation.pendingDiff);
// If the completed change was a deallocation, update used magnitude
if (allocation.pendingDiff < 0) {
info.encumberedMagnitude = _addInt128(info.encumberedMagnitude, allocation.pendingDiff);
}
allocation.effectBlock = 0;
allocation.pendingDiff = 0;
return (info, allocation);
}
function _updateAllocationInfo(
address operator,
bytes32 operatorSetKey,
IStrategy strategy,
StrategyInfo memory info,
Allocation memory allocation
) internal {
// Update encumbered magnitude if it has changed
// The mapping should NOT be updated when there is a deallocation on a delay
if (encumberedMagnitude[operator][strategy] != info.encumberedMagnitude) {
encumberedMagnitude[operator][strategy] = info.encumberedMagnitude;
emit EncumberedMagnitudeUpdated(operator, strategy, info.encumberedMagnitude);
}
// Update allocation for this operator set from the strategy
// We emit an `AllocationUpdated` from the `modifyAllocations` and `slashOperator` functions.
// `clearDeallocationQueue` does not emit an `AllocationUpdated` event since it was
// emitted when the deallocation was queued
allocations[operator][operatorSetKey][strategy] = allocation;
// Note: these no-op if the sets already contain the added values (or do not contain removed ones)
if (allocation.pendingDiff != 0) {
// If we have a pending modification, ensure the allocation is in the operator's
// list of enumerable strategies/sets.
allocatedStrategies[operator][operatorSetKey].add(address(strategy));
allocatedSets[operator].add(operatorSetKey);
} else if (allocation.currentMagnitude == 0) {
// If we do NOT have a pending modification, and no existing magnitude, remove the
// allocation from the operator's lists.
allocatedStrategies[operator][operatorSetKey].remove(address(strategy));
if (allocatedStrategies[operator][operatorSetKey].length() == 0) {
allocatedSets[operator].remove(operatorSetKey);
}
}
}
function _updateMaxMagnitude(address operator, IStrategy strategy, uint64 newMaxMagnitude) internal {
_maxMagnitudeHistory[operator][strategy].push({key: uint32(block.number), value: newMaxMagnitude});
emit MaxMagnitudeUpdated(operator, strategy, newMaxMagnitude);
}
function _calcDelta(uint64 currentMagnitude, uint64 newMagnitude) internal pure returns (int128) {
return int128(uint128(newMagnitude)) - int128(uint128(currentMagnitude));
}
function _addInt128(uint64 a, int128 b) internal pure returns (uint64) {
return uint64(uint128(int128(uint128(a)) + b));
}
/**
*
* VIEW FUNCTIONS
*
*/
/// @inheritdoc IAllocationManager
function getOperatorSetCount(
address avs
) external view returns (uint256) {
return _operatorSets[avs].length();
}
/// @inheritdoc IAllocationManager
function getAllocatedSets(
address operator
) external view returns (OperatorSet[] memory) {
uint256 length = allocatedSets[operator].length();
OperatorSet[] memory operatorSets = new OperatorSet[](length);
for (uint256 i = 0; i < length; i++) {
operatorSets[i] = OperatorSetLib.decode(allocatedSets[operator].at(i));
}
return operatorSets;
}
/// @inheritdoc IAllocationManager
function getAllocatedStrategies(
address operator,
OperatorSet memory operatorSet
) external view returns (IStrategy[] memory) {
address[] memory values = allocatedStrategies[operator][operatorSet.key()].values();
IStrategy[] memory strategies;
assembly {
strategies := values
}
return strategies;
}
/// @inheritdoc IAllocationManager
function getAllocation(
address operator,
OperatorSet memory operatorSet,
IStrategy strategy
) public view returns (Allocation memory) {
(, Allocation memory allocation) = _getUpdatedAllocation(operator, operatorSet.key(), strategy);
return allocation;
}
/// @inheritdoc IAllocationManager
function getAllocations(
address[] memory operators,
OperatorSet memory operatorSet,
IStrategy strategy
) external view returns (Allocation[] memory) {
Allocation[] memory _allocations = new Allocation[](operators.length);
for (uint256 i = 0; i < operators.length; i++) {
_allocations[i] = getAllocation(operators[i], operatorSet, strategy);
}
return _allocations;
}
/// @inheritdoc IAllocationManager
function getStrategyAllocations(
address operator,
IStrategy strategy
) external view returns (OperatorSet[] memory, Allocation[] memory) {
uint256 length = allocatedSets[operator].length();
OperatorSet[] memory operatorSets = new OperatorSet[](length);
Allocation[] memory _allocations = new Allocation[](length);
for (uint256 i = 0; i < length; i++) {
OperatorSet memory operatorSet = OperatorSetLib.decode(allocatedSets[operator].at(i));
operatorSets[i] = operatorSet;
_allocations[i] = getAllocation(operator, operatorSet, strategy);
}
return (operatorSets, _allocations);
}
/// @inheritdoc IAllocationManager
function getAllocatableMagnitude(address operator, IStrategy strategy) external view returns (uint64) {
// This method needs to simulate clearing any pending deallocations.
// This roughly mimics the calculations done in `_clearDeallocationQueue` and
// `_getUpdatedAllocation`, while operating on a `curEncumberedMagnitude`
// rather than continually reading/updating state.
uint64 curEncumberedMagnitude = encumberedMagnitude[operator][strategy];
uint256 length = deallocationQueue[operator][strategy].length();
for (uint256 i = 0; i < length; ++i) {
bytes32 operatorSetKey = deallocationQueue[operator][strategy].at(i);
Allocation memory allocation = allocations[operator][operatorSetKey][strategy];
// If we've reached a pending deallocation that isn't completable yet,
// we can stop. Any subsequent modifications will also be uncompletable.
if (block.number < allocation.effectBlock) {
break;
}
// The diff is a deallocation. Add to encumbered magnitude. Note that this is a deallocation
// queue and allocations aren't considered because encumbered magnitude
// is updated as soon as the allocation is created.
curEncumberedMagnitude = _addInt128(curEncumberedMagnitude, allocation.pendingDiff);
}
// The difference between the operator's max magnitude and its encumbered magnitude
// is the magnitude that can be allocated.
return _maxMagnitudeHistory[operator][strategy].latest() - curEncumberedMagnitude;
}
/// @inheritdoc IAllocationManager
function getMaxMagnitude(address operator, IStrategy strategy) public view returns (uint64) {
return _maxMagnitudeHistory[operator][strategy].latest();
}
/// @inheritdoc IAllocationManager
function getMaxMagnitudes(
address operator,
IStrategy[] memory strategies
) external view returns (uint64[] memory) {
uint64[] memory maxMagnitudes = new uint64[](strategies.length);
for (uint256 i = 0; i < strategies.length; ++i) {
maxMagnitudes[i] = getMaxMagnitude(operator, strategies[i]);
}
return maxMagnitudes;
}
/// @inheritdoc IAllocationManager
function getMaxMagnitudes(address[] memory operators, IStrategy strategy) external view returns (uint64[] memory) {
uint64[] memory maxMagnitudes = new uint64[](operators.length);
for (uint256 i = 0; i < operators.length; ++i) {
maxMagnitudes[i] = getMaxMagnitude(operators[i], strategy);
}
return maxMagnitudes;
}
/// @inheritdoc IAllocationManager
function getMaxMagnitudesAtBlock(
address operator,
IStrategy[] memory strategies,
uint32 blockNumber
) external view returns (uint64[] memory) {
uint64[] memory maxMagnitudes = new uint64[](strategies.length);
for (uint256 i = 0; i < strategies.length; ++i) {
maxMagnitudes[i] = _maxMagnitudeHistory[operator][strategies[i]].upperLookup({key: blockNumber});
}
return maxMagnitudes;
}
/// @inheritdoc IAllocationManager
function getAllocationDelay(
address operator
) public view returns (bool, uint32) {
AllocationDelayInfo memory info = _allocationDelayInfo[operator];
uint32 delay = info.delay;
bool isSet = info.isSet;
// If there is a pending delay that can be applied, apply it
if (info.effectBlock != 0 && block.number >= info.effectBlock) {
delay = info.pendingDelay;
isSet = true;
}
return (isSet, delay);
}
/// @inheritdoc IAllocationManager
function getRegisteredSets(
address operator
) public view returns (OperatorSet[] memory) {
uint256 length = registeredSets[operator].length();
OperatorSet[] memory operatorSets = new OperatorSet[](length);
for (uint256 i = 0; i < length; ++i) {
operatorSets[i] = OperatorSetLib.decode(registeredSets[operator].at(i));
}
return operatorSets;
}
/// @inheritdoc IAllocationManager
function isMemberOfOperatorSet(address operator, OperatorSet memory operatorSet) public view returns (bool) {
return _operatorSetMembers[operatorSet.key()].contains(operator);
}
/// @inheritdoc IAllocationManager
function isOperatorSet(
OperatorSet memory operatorSet
) external view returns (bool) {
return _operatorSets[operatorSet.avs].contains(operatorSet.id);
}
/// @inheritdoc IAllocationManager
function getMembers(
OperatorSet memory operatorSet
) external view returns (address[] memory) {
return _operatorSetMembers[operatorSet.key()].values();
}
/// @inheritdoc IAllocationManager
function getMemberCount(
OperatorSet memory operatorSet
) external view returns (uint256) {
return _operatorSetMembers[operatorSet.key()].length();
}
/// @inheritdoc IAllocationManager
function getAVSRegistrar(
address avs
) public view returns (IAVSRegistrar) {
IAVSRegistrar registrar = _avsRegistrar[avs];
return address(registrar) == address(0) ? IAVSRegistrar(avs) : registrar;
}
/// @inheritdoc IAllocationManager
function getStrategiesInOperatorSet(
OperatorSet memory operatorSet
) external view returns (IStrategy[] memory) {
address[] memory values = _operatorSetStrategies[operatorSet.key()].values();
IStrategy[] memory strategies;
assembly {
strategies := values
}
return strategies;
}
/// @inheritdoc IAllocationManager
function getMinimumSlashableStake(
OperatorSet memory operatorSet,
address[] memory operators,
IStrategy[] memory strategies,
uint32 futureBlock
) external view returns (uint256[][] memory slashableStake) {
slashableStake = new uint256[][](operators.length);
uint256[][] memory delegatedStake = delegation.getOperatorsShares(operators, strategies);
for (uint256 i = 0; i < operators.length; i++) {
address operator = operators[i];
slashableStake[i] = new uint256[](strategies.length);
for (uint256 j = 0; j < strategies.length; j++) {
IStrategy strategy = strategies[j];
// Fetch the max magnitude and allocation for the operator/strategy.
// Prevent division by 0 if needed. This mirrors the "FullySlashed" checks
// in the DelegationManager
uint64 maxMagnitude = _maxMagnitudeHistory[operator][strategy].latest();
if (maxMagnitude == 0) {
continue;
}
Allocation memory alloc = getAllocation(operator, operatorSet, strategy);
// If the pending change takes effect before `futureBlock`, include it in `currentMagnitude`
// However, ONLY include the pending change if it is a deallocation, since this method
// is supposed to return the minimum slashable stake between now and `futureBlock`
if (alloc.effectBlock <= futureBlock && alloc.pendingDiff < 0) {
alloc.currentMagnitude = _addInt128(alloc.currentMagnitude, alloc.pendingDiff);
}
uint256 slashableProportion = uint256(alloc.currentMagnitude).divWad(maxMagnitude);
slashableStake[i][j] = delegatedStake[i][j].mulWad(slashableProportion);
}
}
}
}