From 5d8b350528d3db043bfada700dbb3cf0ba9c24d4 Mon Sep 17 00:00:00 2001 From: wadealexc Date: Fri, 15 Nov 2024 16:59:08 +0000 Subject: [PATCH] test: get tests compiling and running * 2 tests failing with off by 1 --- .../complete_withdrawal_from_strategy.s.sol | 21 ++++++++------ src/test/mocks/EigenPodManagerMock.sol | 19 +++++++++++++ src/test/unit/DelegationUnit.t.sol | 28 +++++++++++++------ 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/script/tasks/complete_withdrawal_from_strategy.s.sol b/script/tasks/complete_withdrawal_from_strategy.s.sol index d62b49da7..a3fd776e0 100644 --- a/script/tasks/complete_withdrawal_from_strategy.s.sol +++ b/script/tasks/complete_withdrawal_from_strategy.s.sol @@ -26,24 +26,27 @@ contract CompleteWithdrawFromStrategy is Script, Test { config_data = vm.readFile(deployConfigPath); // Pull addresses from config - address allocationManager = stdJson.readAddress(config_data, ".addresses.allocationManager"); - address delegationManager = stdJson.readAddress(config_data, ".addresses.delegationManager"); - address eigenPodManager = stdJson.readAddress(config_data, ".addresses.eigenPodManager"); + AllocationManager am = AllocationManager(stdJson.readAddress(config_data, ".addresses.allocationManager")); + DelegationManager dm = DelegationManager(stdJson.readAddress(config_data, ".addresses.delegationManager")); + EigenPodManager em = EigenPodManager(stdJson.readAddress(config_data, ".addresses.eigenPodManager")); // START RECORDING TRANSACTIONS FOR DEPLOYMENT vm.startBroadcast(); - // Attach to Managers - AllocationManager am = AllocationManager(allocationManager); - DelegationManager dm = DelegationManager(delegationManager); - EigenPodManager em = EigenPodManager(eigenPodManager); - // Add token to array IERC20[] memory tokens = new IERC20[](1); tokens[0] = IERC20(token); // Get the withdrawal struct - IDelegationManagerTypes.Withdrawal memory withdrawal = getWithdrawalStruct(am, dm, em, strategy, amount, nonce, startBlock); + IDelegationManagerTypes.Withdrawal memory withdrawal = getWithdrawalStruct( + am, + dm, + em, + strategy, + amount, + nonce, + startBlock + ); // complete dm.completeQueuedWithdrawal(withdrawal, tokens, true); diff --git a/src/test/mocks/EigenPodManagerMock.sol b/src/test/mocks/EigenPodManagerMock.sol index 77db6102b..df51e822c 100644 --- a/src/test/mocks/EigenPodManagerMock.sol +++ b/src/test/mocks/EigenPodManagerMock.sol @@ -13,6 +13,13 @@ contract EigenPodManagerMock is Test, Pausable { mapping(address => uint256) public podOwnerSharesWithdrawn; + struct BeaconChainSlashingFactor { + bool isSet; + uint64 slashingFactor; + } + + mapping(address => BeaconChainSlashingFactor) _beaconChainSlashingFactor; + constructor(IPauserRegistry _pauserRegistry) Pausable(_pauserRegistry) { _setPausedStatus(0); } @@ -44,4 +51,16 @@ contract EigenPodManagerMock is Test, Pausable { function withdrawSharesAsTokens(address podOwner, address /** strategy */, address /** token */, uint256 shares) external { podOwnerSharesWithdrawn[podOwner] += shares; } + + function setBeaconChainSlashingFactor(address staker, uint64 bcsf) external { + _beaconChainSlashingFactor[staker] = BeaconChainSlashingFactor({ + isSet: true, + slashingFactor: bcsf + }); + } + + function beaconChainSlashingFactor(address staker) external view returns (uint64) { + BeaconChainSlashingFactor memory bsf = _beaconChainSlashingFactor[staker]; + return bsf.isSet ? bsf.slashingFactor : WAD; + } } \ No newline at end of file diff --git a/src/test/unit/DelegationUnit.t.sol b/src/test/unit/DelegationUnit.t.sol index 2a8bb0213..5f4cac863 100644 --- a/src/test/unit/DelegationUnit.t.sol +++ b/src/test/unit/DelegationUnit.t.sol @@ -383,7 +383,7 @@ contract DelegationManagerUnitTests is EigenLayerUnitTestSetup, IDelegationManag uint64 operatorMaxMagnitude ) internal view returns (uint256) { if (strategy == beaconChainETHStrategy) { - uint64 beaconChainSlashingFactor = delegationManager.getBeaconChainSlashingFactor(staker); + uint64 beaconChainSlashingFactor = eigenPodManagerMock.beaconChainSlashingFactor(staker); return operatorMaxMagnitude.mulWad(beaconChainSlashingFactor); } @@ -3377,6 +3377,7 @@ contract DelegationManagerUnitTests_queueWithdrawals is DelegationManagerUnitTes contract DelegationManagerUnitTests_completeQueuedWithdrawal is DelegationManagerUnitTests { using SingleItemArrayLib for *; + using SlashingLib for *; function test_Revert_WhenExitWithdrawalQueuePaused() public { cheats.prank(pauser); @@ -3790,11 +3791,18 @@ contract DelegationManagerUnitTests_completeQueuedWithdrawal is DelegationManage uint256 operatorSharesAfterQueue = delegationManager.operatorShares(defaultOperator, beaconChainETHStrategy); assertEq(operatorSharesAfterQueue, operatorSharesBeforeQueue - withdrawalAmount, "operator shares should be decreased after queue"); - // Slash the staker for beacon chain shares while it has queued a withdrawal - uint256 beaconSharesBeforeSlash = uint256(eigenPodManagerMock.podOwnerShares(defaultStaker)); - uint64 stakerBeaconChainScalingFactor = 5e17; - cheats.prank(address(eigenPodManagerMock)); - delegationManager.decreaseBeaconChainScalingFactor(defaultStaker, beaconSharesBeforeSlash, stakerBeaconChainScalingFactor); + { + // Slash the staker for beacon chain shares while it has queued a withdrawal + uint256 beaconSharesBeforeSlash = uint256(eigenPodManagerMock.podOwnerShares(defaultStaker)); + uint64 prevBeaconChainSlashingFactor = eigenPodManagerMock.beaconChainSlashingFactor(defaultStaker); + uint64 newBeaconChainSlashingFactor = 5e17; + eigenPodManagerMock.setBeaconChainSlashingFactor(defaultStaker, newBeaconChainSlashingFactor); + uint256 wadSlashed = newBeaconChainSlashingFactor.divWad(prevBeaconChainSlashingFactor); + + cheats.prank(address(eigenPodManagerMock)); + delegationManager.decreaseDelegatedShares(defaultStaker, beaconSharesBeforeSlash, prevBeaconChainSlashingFactor, wadSlashed); + } + uint256 operatorSharesAfterBeaconSlash = delegationManager.operatorShares(defaultOperator, beaconChainETHStrategy); assertEq(operatorSharesAfterBeaconSlash, operatorSharesAfterQueue / 2, "operator shares should be decreased after beaconChain slash"); @@ -3861,9 +3869,13 @@ contract DelegationManagerUnitTests_completeQueuedWithdrawal is DelegationManage // Slash the staker for beacon chain shares while it has queued a withdrawal uint256 beaconSharesBeforeSlash = uint256(eigenPodManagerMock.podOwnerShares(defaultStaker)); - uint64 stakerBeaconChainScalingFactor = 5e17; + uint64 prevBeaconChainSlashingFactor = eigenPodManagerMock.beaconChainSlashingFactor(defaultStaker); + uint64 newBeaconChainSlashingFactor = 5e17; + eigenPodManagerMock.setBeaconChainSlashingFactor(defaultStaker, newBeaconChainSlashingFactor); + uint256 wadSlashed = newBeaconChainSlashingFactor.divWad(prevBeaconChainSlashingFactor); + cheats.prank(address(eigenPodManagerMock)); - delegationManager.decreaseBeaconChainScalingFactor(defaultStaker, beaconSharesBeforeSlash, stakerBeaconChainScalingFactor); + delegationManager.decreaseDelegatedShares(defaultStaker, beaconSharesBeforeSlash, prevBeaconChainSlashingFactor, wadSlashed); uint256 operatorSharesAfterBeaconSlash = delegationManager.operatorShares(defaultOperator, beaconChainETHStrategy); assertEq(operatorSharesAfterBeaconSlash, operatorSharesAfterQueue / 2, "operator shares should be decreased after beaconChain slash");