Skip to content

Commit 8778517

Browse files
authored
refactor: custom errors in middleware (#355)
* refactor: custom errors * fix: unit tests
1 parent 45eb220 commit 8778517

29 files changed

+300
-271
lines changed

src/BLSApkRegistry.sol

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
7979
* @param quorumNumber The number of the new quorum
8080
*/
8181
function initializeQuorum(uint8 quorumNumber) public virtual onlyRegistryCoordinator {
82-
require(apkHistory[quorumNumber].length == 0, "BLSApkRegistry.initializeQuorum: quorum already exists");
82+
require(apkHistory[quorumNumber].length == 0, QuorumAlreadyExists());
8383

8484
apkHistory[quorumNumber].push(ApkUpdate({
8585
apkHash: bytes24(0),
@@ -100,17 +100,9 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
100100
BN254.G1Point calldata pubkeyRegistrationMessageHash
101101
) external onlyRegistryCoordinator returns (bytes32 operatorId) {
102102
bytes32 pubkeyHash = BN254.hashG1Point(params.pubkeyG1);
103-
require(
104-
pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey"
105-
);
106-
require(
107-
operatorToPubkeyHash[operator] == bytes32(0),
108-
"BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey"
109-
);
110-
require(
111-
pubkeyHashToOperator[pubkeyHash] == address(0),
112-
"BLSApkRegistry.registerBLSPublicKey: public key already registered"
113-
);
103+
require(pubkeyHash != ZERO_PK_HASH, ZeroPubKey());
104+
require(operatorToPubkeyHash[operator] == bytes32(0), OperatorAlreadyRegistered());
105+
require(pubkeyHashToOperator[pubkeyHash] == address(0), BLSPubkeyAlreadyRegistered());
114106

115107
// gamma = h(sigma, P, P', H(m))
116108
uint256 gamma = uint256(keccak256(abi.encodePacked(
@@ -130,7 +122,7 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
130122
BN254.negGeneratorG2(),
131123
pubkeyRegistrationMessageHash.plus(BN254.generatorG1().scalar_mul(gamma)),
132124
params.pubkeyG2
133-
), "BLSApkRegistry.registerBLSPublicKey: either the G1 signature is wrong, or G1 and G2 private key do not match");
125+
), InvalidBLSSignatureOrPrivateKey());
134126

135127
operatorToPubkey[operator] = params.pubkeyG1;
136128
operatorToPubkeyHash[operator] = pubkeyHash;
@@ -151,7 +143,7 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
151143
// Validate quorum exists and get history length
152144
uint8 quorumNumber = uint8(quorumNumbers[i]);
153145
uint256 historyLength = apkHistory[quorumNumber].length;
154-
require(historyLength != 0, "BLSApkRegistry._processQuorumApkUpdate: quorum does not exist");
146+
require(historyLength != 0, QuorumDoesNotExist());
155147

156148
// Update aggregate public key for this quorum
157149
newApk = currentApk[quorumNumber].plus(point);
@@ -185,10 +177,7 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
185177
BN254.G1Point memory pubkey = operatorToPubkey[operator];
186178
bytes32 pubkeyHash = operatorToPubkeyHash[operator];
187179

188-
require(
189-
pubkeyHash != bytes32(0),
190-
"BLSApkRegistry.getRegisteredPubkey: operator is not registered"
191-
);
180+
require(pubkeyHash != bytes32(0), OperatorNotRegistered());
192181

193182
return (pubkey, pubkeyHash);
194183
}
@@ -253,11 +242,11 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
253242
*/
254243
require(
255244
blockNumber >= quorumApkUpdate.updateBlockNumber,
256-
"BLSApkRegistry.getApkHashAtBlockNumberAndIndex: index too recent"
245+
BlockNumberTooRecent()
257246
);
258247
require(
259248
quorumApkUpdate.nextUpdateBlockNumber == 0 || blockNumber < quorumApkUpdate.nextUpdateBlockNumber,
260-
"BLSApkRegistry.getApkHashAtBlockNumberAndIndex: not latest apk update"
249+
BlockNumberNotLatest()
261250
);
262251

263252
return quorumApkUpdate.apkHash;
@@ -280,9 +269,6 @@ contract BLSApkRegistry is BLSApkRegistryStorage {
280269
}
281270

282271
function _checkRegistryCoordinator() internal view {
283-
require(
284-
msg.sender == address(registryCoordinator),
285-
"BLSApkRegistry._checkRegistryCoordinator: caller is not the registry coordinator"
286-
);
272+
require(msg.sender == address(registryCoordinator), OnlyRegistryCoordinatorOwner());
287273
}
288274
}

src/BLSSignatureChecker.sol

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ contract BLSSignatureChecker is IBLSSignatureChecker {
3131
bool public staleStakesForbidden;
3232

3333
modifier onlyCoordinatorOwner() {
34-
require(
35-
msg.sender == registryCoordinator.owner(),
36-
"BLSSignatureChecker.onlyCoordinatorOwner: caller is not the owner of the registryCoordinator"
37-
);
34+
require(msg.sender == registryCoordinator.owner(), OnlyRegistryCoordinatorOwner());
3835
_;
3936
}
4037

@@ -91,29 +88,23 @@ contract BLSSignatureChecker is IBLSSignatureChecker {
9188
uint32 referenceBlockNumber,
9289
NonSignerStakesAndSignature memory params
9390
) public view returns (QuorumStakeTotals memory, bytes32) {
94-
require(
95-
quorumNumbers.length != 0,
96-
"BLSSignatureChecker.checkSignatures: empty quorum input"
97-
);
91+
require(quorumNumbers.length != 0, InputEmptyQuorumNumbers());
9892

9993
require(
10094
(quorumNumbers.length == params.quorumApks.length) &&
10195
(quorumNumbers.length == params.quorumApkIndices.length) &&
10296
(quorumNumbers.length == params.totalStakeIndices.length) &&
10397
(quorumNumbers.length == params.nonSignerStakeIndices.length),
104-
"BLSSignatureChecker.checkSignatures: input quorum length mismatch"
98+
InputArrayLengthMismatch()
10599
);
106100

107101
require(
108102
params.nonSignerPubkeys.length ==
109103
params.nonSignerQuorumBitmapIndices.length,
110-
"BLSSignatureChecker.checkSignatures: input nonsigner length mismatch"
104+
InputNonSignerLengthMismatch()
111105
);
112106

113-
require(
114-
referenceBlockNumber < uint32(block.number),
115-
"BLSSignatureChecker.checkSignatures: invalid reference block"
116-
);
107+
require(referenceBlockNumber < uint32(block.number), InvalidReferenceBlocknumber());
117108

118109
// This method needs to calculate the aggregate pubkey for all signing operators across
119110
// all signing quorums. To do that, we can query the aggregate pubkey for each quorum
@@ -155,7 +146,7 @@ contract BLSSignatureChecker is IBLSSignatureChecker {
155146
require(
156147
uint256(nonSigners.pubkeyHashes[j]) >
157148
uint256(nonSigners.pubkeyHashes[j - 1]),
158-
"BLSSignatureChecker.checkSignatures: nonSignerPubkeys not sorted"
149+
NonSignerPubkeysNotSorted()
159150
);
160151
}
161152

@@ -207,7 +198,7 @@ contract BLSSignatureChecker is IBLSSignatureChecker {
207198
) +
208199
withdrawalDelayBlocks >
209200
referenceBlockNumber,
210-
"BLSSignatureChecker.checkSignatures: StakeRegistry updates must be within withdrawalDelayBlocks window"
201+
StaleStakesForbidden()
211202
);
212203
}
213204

@@ -220,7 +211,7 @@ contract BLSSignatureChecker is IBLSSignatureChecker {
220211
blockNumber: referenceBlockNumber,
221212
index: params.quorumApkIndices[i]
222213
}),
223-
"BLSSignatureChecker.checkSignatures: quorumApk hash in storage does not match provided quorum apk"
214+
InvalidQuorumApkHash()
224215
);
225216
apk = apk.plus(params.quorumApks[i]);
226217

