Skip to content

Commit 23fde22

Browse files
committed
chore: bring back single call to dm
1 parent 5fa2377 commit 23fde22

File tree

2 files changed

+171
-164
lines changed

2 files changed

+171
-164
lines changed

src/contracts/core/AllocationManager.sol

Lines changed: 89 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -67,95 +67,15 @@ contract AllocationManager is
6767
external
6868
onlyWhenNotPaused(PAUSED_OPERATOR_SLASHING)
6969
checkCanCall(avs)
70-
returns (uint256 slashId, uint256[] memory shares)
70+
returns (uint256, uint256[] memory)
7171
{
7272
// Check that the operator set exists and the operator is registered to it
7373
OperatorSet memory operatorSet = OperatorSet(avs, params.operatorSetId);
7474
require(params.strategies.length == params.wadsToSlash.length, InputArrayLengthMismatch());
7575
require(_operatorSets[operatorSet.avs].contains(operatorSet.id), InvalidOperatorSet());
7676
require(isOperatorSlashable(params.operator, operatorSet), OperatorNotSlashable());
7777

78-
uint256[] memory wadSlashed = new uint256[](params.strategies.length);
79-
shares = new uint256[](params.strategies.length);
80-
81-
// Increment the slash count for the operator set.
82-
slashId = ++_slashCount[operatorSet.key()];
83-
84-
// For each strategy in the operator set, slash any existing allocation
85-
for (uint256 i = 0; i < params.strategies.length; i++) {
86-
// Check that `strategies` is in ascending order.
87-
require(
88-
i == 0 || uint160(address(params.strategies[i])) > uint160(address(params.strategies[i - 1])),
89-
StrategiesMustBeInAscendingOrder()
90-
);
91-
// Check that `wadToSlash` is within acceptable bounds.
92-
require(0 < params.wadsToSlash[i] && params.wadsToSlash[i] <= WAD, InvalidWadToSlash());
93-
// Check that the operator set contains the strategy.
94-
require(
95-
_operatorSetStrategies[operatorSet.key()].contains(address(params.strategies[i])),
96-
StrategyNotInOperatorSet()
97-
);
98-
99-
// 1. Get the operator's allocation info for the strategy and operator set
100-
(StrategyInfo memory info, Allocation memory allocation) =
101-
_getUpdatedAllocation(params.operator, operatorSet.key(), params.strategies[i]);
102-
103-
// 2. Skip if the operator does not have a slashable allocation
104-
// NOTE: this "if" is equivalent to: `if (!_isAllocationSlashable)`, because the other
105-
// conditions in this method are already true (isOperatorSlashable + operatorSetStrategies.contains)
106-
if (allocation.currentMagnitude == 0) {
107-
continue;
108-
}
109-
110-
// 3. Calculate the amount of magnitude being slashed, and subtract from
111-
// the operator's currently-allocated magnitude, as well as the strategy's
112-
// max and encumbered magnitudes
113-
uint64 slashedMagnitude = uint64(uint256(allocation.currentMagnitude).mulWadRoundUp(params.wadsToSlash[i]));
114-
uint64 prevMaxMagnitude = info.maxMagnitude;
115-
wadSlashed[i] = uint256(slashedMagnitude).divWad(info.maxMagnitude);
116-
117-
allocation.currentMagnitude -= slashedMagnitude;
118-
info.maxMagnitude -= slashedMagnitude;
119-
info.encumberedMagnitude -= slashedMagnitude;
120-
121-
// 4. If there is a pending deallocation, reduce the pending deallocation proportionally.
122-
// This ensures that when the deallocation is completed, less magnitude is freed.
123-
if (allocation.pendingDiff < 0) {
124-
uint64 slashedPending =
125-
uint64(uint256(uint128(-allocation.pendingDiff)).mulWadRoundUp(params.wadsToSlash[i]));
126-
allocation.pendingDiff += int128(uint128(slashedPending));
127-
128-
emit AllocationUpdated(
129-
params.operator,
130-
operatorSet,
131-
params.strategies[i],
132-
_addInt128(allocation.currentMagnitude, allocation.pendingDiff),
133-
allocation.effectBlock
134-
);
135-
}
136-
137-
// 5. Update state
138-
_updateAllocationInfo(params.operator, operatorSet.key(), params.strategies[i], info, allocation);
139-
140-
// Emit an event for the updated allocation
141-
emit AllocationUpdated(
142-
params.operator, operatorSet, params.strategies[i], allocation.currentMagnitude, uint32(block.number)
143-
);
144-
145-
_updateMaxMagnitude(params.operator, params.strategies[i], info.maxMagnitude);
146-
147-
// 6. Slash operators shares in the DelegationManager
148-
shares[i] = delegation.slashOperatorShares({
149-
operator: params.operator,
150-
operatorSet: operatorSet,
151-
slashId: slashId,
152-
strategy: params.strategies[i],
153-
prevMaxMagnitude: prevMaxMagnitude,
154-
newMaxMagnitude: info.maxMagnitude
155-
});
156-
}
157-
158-
emit OperatorSlashed(params.operator, operatorSet, params.strategies, wadSlashed, params.description);
78+
return _slashOperator(params, operatorSet);
15979
}
16080

