-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathOperatorSetUser.t.sol
283 lines (239 loc) · 11.3 KB
/
OperatorSetUser.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.27;
import "./User.t.sol";
import "forge-std/console.sol";
contract OperatorSetUser is User {
using BN254 for *;
using Strings for *;
using BitmapStrings for *;
using BitmapUtils for *;
SlashingRegistryCoordinator slashingRegistryCoordinator;
AllocationManager allocationManager;
address avs;
constructor(
string memory name,
uint256 _privKey,
IBLSApkRegistryTypes.PubkeyRegistrationParams memory _pubkeyParams
) User(name, _privKey, _pubkeyParams) {
IUserDeployer deployer = IUserDeployer(msg.sender);
slashingRegistryCoordinator = deployer.slashingRegistryCoordinator();
allocationManager = AllocationManager(deployer.allocationManager());
avs = slashingRegistryCoordinator.avs();
// Generate BN254 keypair and registration signature
privKey = _privKey;
pubkeyParams = _pubkeyParams;
BN254.G1Point memory registrationMessageHash =
slashingRegistryCoordinator.pubkeyRegistrationMessageHash(address(this));
pubkeyParams.pubkeyRegistrationSignature = registrationMessageHash.scalar_mul(privKey);
operatorId = pubkeyParams.pubkeyG1.hashG1Point();
}
function registerOperator(
bytes calldata quorums
) public virtual override createSnapshot returns (bytes32) {
_log("registerOperator", quorums);
vm.warp(block.timestamp + 1);
// If operator has previously registered and is still slashable, roll blocks until they are
// no longer slashable
vm.roll(block.number + uint256(allocationManager.DEALLOCATION_DELAY()) + 1);
// encode bytes data field passed into SlashingRegistryCoordinator.registerOperator
// Register params for AllocationManager
bytes memory data = abi.encode(
ISlashingRegistryCoordinatorTypes.RegistrationType.NORMAL, NAME, pubkeyParams
);
IAllocationManagerTypes.RegisterParams memory registerParams = IAllocationManagerTypes
.RegisterParams({avs: avs, operatorSetIds: _getOperatorSetIds(quorums), data: data});
allocationManager.registerForOperatorSets({operator: address(this), params: registerParams});
return pubkeyParams.pubkeyG1.hashG1Point();
}
function registerOperatorWithChurn(
bytes calldata churnQuorums,
User[] calldata churnTargets,
bytes calldata standardQuorums
) public virtual override createSnapshot {
_logChurn("registerOperatorWithChurn", churnQuorums, churnTargets, standardQuorums);
vm.warp(block.timestamp + 1);
// If operator has previously registered and is still slashable, roll blocks until they are
// no longer slashable
vm.roll(block.number + uint256(allocationManager.DEALLOCATION_DELAY()) + 1);
// Sanity check input:
// - churnQuorums and churnTargets should have equal length
// - churnQuorums and standardQuorums should not have any bits in common
uint192 churnBitmap = uint192(churnQuorums.orderedBytesArrayToBitmap());
uint192 standardBitmap = uint192(standardQuorums.orderedBytesArrayToBitmap());
assertEq(
churnQuorums.length,
churnTargets.length,
"User.registerOperatorWithChurn: input length mismatch"
);
assertTrue(
churnBitmap.noBitsInCommon(standardBitmap),
"User.registerOperatorWithChurn: input quorums have common bits"
);
bytes memory allQuorums = churnBitmap.plus(standardBitmap).bitmapToBytesArray();
(
ISlashingRegistryCoordinatorTypes.OperatorKickParam[] memory kickParams,
ISignatureUtilsMixinTypes.SignatureWithSaltAndExpiry memory churnApproverSignature
) = _generateOperatorKickParams(allQuorums, churnQuorums, churnTargets, standardQuorums);
// Encode with RegistrationType.CHURN
bytes memory data = abi.encode(
ISlashingRegistryCoordinatorTypes.RegistrationType.CHURN,
NAME,
pubkeyParams,
kickParams,
churnApproverSignature
);
IAllocationManagerTypes.RegisterParams memory registerParams = IAllocationManagerTypes
.RegisterParams({avs: avs, operatorSetIds: _getOperatorSetIds(allQuorums), data: data});
allocationManager.registerForOperatorSets({operator: address(this), params: registerParams});
}
function deregisterOperator(
bytes calldata quorums
) public virtual override createSnapshot {
_log("deregisterOperator", quorums);
uint32[] memory operatorSetIds = _getOperatorSetIds(quorums);
IAllocationManagerTypes.DeregisterParams memory deregisterParams = IAllocationManagerTypes
.DeregisterParams({avs: avs, operator: address(this), operatorSetIds: operatorSetIds});
allocationManager.deregisterFromOperatorSets({params: deregisterParams});
}
/// @dev Uses updateOperators to update this user's stake
function updateStakes() public virtual override createSnapshot {
_log("updateStakes (updateOperators)");
// get all quorums this operator is registered for
uint192 currentBitmap = slashingRegistryCoordinator.getCurrentQuorumBitmap(operatorId);
bytes memory quorumNumbers = currentBitmap.bitmapToBytesArray();
// get all operators in those quorums
address[][] memory operatorsPerQuorum = new address[][](quorumNumbers.length);
for (uint256 i = 0; i < quorumNumbers.length; i++) {
bytes32[] memory operatorIds = indexRegistry.getOperatorListAtBlockNumber(
uint8(quorumNumbers[i]), uint32(block.number)
);
operatorsPerQuorum[i] = new address[](operatorIds.length);
for (uint256 j = 0; j < operatorIds.length; j++) {
operatorsPerQuorum[i][j] = blsApkRegistry.pubkeyHashToOperator(operatorIds[j]);
}
operatorsPerQuorum[i] = Sort.sortAddresses(operatorsPerQuorum[i]);
}
slashingRegistryCoordinator.updateOperatorsForQuorum(operatorsPerQuorum, quorumNumbers);
}
function _getOperatorSetIds(
bytes memory quorums
) internal pure returns (uint32[] memory) {
uint32[] memory operatorSetIds = new uint32[](quorums.length);
for (uint256 i = 0; i < quorums.length; i++) {
operatorSetIds[i] = uint32(uint8(quorums[i]));
}
return operatorSetIds;
}
function _generateOperatorKickParams(
bytes memory allQuorums,
bytes calldata churnQuorums,
User[] calldata churnTargets,
bytes calldata standardQuorums
)
internal
virtual
override
returns (
ISlashingRegistryCoordinatorTypes.OperatorKickParam[] memory,
ISignatureUtilsMixinTypes.SignatureWithSaltAndExpiry memory
)
{
ISlashingRegistryCoordinator.OperatorKickParam[] memory kickParams =
new ISlashingRegistryCoordinator.OperatorKickParam[](allQuorums.length);
// this constructs OperatorKickParam[] in ascending quorum order
// (yikes)
uint256 churnIdx;
uint256 stdIdx;
while (churnIdx + stdIdx < allQuorums.length) {
if (churnIdx == churnQuorums.length) {
kickParams[churnIdx + stdIdx] = ISlashingRegistryCoordinatorTypes.OperatorKickParam({
quorumNumber: 0,
operator: address(0)
});
stdIdx++;
} else if (
stdIdx == standardQuorums.length || churnQuorums[churnIdx] < standardQuorums[stdIdx]
) {
kickParams[churnIdx + stdIdx] = ISlashingRegistryCoordinatorTypes.OperatorKickParam({
quorumNumber: uint8(churnQuorums[churnIdx]),
operator: address(churnTargets[churnIdx])
});
churnIdx++;
} else if (standardQuorums[stdIdx] < churnQuorums[churnIdx]) {
kickParams[churnIdx + stdIdx] = ISlashingRegistryCoordinatorTypes.OperatorKickParam({
quorumNumber: 0,
operator: address(0)
});
stdIdx++;
} else {
revert("User.registerOperatorWithChurn: malformed input");
}
}
// Generate churn approver signature
bytes32 _salt = keccak256(abi.encodePacked(++salt, address(this)));
uint256 expiry = type(uint256).max;
bytes32 digest = slashingRegistryCoordinator.calculateOperatorChurnApprovalDigestHash({
registeringOperator: address(this),
registeringOperatorId: operatorId,
operatorKickParams: kickParams,
salt: _salt,
expiry: expiry
});
// Sign digest
(uint8 v, bytes32 r, bytes32 s) = cheats.sign(churnApproverPrivateKey, digest);
bytes memory signature = new bytes(65);
assembly {
mstore(add(signature, 0x20), r)
mstore(add(signature, 0x40), s)
}
signature[signature.length - 1] = bytes1(v);
ISignatureUtilsMixinTypes.SignatureWithSaltAndExpiry memory churnApproverSignature =
ISignatureUtilsMixinTypes.SignatureWithSaltAndExpiry({
signature: signature,
salt: _salt,
expiry: expiry
});
return (kickParams, churnApproverSignature);
}
}
contract OperatorSetUser_AltMethods is OperatorSetUser {
using BitmapUtils for *;
modifier createSnapshot() virtual override {
cheats.roll(block.number + 1);
timeMachine.createSnapshot();
_;
}
constructor(
string memory name,
uint256 _privKey,
IBLSApkRegistryTypes.PubkeyRegistrationParams memory _pubkeyParams
) OperatorSetUser(name, _privKey, _pubkeyParams) {}
/// @dev Rather than calling deregisterOperator, this pranks the ejector and calls
/// ejectOperator
function deregisterOperator(
bytes calldata quorums
) public virtual override createSnapshot {
_log("deregisterOperator (eject)", quorums);
address ejector = slashingRegistryCoordinator.ejector();
cheats.prank(ejector);
slashingRegistryCoordinator.ejectOperator(address(this), quorums);
}
/// @dev Uses updateOperatorsForQuorum to update stakes of all operators in all quorums
function updateStakes() public virtual override createSnapshot {
_log("updateStakes (updateOperatorsForQuorum)");
bytes memory allQuorums =
((1 << slashingRegistryCoordinator.quorumCount()) - 1).bitmapToBytesArray();
address[][] memory operatorsPerQuorum = new address[][](allQuorums.length);
for (uint256 i = 0; i < allQuorums.length; i++) {
uint8 quorum = uint8(allQuorums[i]);
bytes32[] memory operatorIds =
indexRegistry.getOperatorListAtBlockNumber(quorum, uint32(block.number));
operatorsPerQuorum[i] = new address[](operatorIds.length);
for (uint256 j = 0; j < operatorIds.length; j++) {
operatorsPerQuorum[i][j] = blsApkRegistry.getOperatorFromPubkeyHash(operatorIds[j]);
}
operatorsPerQuorum[i] = Sort.sortAddresses(operatorsPerQuorum[i]);
}
slashingRegistryCoordinator.updateOperatorsForQuorum(operatorsPerQuorum, allQuorums);
}
}