forked from Layr-Labs/eigenlayer-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrationConfig.t.sol
572 lines (464 loc) · 22.9 KB
/
IntegrationConfig.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.12;
import "forge-std/Test.sol";
import "test/integration/IntegrationDeployer.t.sol";
import "test/ffi/util/G2Operations.sol";
import "test/integration/utils/BitmapStrings.t.sol";
contract Constants {
/// Quorum Config:
/// @dev Default OperatorSetParam values used to initialize quorums
/// NOTE: This means each quorum has an operator limit of MAX_OPERATOR_COUNT by default
/// This is a low number because each operator receives its own BLS keypair, which
/// is very slow to generate.
uint32 constant MAX_OPERATOR_COUNT = 5;
uint16 constant KICK_BIPS_OPERATOR_STAKE = 15000;
uint16 constant KICK_BIPS_TOTAL_STAKE = 150;
/// Other:
/// @dev Number of BLS keypairs to pregenerate. This is a slow operation,
/// so I've set this to a low number.
uint constant NUM_GENERATED_OPERATORS = MAX_OPERATOR_COUNT + 5;
uint constant MAX_QUORUM_COUNT = 192; // From RegistryCoordinator.MAX_QUORUM_COUNT
uint16 internal constant BIPS_DENOMINATOR = 10000;
}
contract IntegrationConfig is IntegrationDeployer, G2Operations, Constants {
using BitmapStrings for *;
using Strings for *;
using BN254 for *;
using BitmapUtils for *;
/// @dev Tracking variables for randomness and _randConfig
// All _rand methods use/update this value
bytes32 random;
// Every time a new user is generated, it uses a random flag from this array
bytes userFlags;
// Every time a quorum is created, it uses random flags pulled from these arrays
bytes numQuorumFlags;
bytes numStrategyFlags;
bytes minStakeFlags;
bytes fillTypeFlags;
uint constant FLAG = 1;
/// @dev Flags for userTypes
uint constant DEFAULT = (FLAG << 0);
uint constant ALT_METHODS = (FLAG << 1);
/// @dev Flags for numQuorums and numStrategies
uint constant ONE = (FLAG << 0);
uint constant TWO = (FLAG << 1);
uint constant MANY = (FLAG << 2);
uint constant FIFTEEN = (FLAG << 3);
uint constant TWENTY = (FLAG << 4);
uint constant TWENTYFIVE = (FLAG << 5);
/// @dev Flags for minimumStake
uint constant NO_MINIMUM = (FLAG << 0);
uint constant HAS_MINIMUM = (FLAG << 1);
/// @dev Flags for fillTypes
uint constant EMPTY = (FLAG << 0);
uint constant SOME_FILL = (FLAG << 1);
uint constant FULL = (FLAG << 2);
/// @dev Tracking variables for pregenerated BLS keypairs:
/// (See _fetchKeypair)
uint fetchIdx = 0;
uint[] privKeys;
IBLSApkRegistry.PubkeyRegistrationParams[] pubkeys;
/// @dev Current initialized quorums are tracked here:
uint quorumCount;
uint192 quorumBitmap;
bytes quorumArray;
/// @dev Number of operators generated so far
uint numOperators = 0;
/// @dev current array of operatorIds registered so far per quorum.
/// does not update and remove if an operator is deregistered however, used for testing updateOperatorsForQuorum
mapping(uint8 => address[]) operatorsForQuorum;
/**
* Since BLS key generation uses FFI, it's pretty slow. Pregenerating keys
* in the constructor apparently ensures that this only happens once,
* so this is the best way to speed things up when running multiple tests.
*/
constructor() {
for (uint i = 0; i < NUM_GENERATED_OPERATORS; i++) {
IBLSApkRegistry.PubkeyRegistrationParams memory pubkey;
uint privKey = uint(keccak256(abi.encodePacked(i + 1)));
pubkey.pubkeyG1 = BN254.generatorG1().scalar_mul(privKey);
pubkey.pubkeyG2 = G2Operations.mul(privKey);
privKeys.push(privKey);
pubkeys.push(pubkey);
}
}
/**
* @dev Used by _configRand to configure what types of quorums get
* created during setup
*/
struct QuorumConfig {
/// @dev The number of quorums created during setup
uint numQuorums; // ONE | TWO | MANY
/// @dev The number of strategies a quorum will consider
uint numStrategies; // ONE | TWO | MANY
/// @dev Whether each quorum has a minimum stake
/// NOTE: Minimum stake is currently MIN_BALANCE by default
uint minimumStake; // NO_MINIMUM | HAS_MINIMUM
/// @dev Whether each quorum created is pre-populated with operators
/// NOTE: Default
uint fillTypes; // EMPTY | SOME_FILL | FULL
}
/**
* @param _randomSeed Fuzz tests supply a random u24 as input
* @param _userTypes [DEFAULT | ALT_METHODS] - every time a user is generated, it will use these values
* @param _quorumConfig Quorums that are created/initialized in this method will be configured according
* to this struct. See `QuorumConfig` above for details on each parameter.
*/
function _configRand(
uint24 _randomSeed,
uint _userTypes,
QuorumConfig memory _quorumConfig
) internal {
emit log_named_uint("_configRand: set random seed to", _randomSeed);
random = keccak256(abi.encodePacked(_randomSeed));
// Convert flag bitmaps to byte arrays for easier random lookup
userFlags = _bitmapToBytes(_userTypes);
numQuorumFlags = _bitmapToBytes(_quorumConfig.numQuorums);
numStrategyFlags = _bitmapToBytes(_quorumConfig.numStrategies);
minStakeFlags = _bitmapToBytes(_quorumConfig.minimumStake);
fillTypeFlags = _bitmapToBytes(_quorumConfig.fillTypes);
// Sanity check config
assertTrue(userFlags.length != 0, "_configRand: invalid _userTypes, no flags passed");
assertTrue(numQuorumFlags.length != 0, "_configRand: invalid numQuorums, no flags passed");
assertTrue(numStrategyFlags.length != 0, "_configRand: invalid numStrategies, no flags passed");
assertTrue(minStakeFlags.length != 0, "_configRand: invalid minimumStake, no flags passed");
assertTrue(fillTypeFlags.length != 0, "_configRand: invalid fillTypes, no flags passed");
// Decide how many quorums to initialize
quorumCount = _randQuorumCount();
quorumBitmap = uint192((1 << quorumCount) - 1);
quorumArray = BitmapUtils.bitmapToBytesArray(quorumBitmap);
emit log_named_uint("_configRand: number of quorums being initialized", quorumCount);
// Default OperatorSetParams for all quorums
IRegistryCoordinator.OperatorSetParam memory operatorSet = IRegistryCoordinator.OperatorSetParam({
maxOperatorCount: MAX_OPERATOR_COUNT,
kickBIPsOfOperatorStake: KICK_BIPS_OPERATOR_STAKE,
kickBIPsOfTotalStake: KICK_BIPS_TOTAL_STAKE
});
// Initialize each quorum
for (uint i = 0; i < quorumCount; i++) {
IStakeRegistry.StrategyParams[] memory strategyParams = _randStrategyParams();
uint96 minimumStake = _randMinStake();
emit log_named_uint("_configRand: creating quorum", i);
emit log_named_uint("- Max operator count", operatorSet.maxOperatorCount);
emit log_named_uint("- Num strategies considered", strategyParams.length);
emit log_named_uint("- Minimum stake", minimumStake);
cheats.prank(registryCoordinatorOwner);
registryCoordinator.createQuorum({
operatorSetParams: operatorSet,
minimumStake: minimumStake,
strategyParams: strategyParams
});
}
// Decide how many operators to register for each quorum initially
uint initialOperators = _randInitialOperators(operatorSet);
emit log(string.concat("Registering ", initialOperators.toString(), " initial operators in each quorum"));
// For each initial operator, register for all quorums
for (uint j = 0; j < initialOperators; j++) {
User operator = _newRandomOperator();
operator.registerOperator(quorumArray);
for (uint k = 0; k < quorumArray.length; k++) {
uint8 quorum = uint8(quorumArray[k]);
operatorsForQuorum[quorum].push(address(operator));
}
}
emit log("=====================");
emit log("_configRand complete; starting test!");
emit log("=====================");
}
/**
* Gen/Init methods:
*/
function _newRandomOperator() internal returns (User) {
string memory operatorName = string.concat("Operator", numOperators.toString());
numOperators++;
(User operator, IStrategy[] memory strategies, uint[] memory tokenBalances) = _randUser(operatorName);
operator.registerAsOperator();
operator.depositIntoEigenlayer(strategies, tokenBalances);
assertTrue(delegationManager.isOperator(address(operator)), "_newRandomOperator: operator should be registered");
return operator;
}
/// @dev Create a new user with token balances in ALL core-whitelisted strategies
function _randUser(string memory name) internal returns (User, IStrategy[] memory, uint[] memory) {
// Create User contract and give it a unique BLS keypair
(uint privKey, IBLSApkRegistry.PubkeyRegistrationParams memory pubkey) = _fetchKeypair();
// Use userFlags to pick the kind of user to generate
User user;
uint userType = _randValue(userFlags);
if (userType == DEFAULT) {
user = new User(name, privKey, pubkey);
} else if (userType == ALT_METHODS) {
name = string.concat(name, "_Alt");
user = new User_AltMethods(name, privKey, pubkey);
}
emit log_named_string("_randUser: Created user", user.NAME());
(IStrategy[] memory strategies, uint[] memory balances) = _dealRandTokens(user);
return (user, strategies, balances);
}
function _dealRandTokens(User user) internal returns (IStrategy[] memory, uint[] memory) {
IStrategy[] memory strategies = new IStrategy[](allStrats.length);
uint[] memory balances = new uint[](allStrats.length);
emit log_named_string("_dealRandTokens: dealing assets to", user.NAME());
// Deal the user a random balance between [MIN_BALANCE, MAX_BALANCE] for each existing strategy
for (uint i = 0; i < allStrats.length; i++) {
IStrategy strat = allStrats[i];
IERC20 underlyingToken = strat.underlyingToken();
uint balance = _randUint({ min: MIN_BALANCE, max: MAX_BALANCE });
StdCheats.deal(address(underlyingToken), address(user), balance);
strategies[i] = strat;
balances[i] = balance;
}
return (strategies, balances);
}
function _dealMaxTokens(User user) internal returns (IStrategy[] memory, uint[] memory) {
IStrategy[] memory strategies = new IStrategy[](allStrats.length);
uint[] memory balances = new uint[](allStrats.length);
emit log_named_string("_dealMaxTokens: dealing assets to", user.NAME());
// Deal the user the 100 * MAX_BALANCE for each existing strategy
for (uint i = 0; i < allStrats.length; i++) {
IStrategy strat = allStrats[i];
IERC20 underlyingToken = strat.underlyingToken();
uint balance = 100 * MAX_BALANCE;
StdCheats.deal(address(underlyingToken), address(user), balance);
strategies[i] = strat;
balances[i] = balance;
}
return (strategies, balances);
}
/// @param incomingOperator the operator that will churn operators in churnQuorums
/// @param churnQuorums the quorums that we need to select churnable operators from
/// @param standardQuorums the quorums that we want to register for WITHOUT churn
/// @return churnTargets: one churnable operator for each churnQuorum
function _getChurnTargets(
User incomingOperator,
bytes memory churnQuorums,
bytes memory standardQuorums
) internal returns (User[] memory) {
emit log_named_string("_getChurnTargets: incoming operator", incomingOperator.NAME());
emit log_named_string("_getChurnTargets: churnQuorums", churnQuorums.toString());
emit log_named_string("_getChurnTargets: standardQuorums", standardQuorums.toString());
// For each standard registration quorum, eject operators to make room
_makeRoom(standardQuorums);
// For each churn quorum, select operators as churn targets
User[] memory churnTargets = new User[](churnQuorums.length);
for (uint i = 0; i < churnQuorums.length; i++) {
uint8 quorum = uint8(churnQuorums[i]);
IRegistryCoordinator.OperatorSetParam memory params
= registryCoordinator.getOperatorSetParams(quorum);
// Sanity check - make sure we're at the operator cap
uint32 curNumOperators = indexRegistry.totalOperatorsForQuorum(quorum);
assertTrue(curNumOperators >= params.maxOperatorCount, "_getChurnTargets: non-full quorum cannot be churned");
// Get a random registered operator
churnTargets[i] = _selectRandRegisteredOperator(quorum);
emit log_named_string(
string.concat("_getChurnTargets: selected churn target for quorum ", uint(quorum).toString()),
churnTargets[i].NAME());
uint96 currentTotalStake = stakeRegistry.getCurrentTotalStake(quorum);
uint96 operatorToChurnStake = stakeRegistry.getCurrentStake(churnTargets[i].operatorId(), quorum);
// Ensure the incoming operator exceeds the individual stake threshold --
// more stake than the outgoing operator by kickBIPsOfOperatorStake
while (
_getWeight(quorum, incomingOperator) <= _individualKickThreshold(operatorToChurnStake, params) ||
operatorToChurnStake >= _totalKickThreshold(currentTotalStake + _getWeight(quorum, incomingOperator), params)
) {
(IStrategy[] memory strategies, uint[] memory balances) = _dealMaxTokens(incomingOperator);
incomingOperator.depositIntoEigenlayer(strategies, balances);
}
}
// Oh jeez that was a lot. Return the churn targets
return churnTargets;
}
/// From RegistryCoordinator._individualKickThreshold
function _individualKickThreshold(
uint96 operatorStake,
IRegistryCoordinator.OperatorSetParam memory setParams
) internal pure returns (uint96) {
return operatorStake * setParams.kickBIPsOfOperatorStake / BIPS_DENOMINATOR;
}
/// From RegistryCoordinator._totalKickThreshold
function _totalKickThreshold(
uint96 totalStake,
IRegistryCoordinator.OperatorSetParam memory setParams
) internal pure returns (uint96) {
return totalStake * setParams.kickBIPsOfTotalStake / BIPS_DENOMINATOR;
}
function _getWeight(
uint8 quorum,
User operator
) internal view returns (uint96) {
return stakeRegistry.weightOfOperatorForQuorum(quorum, address(operator));
}
function _makeRoom(bytes memory quorums) private {
emit log_named_string("_getChurnTargets: making room by removing operators from quorums", quorums.toString());
for (uint i = 0; i < quorums.length; i++) {
uint8 quorum = uint8(quorums[i]);
uint32 maxOperatorCount = registryCoordinator.getOperatorSetParams(quorum).maxOperatorCount;
// Continue deregistering until we're under the cap
// This uses while in case we tested a config change that lowered the max count
while (indexRegistry.totalOperatorsForQuorum(quorum) >= maxOperatorCount) {
// Select a random operator and deregister them from the quorum
User operatorToKick = _selectRandRegisteredOperator(quorum);
bytes memory quorumArr = new bytes(1);
quorumArr[0] = bytes1(quorum);
operatorToKick.deregisterOperator(quorumArr);
}
}
}
function _selectRandRegisteredOperator(uint8 quorum) internal returns (User) {
uint32 curNumOperators = indexRegistry.totalOperatorsForQuorum(quorum);
bytes32 randId = indexRegistry.getLatestOperatorUpdate({
quorumNumber: quorum,
operatorIndex: uint32(_randUint({ min: 0, max: curNumOperators - 1 }))
}).operatorId;
return User(blsApkRegistry.getOperatorFromPubkeyHash(randId));
}
function _fetchKeypair() internal returns (uint, IBLSApkRegistry.PubkeyRegistrationParams memory) {
// should probably just generate another keypair at this point
if (fetchIdx == privKeys.length) {
revert("_fetchKeypair: not enough generated keys. Check IntegrationDeployer.constructor");
}
uint privKey = privKeys[fetchIdx];
IBLSApkRegistry.PubkeyRegistrationParams memory pubkey = pubkeys[fetchIdx];
fetchIdx++;
return (privKey, pubkey);
}
/// @dev Uses `random` to return a random uint, with a range given by `min` and `max` (inclusive)
/// @return `min` <= result <= `max`
function _randUint(uint min, uint max) internal returns (uint) {
uint range = max - min + 1;
// calculate the number of bits needed for the range
uint bitsNeeded = 0;
uint tempRange = range;
while (tempRange > 0) {
bitsNeeded++;
tempRange >>= 1;
}
// create a mask for the required number of bits
// and extract the value from the hash
uint mask = (1 << bitsNeeded) - 1;
uint value = uint(random) & mask;
// in case value is out of range, wrap around or retry
while (value >= range) {
value = (value - range) & mask;
}
// Hash `random` with itself so the next value we generate is different
random = keccak256(abi.encodePacked(random));
return min + value;
}
function _randBool() internal returns (bool) {
return _randUint({ min: 0, max: 1 }) == 0;
}
function _selectRand(bytes memory quorums) internal returns (bytes memory) {
assertTrue(quorums.length != 0, "_selectRand: tried to select from empty quorum list");
uint192 result;
for (uint i = 0; i < quorums.length; i++) {
if (_randBool()) {
result = uint192(result.setBit(uint8(quorums[i])));
}
}
// Ensure we return at least one quorum
if (result.isEmpty()) {
result = uint192(result.setBit(uint8(quorums[0])));
}
bytes memory resultArray = result.bitmapToBytesArray();
emit log_named_uint("_selectRand: input quorum count", quorums.length);
emit log_named_uint("_selectRand: selected quorum count", resultArray.length);
return resultArray;
}
/// @dev Select a random value from `arr` and return it. Reverts if arr is empty
function _randValue(bytes memory arr) internal returns (uint) {
assertTrue(arr.length > 0, "_randValue: tried to select value from empty array");
uint idx = _randUint({ min: 0, max: arr.length - 1 });
return uint(uint8(arr[idx]));
}
/// Private _randX methods used by _configRand:
/// @dev Select a random number of quorums to initialize
/// NOTE: This should only be used when initializing quorums for the first time (in _configRand)
function _randQuorumCount() private returns (uint) {
uint quorumFlag = _randValue(numQuorumFlags);
if (quorumFlag == ONE) {
return 1;
} else if (quorumFlag == TWO) {
return 2;
} else if (quorumFlag == MANY) {
// Ideally this would be MAX_QUORUM_COUNT, but that really slows tests
// that have users register for all quorums
return _randUint({ min: 3, max: 10 });
} else {
revert("_randQuorumCount: flag not recognized");
}
}
/// @dev Select a random number of strategies and multipliers to create a quorum with
/// NOTE: This should only be used when creating a quorum for the first time. If you're
/// selecting strategies to add after the quorum has been initialized, this is likely to
/// return duplicates.
function _randStrategyParams() private returns (IStakeRegistry.StrategyParams[] memory) {
uint strategyFlag = _randValue(numStrategyFlags);
uint strategyCount;
if (strategyFlag == ONE) {
strategyCount = 1;
} else if (strategyFlag == TWO) {
strategyCount = 2;
} else if (strategyFlag == MANY) {
strategyCount = _randUint({ min: 3, max: allStrats.length - 1 });
} else if (strategyFlag == FIFTEEN) {
strategyCount = 15;
} else if (strategyFlag == TWENTY) {
strategyCount = 20;
} else if (strategyFlag == TWENTYFIVE) {
strategyCount = 25;
} else {
revert("_randStrategyCount: flag not recognized");
}
IStakeRegistry.StrategyParams[] memory params = new IStakeRegistry.StrategyParams[](strategyCount);
for (uint i = 0; i < params.length; i++) {
params[i] = IStakeRegistry.StrategyParams({
strategy: allStrats[i],
multiplier: DEFAULT_STRATEGY_MULTIPLIER
});
}
return params;
}
/**
* @dev Uses _randFillType to determine how many operators to register for a quorum initially
* @return The number of operators to register
*/
function _randInitialOperators(IRegistryCoordinator.OperatorSetParam memory operatorSet) private returns (uint) {
uint fillTypeFlag = _randValue(fillTypeFlags);
if (fillTypeFlag == EMPTY) {
return 0;
} else if (fillTypeFlag == SOME_FILL) {
return _randUint({ min: 1, max: operatorSet.maxOperatorCount - 1 });
} else if (fillTypeFlag == FULL) {
return operatorSet.maxOperatorCount;
} else {
revert("_randInitialOperators: flag not recognized");
}
}
/// @dev Select a random number of quorums to initialize
function _randMinStake() private returns (uint96) {
uint minStakeFlag = _randValue(minStakeFlags);
if (minStakeFlag == NO_MINIMUM) {
return 0;
} else if (minStakeFlag == HAS_MINIMUM) {
return uint96(MIN_BALANCE);
} else {
revert("_randQuorumCount: flag not recognized");
}
}
/**
* @dev Converts a bitmap into an array of bytes
* @dev Each byte in the input is processed as indicating a single bit to flip in the bitmap
*/
function _bitmapToBytes(uint bitmap) internal pure returns (bytes memory bytesArray) {
for (uint i = 0; i < 256; ++i) {
// Mask for i-th bit
uint mask = uint(1 << i);
// emit log_named_uint("mask: ", mask);
// If the i-th bit is flipped, add a byte to the return array
if (bitmap & mask != 0) {
bytesArray = bytes.concat(bytesArray, bytes1(uint8(1 << i)));
}
}
return bytesArray;
}
}