From 223ed504ca91bf975a4103f3577549f536634329 Mon Sep 17 00:00:00 2001 From: wadealexc Date: Thu, 25 Jan 2024 21:32:35 +0000 Subject: [PATCH] feat: remove a bunch of public functions to reduce bytecode size --- src/contracts/core/DelegationManager.sol | 37 ++++--------------- .../core/DelegationManagerStorage.sol | 8 ++-- .../interfaces/IDelegationManager.sol | 27 -------------- src/test/Delegation.t.sol | 6 +-- src/test/DelegationFaucet.t.sol | 10 +++-- src/test/EigenLayerDeployer.t.sol | 11 ++++++ src/test/WithdrawalMigration.t.sol | 10 +++-- src/test/integration/IntegrationBase.t.sol | 8 +++- src/test/integration/IntegrationChecks.t.sol | 6 +-- 9 files changed, 49 insertions(+), 74 deletions(-) diff --git a/src/contracts/core/DelegationManager.sol b/src/contracts/core/DelegationManager.sol index c1f333f07..ff7174c67 100644 --- a/src/contracts/core/DelegationManager.sol +++ b/src/contracts/core/DelegationManager.sol @@ -356,7 +356,7 @@ contract DelegationManager is Initializable, OwnableUpgradeable, Pausable, Deleg }); // create the new storage - bytes32 newRoot = calculateWithdrawalRoot(migratedWithdrawal); + bytes32 newRoot = _calculateWithdrawalRoot(migratedWithdrawal); // safety check to ensure that root doesn't exist already -- this should *never* be hit require(!pendingWithdrawals[newRoot], "migrateQueuedWithdrawals: withdrawal already exists"); pendingWithdrawals[newRoot] = true; @@ -551,7 +551,7 @@ contract DelegationManager is Initializable, OwnableUpgradeable, Pausable, Deleg uint256 /*middlewareTimesIndex*/, bool receiveAsTokens ) internal { - bytes32 withdrawalRoot = calculateWithdrawalRoot(withdrawal); + bytes32 withdrawalRoot = _calculateWithdrawalRoot(withdrawal); require( pendingWithdrawals[withdrawalRoot], @@ -725,7 +725,7 @@ contract DelegationManager is Initializable, OwnableUpgradeable, Pausable, Deleg shares: shares }); - bytes32 withdrawalRoot = calculateWithdrawalRoot(withdrawal); + bytes32 withdrawalRoot = _calculateWithdrawalRoot(withdrawal); // Place withdrawal in queue pendingWithdrawals[withdrawalRoot] = true; @@ -791,6 +791,11 @@ contract DelegationManager is Initializable, OwnableUpgradeable, Pausable, Deleg } } + /// @notice Returns the keccak256 hash of `withdrawal`. + function _calculateWithdrawalRoot(Withdrawal memory withdrawal) internal pure returns (bytes32) { + return keccak256(abi.encode(withdrawal)); + } + /******************************************************************************* VIEW FUNCTIONS *******************************************************************************/ @@ -831,27 +836,6 @@ contract DelegationManager is Initializable, OwnableUpgradeable, Pausable, Deleg return _operatorDetails[operator]; } - /* - * @notice Returns the earnings receiver address for an operator - */ - function earningsReceiver(address operator) external view returns (address) { - return _operatorDetails[operator].earningsReceiver; - } - - /** - * @notice Returns the delegationApprover account for an operator - */ - function delegationApprover(address operator) external view returns (address) { - return _operatorDetails[operator].delegationApprover; - } - - /** - * @notice Returns the stakerOptOutWindowBlocks for an operator - */ - function stakerOptOutWindowBlocks(address operator) external view returns (uint256) { - return _operatorDetails[operator].stakerOptOutWindowBlocks; - } - /** * @notice Returns the number of actively-delegatable shares a staker has across all strategies. * @dev Returns two empty arrays in the case that the Staker has no actively-delegateable shares. @@ -915,11 +899,6 @@ contract DelegationManager is Initializable, OwnableUpgradeable, Pausable, Deleg return withdrawalDelay; } - /// @notice Returns the keccak256 hash of `withdrawal`. - function calculateWithdrawalRoot(Withdrawal memory withdrawal) public pure returns (bytes32) { - return keccak256(abi.encode(withdrawal)); - } - /** * @notice Calculates the digestHash for a `staker` to sign to delegate to an `operator` * @param staker The signing staker diff --git a/src/contracts/core/DelegationManagerStorage.sol b/src/contracts/core/DelegationManagerStorage.sol index 104f629b0..fcbc4a737 100644 --- a/src/contracts/core/DelegationManagerStorage.sol +++ b/src/contracts/core/DelegationManagerStorage.sol @@ -14,15 +14,15 @@ import "../interfaces/IEigenPodManager.sol"; */ abstract contract DelegationManagerStorage is IDelegationManager { /// @notice The EIP-712 typehash for the contract's domain - bytes32 public constant DOMAIN_TYPEHASH = + bytes32 internal constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"); /// @notice The EIP-712 typehash for the `StakerDelegation` struct used by the contract - bytes32 public constant STAKER_DELEGATION_TYPEHASH = + bytes32 internal constant STAKER_DELEGATION_TYPEHASH = keccak256("StakerDelegation(address staker,address operator,uint256 nonce,uint256 expiry)"); /// @notice The EIP-712 typehash for the `DelegationApproval` struct used by the contract - bytes32 public constant DELEGATION_APPROVAL_TYPEHASH = + bytes32 internal constant DELEGATION_APPROVAL_TYPEHASH = keccak256("DelegationApproval(address staker,address operator,bytes32 salt,uint256 expiry)"); /** @@ -42,7 +42,7 @@ abstract contract DelegationManagerStorage is IDelegationManager { IEigenPodManager public immutable eigenPodManager; // the number of 12-second blocks in 30 days (60 * 60 * 24 * 30 / 12 = 216,000) - uint256 public constant MAX_WITHDRAWAL_DELAY_BLOCKS = 216000; + uint256 internal constant MAX_WITHDRAWAL_DELAY_BLOCKS = 216000; /** * @notice returns the total number of shares in `strategy` that are delegated to `operator`. diff --git a/src/contracts/interfaces/IDelegationManager.sol b/src/contracts/interfaces/IDelegationManager.sol index 19ae7cf88..61394048c 100644 --- a/src/contracts/interfaces/IDelegationManager.sol +++ b/src/contracts/interfaces/IDelegationManager.sol @@ -322,21 +322,6 @@ interface IDelegationManager is ISignatureUtils { */ function operatorDetails(address operator) external view returns (OperatorDetails memory); - /* - * @notice Returns the earnings receiver address for an operator - */ - function earningsReceiver(address operator) external view returns (address); - - /** - * @notice Returns the delegationApprover account for an operator - */ - function delegationApprover(address operator) external view returns (address); - - /** - * @notice Returns the stakerOptOutWindowBlocks for an operator - */ - function stakerOptOutWindowBlocks(address operator) external view returns (uint256); - /** * @notice Given a list of strategies, return the minimum number of blocks that must pass to withdraw * from all the inputted strategies. Return value is >= minWithdrawalDelayBlocks as this is the global min withdrawal delay. @@ -429,15 +414,6 @@ interface IDelegationManager is ISignatureUtils { uint256 expiry ) external view returns (bytes32); - /// @notice The EIP-712 typehash for the contract's domain - function DOMAIN_TYPEHASH() external view returns (bytes32); - - /// @notice The EIP-712 typehash for the StakerDelegation struct used by the contract - function STAKER_DELEGATION_TYPEHASH() external view returns (bytes32); - - /// @notice The EIP-712 typehash for the DelegationApproval struct used by the contract - function DELEGATION_APPROVAL_TYPEHASH() external view returns (bytes32); - /** * @notice Getter function for the current EIP-712 domain separator for this contract. * @@ -451,8 +427,5 @@ interface IDelegationManager is ISignatureUtils { /// @dev This only increments (doesn't decrement), and is used to help ensure that otherwise identical withdrawals have unique hashes. function cumulativeWithdrawalsQueued(address staker) external view returns (uint256); - /// @notice Returns the keccak256 hash of `withdrawal`. - function calculateWithdrawalRoot(Withdrawal memory withdrawal) external pure returns (bytes32); - function migrateQueuedWithdrawals(IStrategyManager.DeprecatedStruct_QueuedWithdrawal[] memory withdrawalsToQueue) external; } diff --git a/src/test/Delegation.t.sol b/src/test/Delegation.t.sol index cb3f51960..ac4c78551 100644 --- a/src/test/Delegation.t.sol +++ b/src/test/Delegation.t.sol @@ -163,7 +163,7 @@ contract DelegationTests is EigenLayerTestHelper { uint256 nonceBefore = delegation.stakerNonce(staker); bytes32 structHash = keccak256( - abi.encode(delegation.STAKER_DELEGATION_TYPEHASH(), staker, operator, nonceBefore, expiry) + abi.encode(STAKER_DELEGATION_TYPEHASH, staker, operator, nonceBefore, expiry) ); bytes32 digestHash = keccak256(abi.encodePacked("\x19\x01", delegation.domainSeparator(), structHash)); @@ -207,7 +207,7 @@ contract DelegationTests is EigenLayerTestHelper { uint256 nonceBefore = delegation.stakerNonce(staker); bytes32 structHash = keccak256( - abi.encode(delegation.STAKER_DELEGATION_TYPEHASH(), staker, operator, nonceBefore, type(uint256).max) + abi.encode(STAKER_DELEGATION_TYPEHASH, staker, operator, nonceBefore, type(uint256).max) ); bytes32 digestHash = keccak256(abi.encodePacked("\x19\x01", delegation.domainSeparator(), structHash)); @@ -246,7 +246,7 @@ contract DelegationTests is EigenLayerTestHelper { uint256 nonceBefore = delegation.stakerNonce(staker); bytes32 structHash = keccak256( - abi.encode(delegation.STAKER_DELEGATION_TYPEHASH(), staker, operator, nonceBefore, type(uint256).max) + abi.encode(STAKER_DELEGATION_TYPEHASH, staker, operator, nonceBefore, type(uint256).max) ); bytes32 digestHash = keccak256(abi.encodePacked("\x19\x01", delegation.domainSeparator(), structHash)); diff --git a/src/test/DelegationFaucet.t.sol b/src/test/DelegationFaucet.t.sol index 3bd26ef80..3ff7ad86c 100644 --- a/src/test/DelegationFaucet.t.sol +++ b/src/test/DelegationFaucet.t.sol @@ -349,7 +349,7 @@ contract DelegationFaucetTests is EigenLayerTestHelper { }); } cheats.expectEmit(true, true, true, true, address(delegation)); - emit WithdrawalCompleted(delegation.calculateWithdrawalRoot(queuedWithdrawal)); + emit WithdrawalCompleted(_calculateWithdrawalRoot(queuedWithdrawal)); uint256 middlewareTimesIndex = 0; bool receiveAsTokens = false; delegationFaucet.completeQueuedWithdrawal( @@ -410,7 +410,7 @@ contract DelegationFaucetTests is EigenLayerTestHelper { }); } cheats.expectEmit(true, true, true, true, address(delegation)); - emit WithdrawalCompleted(delegation.calculateWithdrawalRoot(queuedWithdrawal)); + emit WithdrawalCompleted(_calculateWithdrawalRoot(queuedWithdrawal)); uint256 middlewareTimesIndex = 0; bool receiveAsTokens = true; delegationFaucet.completeQueuedWithdrawal( @@ -506,7 +506,11 @@ contract DelegationFaucetTests is EigenLayerTestHelper { delegatedTo: strategyManager.delegation().delegatedTo(staker) }); // calculate the withdrawal root - withdrawalRoot = delegation.calculateWithdrawalRoot(queuedWithdrawal); + withdrawalRoot = _calculateWithdrawalRoot(queuedWithdrawal); return (queuedWithdrawal, tokensArray, withdrawalRoot); } + + function _calculateWithdrawalRoot(IDelegationManager.Withdrawal memory withdrawal) internal pure returns (bytes32) { + return keccak256(abi.encode(withdrawal)); + } } diff --git a/src/test/EigenLayerDeployer.t.sol b/src/test/EigenLayerDeployer.t.sol index 05998820f..88d61889e 100644 --- a/src/test/EigenLayerDeployer.t.sol +++ b/src/test/EigenLayerDeployer.t.sol @@ -112,6 +112,17 @@ contract EigenLayerDeployer is Operators { // addresses excluded from fuzzing due to abnormal behavior. TODO: @Sidu28 define this better and give it a clearer name mapping(address => bool) fuzzedAddressMapping; + /// Typehash definitions for DelegationManager: + + bytes32 internal constant DOMAIN_TYPEHASH = + keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"); + + bytes32 internal constant STAKER_DELEGATION_TYPEHASH = + keccak256("StakerDelegation(address staker,address operator,uint256 nonce,uint256 expiry)"); + + bytes32 internal constant DELEGATION_APPROVAL_TYPEHASH = + keccak256("DelegationApproval(address staker,address operator,bytes32 salt,uint256 expiry)"); + //ensures that a passed in address is not set to true in the fuzzedAddressMapping modifier fuzzedAddress(address addr) virtual { cheats.assume(fuzzedAddressMapping[addr] == false); diff --git a/src/test/WithdrawalMigration.t.sol b/src/test/WithdrawalMigration.t.sol index beac35874..b77754fda 100644 --- a/src/test/WithdrawalMigration.t.sol +++ b/src/test/WithdrawalMigration.t.sol @@ -96,7 +96,7 @@ contract WithdrawalMigrationTests is EigenLayerTestHelper, Utils { strategies: queuedWithdrawal.strategies, shares: queuedWithdrawal.shares }); - bytes32 withdrawalRootDM = m1DelegationManager.calculateWithdrawalRoot(migratedWithdrawal); + bytes32 withdrawalRootDM = _calculateWithdrawalRoot(migratedWithdrawal); uint256 nonceBefore = m1DelegationManager.cumulativeWithdrawalsQueued(queuedWithdrawal.staker); // Migrate withdrawal @@ -159,8 +159,8 @@ contract WithdrawalMigrationTests is EigenLayerTestHelper, Utils { shares: queuedWithdrawal2.shares }); - bytes32 withdrawalRootDM1 = m1DelegationManager.calculateWithdrawalRoot(migratedWithdrawal1); - bytes32 withdrawalRootDM2 = m1DelegationManager.calculateWithdrawalRoot(migratedWithdrawal2); + bytes32 withdrawalRootDM1 = _calculateWithdrawalRoot(migratedWithdrawal1); + bytes32 withdrawalRootDM2 = _calculateWithdrawalRoot(migratedWithdrawal2); uint256 nonceBefore = m1DelegationManager.cumulativeWithdrawalsQueued(queuedWithdrawal1.staker); @@ -353,6 +353,10 @@ contract WithdrawalMigrationTests is EigenLayerTestHelper, Utils { cheats.stopPrank(); } + function _calculateWithdrawalRoot(IDelegationManager.Withdrawal memory withdrawal) internal pure returns (bytes32) { + return keccak256(abi.encode(withdrawal)); + } + // Events event WithdrawalMigrated(bytes32 oldWithdrawalRoot, bytes32 newWithdrawalRoot); event WithdrawalQueued(bytes32 withdrawalRoot, IDelegationManager.Withdrawal withdrawal); diff --git a/src/test/integration/IntegrationBase.t.sol b/src/test/integration/IntegrationBase.t.sol index c028f0b5b..1ab229aef 100644 --- a/src/test/integration/IntegrationBase.t.sol +++ b/src/test/integration/IntegrationBase.t.sol @@ -170,7 +170,7 @@ abstract contract IntegrationBase is IntegrationDeployer { bytes32 withdrawalRoot, string memory err ) internal { - assertEq(withdrawalRoot, delegationManager.calculateWithdrawalRoot(withdrawal), err); + assertEq(withdrawalRoot, _calculateWithdrawalRoot(withdrawal), err); } /******************************************************************************* @@ -640,12 +640,16 @@ abstract contract IntegrationBase is IntegrationDeployer { bytes32[] memory withdrawalRoots = new bytes32[](withdrawals.length); for (uint i = 0; i < withdrawals.length; i++) { - withdrawalRoots[i] = delegationManager.calculateWithdrawalRoot(withdrawals[i]); + withdrawalRoots[i] = _calculateWithdrawalRoot(withdrawals[i]); } return withdrawalRoots; } + function _calculateWithdrawalRoot(IDelegationManager.Withdrawal memory withdrawal) internal pure returns (bytes32) { + return keccak256(abi.encode(withdrawal)); + } + /// @dev Converts a list of strategies to underlying tokens function _getUnderlyingTokens(IStrategy[] memory strategies) internal view returns (IERC20[] memory) { IERC20[] memory tokens = new IERC20[](strategies.length); diff --git a/src/test/integration/IntegrationChecks.t.sol b/src/test/integration/IntegrationChecks.t.sol index fef09682e..cc633ecfd 100644 --- a/src/test/integration/IntegrationChecks.t.sol +++ b/src/test/integration/IntegrationChecks.t.sol @@ -124,7 +124,7 @@ contract IntegrationCheckUtils is IntegrationBase { uint[] memory expectedTokens ) internal { // Common checks - assert_WithdrawalNotPending(delegationManager.calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); + assert_WithdrawalNotPending(_calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); assert_Snap_Added_TokenBalances(staker, tokens, expectedTokens, "staker should have received expected tokens"); assert_Snap_Unchanged_StakerShares(staker, "staker shares should not have changed"); assert_Snap_Removed_StrategyShares(strategies, shares, "strategies should have total shares decremented"); @@ -146,7 +146,7 @@ contract IntegrationCheckUtils is IntegrationBase { uint[] memory shares ) internal { // Common checks applicable to both user and non-user operator types - assert_WithdrawalNotPending(delegationManager.calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); + assert_WithdrawalNotPending(_calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); assert_Snap_Unchanged_TokenBalances(staker, "staker should not have any change in underlying token balances"); assert_Snap_Added_StakerShares(staker, strategies, shares, "staker should have received expected shares"); assert_Snap_Unchanged_StrategyShares(strategies, "strategies should have total shares unchanged"); @@ -174,7 +174,7 @@ contract IntegrationCheckUtils is IntegrationBase { // ... check that the withdrawal is not pending, that the token balances of the staker and operator are unchanged, // that the withdrawer received the expected shares, and that that the total shares of each o // strategy withdrawn remains unchanged - assert_WithdrawalNotPending(delegationManager.calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); + assert_WithdrawalNotPending(_calculateWithdrawalRoot(withdrawal), "staker withdrawal should no longer be pending"); assert_Snap_Unchanged_TokenBalances(staker, "staker should not have any change in underlying token balances"); assert_Snap_Unchanged_TokenBalances(operator, "operator should not have any change in underlying token balances"); assert_Snap_Added_StakerShares(staker, strategies, shares, "staker should have received expected shares");