-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathAlignedLayerServiceManager.sol
More file actions
378 lines (325 loc) · 12.8 KB
/
AlignedLayerServiceManager.sol
File metadata and controls
378 lines (325 loc) · 12.8 KB
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.12;
import {ServiceManagerBase, IAVSDirectory} from "eigenlayer-middleware/ServiceManagerBase.sol";
import {BLSSignatureChecker} from "eigenlayer-middleware/BLSSignatureChecker.sol";
import {IRegistryCoordinator} from "eigenlayer-middleware/interfaces/IRegistryCoordinator.sol";
import {IStakeRegistry} from "eigenlayer-middleware/interfaces/IStakeRegistry.sol";
import {Merkle} from "eigenlayer-core/contracts/libraries/Merkle.sol";
import {IRewardsCoordinator} from "eigenlayer-contracts/src/contracts/interfaces/IRewardsCoordinator.sol";
import {AlignedLayerServiceManagerStorage} from "./AlignedLayerServiceManagerStorage.sol";
import {IAlignedLayerServiceManager} from "./IAlignedLayerServiceManager.sol";
import {IPauserRegistry} from "eigenlayer-core/contracts/interfaces/IPauserRegistry.sol";
import {Pausable} from "eigenlayer-core/contracts/permissions/Pausable.sol";
/**
* @title Primary entrypoint for procuring services from Aligned.
*/
contract AlignedLayerServiceManager is
IAlignedLayerServiceManager,
ServiceManagerBase,
BLSSignatureChecker,
AlignedLayerServiceManagerStorage,
Pausable
{
uint256 internal constant THRESHOLD_DENOMINATOR = 100;
uint8 internal constant QUORUM_THRESHOLD_PERCENTAGE = 67;
constructor(
IAVSDirectory __avsDirectory,
IRewardsCoordinator __rewardsCoordinator,
IRegistryCoordinator __registryCoordinator,
IStakeRegistry __stakeRegistry
)
BLSSignatureChecker(__registryCoordinator)
ServiceManagerBase(
__avsDirectory,
__rewardsCoordinator,
__registryCoordinator,
__stakeRegistry
)
{
if (address(__avsDirectory) == address(0)) {
revert InvalidAddress("avsDirectory");
}
if (address(__rewardsCoordinator) == address(0)) {
revert InvalidAddress("rewardsCoordinator");
}
if (address(__registryCoordinator) == address(0)) {
revert InvalidAddress("registryCoordinator");
}
if (address(__stakeRegistry) == address(0)) {
revert InvalidAddress("stakeRegistry");
}
_disableInitializers();
}
/**
* @notice Initializes the contract with the initial owner.
* @param _initialOwner The initial owner of the contract.
* @param _rewardsInitiator The address which is allowed to create AVS rewards submissions.
* @param _alignedAggregator The address of the aggregator.
* @param _pauserRegistry a registry of addresses that can pause the contract
* @param _initialPausedStatus pause status after calling initialize
*/
function initialize(
address _initialOwner,
address _rewardsInitiator,
address _alignedAggregator,
IPauserRegistry _pauserRegistry,
uint256 _initialPausedStatus
) public initializer {
if (_initialOwner == address(0)) {
revert InvalidAddress("initialOwner");
}
if (_rewardsInitiator == address(0)) {
revert InvalidAddress("rewardsInitiator");
}
if (_alignedAggregator == address(0)) {
revert InvalidAddress("alignedAggregator");
}
__ServiceManagerBase_init(_initialOwner, _rewardsInitiator);
alignedAggregator = _alignedAggregator; //can't do setAggregator(aggregator) since caller is not the owner
_transferOwnership(_initialOwner); // TODO check is this needed? is it not called in __ServiceManagerBase_init ?
_initializePauser(_pauserRegistry, _initialPausedStatus);
}
// Reinitializers:
// Notice Testnet had more upgrades than Mainnet.
// In Testnet, we executed the reinitializer(2) and reinitializer(3)
// These are not needed in Mainnet.
// In the future, in case of needing to add a reinitializer,
// either add it as reinitializer(4) or redeploy Testnet from scratch
// function initializeAggregator( // applied on Testnet
// address _alignedAggregator
// ) public reinitializer(2) {
// setAggregator(_alignedAggregator);
// }
// function initializePauser( // applied on Testnet
// IPauserRegistry _pauserRegistry,
// uint256 _initialPausedStatus
// ) public reinitializer(3) {
// _initializePauser(_pauserRegistry, _initialPausedStatus);
// }
function createNewTask(
bytes32 batchMerkleRoot,
string calldata batchDataPointer,
uint256 respondToTaskFeeLimit
) external payable onlyWhenNotPaused(0) {
bytes32 batchIdentifier = keccak256(
abi.encodePacked(batchMerkleRoot, msg.sender)
);
if (batchesState[batchIdentifier].taskCreatedBlock != 0) {
revert BatchAlreadySubmitted(batchIdentifier);
}
if (msg.value > 0) {
batchersBalances[msg.sender] += msg.value;
emit BatcherBalanceUpdated(
msg.sender,
batchersBalances[msg.sender]
);
}
if (batchersBalances[msg.sender] < respondToTaskFeeLimit) {
revert InsufficientFunds(
msg.sender,
respondToTaskFeeLimit,
batchersBalances[msg.sender]
);
}
BatchState memory batchState;
batchState.taskCreatedBlock = uint32(block.number);
batchState.responded = false;
batchState.respondToTaskFeeLimit = respondToTaskFeeLimit;
batchesState[batchIdentifier] = batchState;
// For aggregator and operators in v0.7.0
emit NewBatchV3(
batchMerkleRoot,
msg.sender,
uint32(block.number),
batchDataPointer,
respondToTaskFeeLimit
);
}
function respondToTaskV2(
// (batchMerkleRoot,senderAddress) is signed as a way to verify the batch was right
bytes32 batchMerkleRoot,
address senderAddress,
NonSignerStakesAndSignature memory nonSignerStakesAndSignature
) external onlyAggregator onlyWhenNotPaused(1) {
uint256 initialGasLeft = gasleft();
bytes32 batchIdentifierHash = keccak256(
abi.encodePacked(batchMerkleRoot, senderAddress)
);
BatchState storage currentBatch = batchesState[batchIdentifierHash];
// Note: This is a hacky solidity way to see that the element exists
// Value 0 would mean that the task is in block 0 so this can't happen.
if (currentBatch.taskCreatedBlock == 0) {
revert BatchDoesNotExist(batchIdentifierHash);
}
// Check task hasn't been responsed yet
if (currentBatch.responded) {
revert BatchAlreadyResponded(batchIdentifierHash);
}
currentBatch.responded = true;
// Check that batcher has enough funds to fund response
if (
batchersBalances[senderAddress] < currentBatch.respondToTaskFeeLimit
) {
revert InsufficientFunds(
senderAddress,
currentBatch.respondToTaskFeeLimit,
batchersBalances[senderAddress]
);
}
/* CHECKING SIGNATURES & WHETHER THRESHOLD IS MET OR NOT */
// check that aggregated BLS signature is valid
(QuorumStakeTotals memory quorumStakeTotals, ) = checkSignatures(
batchIdentifierHash,
currentBatch.taskCreatedBlock,
nonSignerStakesAndSignature
);
// check that signatories own at least a threshold percentage of each quourm
if (
quorumStakeTotals.signedStakeForQuorum[0] * THRESHOLD_DENOMINATOR <
quorumStakeTotals.totalStakeForQuorum[0] *
QUORUM_THRESHOLD_PERCENTAGE
) {
revert InvalidQuorumThreshold(
quorumStakeTotals.signedStakeForQuorum[0] *
THRESHOLD_DENOMINATOR,
quorumStakeTotals.totalStakeForQuorum[0] *
QUORUM_THRESHOLD_PERCENTAGE
);
}
emit BatchVerified(batchMerkleRoot, senderAddress);
// 70k was measured by trial and error until the aggregator got paid a bit over what it needed
uint256 txCost = (initialGasLeft - gasleft() + 70_000) * tx.gasprice;
// limit amount to spend is respondToTaskFeeLimit
uint256 transferAmount = txCost < currentBatch.respondToTaskFeeLimit ?
txCost : currentBatch.respondToTaskFeeLimit;
batchersBalances[senderAddress] -= transferAmount;
emit BatcherBalanceUpdated(
senderAddress,
batchersBalances[senderAddress]
);
payable(alignedAggregator).transfer(transferAmount);
}
function isVerifierDisabled(
uint8 verifierIdx
) external view returns (bool) {
uint256 bit = disabledVerifiers & (1 << verifierIdx);
return bit > 0;
}
function disableVerifier(
uint8 verifierIdx
) external onlyOwner {
disabledVerifiers |= (1 << verifierIdx);
emit VerifierDisabled(verifierIdx);
}
function enableVerifier(
uint8 verifierIdx
) external onlyOwner {
disabledVerifiers &= ~(1 << verifierIdx);
emit VerifierEnabled(verifierIdx);
}
function setDisabledVerifiers(uint256 bitmap) external onlyOwner {
disabledVerifiers = bitmap;
}
function verifyBatchInclusion(
bytes32 proofCommitment,
bytes32 pubInputCommitment,
bytes32 provingSystemAuxDataCommitment,
bytes20 proofGeneratorAddr,
bytes32 batchMerkleRoot,
bytes memory merkleProof,
uint256 verificationDataBatchIndex,
address senderAddress
) external view onlyWhenNotPaused(2) returns (bool) {
bytes32 batchIdentifier;
if (senderAddress == address(0)) {
batchIdentifier = batchMerkleRoot;
} else {
batchIdentifier = keccak256(
abi.encodePacked(batchMerkleRoot, senderAddress)
);
}
if (batchesState[batchIdentifier].taskCreatedBlock == 0) {
return false;
}
if (!batchesState[batchIdentifier].responded) {
return false;
}
bytes memory leaf = abi.encodePacked(
proofCommitment,
pubInputCommitment,
provingSystemAuxDataCommitment,
proofGeneratorAddr
);
bytes32 hashedLeaf = keccak256(leaf);
return
Merkle.verifyInclusionKeccak(
merkleProof,
batchMerkleRoot,
hashedLeaf,
verificationDataBatchIndex
);
}
function verifyBatchInclusion(
bytes32 proofCommitment,
bytes32 pubInputCommitment,
bytes32 provingSystemAuxDataCommitment,
bytes20 proofGeneratorAddr,
bytes32 batchMerkleRoot,
bytes memory merkleProof,
uint256 verificationDataBatchIndex
) external view onlyWhenNotPaused(2) returns (bool) {
return this.verifyBatchInclusion(
proofCommitment,
pubInputCommitment,
provingSystemAuxDataCommitment,
proofGeneratorAddr,
batchMerkleRoot,
merkleProof,
verificationDataBatchIndex,
address(0)
);
}
function setAggregator(address _alignedAggregator) public onlyOwner {
alignedAggregator = _alignedAggregator;
}
function withdraw(uint256 amount) external onlyWhenNotPaused(3) {
if (batchersBalances[msg.sender] < amount) {
revert InsufficientFunds(
msg.sender,
amount,
batchersBalances[msg.sender]
);
}
batchersBalances[msg.sender] -= amount;
emit BatcherBalanceUpdated(msg.sender, batchersBalances[msg.sender]);
payable(msg.sender).transfer(amount);
}
function balanceOf(address account) public view returns (uint256) {
return batchersBalances[account];
}
function depositToBatcher(address account) external payable onlyWhenNotPaused(4) {
_depositToBatcher(account, msg.value);
}
function _depositToBatcher(address account, uint256 amount) internal {
if (amount == 0) {
revert InvalidDepositAmount(amount);
}
batchersBalances[account] += amount;
emit BatcherBalanceUpdated(account, batchersBalances[account]);
}
receive() external payable onlyWhenNotPaused(5) {
_depositToBatcher(msg.sender, msg.value);
}
function checkPublicInput(
bytes calldata publicInput,
bytes32 hash
) external pure returns (bool) {
return keccak256(publicInput) == hash;
}
modifier onlyAggregator() {
if (msg.sender != alignedAggregator) {
revert SenderIsNotAggregator(msg.sender, alignedAggregator);
}
_;
}
}