Skip to content

Commit

Permalink
change types
Browse files Browse the repository at this point in the history
  • Loading branch information
gpsanant committed Sep 21, 2024
1 parent a35f181 commit 7190c66
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
15 changes: 8 additions & 7 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ contract DelegationManager is

// all shares and queued withdrawn and no delegated operator
// reset staker's depositScalingFactor to default
depositScalingFactors[staker][strategies[i]] = DefaultWadUint256.wrap(WAD);
depositScalingFactors[staker][strategies[i]].set(WAD);
}
}

Expand Down Expand Up @@ -418,7 +418,7 @@ contract DelegationManager is
function decreaseBeaconChainScalingFactor(
address staker,
uint256 existingPrincipalShares,
uint256 proportionPodBalanceDecrease
uint64 proportionPodBalanceDecrease
) external onlyEigenPodManager {
// todo: set beaconChainScalingFactors
DelegatedShares delegatedSharesBefore = PrincipalShares.wrap(existingPrincipalShares).toDelegatedShares(
Expand All @@ -427,7 +427,7 @@ contract DelegationManager is
);

// decrease the staker's beaconChainScalingFactor proportionally
beaconChainScalingFactors[staker][beaconChainETHStrategy] = DefaultWadUint256.wrap(
beaconChainScalingFactors[staker][beaconChainETHStrategy].set(
beaconChainScalingFactors[staker][beaconChainETHStrategy].get() * (WAD - proportionPodBalanceDecrease) / WAD
);

Expand Down Expand Up @@ -761,7 +761,8 @@ contract DelegationManager is
PrincipalShares existingPrincipalShares,
Shares addedShares
) internal returns (uint256) {
uint256 newDepositScalingFactor;
// TODO: overflow analysis
uint128 newDepositScalingFactor;

if (PrincipalShares.unwrap(existingPrincipalShares) == 0) {
// existing shares are 0, meaning no existing delegated shares. In this case, the new depositScalingFactor
Expand Down Expand Up @@ -789,13 +790,13 @@ contract DelegationManager is
depositScalingFactors[staker][strategy], beaconChainScalingFactors[staker][strategy]
).toShares(totalMagnitude)
);
newDepositScalingFactor = (existingShares + Shares.unwrap(addedShares)) * WAD
newDepositScalingFactor = uint128((existingShares + Shares.unwrap(addedShares)) * WAD
/ (PrincipalShares.unwrap(existingPrincipalShares) + Shares.unwrap(addedShares)) * WAD
/ beaconChainScalingFactors[staker][strategy].get() * WAD / totalMagnitude;
/ beaconChainScalingFactors[staker][strategy].get() * WAD / totalMagnitude);
}

// update the staker's depositScalingFactor
depositScalingFactors[staker][strategy] = DefaultWadUint256.wrap(newDepositScalingFactor);
depositScalingFactors[staker][strategy].set(newDepositScalingFactor);
}

