Skip to content

Commit 4ade403

Browse files
committed
refactor: KeyRegistry unit testing (#1482)
**Motivation:** Update `KeyRegistrar` unit tests **Modifications:** - Update bindings - Add .tree file - Add `_` to internal storage vars **Result:** Updated tests. Full coverage (aside from unreachable else statements)
1 parent 15674c1 commit 4ade403

File tree

11 files changed

+1638
-467
lines changed

11 files changed

+1638
-467
lines changed

pkg/bindings/BN254TableCalculator/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/CrossChainRegistry/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/ECDSATableCalculator/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/IKeyRegistrar/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/KeyRegistrar/binding.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/KeyRegistrarStorage/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/contracts/permissions/KeyRegistrar.sol

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
5555
require(curveType == CurveType.ECDSA || curveType == CurveType.BN254, InvalidCurveType());
5656

5757
// Prevent overwriting existing configurations
58-
CurveType _curveType = operatorSetCurveTypes[operatorSet.key()];
58+
CurveType _curveType = _operatorSetCurveTypes[operatorSet.key()];
5959
require(_curveType == CurveType.NONE, ConfigurationAlreadySet());
6060

61-
operatorSetCurveTypes[operatorSet.key()] = curveType;
61+
_operatorSetCurveTypes[operatorSet.key()] = curveType;
6262

6363
emit OperatorSetConfigured(operatorSet, curveType);
6464
}
@@ -70,11 +70,11 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
7070
bytes calldata keyData,
7171
bytes calldata signature
7272
) external checkCanCall(operator) {
73-
CurveType curveType = operatorSetCurveTypes[operatorSet.key()];
73+
CurveType curveType = _operatorSetCurveTypes[operatorSet.key()];
7474
require(curveType != CurveType.NONE, OperatorSetNotConfigured());
7575

7676
// Check if the key is already registered
77-
require(!operatorKeyInfo[operatorSet.key()][operator].isRegistered, KeyAlreadyRegistered());
77+
require(!_operatorKeyInfo[operatorSet.key()][operator].isRegistered, KeyAlreadyRegistered());
7878

7979
// Register key based on curve type - both now require signature verification
8080
if (curveType == CurveType.ECDSA) {
@@ -95,25 +95,25 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
9595
!allocationManager.isOperatorSlashable(operator, operatorSet), OperatorStillSlashable(operatorSet, operator)
9696
);
9797

98-
CurveType curveType = operatorSetCurveTypes[operatorSet.key()];
98+
CurveType curveType = _operatorSetCurveTypes[operatorSet.key()];
9999
require(curveType != CurveType.NONE, OperatorSetNotConfigured());
100100

101-
KeyInfo memory keyInfo = operatorKeyInfo[operatorSet.key()][operator];
101+
KeyInfo memory keyInfo = _operatorKeyInfo[operatorSet.key()][operator];
102102

103103
require(keyInfo.isRegistered, KeyNotFound(operatorSet, operator));
104104

105105
// Clear key info
106-
delete operatorKeyInfo[operatorSet.key()][operator];
106+
delete _operatorKeyInfo[operatorSet.key()][operator];
107107

108108
emit KeyDeregistered(operatorSet, operator, curveType);
109109
}
110110

111111
/// @inheritdoc IKeyRegistrar
112112
function checkKey(OperatorSet memory operatorSet, address operator) external view returns (bool) {
113-
CurveType curveType = operatorSetCurveTypes[operatorSet.key()];
113+
CurveType curveType = _operatorSetCurveTypes[operatorSet.key()];
114114
require(curveType != CurveType.NONE, OperatorSetNotConfigured());
115115

116-
KeyInfo memory keyInfo = operatorKeyInfo[operatorSet.key()][operator];
116+
KeyInfo memory keyInfo = _operatorKeyInfo[operatorSet.key()][operator];
117117
return keyInfo.isRegistered;
118118
}
119119

@@ -148,7 +148,7 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
148148
bytes32 keyHash = _getKeyHashForKeyData(keyData, CurveType.ECDSA);
149149

150150
// Check global uniqueness
151-
require(!globalKeyRegistry[keyHash], KeyAlreadyRegistered());
151+
require(!_globalKeyRegistry[keyHash], KeyAlreadyRegistered());
152152

153153
// Get the signable digest for the ECDSA key registration message
154154
bytes32 signableDigest = getECDSAKeyRegistrationMessageHash(operator, operatorSet, keyAddress);
@@ -203,7 +203,7 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
203203

204204
// Calculate key hash and check global uniqueness
205205
bytes32 keyHash = _getKeyHashForKeyData(keyData, CurveType.BN254);
206-
require(!globalKeyRegistry[keyHash], KeyAlreadyRegistered());
206+
require(!_globalKeyRegistry[keyHash], KeyAlreadyRegistered());
207207

