-
Notifications
You must be signed in to change notification settings - Fork 10
/
MachServiceManager.sol
434 lines (382 loc) · 15.1 KB
/
MachServiceManager.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
// SPDX-License-Identifier: UNLICENSED
// SEE LICENSE IN https://files.altlayer.io/Alt-Research-License-1.md
// Copyright Alt Research Ltd. 2023. All rights reserved.
//
// You acknowledge and agree that Alt Research Ltd. ("Alt Research") (or Alt
// Research's licensors) own all legal rights, titles and interests in and to the
// work, software, application, source code, documentation and any other documents
pragma solidity =0.8.12;
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {Pausable} from "eigenlayer-core/contracts/permissions/Pausable.sol";
import {IRewardsCoordinator} from "eigenlayer-core/contracts/interfaces/IRewardsCoordinator.sol";
import {IAVSDirectory} from "eigenlayer-core/contracts/interfaces/IAVSDirectory.sol";
import {ISignatureUtils} from "eigenlayer-core/contracts/interfaces/ISignatureUtils.sol";
import {IPauserRegistry} from "eigenlayer-core/contracts/interfaces/IPauserRegistry.sol";
import {IServiceManager, IServiceManagerUI} from "eigenlayer-middleware/interfaces/IServiceManager.sol";
import {IStakeRegistry} from "eigenlayer-middleware/interfaces/IStakeRegistry.sol";
import {IRegistryCoordinator} from "eigenlayer-middleware/interfaces/IRegistryCoordinator.sol";
import {BLSSignatureChecker} from "eigenlayer-middleware/BLSSignatureChecker.sol";
import {ServiceManagerBase} from "eigenlayer-middleware/ServiceManagerBase.sol";
import {MachServiceManagerStorage} from "./MachServiceManagerStorage.sol";
import {
InvalidConfirmer,
NotWhitelister,
ZeroAddress,
AlreadyInAllowlist,
NotAdded,
NoStatusChange,
InvalidRollupChainID,
InvalidReferenceBlockNum,
InsufficientThreshold,
InvalidStartIndex,
InsufficientThresholdPercentages,
InvalidSender,
InvalidQuorumParam,
InvalidQuorumThresholdPercentage,
AlreadyAdded,
ResolvedAlert,
AlreadyEnabled,
AlreadyDisabled
} from "../error/Errors.sol";
import {IMachServiceManager} from "../interfaces/IMachServiceManager.sol";
/**
* @title Primary entrypoint for procuring services from Altlayer Mach Service.
* @author Altlayer, Inc.
* @notice This contract is used for:
* - whitelisting operators
* - confirming the alert store by the aggregator with inferred aggregated signatures of the quorum
*/
contract MachServiceManager is
IMachServiceManager,
MachServiceManagerStorage,
ServiceManagerBase,
BLSSignatureChecker,
Pausable
{
using EnumerableSet for EnumerableSet.Bytes32Set;
using EnumerableSet for EnumerableSet.AddressSet;
/**
* @dev Ensures that the function is only callable by the `alertConfirmer`.
*/
modifier onlyAlertConfirmer() {
if (_msgSender() != alertConfirmer) {
revert InvalidConfirmer();
}
_;
}
/**
* @dev Ensures that the function is only callable by the `whitelister`.
*/
modifier onlyWhitelister() {
if (_msgSender() != whitelister) {
revert NotWhitelister();
}
_;
}
/**
* @dev Ensures that the `rollupChainID` is valid.
*/
modifier onlyValidRollupChainID(uint256 rollupChainID) {
if (!rollupChainIDs[rollupChainID]) {
revert InvalidRollupChainID();
}
_;
}
constructor(
IAVSDirectory __avsDirectory,
IRewardsCoordinator __rewardsCoordinator,
IRegistryCoordinator __registryCoordinator,
IStakeRegistry __stakeRegistry
)
BLSSignatureChecker(__registryCoordinator)
ServiceManagerBase(__avsDirectory, __rewardsCoordinator, __registryCoordinator, __stakeRegistry)
{
_disableInitializers();
}
function initialize(
IPauserRegistry pauserRegistry_,
uint256 initialPausedStatus_,
address initialOwner_,
address rewardsInitiator_,
address alertConfirmer_,
address whitelister_,
uint256[] calldata rollupChainIDs_
) public initializer {
_initializePauser(pauserRegistry_, initialPausedStatus_);
__ServiceManagerBase_init(initialOwner_, rewardsInitiator_);
_setAlertConfirmer(alertConfirmer_);
_setWhitelister(whitelister_);
for (uint256 i; i < rollupChainIDs_.length; ++i) {
_setRollupChainID(rollupChainIDs_[i], true);
}
allowlistEnabled = true;
quorumThresholdPercentage = 66;
}
//////////////////////////////////////////////////////////////////////////////
// Admin Functions //
//////////////////////////////////////////////////////////////////////////////
function setAllowlist(address[] calldata operators, bool[] calldata status) external onlyWhitelister {
require(operators.length == status.length, "Input arrays length mismatch");
for (uint256 i = 0; i < operators.length; ++i) {
address operator = operators[i];
if (operator == address(0)) {
revert ZeroAddress();
}
if (status[i]) {
_allowlist.add(operator);
} else {
_allowlist.remove(operator);
}
}
emit AllowlistUpdated(operators, status);
}
/**
* @inheritdoc IMachServiceManager
*/
function enableAllowlist() external onlyOwner {
if (allowlistEnabled) {
revert AlreadyEnabled();
} else {
allowlistEnabled = true;
emit AllowlistEnabled();
}
}
/**
* @inheritdoc IMachServiceManager
*/
function disableAllowlist() external onlyOwner {
if (!allowlistEnabled) {
revert AlreadyDisabled();
} else {
allowlistEnabled = false;
emit AllowlistDisabled();
}
}
/**
* @inheritdoc IMachServiceManager
*/
function setConfirmer(address confirmer) external onlyOwner {
_setAlertConfirmer(confirmer);
}
/**
* @inheritdoc IMachServiceManager
*/
function setWhitelister(address whitelister) external onlyOwner {
_setWhitelister(whitelister);
}
/**
* @inheritdoc IMachServiceManager
*/
function setRollupChainID(uint256 rollupChainId, bool status) external onlyOwner {
_setRollupChainID(rollupChainId, status);
}
/**
* @inheritdoc IMachServiceManager
*/
function removeAlert(uint256 rollupChainId, bytes32 messageHash)
external
onlyValidRollupChainID(rollupChainId)
onlyOwner
{
bool ret = _messageHashes[rollupChainId].remove(messageHash);
if (ret) {
_resolvedMessageHashes[rollupChainId].add(messageHash);
emit AlertRemoved(messageHash, _msgSender());
}
}
/**
* @inheritdoc IMachServiceManager
*/
function updateQuorumThresholdPercentage(uint8 thresholdPercentage) external onlyOwner {
if (thresholdPercentage > 100) {
revert InvalidQuorumThresholdPercentage();
}
quorumThresholdPercentage = thresholdPercentage;
emit QuorumThresholdPercentageChanged(thresholdPercentage);
}
//////////////////////////////////////////////////////////////////////////////
// Operator Registration //
//////////////////////////////////////////////////////////////////////////////
/**
* @inheritdoc IServiceManagerUI
*/
function registerOperatorToAVS(
address operator,
ISignatureUtils.SignatureWithSaltAndExpiry memory operatorSignature
) public override(ServiceManagerBase, IServiceManagerUI) whenNotPaused onlyRegistryCoordinator {
if (allowlistEnabled && !isOperatorAllowed(operator)) {
revert NotAdded();
}
// Stake requirement for quorum is checked in StakeRegistry.sol
// https://github.com/Layr-Labs/eigenlayer-middleware/blob/dev/src/RegistryCoordinator.sol#L488
// https://github.com/Layr-Labs/eigenlayer-middleware/blob/dev/src/StakeRegistry.sol#L84
_avsDirectory.registerOperatorToAVS(operator, operatorSignature);
}
/**
* @inheritdoc IServiceManagerUI
*/
function deregisterOperatorFromAVS(address operator)
public
override(ServiceManagerBase, IServiceManagerUI)
whenNotPaused
onlyRegistryCoordinator
{
_avsDirectory.deregisterOperatorFromAVS(operator);
}
//////////////////////////////////////////////////////////////////////////////
// Alert Functions //
//////////////////////////////////////////////////////////////////////////////
/**
* @inheritdoc IMachServiceManager
*/
function confirmAlert(
AlertHeader calldata alertHeader,
NonSignerStakesAndSignature memory nonSignerStakesAndSignature
) external whenNotPaused onlyAlertConfirmer onlyValidRollupChainID(alertHeader.rollupChainID) {
// make sure the information needed to derive the non-signers and batch is in calldata to avoid emitting events
if (tx.origin != msg.sender) {
revert InvalidSender();
}
// check is it is the resolved alert before
if (_resolvedMessageHashes[alertHeader.rollupChainID].contains(alertHeader.messageHash)) {
revert ResolvedAlert();
}
// make sure the stakes against which the Batch is being confirmed are not stale
if (alertHeader.referenceBlockNumber >= block.number) {
revert InvalidReferenceBlockNum();
}
bytes32 hashedHeader = _hashAlertHeader(alertHeader);
// check quorum parameters
if (alertHeader.quorumNumbers.length != alertHeader.quorumThresholdPercentages.length) {
revert InvalidQuorumParam();
}
// check the signature
(QuorumStakeTotals memory quorumStakeTotals, /* bytes32 signatoryRecordHash */ ) = checkSignatures(
hashedHeader,
alertHeader.quorumNumbers, // use list of uint8s instead of uint256 bitmap to not iterate 256 times
alertHeader.referenceBlockNumber,
nonSignerStakesAndSignature
);
// check that signatories own at least a threshold percentage of each quourm
for (uint256 i = 0; i < alertHeader.quorumThresholdPercentages.length; i++) {
// signed stake > total stake
// signedStakeForQuorum[i] / totalStakeForQuorum[i] * THRESHOLD_DENOMINATOR >= quorumThresholdPercentages[i]
// => signedStakeForQuorum[i] * THRESHOLD_DENOMINATOR >= totalStakeForQuorum[i] * quorumThresholdPercentages[i]
uint8 currentQuorumThresholdPercentages = uint8(alertHeader.quorumThresholdPercentages[i]);
if (currentQuorumThresholdPercentages > 100) {
revert InvalidQuorumThresholdPercentage();
}
if (currentQuorumThresholdPercentages < quorumThresholdPercentage) {
revert InsufficientThresholdPercentages();
}
if (
quorumStakeTotals.signedStakeForQuorum[i] * THRESHOLD_DENOMINATOR
< quorumStakeTotals.totalStakeForQuorum[i] * currentQuorumThresholdPercentages
) {
revert InsufficientThreshold();
}
}
// store alert
bool success = _messageHashes[alertHeader.rollupChainID].add(alertHeader.messageHash);
if (!success) {
revert AlreadyAdded();
}
emit AlertConfirmed(hashedHeader, alertHeader.messageHash);
}
//////////////////////////////////////////////////////////////////////////////
// View Functions //
//////////////////////////////////////////////////////////////////////////////
/**
* @inheritdoc IMachServiceManager
*/
function totalAlerts(uint256 rollupChainId) external view returns (uint256) {
return _messageHashes[rollupChainId].length();
}
/**
* @inheritdoc IMachServiceManager
*/
function contains(uint256 rollupChainId, bytes32 messageHash) external view returns (bool) {
return _messageHashes[rollupChainId].contains(messageHash);
}
/**
* @inheritdoc IMachServiceManager
*/
function queryMessageHashes(uint256 rollupChainId, uint256 start, uint256 querySize)
external
view
returns (bytes32[] memory)
{
uint256 length = _messageHashes[rollupChainId].length();
if (start >= length) {
revert InvalidStartIndex();
}
uint256 end = start + querySize;
if (end > length) {
end = length;
}
bytes32[] memory output = new bytes32[](end - start);
for (uint256 i = start; i < end; ++i) {
output[i - start] = _messageHashes[rollupChainId].at(i);
}
return output;
}
function isOperatorAllowed(address operator) public view returns (bool) {
return _allowlist.contains(operator);
}
function getAllowlistSize() public view returns (uint256) {
return _allowlist.length();
}
function getAllowlistAtIndex(uint256 index) public view returns (address) {
return _allowlist.at(index);
}
//////////////////////////////////////////////////////////////////////////////
// Internal Functions //
//////////////////////////////////////////////////////////////////////////////
/**
* @dev Hashes an alert header
*/
function _hashAlertHeader(AlertHeader calldata alertHeader) internal pure returns (bytes32) {
return keccak256(abi.encode(_convertAlertHeaderToReducedAlertHeader(alertHeader)));
}
/**
* @dev Changes the alert confirmer
*/
function _setAlertConfirmer(address _alertConfirmer) internal {
address previousBatchConfirmer = alertConfirmer;
alertConfirmer = _alertConfirmer;
emit AlertConfirmerChanged(previousBatchConfirmer, alertConfirmer);
}
/**
* @dev Changes the whitelister
*/
function _setWhitelister(address _whitelister) internal {
address previousWhitelister = whitelister;
whitelister = _whitelister;
emit WhitelisterChanged(previousWhitelister, _whitelister);
}
/**
* @dev Converts a alert header to a reduced alert header
* @param alertHeader the alert header to convert
*/
function _convertAlertHeaderToReducedAlertHeader(AlertHeader calldata alertHeader)
internal
pure
returns (ReducedAlertHeader memory)
{
return ReducedAlertHeader({
messageHash: alertHeader.messageHash,
referenceBlockNumber: alertHeader.referenceBlockNumber,
rollupChainID: alertHeader.rollupChainID
});
}
function _setRollupChainID(uint256 rollupChainId, bool status) internal {
if (rollupChainId < 1) {
revert InvalidRollupChainID();
}
if (rollupChainIDs[rollupChainId] == status) {
revert NoStatusChange();
}
rollupChainIDs[rollupChainId] = status;
emit RollupChainIDUpdated(rollupChainId, status);
}
}