function _getShareManager(
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/core/DelegationManagerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ abstract contract DelegationManagerStorage is IDelegationManager {

/// @notice Mapping: staker => strategy => scaling factor used to calculate the staker's shares in the strategy
/// This is updated upon each deposit of a delegated staker.
mapping(address => mapping(IStrategy => DefaultWadUint256)) public depositScalingFactors;
mapping(address => mapping(IStrategy => Uint128DefaultWad)) public depositScalingFactors;

/// @notice Mapping: staker => strategy => their beacon chain scaling factor used to calculate the staker's withdrawable shares in the strategy.
/// This is updated upon each ending checkpoints that include beacon chain slashing.
/// Note that we don't need the Stratgy mapping here, since this will only be non zero for beaconChainETHStrategy, but it's nicer syntactically to keep it.
mapping(address => mapping(IStrategy => DefaultWadUint256)) public beaconChainScalingFactors;
mapping(address => mapping(IStrategy => Uint128DefaultWad)) public beaconChainScalingFactors;

constructor(
IStrategyManager _strategyManager,
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/interfaces/IDelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ interface IDelegationManager is ISignatureUtils {
function decreaseBeaconChainScalingFactor(
address staker,
uint256 shares,
uint256 proportionPodBalanceDecrease
uint64 proportionPodBalanceDecrease
) external;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/interfaces/IEigenPodManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ interface IEigenPodManager is IShareManager, IPausable {
* to ensure that delegated shares are also tracked correctly
* @param podOwner is the pod owner whose balance is being updated.
* @param sharesDelta is the change in podOwner's beaconChainETHStrategy shares
* @param proportionPodBalanceDecrease is the proportion (of PRECISION_FACTOR) of the podOwner's balance that has changed
* @param proportionPodBalanceDecrease is the proportion (of WAD) of the podOwner's balance that has changed
* @dev Callable only by the podOwner's EigenPod contract.
* @dev Reverts if `sharesDelta` is not a whole Gwei amount
*/
function recordBeaconChainETHBalanceUpdate(
address podOwner,
int256 sharesDelta,
uint256 proportionPodBalanceDecrease
uint64 proportionPodBalanceDecrease
) external;

/// @notice Returns the address of the `podOwner`'s EigenPod if it has been deployed.
Expand Down
33 changes: 20 additions & 13 deletions src/contracts/libraries/SlashingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ type DelegatedShares is uint256;

type PrincipalShares is uint256;

type DefaultWadUint256 is uint256;
struct Uint128DefaultWad {
uint128 inner;
bool isSet;
}

using SlashingLib for Shares global;
using SlashingLib for DelegatedShares global;
using SlashingLib for PrincipalShares global;
using SlashingLib for DefaultWadUint256 global;
using SlashingLib for Uint128DefaultWad global;

// TODO: validate order of operations everywhere
library SlashingLib {
Expand All @@ -62,26 +65,26 @@ library SlashingLib {
/// @dev beaconChainScalingFactor = 0 -> WAD for all non beaconChainETH strategies
function toPrincipalShares(
DelegatedShares delegatedShares,
DefaultWadUint256 depositScalingFactor,
DefaultWadUint256 beaconChainScalingFactor
) internal pure returns (PrincipalShares) {
Uint128DefaultWad storage depositScalingFactor,
Uint128DefaultWad storage beaconChainScalingFactor
) internal view returns (PrincipalShares) {
return PrincipalShares.wrap(DelegatedShares.unwrap(delegatedShares).divWad(depositScalingFactor.get()).divWad(beaconChainScalingFactor.get()));
}

/// @dev beaconChainScalingFactor = 0 -> WAD for all non beaconChainETH strategies
function toDelegatedShares(
PrincipalShares principalShares,
DefaultWadUint256 depositScalingFactor,
DefaultWadUint256 beaconChainScalingFactor
) internal pure returns (DelegatedShares) {
Uint128DefaultWad storage depositScalingFactor,
Uint128DefaultWad storage beaconChainScalingFactor
) internal view returns (DelegatedShares) {
return DelegatedShares.wrap(PrincipalShares.unwrap(principalShares).mulWad(depositScalingFactor.get()).mulWad(beaconChainScalingFactor.get()));
}

function toShares(DelegatedShares delegatedShares, uint256 magnitude) internal pure returns (Shares) {
function toShares(DelegatedShares delegatedShares, uint256 magnitude) internal view returns (Shares) {
return Shares.wrap(DelegatedShares.unwrap(delegatedShares).mulWad(magnitude));
}

function toDelegatedShares(Shares shares, uint256 magnitude) internal pure returns (DelegatedShares) {
function toDelegatedShares(Shares shares, uint256 magnitude) internal view returns (DelegatedShares) {
return DelegatedShares.wrap(Shares.unwrap(shares).divWad(magnitude));
}

Expand All @@ -95,8 +98,12 @@ library SlashingLib {
return a.mulDiv(WAD, b);
}

function get(DefaultWadUint256 a) internal pure returns (uint256) {
uint256 inner = DefaultWadUint256.unwrap(a);
return inner == 0 ? WAD : inner;
function get(Uint128DefaultWad storage a) internal view returns (uint128) {
return a.inner == 0 && !a.isSet ? WAD : a.inner;
}

function set(Uint128DefaultWad storage a, uint128 newVal) internal {
a.isSet = true;
a.inner = newVal;
}
}
4 changes: 2 additions & 2 deletions src/contracts/pods/EigenPod.sol
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,10 @@ contract EigenPod is Initializable, ReentrancyGuardUpgradeable, EigenPodPausingC
delete _currentCheckpoint;

// Calculate the slashing proportion
uint256 proportionPodBalanceDecrease = 0;
uint64 proportionPodBalanceDecrease = 0;
if (totalShareDeltaWei < 0) {
uint256 totalBefore = withdrawableRestakedExecutionLayerGwei + checkpoint.beaconChainBalanceBefore;
proportionPodBalanceDecrease = uint256(-totalShareDeltaWei) * 1e18 / totalBefore;
proportionPodBalanceDecrease = uint64(uint256(-totalShareDeltaWei) * WAD / totalBefore);
}

// Update pod owner's shares
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/pods/EigenPodManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ contract EigenPodManager is
* to ensure that delegated shares are also tracked correctly
* @param podOwner is the pod owner whose balance is being updated.
* @param sharesDelta is the change in podOwner's beaconChainETHStrategy shares
* @param proportionPodBalanceDecrease is the proportion (of PRECISION_FACTOR) of the podOwner's balance that has changed
* @param proportionPodBalanceDecrease is the proportion (of WAD) of the podOwner's balance that has changed
* @dev Callable only by the podOwner's EigenPod contract.
* @dev Reverts if `sharesDelta` is not a whole Gwei amount
*/
function recordBeaconChainETHBalanceUpdate(
address podOwner,
int256 sharesDelta,
uint256 proportionPodBalanceDecrease
uint64 proportionPodBalanceDecrease
) external onlyEigenPod(podOwner) nonReentrant {
require(podOwner != address(0), InputAddressZero());
require(sharesDelta % int256(GWEI_TO_WEI) == 0, SharesNotMultipleOfGwei());
Expand Down

0 comments on commit 7190c66

Please sign in to comment.