This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Rollup.sol
521 lines (481 loc) · 17.2 KB
/
Rollup.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
// This moves in 4.x to openzeppelin/contracts/utils/cryptography/draft-EIP712.sol
import { EIP712 } from "@openzeppelin/contracts/drafts/EIP712.sol";
import { IEIP712 } from "../libs/EIP712.sol";
import { Types } from "../libs/Types.sol";
import { Tx } from "../libs/Tx.sol";
import { MerkleTree } from "../libs/MerkleTree.sol";
import { Chooser } from "../proposers/Chooser.sol";
import { BLSAccountRegistry } from "../BLSAccountRegistry.sol";
import { Transfer } from "../Transfer.sol";
import { MassMigration } from "../MassMigrations.sol";
import { Create2Transfer } from "../Create2Transfer.sol";
import { BatchManager } from "./BatchManager.sol";
import { IDepositManager } from "../DepositManager.sol";
/**
* @notice Primary contract for submitting and disputing batches of transactions
*/
contract Rollup is BatchManager, EIP712, IEIP712 {
using Tx for bytes;
using Types for Types.Commitment;
using Types for Types.TransferCommitment;
using Types for Types.MassMigrationCommitment;
string public constant DOMAIN_NAME = "Hubble";
string public constant DOMAIN_VERSION = "1";
bytes32 public constant ZERO_BYTES32 =
0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563;
// External contracts
BLSAccountRegistry public immutable accountRegistry;
Transfer public immutable transfer;
MassMigration public immutable massMigration;
Create2Transfer public immutable create2Transfer;
uint256 public immutable paramMaxTxsPerCommit;
bytes32 public immutable zeroHashAtSubtreeDepth;
event DepositsFinalised(
uint256 subtreeID,
bytes32 depositSubTreeRoot,
uint256 pathToSubTree
);
constructor(
Chooser _chooser,
IDepositManager _depositManager,
BLSAccountRegistry _accountRegistry,
Transfer _transfer,
MassMigration _massMigration,
Create2Transfer _create2Transfer,
bytes32 genesisStateRoot,
uint256 stakeAmount,
uint256 blocksToFinalise,
uint256 minGasLeft,
uint256 maxTxsPerCommit
)
public
BatchManager(
stakeAmount,
blocksToFinalise,
minGasLeft,
_chooser,
_depositManager
)
EIP712(DOMAIN_NAME, DOMAIN_VERSION)
{
accountRegistry = _accountRegistry;
transfer = _transfer;
massMigration = _massMigration;
create2Transfer = _create2Transfer;
paramMaxTxsPerCommit = maxTxsPerCommit;
zeroHashAtSubtreeDepth = MerkleTree.getRoot(
_depositManager.paramMaxSubtreeDepth()
);
bytes32 genesisCommitment =
keccak256(abi.encode(genesisStateRoot, ZERO_BYTES32));
// Same effect as `MerkleTree.merklize`
bytes32 commitmentRoot =
keccak256(abi.encode(genesisCommitment, ZERO_BYTES32));
batches[nextBatchID] = Types.Batch({
commitmentRoot: commitmentRoot,
meta: Types.encodeMeta(
uint256(Types.Usage.Genesis),
1,
msg.sender,
block.number // genesis finalise instantly
)
});
// AccountRoot doesn't matter for genesis, add dummy value
emit NewBatch(nextBatchID, bytes32(0), Types.Usage.Genesis);
nextBatchID++;
}
modifier onlyCoordinator() {
require(
msg.sender == chooser.getProposer(),
"Rollup: Invalid proposer"
);
_;
}
/**
* @dev When running multiple batch submissions in a single transaction,
* if one of the earlier batch submissions fails the latter submission
* will still proceed, be invalid since it depended on the earlier
* submission's state, and be slashed. To prevent this scenario,
* verify the expected batchID matches nextBatchID when a
* submission function is executed.
*
* @param batchID expected batch ID
*/
modifier correctBatchID(uint256 batchID) {
require(batchID == nextBatchID, "batchID does not match nextBatchID");
_;
}
function checkInclusion(
bytes32 root,
Types.CommitmentInclusionProof memory proof
) internal pure returns (bool) {
return
MerkleTree.verify(
root,
proof.commitment.toHash(),
proof.path,
proof.witness
);
}
modifier checkPreviousCommitment(
uint256 batchID,
Types.CommitmentInclusionProof memory previous,
uint256 targetPath
) {
uint256 previousPath = 0;
uint256 expectedBatchID = 0;
if (targetPath == 0) {
// target is the first commit in the batch, so the previous commit is in the previous batch
expectedBatchID = batchID - 1;
previousPath = batches[expectedBatchID].size() - 1;
} else {
// target and previous commits are both in the current batch
expectedBatchID = batchID;
previousPath = targetPath - 1;
}
require(
previous.path == previousPath,
"previous commitment has wrong path"
);
require(
checkInclusion(batches[expectedBatchID].commitmentRoot, previous),
"previous commitment is absent in the current batch"
);
_;
}
function checkInclusion(
bytes32 root,
Types.TransferCommitmentInclusionProof memory proof
) internal pure returns (bool) {
return
MerkleTree.verify(
root,
proof.commitment.toHash(),
proof.path,
proof.witness
);
}
function checkInclusion(
bytes32 root,
Types.MMCommitmentInclusionProof memory proof
) internal pure returns (bool) {
return
MerkleTree.verify(
root,
proof.commitment.toHash(),
proof.path,
proof.witness
);
}
function domainSeparator() public view override returns (bytes32) {
return _domainSeparatorV4();
}
/**
* @dev This function should be highly optimized so that it can include as many commitments as possible
*/
function submitTransfer(
uint256 batchID,
bytes32[] calldata stateRoots,
uint256[2][] calldata signatures,
uint256[] calldata feeReceivers,
bytes[] calldata txss
)
external
payable
onlyCoordinator
isNotRollingBack
correctBatchID(batchID)
{
bytes32[] memory leaves = new bytes32[](stateRoots.length);
bytes32 accountRoot = accountRegistry.root();
bytes32 bodyRoot;
for (uint256 i = 0; i < stateRoots.length; i++) {
// This is TransferBody toHash() but we don't want the overhead of struct
bodyRoot = keccak256(
abi.encodePacked(
accountRoot,
signatures[i],
feeReceivers[i],
txss[i]
)
);
leaves[i] = keccak256(abi.encodePacked(stateRoots[i], bodyRoot));
}
submitBatch(
MerkleTree.merklize(leaves),
stateRoots.length,
accountRoot,
Types.Usage.Transfer
);
}
/**
* @dev This function should be highly optimized so that it can include as many commitments as possible
*/
function submitCreate2Transfer(
uint256 batchID,
bytes32[] calldata stateRoots,
uint256[2][] calldata signatures,
uint256[] calldata feeReceivers,
bytes[] calldata txss
)
external
payable
onlyCoordinator
isNotRollingBack
correctBatchID(batchID)
{
bytes32[] memory leaves = new bytes32[](stateRoots.length);
bytes32 accountRoot = accountRegistry.root();
bytes32 bodyRoot;
for (uint256 i = 0; i < stateRoots.length; i++) {
// This is TransferBody toHash() but we don't want the overhead of struct
bodyRoot = keccak256(
abi.encodePacked(
accountRoot,
signatures[i],
feeReceivers[i],
txss[i]
)
);
leaves[i] = keccak256(abi.encodePacked(stateRoots[i], bodyRoot));
}
submitBatch(
MerkleTree.merklize(leaves),
stateRoots.length,
accountRoot,
Types.Usage.Create2Transfer
);
}
/**
* @param meta is spokeID, tokenID, amount, and feeReceiver combined
* @dev This function should be highly optimized so that it can include as many commitments as possible
*/
function submitMassMigration(
uint256 batchID,
bytes32[] calldata stateRoots,
uint256[2][] calldata signatures,
uint256[4][] calldata meta,
bytes32[] calldata withdrawRoots,
bytes[] calldata txss
)
external
payable
onlyCoordinator
isNotRollingBack
correctBatchID(batchID)
{
bytes32[] memory leaves = new bytes32[](stateRoots.length);
bytes32 accountRoot = accountRegistry.root();
for (uint256 i = 0; i < stateRoots.length; i++) {
Types.MassMigrationBody memory body =
Types.MassMigrationBody(
accountRoot,
signatures[i],
meta[i][0],
withdrawRoots[i],
meta[i][1],
meta[i][2],
meta[i][3],
txss[i]
);
leaves[i] = keccak256(
abi.encodePacked(stateRoots[i], Types.toHash(body))
);
}
submitBatch(
MerkleTree.merklize(leaves),
stateRoots.length,
accountRoot,
Types.Usage.MassMigration
);
}
function submitDeposits(
uint256 batchID,
Types.CommitmentInclusionProof memory previous,
Types.SubtreeVacancyProof memory vacant
) public payable onlyCoordinator isNotRollingBack correctBatchID(batchID) {
uint256 preBatchID = batchID - 1;
require(
previous.path == batches[preBatchID].size() - 1,
"previous commitment has wrong path"
);
require(
checkInclusion(batches[preBatchID].commitmentRoot, previous),
"previous commitment is absent in the previous batch"
);
require(
MerkleTree.verify(
previous.commitment.stateRoot,
zeroHashAtSubtreeDepth,
vacant.pathAtDepth,
vacant.witness
),
"Rollup: State subtree is not vacant"
);
(uint256 subtreeID, bytes32 depositSubTreeRoot) =
depositManager.dequeueToSubmit();
uint256 postBatchID = preBatchID + 1;
// This deposit subtree is included in the batch whose ID is postBatchID
deposits[postBatchID] = depositSubTreeRoot;
emit DepositsFinalised(
subtreeID,
depositSubTreeRoot,
vacant.pathAtDepth
);
bytes32 newRoot =
MerkleTree.computeRoot(
depositSubTreeRoot,
vacant.pathAtDepth,
vacant.witness
);
bytes32 depositCommitment =
keccak256(abi.encode(newRoot, ZERO_BYTES32));
// Same effect as `MerkleTree.merklize`
bytes32 root = keccak256(abi.encode(depositCommitment, ZERO_BYTES32));
// AccountRoot doesn't matter for deposit, add dummy value
submitBatch(root, 1, bytes32(0), Types.Usage.Deposit);
}
function disputeTransitionTransfer(
uint256 batchID,
Types.CommitmentInclusionProof memory previous,
Types.TransferCommitmentInclusionProof memory target,
Types.StateMerkleProof[] memory proofs
)
public
isDisputable(batchID)
checkPreviousCommitment(batchID, previous, target.path)
{
require(
checkInclusion(batches[batchID].commitmentRoot, target),
"Target commitment is absent in the batch"
);
Types.Result result =
transfer.processTransferCommit(
previous.commitment.stateRoot,
target.commitment.stateRoot,
paramMaxTxsPerCommit,
target.commitment.body.feeReceiver,
target.commitment.body.txs,
proofs
);
if (result != Types.Result.Ok) startRollingBack(batchID, result);
}
function disputeTransitionMassMigration(
uint256 batchID,
Types.CommitmentInclusionProof memory previous,
Types.MMCommitmentInclusionProof memory target,
Types.StateMerkleProof[] memory proofs
)
public
isDisputable(batchID)
checkPreviousCommitment(batchID, previous, target.path)
{
require(
checkInclusion(batches[batchID].commitmentRoot, target),
"Target commitment is absent in the batch"
);
Types.Result result =
massMigration.processMassMigrationCommit(
previous.commitment.stateRoot,
target.commitment.stateRoot,
paramMaxTxsPerCommit,
target.commitment.body,
proofs
);
if (result != Types.Result.Ok) startRollingBack(batchID, result);
}
function disputeTransitionCreate2Transfer(
uint256 batchID,
Types.CommitmentInclusionProof memory previous,
Types.TransferCommitmentInclusionProof memory target,
Types.StateMerkleProof[] memory proofs
)
public
isDisputable(batchID)
checkPreviousCommitment(batchID, previous, target.path)
{
require(
checkInclusion(batches[batchID].commitmentRoot, target),
"Target commitment is absent in the batch"
);
Types.Result result =
create2Transfer.processCreate2TransferCommit(
previous.commitment.stateRoot,
target.commitment.stateRoot,
paramMaxTxsPerCommit,
target.commitment.body.feeReceiver,
target.commitment.body.txs,
proofs
);
if (result != Types.Result.Ok) startRollingBack(batchID, result);
}
function disputeSignatureTransfer(
uint256 batchID,
Types.TransferCommitmentInclusionProof memory target,
Types.SignatureProof memory signatureProof
) public isDisputable(batchID) {
require(
checkInclusion(batches[batchID].commitmentRoot, target),
"Rollup: Commitment not present in batch"
);
Types.AuthCommon memory common =
Types.AuthCommon({
signature: target.commitment.body.signature,
stateRoot: target.commitment.stateRoot,
accountRoot: target.commitment.body.accountRoot,
domain: domainSeparator(),
txs: target.commitment.body.txs
});
Types.Result result = transfer.checkSignature(common, signatureProof);
if (result != Types.Result.Ok) startRollingBack(batchID, result);
}
function disputeSignatureMassMigration(
uint256 batchID,
Types.MMCommitmentInclusionProof memory target,
Types.SignatureProof memory signatureProof
) public isDisputable(batchID) {
require(
checkInclusion(batches[batchID].commitmentRoot, target),
"Commitment not present in batch"
);
Types.AuthCommon memory common =
Types.AuthCommon({
signature: target.commitment.body.signature,
stateRoot: target.commitment.stateRoot,
accountRoot: target.commitment.body.accountRoot,
domain: domainSeparator(),
txs: target.commitment.body.txs
});
Types.Result result =
massMigration.checkSignature(
common,
signatureProof,
target.commitment.body.spokeID
);
if (result != Types.Result.Ok) startRollingBack(batchID, result);
}
function disputeSignatureCreate2Transfer(
uint256 batchID,
Types.TransferCommitmentInclusionProof memory target,
Types.SignatureProofWithReceiver memory signatureProof
) public isDisputable(batchID) {
require(
checkInclusion(batches[batchID].commitmentRoot, target),
"Rollup: Commitment not present in batch"
);
Types.AuthCommon memory common =
Types.AuthCommon({
signature: target.commitment.body.signature,
stateRoot: target.commitment.stateRoot,
accountRoot: target.commitment.body.accountRoot,
domain: domainSeparator(),
txs: target.commitment.body.txs
});
Types.Result result =
create2Transfer.checkSignature(common, signatureProof);
if (result != Types.Result.Ok) startRollingBack(batchID, result);
}
}