Skip to content

perf: refactor modifiers to use internal functions for stake registry #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/StakeRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ contract StakeRegistry is StakeRegistryStorage {
using BitmapUtils for *;

modifier onlyRegistryCoordinator() {
require(
msg.sender == address(registryCoordinator),
"StakeRegistry.onlyRegistryCoordinator: caller is not the RegistryCoordinator"
);
_checkRegistryCoordinator();
_;
}

modifier onlyCoordinatorOwner() {
require(msg.sender == IRegistryCoordinator(registryCoordinator).owner(), "StakeRegistry.onlyCoordinatorOwner: caller is not the owner of the registryCoordinator");
_checkRegistryCoordinatorOwner();
_;
}

modifier quorumExists(uint8 quorumNumber) {
require(_quorumExists(quorumNumber), "StakeRegistry.quorumExists: quorum does not exist");
_checkQuorumExists(quorumNumber);
_;
}

Expand Down Expand Up @@ -74,7 +71,7 @@ contract StakeRegistry is StakeRegistryStorage {
for (uint256 i = 0; i < quorumNumbers.length; i++) {

uint8 quorumNumber = uint8(quorumNumbers[i]);
require(_quorumExists(quorumNumber), "StakeRegistry.registerOperator: quorum does not exist");
_checkQuorumExists(quorumNumber);

// Retrieve the operator's current weighted stake for the quorum, reverting if they have not met
// the minimum.
Expand Down Expand Up @@ -121,7 +118,7 @@ contract StakeRegistry is StakeRegistryStorage {
*/
for (uint256 i = 0; i < quorumNumbers.length; i++) {
uint8 quorumNumber = uint8(quorumNumbers[i]);
require(_quorumExists(quorumNumber), "StakeRegistry.deregisterOperator: quorum does not exist");
_checkQuorumExists(quorumNumber);

// Update the operator's stake for the quorum and retrieve the shares removed
int256 stakeDelta = _recordOperatorStakeUpdate({
Expand Down Expand Up @@ -161,7 +158,7 @@ contract StakeRegistry is StakeRegistryStorage {
*/
for (uint256 i = 0; i < quorumNumbers.length; i++) {
uint8 quorumNumber = uint8(quorumNumbers[i]);
require(_quorumExists(quorumNumber), "StakeRegistry.updateOperatorStake: quorum does not exist");
_checkQuorumExists(quorumNumber);

// Fetch the operator's current stake, applying weighting parameters and checking
// against the minimum stake requirements for the quorum.
Expand Down Expand Up @@ -697,7 +694,7 @@ contract StakeRegistry is StakeRegistryStorage {
uint32[] memory indices = new uint32[](quorumNumbers.length);
for (uint256 i = 0; i < quorumNumbers.length; i++) {
uint8 quorumNumber = uint8(quorumNumbers[i]);
require(_quorumExists(quorumNumber), "StakeRegistry.getTotalStakeIndicesAtBlockNumber: quorum does not exist");
_checkQuorumExists(quorumNumber);
require(
_totalStakeHistory[quorumNumber][0].updateBlockNumber <= blockNumber,
"StakeRegistry.getTotalStakeIndicesAtBlockNumber: quorum has no stake history at blockNumber"
Expand All @@ -712,4 +709,19 @@ contract StakeRegistry is StakeRegistryStorage {
}
return indices;
}

function _checkRegistryCoordinator() internal view {
require(
msg.sender == address(registryCoordinator),
"StakeRegistry.onlyRegistryCoordinator: caller is not the RegistryCoordinator"
);
}

function _checkRegistryCoordinatorOwner() internal view {
require(msg.sender == IRegistryCoordinator(registryCoordinator).owner(), "StakeRegistry.onlyCoordinatorOwner: caller is not the owner of the registryCoordinator");
}

function _checkQuorumExists(uint8 quorumNumber) internal view {
require(_quorumExists(quorumNumber), "StakeRegistry.quorumExists: quorum does not exist");
}
}
Loading