Skip to content

Commit

Permalink
chore: forge fmt src/contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
0xClandestine committed Oct 2, 2024
1 parent 399e6f7 commit 319816c
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 165 deletions.
10 changes: 7 additions & 3 deletions src/contracts/core/AVSDirectory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ contract AVSDirectory is
address operator
) external override onlyWhenNotPaused(PAUSED_OPERATOR_REGISTER_DEREGISTER_TO_AVS) {
// Assert that operator is registered for the AVS.
require(avsOperatorStatus[msg.sender][operator] == OperatorAVSRegistrationStatus.REGISTERED, OperatorNotRegistered());
require(
avsOperatorStatus[msg.sender][operator] == OperatorAVSRegistrationStatus.REGISTERED, OperatorNotRegistered()
);
// Assert that the AVS is not an operator set AVS.
require(!isOperatorSetAVS[msg.sender], InvalidAVS());

Expand Down Expand Up @@ -484,12 +486,14 @@ contract AVSDirectory is

OperatorSetRegistrationStatus memory status =
operatorSetStatus[operatorSet.avs][operator][operatorSet.operatorSetId];

return block.timestamp < status.lastDeregisteredTimestamp + DEALLOCATION_DELAY;
}

/// @notice Returns true if all provided operator sets are valid.
function isOperatorSetBatch(OperatorSet[] calldata operatorSets) public view returns (bool) {
function isOperatorSetBatch(
OperatorSet[] calldata operatorSets
) public view returns (bool) {
for (uint256 i = 0; i < operatorSets.length; ++i) {
if (!isOperatorSet[operatorSets[i].avs][operatorSets[i].operatorSetId]) return false;
}
Expand Down
9 changes: 3 additions & 6 deletions src/contracts/core/AVSDirectoryStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ abstract contract AVSDirectoryStorage is IAVSDirectory {

/// @dev Returns the chain ID from the time the contract was deployed.
uint256 internal immutable ORIGINAL_CHAIN_ID;

// Mutatables

/**
* @notice Original EIP-712 Domain separator for this contract.
* @dev The domain separator may change in the event of a fork that modifies the ChainID.
Expand Down Expand Up @@ -86,10 +86,7 @@ abstract contract AVSDirectoryStorage is IAVSDirectory {

// Construction

constructor(
IDelegationManager _delegation,
uint32 _DEALLOCATION_DELAY
) {
constructor(IDelegationManager _delegation, uint32 _DEALLOCATION_DELAY) {
delegation = _delegation;
DEALLOCATION_DELAY = _DEALLOCATION_DELAY;
ORIGINAL_CHAIN_ID = block.chainid;
Expand Down
68 changes: 41 additions & 27 deletions src/contracts/core/AllocationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ contract AllocationManager is
IAVSDirectory _avsDirectory,
uint32 _DEALLOCATION_DELAY,
uint32 _ALLOCATION_CONFIGURATION_DELAY
)
AllocationManagerStorage(
_delegation,
_avsDirectory,
_DEALLOCATION_DELAY,
_ALLOCATION_CONFIGURATION_DELAY
)
{
) AllocationManagerStorage(_delegation, _avsDirectory, _DEALLOCATION_DELAY, _ALLOCATION_CONFIGURATION_DELAY) {
_disableInitializers();
}

Expand Down Expand Up @@ -133,7 +126,7 @@ contract AllocationManager is
MagnitudeAllocation calldata allocation = allocations[i];
require(allocation.operatorSets.length == allocation.magnitudes.length, InputArrayLengthMismatch());
require(avsDirectory.isOperatorSetBatch(allocation.operatorSets), InvalidOperatorSet());

// 1. For the given (operator,strategy) complete any pending deallocations to update free magnitude
_completePendingDeallocations({
operator: operator,
Expand All @@ -147,34 +140,46 @@ contract AllocationManager is
// proprtional magnitudes relative to each other.

// Load the operator's total magnitude for the strategy.
(bool exists,, uint224 currentTotalMagnitude) = _totalMagnitudeUpdate[operator][allocation.strategy].latestSnapshot();
(bool exists,, uint224 currentTotalMagnitude) =
_totalMagnitudeUpdate[operator][allocation.strategy].latestSnapshot();

// If the operator has no total magnitude snapshot, set it to WAD, which denotes an unslashed operator.
if (!exists) {
currentTotalMagnitude = WAD;
_totalMagnitudeUpdate[operator][allocation.strategy].push({key: uint32(block.timestamp), value: currentTotalMagnitude});
operatorFreeMagnitudeInfo[operator][allocation.strategy].freeMagnitude = uint64(currentTotalMagnitude);
_totalMagnitudeUpdate[operator][allocation.strategy].push({
key: uint32(block.timestamp),
value: currentTotalMagnitude
});
operatorFreeMagnitudeInfo[operator][allocation.strategy].freeMagnitude =
uint64(currentTotalMagnitude);
}

// 3. set allocations for the strategy after updating freeMagnitude
require(uint64(currentTotalMagnitude) == allocation.expectedTotalMagnitude, InvalidExpectedTotalMagnitude());
require(
uint64(currentTotalMagnitude) == allocation.expectedTotalMagnitude, InvalidExpectedTotalMagnitude()
);
}

for (uint256 j = 0; j < allocation.operatorSets.length; ++j) {
// Check that there are no pending allocations & deallocations for the operator, operatorSet, strategy
MagnitudeInfo memory mInfo = _getCurrentEffectiveMagnitude(operator, allocation.strategy, _encodeOperatorSet(allocation.operatorSets[j]));
MagnitudeInfo memory mInfo = _getCurrentEffectiveMagnitude(
operator, allocation.strategy, _encodeOperatorSet(allocation.operatorSets[j])
);
require(block.timestamp >= mInfo.effectTimestamp, ModificationAlreadyPending());

// Calculate the new pending diff with this modification
mInfo.pendingMagnitudeDiff = int128(uint128(allocation.magnitudes[j])) - int128(uint128(mInfo.currentMagnitude));
mInfo.pendingMagnitudeDiff =
int128(uint128(allocation.magnitudes[j])) - int128(uint128(mInfo.currentMagnitude));
require(mInfo.pendingMagnitudeDiff != 0, SameMagnitude());

// Handle deallocation/allocation and modification effect timestamp
if (mInfo.pendingMagnitudeDiff < 0) {
// This is a deallocation

// 1. push PendingFreeMagnitude and respective array index into (op,opSet,Strategy) queued deallocations
deallocationQueue[operator][allocation.strategy].push(_encodeOperatorSet(allocation.operatorSets[j]));
deallocationQueue[operator][allocation.strategy].push(
_encodeOperatorSet(allocation.operatorSets[j])
);

// 2. Update the effect timestamp for the deallocation
mInfo.effectTimestamp = uint32(block.timestamp) + DEALLOCATION_DELAY;
Expand All @@ -188,13 +193,14 @@ contract AllocationManager is
freeInfo.freeMagnitude -= magnitudeToAllocate;

// 2. Update the effectTimestamp for the allocation
mInfo.effectTimestamp = uint32(block.timestamp) + operatorAllocationDelay;
mInfo.effectTimestamp = uint32(block.timestamp) + operatorAllocationDelay;

operatorFreeMagnitudeInfo[operator][allocation.strategy] = freeInfo;
}

// Allocate magnitude which will take effect at the `effectTimestamp`
_operatorMagnitudeInfo[operator][allocation.strategy][_encodeOperatorSet(allocation.operatorSets[j])] = mInfo;
_operatorMagnitudeInfo[operator][allocation.strategy][_encodeOperatorSet(allocation.operatorSets[j])] =
mInfo;
}
}
}
Expand Down Expand Up @@ -224,12 +230,12 @@ contract AllocationManager is
for (uint256 i = 0; i < strategies.length; ++i) {
// 1. Slash from pending deallocations and allocations
MagnitudeInfo memory mInfo = _getCurrentEffectiveMagnitude(operator, strategies[i], operatorSetKey);

uint64 slashedMagnitude = uint64(mInfo.currentMagnitude * bipsToSlash / BIPS_FACTOR);
mInfo.currentMagnitude -= slashedMagnitude;
// if there is a pending deallocation, slash pending deallocation proportionally
if (mInfo.pendingMagnitudeDiff < 0) {
uint128 slashedPending = uint128(uint128(-mInfo.pendingMagnitudeDiff )* bipsToSlash / BIPS_FACTOR);
uint128 slashedPending = uint128(uint128(-mInfo.pendingMagnitudeDiff) * bipsToSlash / BIPS_FACTOR);
mInfo.pendingMagnitudeDiff += int128(slashedPending);
}
// update operatorMagnitudeInfo
Expand Down Expand Up @@ -285,7 +291,7 @@ contract AllocationManager is
*/
function _completePendingDeallocations(address operator, IStrategy strategy, uint16 numToComplete) internal {
FreeMagnitudeInfo memory freeInfo = operatorFreeMagnitudeInfo[operator][strategy];

uint256 numDeallocations = deallocationQueue[operator][strategy].length;
uint256 completed;

Expand Down Expand Up @@ -319,7 +325,11 @@ contract AllocationManager is

/// @dev Fetch the operator's current magnitude, applying a pending diff if the effect timestamp is passed
/// @notice This may return something that is not recorded in state. Remember to store this updated value if needed!
function _getCurrentEffectiveMagnitude(address operator, IStrategy strategy, bytes32 operatorSetKey) internal view returns (MagnitudeInfo memory) {
function _getCurrentEffectiveMagnitude(
address operator,
IStrategy strategy,
bytes32 operatorSetKey
) internal view returns (MagnitudeInfo memory) {
MagnitudeInfo memory mInfo = _operatorMagnitudeInfo[operator][strategy][operatorSetKey];

// If the magnitude change is not yet in effect, return unaltered
Expand Down Expand Up @@ -369,7 +379,8 @@ contract AllocationManager is
for (uint256 i = 0; i < strategies.length; ++i) {
slashableMagnitudes[i] = new uint64[](operatorSets.length);
for (uint256 j = 0; j < operatorSets.length; ++j) {
MagnitudeInfo memory mInfo = _getCurrentEffectiveMagnitude(operator, strategies[i], _encodeOperatorSet(operatorSets[j]));
MagnitudeInfo memory mInfo =
_getCurrentEffectiveMagnitude(operator, strategies[i], _encodeOperatorSet(operatorSets[j]));
slashableMagnitudes[i][j] = mInfo.currentMagnitude;
}
}
Expand Down Expand Up @@ -479,10 +490,12 @@ contract AllocationManager is
pendingMagnitudes = new uint64[](operatorSets.length);
timestamps = new uint32[](operatorSets.length);
for (uint256 i = 0; i < operatorSets.length; ++i) {
MagnitudeInfo memory opsetMagnitudeInfo = _operatorMagnitudeInfo[operator][strategy][_encodeOperatorSet(operatorSets[i])];
MagnitudeInfo memory opsetMagnitudeInfo =
_operatorMagnitudeInfo[operator][strategy][_encodeOperatorSet(operatorSets[i])];

if (opsetMagnitudeInfo.effectTimestamp < block.timestamp && opsetMagnitudeInfo.pendingMagnitudeDiff > 0) {
pendingMagnitudes[i] = opsetMagnitudeInfo.currentMagnitude + uint64(uint128(opsetMagnitudeInfo.pendingMagnitudeDiff));
pendingMagnitudes[i] =
opsetMagnitudeInfo.currentMagnitude + uint64(uint128(opsetMagnitudeInfo.pendingMagnitudeDiff));
timestamps[i] = opsetMagnitudeInfo.effectTimestamp;
} else {
pendingMagnitudes[i] = 0;
Expand Down Expand Up @@ -510,7 +523,8 @@ contract AllocationManager is
timestamps = new uint32[](operatorSets.length);

for (uint256 i = 0; i < operatorSets.length; ++i) {
MagnitudeInfo memory opsetMagnitudeInfo = _operatorMagnitudeInfo[operator][strategy][_encodeOperatorSet(operatorSets[i])];
MagnitudeInfo memory opsetMagnitudeInfo =
_operatorMagnitudeInfo[operator][strategy][_encodeOperatorSet(operatorSets[i])];

if (opsetMagnitudeInfo.effectTimestamp < block.timestamp && opsetMagnitudeInfo.pendingMagnitudeDiff < 0) {
pendingMagnitudeDiffs[i] = uint64(uint128(-opsetMagnitudeInfo.pendingMagnitudeDiff));
Expand Down
54 changes: 25 additions & 29 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ contract DelegationManager is
IEigenPodManager _eigenPodManager,
IAllocationManager _allocationManager,
uint32 _MIN_WITHDRAWAL_DELAY
)
)
DelegationManagerStorage(
_avsDirectory,
_strategyManager,
_eigenPodManager,
_allocationManager,
_avsDirectory,
_strategyManager,
_eigenPodManager,
_allocationManager,
_MIN_WITHDRAWAL_DELAY
)
{
)
{
_disableInitializers();
}

Expand Down Expand Up @@ -396,13 +396,15 @@ contract DelegationManager is
Shares existingShares,
uint64 proportionOfOldBalance
) external onlyEigenPodManager {
DelegatedShares delegatedSharesBefore = existingShares.toDelegatedShares(stakerScalingFactors[staker][beaconChainETHStrategy]);
DelegatedShares delegatedSharesBefore =
existingShares.toDelegatedShares(stakerScalingFactors[staker][beaconChainETHStrategy]);

// decrease the staker's beaconChainScalingFactor proportionally
// forgefmt: disable-next-item
stakerScalingFactors[staker][beaconChainETHStrategy].decreaseBeaconChainScalingFactor(proportionOfOldBalance);

DelegatedShares delegatedSharesAfter = existingShares.toDelegatedShares(stakerScalingFactors[staker][beaconChainETHStrategy]);
DelegatedShares delegatedSharesAfter =
existingShares.toDelegatedShares(stakerScalingFactors[staker][beaconChainETHStrategy]);

// if the staker is delegated to an operators
if (isDelegated(staker)) {
Expand Down Expand Up @@ -538,10 +540,9 @@ contract DelegationManager is

for (uint256 i = 0; i < withdrawal.strategies.length; i++) {
IShareManager shareManager = _getShareManager(withdrawal.strategies[i]);
OwnedShares ownedSharesToWithdraw =
withdrawal.delegatedShares[i]
.scaleForCompleteWithdrawal(stakerScalingFactors[withdrawal.staker][withdrawal.strategies[i]])
.toOwnedShares(totalMagnitudes[i]);
OwnedShares ownedSharesToWithdraw = withdrawal.delegatedShares[i].scaleForCompleteWithdrawal(
stakerScalingFactors[withdrawal.staker][withdrawal.strategies[i]]
).toOwnedShares(totalMagnitudes[i]);

if (receiveAsTokens) {
// Withdraws `shares` in `strategy` to `withdrawer`. If the shares are virtual beaconChainETH shares,
Expand Down Expand Up @@ -619,7 +620,7 @@ contract DelegationManager is
) internal {
// based on total magnitude, decrement operator's delegatedShares
operatorDelegatedShares[operator][strategy] = operatorDelegatedShares[operator][strategy].sub(delegatedShares);

// TODO: What to do about event wrt scaling?
emit OperatorSharesDecreased(operator, staker, strategy, delegatedShares);
}
Expand Down Expand Up @@ -647,7 +648,7 @@ contract DelegationManager is

// delegatedShares for staker to place into queueWithdrawal
DelegatedShares delegatedSharesToRemove = ownedSharesToWithdraw[i].toDelegatedShares(totalMagnitudes[i]);
// TODO: should this include beaconChainScalingFactor?
// TODO: should this include beaconChainScalingFactor?
Shares sharesToWithdraw = delegatedSharesToRemove.toShares(stakerScalingFactors[staker][strategies[i]]);
// TODO: maybe have a getter to get shares for all strategies, like getDelegatableShares
// check sharesToWithdraw is valid
Expand All @@ -665,7 +666,8 @@ contract DelegationManager is
delegatedShares: delegatedSharesToRemove
});
}
delegatedSharesToWithdraw[i] = delegatedSharesToRemove.scaleForQueueWithdrawal(stakerScalingFactors[staker][strategies[i]]);
delegatedSharesToWithdraw[i] =
delegatedSharesToRemove.scaleForQueueWithdrawal(stakerScalingFactors[staker][strategies[i]]);

// Remove active shares from EigenPodManager/StrategyManager
// EigenPodManager: this call will revert if it would reduce the Staker's virtual beacon chain ETH shares below zero
Expand Down Expand Up @@ -747,21 +749,15 @@ contract DelegationManager is
// newShares
// = newPrincipalShares.toDelegatedShares(stakerScalingFactors[staker][strategy).toOwnedShares(totalMagnitude)
// = newPrincipalShares * newDepositScalingFactor / WAD * beaonChainScalingFactor / WAD * totalMagnitude / WAD
// = (existingPrincipalShares + addedShares) * newDepositScalingFactor / WAD * beaonChainScalingFactor / WAD * totalMagnitude / WAD
// = (existingPrincipalShares + addedShares) * newDepositScalingFactor / WAD * beaonChainScalingFactor / WAD * totalMagnitude / WAD
//
// we can solve for
//
OwnedShares existingOwnedShares =
existingShares
.toDelegatedShares(stakerScalingFactors[staker][strategy])
.toOwnedShares(totalMagnitude);
newDepositScalingFactor =
existingOwnedShares
.add(addedOwnedShares)
.unwrap()
.divWad(existingShares.unwrap() + addedOwnedShares.unwrap())
.divWad(stakerScalingFactors[staker][strategy].getBeaconChainScalingFactor())
.divWad(totalMagnitude);
existingShares.toDelegatedShares(stakerScalingFactors[staker][strategy]).toOwnedShares(totalMagnitude);
newDepositScalingFactor = existingOwnedShares.add(addedOwnedShares).unwrap().divWad(
existingShares.unwrap() + addedOwnedShares.unwrap()
).divWad(stakerScalingFactors[staker][strategy].getBeaconChainScalingFactor()).divWad(totalMagnitude);
}

// update the staker's depositScalingFactor
Expand Down Expand Up @@ -863,15 +859,15 @@ contract DelegationManager is
IShareManager shareManager = _getShareManager(strategies[i]);
// TODO: batch call for strategyManager shares?
// 1. read strategy deposit shares

// forgefmt: disable-next-item
Shares shares = shareManager.stakerStrategyShares(staker, strategies[i]);

// 2. if the staker is delegated, actual withdrawable shares can be different from what is stored
// in the StrategyManager/EigenPodManager because they could have been slashed
if (operator != address(0)) {
uint64 totalMagnitude = allocationManager.getTotalMagnitude(operator, strategies[i]);

// forgefmt: disable-next-item
ownedShares[i] = shares
.toDelegatedShares(stakerScalingFactors[staker][strategies[i]])
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/core/DelegationManagerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract contract DelegationManagerStorage is IDelegationManager {
uint256 internal immutable ORIGINAL_CHAIN_ID;

// Mutatables

/**
* @notice Original EIP-712 Domain separator for this contract.
* @dev The domain separator may change in the event of a fork that modifies the ChainID.
Expand Down
16 changes: 8 additions & 8 deletions src/contracts/core/RewardsCoordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ contract RewardsCoordinator is
uint32 _MAX_RETROACTIVE_LENGTH,
uint32 _MAX_FUTURE_LENGTH,
uint32 _GENESIS_REWARDS_TIMESTAMP
)
)
RewardsCoordinatorStorage(
_delegationManager,
_strategyManager,
_CALCULATION_INTERVAL_SECONDS,
_MAX_REWARDS_DURATION,
_MAX_RETROACTIVE_LENGTH,
_MAX_FUTURE_LENGTH,
_delegationManager,
_strategyManager,
_CALCULATION_INTERVAL_SECONDS,
_MAX_REWARDS_DURATION,
_MAX_RETROACTIVE_LENGTH,
_MAX_FUTURE_LENGTH,
_GENESIS_REWARDS_TIMESTAMP
)
)
{
_disableInitializers();
}
Expand Down
Loading

0 comments on commit 319816c

Please sign in to comment.