Skip to content

Commit

Permalink
feat: remove a bunch of public functions to reduce bytecode size
Browse files Browse the repository at this point in the history
  • Loading branch information
wadealexc committed Jan 25, 2024
1 parent 176bc04 commit 223ed50
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 74 deletions.
37 changes: 8 additions & 29 deletions src/contracts/core/DelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*******************************************************************************/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/contracts/core/DelegationManagerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)");

/**
Expand All @@ -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`.
Expand Down
27 changes: 0 additions & 27 deletions src/contracts/interfaces/IDelegationManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
}
6 changes: 3 additions & 3 deletions src/test/Delegation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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));

Expand Down
10 changes: 7 additions & 3 deletions src/test/DelegationFaucet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}
}
11 changes: 11 additions & 0 deletions src/test/EigenLayerDeployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/test/WithdrawalMigration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/test/integration/IntegrationBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*******************************************************************************
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/test/integration/IntegrationChecks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 223ed50

Please sign in to comment.