Skip to content

Commit

Permalink
refactor: pull beacon chain slashing out of slashing lib (#876)
Browse files Browse the repository at this point in the history
* fix: correctly update dsf when increasing delegation

* fix: fix bug where we modify an array while iterating over it

* chore: address review nits

* refactor: minor refactors for codesize

* refactor(nit): function ordering

* fix: only check input lengths when receiving tokens

* refactor: remove callstack indirection when completing a withdrawal

* chore: update comment
  • Loading branch information
wadealexc authored Nov 14, 2024
1 parent d59be03 commit d14790a
Show file tree
Hide file tree
Showing 14 changed files with 667 additions and 385 deletions.
38 changes: 27 additions & 11 deletions script/tasks/complete_withdrawal_from_strategy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import "forge-std/Test.sol";
// RUST_LOG=forge,foundry=trace forge script script/tasks/complete_withdrawal_from_strategy.s.sol --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig "run(string memory configFile,,address strategy,address token,uint256 amount)" -- <DEPLOYMENT_OUTPUT_JSON> <STRATEGY_ADDRESS> <TOKEN_ADDRESS> <AMOUNT> <NONCE> <START_BLOCK_NUMBER>
// RUST_LOG=forge,foundry=trace forge script script/tasks/complete_withdrawal_from_strategy.s.sol --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig "run(string memory configFile,,address strategy,address token,uint256 amount,uint256 nonce,uint32 startBlock)" -- local/slashing_output.json 0x8aCd85898458400f7Db866d53FCFF6f0D49741FF 0x67d269191c92Caf3cD7723F116c85e6E9bf55933 750 0 630
contract CompleteWithdrawFromStrategy is Script, Test {
using SlashingLib for *;

Vm cheats = Vm(VM_ADDRESS);

string public deployConfigPath;
Expand Down Expand Up @@ -55,23 +57,23 @@ contract CompleteWithdrawFromStrategy is Script, Test {
uint256[] memory shares = new uint256[](1);
shares[0] = amount;

// Get SSF for Staker in strategy
(uint184 depositScalingFactor, uint64 beaconChainScalingFactor, bool isBeaconChainScalingFactorSet) = dm.stakerScalingFactor(msg.sender, strategies[0]);
// Populate the StakerScalingFactors struct with the returned values
StakerScalingFactors memory ssf = StakerScalingFactors({
depositScalingFactor: depositScalingFactor,
beaconChainScalingFactor: beaconChainScalingFactor,
isBeaconChainScalingFactorSet: isBeaconChainScalingFactorSet
});

// Get DSF for Staker in strategy
DepositScalingFactor memory dsf = DepositScalingFactor(dm.depositScalingFactor(msg.sender, strategies[0]));

// Get TM for Operator in strategies
uint64[] memory maxMagnitudes = am.getMaxMagnitudesAtBlock(msg.sender, strategies, startBlock);
uint256 slashingFactor = _getSlashingFactor(dm, msg.sender, strategies[0], maxMagnitudes[0]);
uint256 sharesToWithdraw = dsf.calcWithdrawable(amount, slashingFactor);

// Get scaled shares for the given amount
uint256[] memory scaledShares = new uint256[](1);
scaledShares[0] = SlashingLib.scaleSharesForQueuedWithdrawal(amount, ssf, maxMagnitudes[0]);
scaledShares[0] = SlashingLib.scaleSharesForQueuedWithdrawal({
sharesToWithdraw: sharesToWithdraw,
slashingFactor: slashingFactor
});

// Log the current state before completing
emit log_uint(depositScalingFactor);
emit log_uint(dsf.scalingFactor());
emit log_uint(maxMagnitudes[0]);
emit log_uint(scaledShares[0]);

Expand All @@ -89,4 +91,18 @@ contract CompleteWithdrawFromStrategy is Script, Test {
// Return the withdrawal struct
return withdrawal;
}

function _getSlashingFactor(
DelegationManager dm,
address staker,
IStrategy strategy,
uint64 operatorMaxMagnitude
) internal view returns (uint256) {
if (strategy == dm.beaconChainETHStrategy()) {
uint64 beaconChainSlashingFactor = dm.getBeaconChainSlashingFactor(staker);
return operatorMaxMagnitude.mulWad(beaconChainSlashingFactor);
}

return operatorMaxMagnitude;
}
}
9 changes: 7 additions & 2 deletions src/contracts/core/AllocationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ contract AllocationManager is
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,
Expand All @@ -631,7 +636,7 @@ contract AllocationManager is
uint64[] memory maxMagnitudes = new uint64[](strategies.length);

for (uint256 i = 0; i < strategies.length; ++i) {
maxMagnitudes[i] = _maxMagnitudeHistory[operator][strategies[i]].latest();
maxMagnitudes[i] = getMaxMagnitude(operator, strategies[i]);
}

return maxMagnitudes;
Expand All @@ -642,7 +647,7 @@ contract AllocationManager is
uint64[] memory maxMagnitudes = new uint64[](operators.length);

for (uint256 i = 0; i < operators.length; ++i) {
maxMagnitudes[i] = _maxMagnitudeHistory[operators[i]][strategy].latest();
maxMagnitudes[i] = getMaxMagnitude(operators[i], strategy);
}

return maxMagnitudes;
Expand Down
Loading

0 comments on commit d14790a

Please sign in to comment.