16181
/// @inheritdoc IAllocationManager
@@ -401,6 +321,93 @@ contract AllocationManager is
401321
*
402322
*/
403323

324+
function _slashOperator(
325+
SlashingParams calldata params,
326+
OperatorSet memory operatorSet
327+
) internal returns (uint256 slashId, uint256[] memory shares) {
328+
uint256[] memory wadSlashed = new uint256[](params.strategies.length);
329+
shares = new uint256[](params.strategies.length);
330+
331+
// Increment the slash count for the operator set.
332+
slashId = ++_slashCount[operatorSet.key()];
333+
334+
// For each strategy in the operator set, slash any existing allocation
335+
for (uint256 i = 0; i < params.strategies.length; i++) {
336+
// Check that `strategies` is in ascending order.
337+
require(
338+
i == 0 || uint160(address(params.strategies[i])) > uint160(address(params.strategies[i - 1])),
339+
StrategiesMustBeInAscendingOrder()
340+
);
341+
// Check that `wadToSlash` is within acceptable bounds.
342+
require(0 < params.wadsToSlash[i] && params.wadsToSlash[i] <= WAD, InvalidWadToSlash());
343+
// Check that the operator set contains the strategy.
344+
require(
345+
_operatorSetStrategies[operatorSet.key()].contains(address(params.strategies[i])),
346+
StrategyNotInOperatorSet()
347+
);
348+
349+
// 1. Get the operator's allocation info for the strategy and operator set
350+
(StrategyInfo memory info, Allocation memory allocation) =
351+
_getUpdatedAllocation(params.operator, operatorSet.key(), params.strategies[i]);
352+
353+
// 2. Skip if the operator does not have a slashable allocation
354+
// NOTE: this "if" is equivalent to: `if (!_isAllocationSlashable)`, because the other
355+
// conditions in this method are already true (isOperatorSlashable + operatorSetStrategies.contains)
356+
if (allocation.currentMagnitude == 0) {
357+
continue;
358+
}
359+
360+
// 3. Calculate the amount of magnitude being slashed, and subtract from
361+
// the operator's currently-allocated magnitude, as well as the strategy's
362+
// max and encumbered magnitudes
363+
uint64 slashedMagnitude = uint64(uint256(allocation.currentMagnitude).mulWadRoundUp(params.wadsToSlash[i]));
364+
uint64 prevMaxMagnitude = info.maxMagnitude;
365+
wadSlashed[i] = uint256(slashedMagnitude).divWad(info.maxMagnitude);
366+
367+
allocation.currentMagnitude -= slashedMagnitude;
368+
info.maxMagnitude -= slashedMagnitude;
369+
info.encumberedMagnitude -= slashedMagnitude;
370+
371+
// 4. If there is a pending deallocation, reduce the pending deallocation proportionally.
372+
// This ensures that when the deallocation is completed, less magnitude is freed.
373+
if (allocation.pendingDiff < 0) {
374+
uint64 slashedPending =
375+
uint64(uint256(uint128(-allocation.pendingDiff)).mulWadRoundUp(params.wadsToSlash[i]));
376+
allocation.pendingDiff += int128(uint128(slashedPending));
377+
378+
emit AllocationUpdated(
379+
params.operator,
380+
operatorSet,
381+
params.strategies[i],
382+
_addInt128(allocation.currentMagnitude, allocation.pendingDiff),
383+
allocation.effectBlock
384+
);
385+
}
386+
387+
// 5. Update state
388+
_updateAllocationInfo(params.operator, operatorSet.key(), params.strategies[i], info, allocation);
389+
390+
// Emit an event for the updated allocation
391+
emit AllocationUpdated(
392+
params.operator, operatorSet, params.strategies[i], allocation.currentMagnitude, uint32(block.number)
393+
);
394+
395+
_updateMaxMagnitude(params.operator, params.strategies[i], info.maxMagnitude);
396+
397+
// 6. Slash operators shares in the DelegationManager
398+
shares[i] = delegation.slashOperatorShares({
399+
operator: params.operator,
400+
operatorSet: operatorSet,
401+
slashId: slashId,
402+
strategy: params.strategies[i],
403+
prevMaxMagnitude: prevMaxMagnitude,
404+
newMaxMagnitude: info.maxMagnitude
405+
});
406+
}
407+
408+
emit OperatorSlashed(params.operator, operatorSet, params.strategies, wadSlashed, params.description);
409+
}
410+
404411
/**
405412
* @dev Adds a strategy to an operator set.
406413
* @param operatorSet The operator set to add the strategy to.

0 commit comments

Comments
 (0)