Skip to content

Commit

Permalink
gas optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Sep 27, 2024
1 parent 00df501 commit 77663c2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
18 changes: 10 additions & 8 deletions contracts/contracts/validator-registry/VanillaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
* @param pubKey The single BLS public key to unstake.
*/
function _unstakeSingle(bytes calldata pubKey) internal {
BlockHeightOccurrence.captureOccurrence(stakedValidators[pubKey].unstakeOccurrence);
emit Unstaked(msg.sender, stakedValidators[pubKey].withdrawalAddress,
pubKey, stakedValidators[pubKey].balance);
IVanillaRegistry.StakedValidator storage validator = stakedValidators[pubKey];
BlockHeightOccurrence.captureOccurrence(validator.unstakeOccurrence);
emit Unstaked(msg.sender, validator.withdrawalAddress, pubKey, validator.balance);
}

/*
Expand All @@ -331,10 +331,11 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
uint256 totalAmount = 0;
for (uint256 i = 0; i < len; ++i) {
bytes calldata pubKey = blsPubKeys[i];
IVanillaRegistry.StakedValidator storage validator = stakedValidators[pubKey];
require(_isUnstaking(pubKey), IVanillaRegistry.MustUnstakeToWithdraw());
require(block.number > stakedValidators[pubKey].unstakeOccurrence.blockHeight + unstakePeriodBlocks,
require(block.number > validator.unstakeOccurrence.blockHeight + unstakePeriodBlocks,
IVanillaRegistry.WithdrawingTooSoon());
uint256 balance = stakedValidators[pubKey].balance;
uint256 balance = validator.balance;
require(balance != 0, IVanillaRegistry.NothingToWithdraw());
totalAmount += balance;
delete stakedValidators[pubKey];
Expand All @@ -353,17 +354,18 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
uint256 len = blsPubKeys.length;
for (uint256 i = 0; i < len; ++i) {
bytes calldata pubKey = blsPubKeys[i];
require(stakedValidators[pubKey].balance >= minStake, IVanillaRegistry.NotEnoughBalanceToSlash());
IVanillaRegistry.StakedValidator storage validator = stakedValidators[pubKey];
require(validator.balance >= minStake, IVanillaRegistry.NotEnoughBalanceToSlash());
if (!_isUnstaking(pubKey)) {
_unstakeSingle(pubKey);
}
stakedValidators[pubKey].balance -= minStake;
validator.balance -= minStake;
slashingFundsTracker.accumulatedAmount += minStake;
bool isLastEntry = i == len - 1;
if (payoutIfDue && FeePayout.isPayoutDue(slashingFundsTracker) && isLastEntry) {
FeePayout.transferToRecipient(slashingFundsTracker);
}
emit Slashed(msg.sender, slashingFundsTracker.recipient, stakedValidators[pubKey].withdrawalAddress, pubKey, minStake);
emit Slashed(msg.sender, slashingFundsTracker.recipient, validator.withdrawalAddress, pubKey, minStake);
}
}

Expand Down
46 changes: 26 additions & 20 deletions contracts/contracts/validator-registry/avs/MevCommitAVS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ contract MevCommitAVS is IMevCommitAVS, MevCommitAVSStorage,
modifier onlyPodOwnerOrOperatorOfValidators(bytes[] calldata valPubKeys) {
uint256 len = valPubKeys.length;
for (uint256 i = 0; i < len; ++i) {
IMevCommitAVS.ValidatorRegistrationInfo memory regInfo = validatorRegistrations[valPubKeys[i]];
IMevCommitAVS.ValidatorRegistrationInfo storage regInfo = validatorRegistrations[valPubKeys[i]];
require(msg.sender == regInfo.podOwner
|| msg.sender == _delegationManager.delegatedTo(regInfo.podOwner),
IMevCommitAVS.SenderNotPodOwnerOrOperatorOfValidator(valPubKeys[i]));
Expand Down Expand Up @@ -399,15 +399,17 @@ contract MevCommitAVS is IMevCommitAVS, MevCommitAVSStorage,

/// @dev Internal function to request deregistration of an operator.
function _requestOperatorDeregistration(address operator) internal {
require(!operatorRegistrations[operator].deregRequestOccurrence.exists, IMevCommitAVS.OperatorDeregAlreadyRequested());
BlockHeightOccurrence.captureOccurrence(operatorRegistrations[operator].deregRequestOccurrence);
IMevCommitAVS.OperatorRegistrationInfo storage reg = operatorRegistrations[operator];
require(!reg.deregRequestOccurrence.exists, IMevCommitAVS.OperatorDeregAlreadyRequested());
BlockHeightOccurrence.captureOccurrence(reg.deregRequestOccurrence);
emit OperatorDeregistrationRequested(operator);
}

/// @dev Internal function to deregister an operator.
function _deregisterOperator(address operator) internal {
require(operatorRegistrations[operator].deregRequestOccurrence.exists, IMevCommitAVS.DeregistrationNotRequested());
require(block.number > operatorRegistrations[operator].deregRequestOccurrence.blockHeight + operatorDeregPeriodBlocks, IMevCommitAVS.DeregistrationTooSoon());
IMevCommitAVS.OperatorRegistrationInfo storage reg = operatorRegistrations[operator];
require(reg.deregRequestOccurrence.exists, IMevCommitAVS.DeregistrationNotRequested());
require(block.number > reg.deregRequestOccurrence.blockHeight + operatorDeregPeriodBlocks, IMevCommitAVS.DeregistrationTooSoon());
_eigenAVSDirectory.deregisterOperatorFromAVS(operator);
delete operatorRegistrations[operator];
emit OperatorDeregistered(operator);
Expand All @@ -423,8 +425,9 @@ contract MevCommitAVS is IMevCommitAVS, MevCommitAVSStorage,
address podOwner
) internal onlyNonRegisteredValidators(valPubKeys) onlyPodOwnerOrOperator(podOwner) {
address operator = _delegationManager.delegatedTo(podOwner);
require(operatorRegistrations[operator].exists, IMevCommitAVS.OperatorNotRegistered(operator));
require(!operatorRegistrations[operator].deregRequestOccurrence.exists, IMevCommitAVS.OperatorDeregAlreadyRequested());
IMevCommitAVS.OperatorRegistrationInfo storage operatorReg = operatorRegistrations[operator];
require(operatorReg.exists, IMevCommitAVS.OperatorNotRegistered(operator));
require(!operatorReg.deregRequestOccurrence.exists, IMevCommitAVS.OperatorDeregAlreadyRequested());
require(_eigenPodManager.hasPod(podOwner), IMevCommitAVS.NoPodExists(podOwner));
IEigenPod pod = _eigenPodManager.getPod(podOwner);
uint256 len = valPubKeys.length;
Expand Down Expand Up @@ -453,17 +456,19 @@ contract MevCommitAVS is IMevCommitAVS, MevCommitAVSStorage,

/// @dev Internal function to request deregistration of a validator.
function _requestValidatorDeregistration(bytes calldata valPubKey) internal {
require(!validatorRegistrations[valPubKey].deregRequestOccurrence.exists, IMevCommitAVS.ValidatorDeregAlreadyRequested());
BlockHeightOccurrence.captureOccurrence(validatorRegistrations[valPubKey].deregRequestOccurrence);
emit ValidatorDeregistrationRequested(valPubKey, validatorRegistrations[valPubKey].podOwner);
IMevCommitAVS.ValidatorRegistrationInfo storage valReg = validatorRegistrations[valPubKey];
require(!valReg.deregRequestOccurrence.exists, IMevCommitAVS.ValidatorDeregAlreadyRequested());
BlockHeightOccurrence.captureOccurrence(valReg.deregRequestOccurrence);
emit ValidatorDeregistrationRequested(valPubKey, valReg.podOwner);
}

/// @dev Internal function to deregister a validator.
function _deregisterValidator(bytes calldata valPubKey) internal {
require(!validatorRegistrations[valPubKey].freezeOccurrence.exists, IMevCommitAVS.FrozenValidatorCannotDeregister());
require(validatorRegistrations[valPubKey].deregRequestOccurrence.exists, IMevCommitAVS.DeregistrationNotRequested());
require(block.number > validatorRegistrations[valPubKey].deregRequestOccurrence.blockHeight + validatorDeregPeriodBlocks, IMevCommitAVS.DeregistrationTooSoon());
address podOwner = validatorRegistrations[valPubKey].podOwner;
IMevCommitAVS.ValidatorRegistrationInfo storage valReg = validatorRegistrations[valPubKey];
require(!valReg.freezeOccurrence.exists, IMevCommitAVS.FrozenValidatorCannotDeregister());
require(valReg.deregRequestOccurrence.exists, IMevCommitAVS.DeregistrationNotRequested());
require(block.number > valReg.deregRequestOccurrence.blockHeight + validatorDeregPeriodBlocks, IMevCommitAVS.DeregistrationTooSoon());
address podOwner = valReg.podOwner;
delete validatorRegistrations[valPubKey];
emit ValidatorDeregistered(valPubKey, podOwner);
}
Expand Down Expand Up @@ -518,9 +523,10 @@ contract MevCommitAVS is IMevCommitAVS, MevCommitAVSStorage,

/// @dev Internal function to unfreeze a validator.
function _unfreeze(bytes calldata valPubKey) internal {
require(block.number > validatorRegistrations[valPubKey].freezeOccurrence.blockHeight + unfreezePeriodBlocks, UnfreezeTooSoon());
BlockHeightOccurrence.del(validatorRegistrations[valPubKey].freezeOccurrence);
emit ValidatorUnfrozen(valPubKey, validatorRegistrations[valPubKey].podOwner);
IMevCommitAVS.ValidatorRegistrationInfo storage valReg = validatorRegistrations[valPubKey];
require(block.number > valReg.freezeOccurrence.blockHeight + unfreezePeriodBlocks, UnfreezeTooSoon());
BlockHeightOccurrence.del(valReg.freezeOccurrence);
emit ValidatorUnfrozen(valPubKey, valReg.podOwner);
}

/// @dev Internal function to set the AVS directory.
Expand Down Expand Up @@ -596,13 +602,13 @@ contract MevCommitAVS is IMevCommitAVS, MevCommitAVSStorage,
}

/// @dev Internal function to update the eigenlayer metadata URI.
function _updateMetadataURI(string memory metadataURI_) internal {
function _updateMetadataURI(string calldata metadataURI_) internal {
_eigenAVSDirectory.updateAVSMetadataURI(metadataURI_);
}

/// @dev Internal function to check if a validator is opted-in.
function _isValidatorOptedIn(bytes calldata valPubKey) internal view returns (bool) {
IMevCommitAVS.ValidatorRegistrationInfo memory valRegistration = validatorRegistrations[valPubKey];
IMevCommitAVS.ValidatorRegistrationInfo storage valRegistration = validatorRegistrations[valPubKey];
bool isValRegistered = valRegistration.exists;
if (!isValRegistered) {
return false;
Expand All @@ -625,7 +631,7 @@ contract MevCommitAVS is IMevCommitAVS, MevCommitAVSStorage,
return false;
}
address delegatedOperator = _delegationManager.delegatedTo(valRegistration.podOwner);
IMevCommitAVS.OperatorRegistrationInfo memory operatorRegistration = operatorRegistrations[delegatedOperator];
IMevCommitAVS.OperatorRegistrationInfo storage operatorRegistration = operatorRegistrations[delegatedOperator];
bool isOperatorRegistered = operatorRegistration.exists;
if (!isOperatorRegistered) {
return false;
Expand Down

0 comments on commit 77663c2

Please sign in to comment.