@@ -274,14 +265,8 @@ contract BLSSignatureChecker is IBLSSignatureChecker {
274265
params.apkG2,
275266
params.sigma
276267
);
277-
require(
278-
pairingSuccessful,
279-
"BLSSignatureChecker.checkSignatures: pairing precompile call failed"
280-
);
281-
require(
282-
signatureIsValid,
283-
"BLSSignatureChecker.checkSignatures: signature is invalid"
284-
);
268+
require(pairingSuccessful, InvalidBLSPairingKey());
269+
require(signatureIsValid, InvalidBLSSignature());
285270
}
286271
// set signatoryRecordHash variable used for fraudproofs
287272
bytes32 signatoryRecordHash = keccak256(

src/EjectionManager.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ contract EjectionManager is IEjectionManager, OwnableUpgradeable {
6767
* @dev The owner can eject operators without recording of stake ejection
6868
*/
6969
function ejectOperators(bytes32[][] memory _operatorIds) external {
70-
require(isEjector[msg.sender] || msg.sender == owner(), "EjectionManager.ejectOperators: Only owner or ejector can eject");
70+
require(isEjector[msg.sender] || msg.sender == owner(), OnlyOwnerOrEjector());
7171

7272
for(uint i = 0; i < _operatorIds.length; ++i) {
7373
uint8 quorumNumber = uint8(i);
@@ -137,7 +137,7 @@ contract EjectionManager is IEjectionManager, OwnableUpgradeable {
137137

138138
///@dev internal function to set the quorum ejection params
139139
function _setQuorumEjectionParams(uint8 _quorumNumber, QuorumEjectionParams memory _quorumEjectionParams) internal {
140-
require(_quorumNumber < MAX_QUORUM_COUNT, "EjectionManager._setQuorumEjectionParams: Quorum number exceeds MAX_QUORUM_COUNT");
140+
require(_quorumNumber < MAX_QUORUM_COUNT, MaxQuorumCount());
141141
quorumEjectionParams[_quorumNumber] = _quorumEjectionParams;
142142
emit QuorumEjectionParamsSet(_quorumNumber, _quorumEjectionParams.rateLimitWindow, _quorumEjectionParams.ejectableStakePercent);
143143
}

src/IndexRegistry.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ contract IndexRegistry is IndexRegistryStorage {
4747
// Validate quorum exists and get current operator count
4848
uint8 quorumNumber = uint8(quorumNumbers[i]);
4949
uint256 historyLength = _operatorCountHistory[quorumNumber].length;
50-
require(historyLength != 0, "IndexRegistry.registerOperator: quorum does not exist");
50+
require(historyLength != 0, QuorumDoesNotExist());
5151

5252
/**
5353
* Increase the number of operators currently active for this quorum,
@@ -87,7 +87,7 @@ contract IndexRegistry is IndexRegistryStorage {
8787
// Validate quorum exists and get the operatorIndex of the operator being deregistered
8888
uint8 quorumNumber = uint8(quorumNumbers[i]);
8989
uint256 historyLength = _operatorCountHistory[quorumNumber].length;
90-
require(historyLength != 0, "IndexRegistry.registerOperator: quorum does not exist");
90+
require(historyLength != 0, QuorumDoesNotExist());
9191
uint32 operatorIndexToRemove = currentOperatorIndex[quorumNumber][operatorId];
9292

9393
/**
@@ -113,7 +113,7 @@ contract IndexRegistry is IndexRegistryStorage {
113113
* @param quorumNumber The number of the new quorum
114114
*/
115115
function initializeQuorum(uint8 quorumNumber) public virtual onlyRegistryCoordinator {
116-
require(_operatorCountHistory[quorumNumber].length == 0, "IndexRegistry.createQuorum: quorum already exists");
116+
require(_operatorCountHistory[quorumNumber].length == 0, QuorumDoesNotExist());
117117

118118
_operatorCountHistory[quorumNumber].push(QuorumUpdate({
119119
numOperators: 0,
@@ -329,7 +329,7 @@ contract IndexRegistry is IndexRegistryStorage {
329329
operatorList[i] = _operatorIdForIndexAtBlockNumber(quorumNumber, uint32(i), blockNumber);
330330
require(
331331
operatorList[i] != OPERATOR_DOES_NOT_EXIST_ID,
332-
"IndexRegistry.getOperatorListAtBlockNumber: operator does not exist at the given block number"
332+
OperatorIdDoesNotExist()
333333
);
334334
}
335335
return operatorList;
@@ -342,6 +342,6 @@ contract IndexRegistry is IndexRegistryStorage {
342342
}
343343

344344
function _checkRegistryCoordinator() internal view {
345-
require(msg.sender == address(registryCoordinator), "IndexRegistry._checkRegistryCoordinator: caller is not the registry coordinator");
345+
require(msg.sender == address(registryCoordinator), OnlyRegistryCoordinator());
346346
}
347347
}

src/OperatorStateRetriever.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ contract OperatorStateRetriever {
2626
uint32[][] nonSignerStakeIndices; // nonSignerStakeIndices[quorumNumberIndex][nonSignerIndex]
2727
}
2828

29+
error OperatorNotRegistered();
30+
2931
/**
3032
* @notice This function is intended to to be called by AVS operators every time a new task is created (i.e.)
3133
* the AVS coordinator makes a request to AVS operators. Since all of the crucial information is kept onchain,
@@ -131,7 +133,7 @@ contract OperatorStateRetriever {
131133
checkSignaturesIndices.nonSignerQuorumBitmapIndices[i]
132134
);
133135

134-
require(nonSignerQuorumBitmap != 0, "OperatorStateRetriever.getCheckSignaturesIndices: operator must be registered at blocknumber");
136+
require(nonSignerQuorumBitmap != 0, OperatorNotRegistered());
135137

136138
// if the operator was a part of the quorum and the quorum is a part of the provided quorumNumbers
137139
if ((nonSignerQuorumBitmap >> uint8(quorumNumbers[quorumNumberIndex])) & 1 == 1) {

src/ServiceManagerBase.sol

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ abstract contract ServiceManagerBase is ServiceManagerBaseStorage {
3030

3131
/// @notice when applied to a function, only allows the RegistryCoordinator to call it
3232
modifier onlyRegistryCoordinator() {
33-
require(
34-
msg.sender == address(_registryCoordinator),
35-
"ServiceManagerBase.onlyRegistryCoordinator: caller is not the registry coordinator"
36-
);
33+
require(msg.sender == address(_registryCoordinator), OnlyRegistryCoordinator());
3734
_;
3835
}
3936

@@ -209,7 +206,7 @@ abstract contract ServiceManagerBase is ServiceManagerBaseStorage {
209206
function acceptProposedSlasher() external onlyOwner {
210207
require(
211208
block.timestamp >= slasherProposalTimestamp + SLASHER_PROPOSAL_DELAY,
212-
"ServiceManager: Slasher proposal delay not met"
209+
DelayPeriodNotPassed()
213210
);
214211
_setSlasher(proposedSlasher);
215212
delete proposedSlasher;
@@ -314,24 +311,14 @@ abstract contract ServiceManagerBase is ServiceManagerBaseStorage {
314311
}
315312

316313
function _checkRewardsInitiator() internal view {
317-
require(
318-
msg.sender == rewardsInitiator,
319-
"ServiceManagerBase.onlyRewardsInitiator: caller is not the rewards initiator"
320-
);
314+
require(msg.sender == rewardsInitiator, OnlyRewardsInitiator());
321315
}
322316

323317
function _checkStakeRegistry() internal view {
324-
require(
325-
msg.sender == address(_stakeRegistry),
326-
"ServiceManagerBase.onlyStakeRegistry: caller is not the stake registry"
327-
);
318+
require(msg.sender == address(_stakeRegistry), OnlyStakeRegistry());
328319
}
329320

330-
331321
function _checkSlasher() internal view {
332-
require(
333-
msg.sender == slasher,
334-
"ServiceManagerBase.onlySlasher: caller is not the slasher"
335-
);
322+
require(msg.sender == slasher, OnlySlasher());
336323
}
337324
}

0 commit comments

Comments
 (0)