208208
// Store key data
209209
_storeKeyData(operatorSet, operator, keyData, keyHash);
@@ -223,10 +223,10 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
223223
bytes32 keyHash
224224
) internal {
225225
// Store key data
226-
operatorKeyInfo[operatorSet.key()][operator] = KeyInfo({isRegistered: true, keyData: pubkey});
226+
_operatorKeyInfo[operatorSet.key()][operator] = KeyInfo({isRegistered: true, keyData: pubkey});
227227

228228
// Update global key registry
229-
globalKeyRegistry[keyHash] = true;
229+
_globalKeyRegistry[keyHash] = true;
230230
}
231231

232232
/**
@@ -254,14 +254,14 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
254254

255255
/// @inheritdoc IKeyRegistrar
256256
function isRegistered(OperatorSet memory operatorSet, address operator) external view returns (bool) {
257-
return operatorKeyInfo[operatorSet.key()][operator].isRegistered;
257+
return _operatorKeyInfo[operatorSet.key()][operator].isRegistered;
258258
}
259259

260260
/// @inheritdoc IKeyRegistrar
261261
function getOperatorSetCurveType(
262262
OperatorSet memory operatorSet
263263
) external view returns (CurveType) {
264-
return operatorSetCurveTypes[operatorSet.key()];
264+
return _operatorSetCurveTypes[operatorSet.key()];
265265
}
266266

267267
/// @inheritdoc IKeyRegistrar
@@ -270,10 +270,10 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
270270
address operator
271271
) external view returns (BN254.G1Point memory g1Point, BN254.G2Point memory g2Point) {
272272
// Validate operator set curve type
273-
CurveType curveType = operatorSetCurveTypes[operatorSet.key()];
273+
CurveType curveType = _operatorSetCurveTypes[operatorSet.key()];
274274
require(curveType == CurveType.BN254, InvalidCurveType());
275275

276-
KeyInfo memory keyInfo = operatorKeyInfo[operatorSet.key()][operator];
276+
KeyInfo memory keyInfo = _operatorKeyInfo[operatorSet.key()][operator];
277277

278278
if (!keyInfo.isRegistered) {
279279
// Create default values for an empty key
@@ -290,10 +290,10 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
290290
/// @inheritdoc IKeyRegistrar
291291
function getECDSAKey(OperatorSet memory operatorSet, address operator) public view returns (bytes memory) {
292292
// Validate operator set curve type
293-
CurveType curveType = operatorSetCurveTypes[operatorSet.key()];
293+
CurveType curveType = _operatorSetCurveTypes[operatorSet.key()];
294294
require(curveType == CurveType.ECDSA, InvalidCurveType());
295295

296-
KeyInfo memory keyInfo = operatorKeyInfo[operatorSet.key()][operator];
296+
KeyInfo memory keyInfo = _operatorKeyInfo[operatorSet.key()][operator];
297297
return keyInfo.keyData; // Returns the 20-byte address as bytes
298298
}
299299

@@ -306,13 +306,13 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
306306
function isKeyGloballyRegistered(
307307
bytes32 keyHash
308308
) external view returns (bool) {
309-
return globalKeyRegistry[keyHash];
309+
return _globalKeyRegistry[keyHash];
310310
}
311311

312312
/// @inheritdoc IKeyRegistrar
313313
function getKeyHash(OperatorSet memory operatorSet, address operator) external view returns (bytes32) {
314-
KeyInfo memory keyInfo = operatorKeyInfo[operatorSet.key()][operator];
315-
CurveType curveType = operatorSetCurveTypes[operatorSet.key()];
314+
KeyInfo memory keyInfo = _operatorKeyInfo[operatorSet.key()][operator];
315+
CurveType curveType = _operatorSetCurveTypes[operatorSet.key()];
316316

317317
if (!keyInfo.isRegistered) {
318318
return bytes32(0);

src/contracts/permissions/KeyRegistrarStorage.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ abstract contract KeyRegistrarStorage is IKeyRegistrar {
2121
// Mutatables
2222

2323
/// @dev Maps (operatorSetKey, operator) to their key info
24-
mapping(bytes32 => mapping(address => KeyInfo)) internal operatorKeyInfo;
24+
mapping(bytes32 operatorSetKey => mapping(address operator => KeyInfo keyInfo)) internal _operatorKeyInfo;
2525

2626
/// @dev Maps operatorSetKey to the key type
27-
mapping(bytes32 => CurveType) internal operatorSetCurveTypes;
27+
mapping(bytes32 operatorSetKey => CurveType curveType) internal _operatorSetCurveTypes;
2828

2929
/// @dev Global mapping of key hash to registration status - enforces global uniqueness
30-
mapping(bytes32 => bool) internal globalKeyRegistry;
30+
mapping(bytes32 keyHash => bool isRegistered) internal _globalKeyRegistry;
3131

3232
// Construction
3333

src/test/tree/KeyRegistrar.tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
.
2+
└── KeyRegistrar (**** denotes that integration tests are needed to fully validate path)
3+
├── when configureOperatorSet is called
4+
│ ├── given that the caller does not have permission
5+
│ │ └── it should revert
6+
│ ├── given that the curve type is NONE
7+
│ │ └── it should revert
8+
│ ├── given that the curve type is invalid (not ECDSA or BN254)
9+
│ │ └── it should revert
10+
│ ├── given that the operator set configuration already exists
11+
│ │ └── it should revert
12+
│ └── given that all parameters are valid
13+
│ ├── given that the curve type is ECDSA
14+
│ │ └── it should configure the operator set for ECDSA and emit OperatorSetConfigured
15+
│ └── given that the curve type is BN254
16+
│ └── it should configure the operator set for BN254 and emit OperatorSetConfigured
17+
├── when registerKey is called
18+
│ ├── given that the caller does not have permission
19+
│ │ └── it should revert
20+
│ ├── given that the operator set is not configured
21+
│ │ └── it should revert
22+
│ ├── given that the key is already registered for this operator set
23+
│ │ └── it should revert
24+
│ ├── given that the operator set is configured for ECDSA
25+
│ │ ├── given that the key data length is not 20 bytes
26+
│ │ │ └── it should revert
27+
│ │ ├── given that the decoded address is zero
28+
│ │ │ └── it should revert
29+
│ │ ├── given that the key hash is already globally registered
30+
│ │ │ └── it should revert
31+
│ │ ├── given that the signature is invalid
32+
│ │ │ └── it should revert
33+
│ │ └── given that all validations pass
34+
│ │ └── it should register the ECDSA key, update global registry, and emit KeyRegistered
35+
│ └── given that the operator set is configured for BN254
36+
│ ├── given that the G1 point is zero (0,0)
37+
│ │ └── it should revert
38+
│ ├── given that the signature verification fails
39+
│ │ └── it should revert
40+
│ ├── given that the key hash is already globally registered
41+
│ │ └── it should revert
42+
│ └── given that all validations pass
43+
│ └── it should register the BN254 key, update global registry, and emit KeyRegistered
44+
├── when deregisterKey is called
45+
│ ├── given that the caller does not have permission
46+
│ │ └── it should revert
47+
│ ├── given that the operator is still slashable for this operator set ****
48+
│ │ └── it should revert
49+
│ ├── given that the operator set is not configured
50+
│ │ └── it should revert
51+
│ ├── given that the key is not registered
52+
│ │ └── it should revert
53+
│ └── given that all conditions are met
54+
│ ├── given that the operator set uses ECDSA
55+
│ │ └── it should delete the key info and emit KeyDeregistered (global registry remains)
56+
│ └── given that the operator set uses BN254
57+
│ └── it should delete the key info and emit KeyDeregistered (global registry remains)
58+
├── when checkKey is called
59+
│ ├── given that the operator set is not configured
60+
│ │ └── it should revert
61+
│ ├── given that the operator has a registered key
62+
│ │ └── it should return true
63+
│ └── given that the operator does not have a registered key
64+
│ └── it should return false
65+
├── when isRegistered is called
66+
│ ├── given that the operator has a registered key for the operator set
67+
│ │ └── it should return true
68+
│ └── given that the operator does not have a registered key for the operator set
69+
│ └── it should return false
70+
├── when getOperatorSetCurveType is called
71+
│ ├── given that the operator set is not configured
72+
│ │ └── it should return CurveType.NONE
73+
│ ├── given that the operator set is configured for ECDSA
74+
│ │ └── it should return CurveType.ECDSA
75+
│ └── given that the operator set is configured for BN254
76+
│ └── it should return CurveType.BN254
77+
├── when getBN254Key is called
78+
│ ├── given that the operator set is not configured for BN254
79+
│ │ └── it should revert
80+
│ ├── given that the operator is not registered
81+
│ │ └── it should return zero G1 and G2 points
82+
│ └── given that the operator is registered
83+
│ └── it should return the decoded G1 and G2 points
84+
├── when getECDSAKey is called
85+
│ ├── given that the operator set is not configured for ECDSA
86+
│ │ └── it should revert
87+
│ ├── given that the operator is not registered
88+
│ │ └── it should return empty bytes
89+
│ └── given that the operator is registered
90+
│ └── it should return the 20-byte address as bytes
91+
├── when getECDSAAddress is called
92+
│ ├── given that the operator set is not configured for ECDSA
93+
│ │ └── it should revert
94+
│ ├── given that the operator is not registered
95+
│ │ └── it should return address(0)
96+
│ └── given that the operator is registered
97+
│ └── it should return the decoded address
98+
├── when isKeyGloballyRegistered is called
99+
│ ├── given that the key hash exists in global registry
100+
│ │ └── it should return true
101+
│ └── given that the key hash does not exist in global registry
102+
│ └── it should return false
103+
├── when getKeyHash is called
104+
│ ├── given that the operator is not registered
105+
│ │ └── it should return bytes32(0)
106+
│ ├── given that the operator has an ECDSA key
107+
│ │ └── it should return keccak256 of the key data
108+
│ └── given that the operator has a BN254 key
109+
│ └── it should return the BN254 G1 point hash
110+
├── when getECDSAKeyRegistrationMessageHash is called
111+
│ └── it should return the EIP-712 compliant message hash for ECDSA registration
112+
├── when getBN254KeyRegistrationMessageHash is called
113+
│ └── it should return the EIP-712 compliant message hash for BN254 registration
114+
└── when encodeBN254KeyData is called
115+
└── it should return the encoded bytes of G1 and G2 points

0 commit comments

Comments
 (0)