-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathUser.t.sol
479 lines (397 loc) · 17.1 KB
/
User.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.27;
import "forge-std/Test.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
// Interfaces
import {
ISignatureUtilsMixin,
ISignatureUtilsMixinTypes
} from "eigenlayer-contracts/src/contracts/interfaces/ISignatureUtilsMixin.sol";
import "eigenlayer-contracts/src/contracts/interfaces/IStrategy.sol";
import "eigenlayer-contracts/src/contracts/interfaces/IDelegationManager.sol";
// Core
import "eigenlayer-contracts/src/contracts/core/DelegationManager.sol";
import "eigenlayer-contracts/src/contracts/interfaces/IDelegationManager.sol";
import "eigenlayer-contracts/src/contracts/core/StrategyManager.sol";
import "eigenlayer-contracts/src/contracts/core/AVSDirectory.sol";
import "eigenlayer-contracts/src/contracts/core/AllocationManager.sol";
// Middleware
import "../../src/interfaces/IRegistryCoordinator.sol";
import "../../src/RegistryCoordinator.sol";
import "../../src/BLSApkRegistry.sol";
import "../../src/IndexRegistry.sol";
import "../../src/StakeRegistry.sol";
import "../../src/ServiceManagerBase.sol";
import "../../src/libraries/BN254.sol";
import "../../src/libraries/BitmapUtils.sol";
import "./TimeMachine.t.sol";
import "./utils/Sort.t.sol";
import "./utils/BitmapStrings.t.sol";
import "../mocks/ServiceManagerMock.sol";
interface IUserDeployer {
function slashingRegistryCoordinator() external view returns (SlashingRegistryCoordinator);
function registryCoordinator() external view returns (RegistryCoordinator);
function avsDirectory() external view returns (AVSDirectory);
function allocationManager() external view returns (AllocationManager);
function serviceManager() external view returns (ServiceManagerMock);
function timeMachine() external view returns (TimeMachine);
function churnApproverPrivateKey() external view returns (uint256);
function churnApprover() external view returns (address);
}
contract User is Test {
using BN254 for *;
using Strings for *;
using BitmapStrings for *;
using BitmapUtils for *;
Vm cheats = Vm(VM_ADDRESS);
// Core contracts
DelegationManager delegationManager;
StrategyManager strategyManager;
AVSDirectory avsDirectory;
// Middleware contracts
RegistryCoordinator registryCoordinator;
ServiceManagerBase serviceManager;
BLSApkRegistry blsApkRegistry;
StakeRegistry stakeRegistry;
IndexRegistry indexRegistry;
TimeMachine timeMachine;
uint256 churnApproverPrivateKey;
address churnApprover;
string public NAME;
bytes32 public operatorId;
// BLS keypair:
uint256 privKey;
IBLSApkRegistryTypes.PubkeyRegistrationParams pubkeyParams;
// EIP1271 sigs:
mapping(bytes32 => bool) digests;
uint256 salt = 0;
constructor(
string memory name,
uint256 _privKey,
IBLSApkRegistryTypes.PubkeyRegistrationParams memory _pubkeyParams
) {
IUserDeployer deployer = IUserDeployer(msg.sender);
registryCoordinator = deployer.registryCoordinator();
avsDirectory = deployer.avsDirectory();
serviceManager = ServiceManagerBase(address(deployer.serviceManager()));
blsApkRegistry = BLSApkRegistry(address(registryCoordinator.blsApkRegistry()));
stakeRegistry = StakeRegistry(address(registryCoordinator.stakeRegistry()));
indexRegistry = IndexRegistry(address(registryCoordinator.indexRegistry()));
delegationManager = DelegationManager(address(stakeRegistry.delegation()));
strategyManager = StrategyManager(address(delegationManager.strategyManager()));
avsDirectory = AVSDirectory(address(serviceManager.avsDirectory()));
timeMachine = deployer.timeMachine();
churnApproverPrivateKey = deployer.churnApproverPrivateKey();
churnApprover = deployer.churnApprover();
NAME = name;
// Generate BN254 keypair and registration signature
privKey = _privKey;
pubkeyParams = _pubkeyParams;
BN254.G1Point memory registrationMessageHash =
registryCoordinator.pubkeyRegistrationMessageHash(address(this));
pubkeyParams.pubkeyRegistrationSignature = registrationMessageHash.scalar_mul(privKey);
operatorId = pubkeyParams.pubkeyG1.hashG1Point();
}
modifier createSnapshot() virtual {
timeMachine.createSnapshot();
_;
}
/**
* Middleware contracts:
*/
function registerOperator(
bytes calldata quorums
) public virtual createSnapshot returns (bytes32) {
_log("registerOperator", quorums);
vm.warp(block.timestamp + 1);
registryCoordinator.registerOperator({
quorumNumbers: quorums,
socket: NAME,
params: pubkeyParams,
operatorSignature: _genAVSRegistrationSig()
});
return pubkeyParams.pubkeyG1.hashG1Point();
}
/// @param churnQuorums Quorums to register for that have an associated churnTarget
/// @param churnTargets A user that can be churned for each of churnQuorums
/// @param standardQuorums Any additional quorums to register for that don't require churn
function registerOperatorWithChurn(
bytes calldata churnQuorums,
User[] calldata churnTargets,
bytes calldata standardQuorums
) public virtual createSnapshot {
_logChurn("registerOperatorWithChurn", churnQuorums, churnTargets, standardQuorums);
// 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);
vm.warp(block.timestamp + 1);
registryCoordinator.registerOperatorWithChurn({
quorumNumbers: allQuorums,
socket: NAME,
params: pubkeyParams,
operatorKickParams: kickParams,
churnApproverSignature: churnApproverSignature,
operatorSignature: _genAVSRegistrationSig()
});
}
function deregisterOperator(
bytes calldata quorums
) public virtual createSnapshot {
_log("deregisterOperator", quorums);
registryCoordinator.deregisterOperator(quorums);
}
/// @dev Uses updateOperators to update this user's stake
function updateStakes() public virtual createSnapshot {
_log("updateStakes (updateOperators)");
// get all quorums this operator is registered for
uint192 currentBitmap = registryCoordinator.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]);
}
registryCoordinator.updateOperatorsForQuorum(operatorsPerQuorum, quorumNumbers);
}
/**
* Core contracts:
*/
function registerAsOperator() public virtual createSnapshot {
_log("registerAsOperator (core)");
/// TODO: check
delegationManager.registerAsOperator(msg.sender, 0, NAME);
}
// Deposit LSTs into the StrategyManager. This setup does not use the EPMgr or native ETH.
function depositIntoEigenlayer(
IStrategy[] memory strategies,
uint256[] memory tokenBalances
) public virtual createSnapshot {
_log("depositIntoEigenLayer (core)");
for (uint256 i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
uint256 tokenBalance = tokenBalances[i];
IERC20 underlyingToken = strat.underlyingToken();
underlyingToken.approve(address(strategyManager), tokenBalance);
strategyManager.depositIntoStrategy(strat, underlyingToken, tokenBalance);
}
}
function exitEigenlayer()
public
virtual
createSnapshot
returns (IStrategy[] memory, uint256[] memory)
{
_log("exitEigenlayer (core)");
(IStrategy[] memory strategies, uint256[] memory shares) =
delegationManager.getDepositedShares(address(this));
IDelegationManagerTypes.QueuedWithdrawalParams[] memory params =
new IDelegationManager.QueuedWithdrawalParams[](1);
params[0] = IDelegationManagerTypes.QueuedWithdrawalParams({
strategies: strategies,
depositShares: shares,
__deprecated_withdrawer: address(this)
});
delegationManager.queueWithdrawals(params);
return (strategies, shares);
}
/**
* EIP1271 Signatures:
*/
bytes4 internal constant EIP1271_MAGICVALUE = 0x1626ba7e;
function isValidSignature(bytes32 digestHash, bytes memory) public view returns (bytes4) {
if (digests[digestHash]) {
return EIP1271_MAGICVALUE;
}
return bytes4(0);
}
function pubkeyG1() public view returns (BN254.G1Point memory) {
return pubkeyParams.pubkeyG1;
}
function _genAVSRegistrationSig()
internal
returns (ISignatureUtilsMixinTypes.SignatureWithSaltAndExpiry memory)
{
ISignatureUtilsMixinTypes.SignatureWithSaltAndExpiry memory signature =
ISignatureUtilsMixinTypes.SignatureWithSaltAndExpiry({
signature: new bytes(0),
salt: bytes32(salt++),
expiry: type(uint256).max
});
bytes32 digest = avsDirectory.calculateOperatorAVSRegistrationDigestHash({
operator: address(this),
avs: address(serviceManager),
salt: signature.salt,
expiry: signature.expiry
});
digests[digest] = true;
return signature;
}
// Operator0.registerOperator
function _log(
string memory s
) internal virtual {
emit log(string.concat(NAME, ".", s));
}
// Operator0.registerOperator: 0x00010203...
function _log(string memory s, bytes calldata quorums) internal virtual {
emit log_named_string(string.concat(NAME, ".", s), quorums.toString());
}
// Operator0.registerOperatorWithChurn
// - standardQuorums: 0x00010203...
// - churnQuorums: 0x0405...
// - churnTargets: Operator1, Operator2, ...
function _logChurn(
string memory s,
bytes memory churnQuorums,
User[] memory churnTargets,
bytes memory standardQuorums
) internal virtual {
emit log(string.concat(NAME, ".", s));
emit log_named_string("- standardQuorums", standardQuorums.toString());
emit log_named_string("- churnQuorums", churnQuorums.toString());
string memory targetString = "[";
for (uint256 i = 0; i < churnTargets.length; i++) {
if (i == churnTargets.length - 1) {
targetString = string.concat(targetString, churnTargets[i].NAME());
} else {
targetString = string.concat(targetString, churnTargets[i].NAME(), ", ");
}
}
targetString = string.concat(targetString, "]");
emit log_named_string("- churnTargets", targetString);
}
function _generateOperatorKickParams(
bytes memory allQuorums,
bytes calldata churnQuorums,
User[] calldata churnTargets,
bytes calldata standardQuorums
)
internal
virtual
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 = registryCoordinator.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 User_AltMethods is User {
using BitmapUtils for *;
modifier createSnapshot() virtual override {
cheats.roll(block.number + 1);
timeMachine.createSnapshot();
_;
}
constructor(
string memory name,
uint256 _privKey,
IBLSApkRegistryTypes.PubkeyRegistrationParams memory _pubkeyParams
) User(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 = registryCoordinator.ejector();
cheats.prank(ejector);
registryCoordinator.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 << registryCoordinator.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]);
}
registryCoordinator.updateOperatorsForQuorum(operatorsPerQuorum, allQuorums);
}
}