-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathRollup.t.sol
More file actions
203 lines (158 loc) · 7.03 KB
/
Rollup.t.sol
File metadata and controls
203 lines (158 loc) · 7.03 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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 Aztec Labs.
pragma solidity >=0.8.18;
import {IERC20} from "@oz/token/ERC20/IERC20.sol";
import {DecoderBase} from "./decoders/Base.sol";
import {DataStructures} from "../src/core/libraries/DataStructures.sol";
import {Constants} from "../src/core/libraries/ConstantsGen.sol";
import {Registry} from "../src/core/messagebridge/Registry.sol";
import {Inbox} from "../src/core/messagebridge/Inbox.sol";
import {Outbox} from "../src/core/messagebridge/Outbox.sol";
import {Errors} from "../src/core/libraries/Errors.sol";
import {Rollup} from "../src/core/Rollup.sol";
import {AvailabilityOracle} from "../src/core/availability_oracle/AvailabilityOracle.sol";
import {NaiveMerkle} from "./merkle/Naive.sol";
import {MerkleTestUtil} from "./merkle/TestUtil.sol";
import {PortalERC20} from "./portals/PortalERC20.sol";
import {TxsDecoderHelper} from "./decoders/helpers/TxsDecoderHelper.sol";
/**
* Blocks are generated using the `integration_l1_publisher.test.ts` tests.
* Main use of these test is shorter cycles when updating the decoder contract.
*/
contract RollupTest is DecoderBase {
Registry internal registry;
Inbox internal inbox;
Outbox internal outbox;
Rollup internal rollup;
MerkleTestUtil internal merkleTestUtil;
TxsDecoderHelper internal txsHelper;
PortalERC20 internal portalERC20;
AvailabilityOracle internal availabilityOracle;
function setUp() public virtual {
registry = new Registry();
availabilityOracle = new AvailabilityOracle();
portalERC20 = new PortalERC20();
rollup = new Rollup(registry, availabilityOracle, IERC20(address(portalERC20)));
inbox = Inbox(address(rollup.INBOX()));
outbox = Outbox(address(rollup.OUTBOX()));
registry.upgrade(address(rollup), address(inbox), address(outbox));
// mint some tokens to the rollup
portalERC20.mint(address(rollup), 1000000);
merkleTestUtil = new MerkleTestUtil();
txsHelper = new TxsDecoderHelper();
}
function testMixedBlock() public {
_testBlock("mixed_block_0");
}
function testConsecutiveMixedBlocks() public {
_testBlock("mixed_block_0");
_testBlock("mixed_block_1");
}
function testEmptyBlock() public {
_testBlock("empty_block_0");
}
function testConsecutiveEmptyBlocks() public {
_testBlock("empty_block_0");
_testBlock("empty_block_1");
}
function testRevertInvalidChainId() public {
DecoderBase.Data memory data = load("empty_block_0").block;
bytes memory header = data.header;
bytes32 archive = data.archive;
bytes memory body = data.body;
assembly {
// TODO: Hardcoding offsets in the middle of tests is annoying to say the least.
mstore(add(header, add(0x20, 0x0134)), 0x420)
}
availabilityOracle.publish(body);
vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidChainId.selector, 0x420, 31337));
rollup.process(header, archive, bytes(""), bytes(""));
}
function testRevertInvalidVersion() public {
DecoderBase.Data memory data = load("empty_block_0").block;
bytes memory header = data.header;
bytes32 archive = data.archive;
bytes memory body = data.body;
assembly {
mstore(add(header, add(0x20, 0x0154)), 0x420)
}
availabilityOracle.publish(body);
vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidVersion.selector, 0x420, 1));
rollup.process(header, archive, bytes(""), bytes(""));
}
function testRevertTimestampInFuture() public {
DecoderBase.Data memory data = load("empty_block_0").block;
bytes memory header = data.header;
bytes32 archive = data.archive;
bytes memory body = data.body;
uint256 ts = block.timestamp + 1;
assembly {
mstore(add(header, add(0x20, 0x0194)), ts)
}
availabilityOracle.publish(body);
vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__TimestampInFuture.selector));
rollup.process(header, archive, bytes(""), bytes(""));
}
function testRevertTimestampTooOld() public {
DecoderBase.Data memory data = load("empty_block_0").block;
bytes memory header = data.header;
bytes32 archive = data.archive;
bytes memory body = data.body;
// Overwrite in the rollup contract
vm.store(address(rollup), bytes32(uint256(2)), bytes32(uint256(block.timestamp)));
availabilityOracle.publish(body);
vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__TimestampTooOld.selector));
rollup.process(header, archive, bytes(""), bytes(""));
}
function _testBlock(string memory name) public {
DecoderBase.Full memory full = load(name);
bytes memory header = full.block.header;
bytes32 archive = full.block.archive;
bytes memory body = full.block.body;
uint32 numTxs = full.block.numTxs;
// We jump to the time of the block.
vm.warp(full.block.decodedHeader.globalVariables.timestamp);
_populateInbox(full.populate.sender, full.populate.recipient, full.populate.l1ToL2Content);
availabilityOracle.publish(body);
uint256 toConsume = inbox.toConsume();
vm.record();
rollup.process(header, archive, bytes(""), bytes(""));
assertEq(inbox.toConsume(), toConsume + 1, "Message subtree not consumed");
bytes32 l2ToL1MessageTreeRoot;
{
// NB: The below works with full blocks because we require the largest possible subtrees
// for L2 to L1 messages - usually we make variable height subtrees, the roots of which
// form a balanced tree
uint256 numTxsWithPadding = txsHelper.computeNumTxEffectsToPad(numTxs) + numTxs;
// The below is a little janky - we know that this test deals with full txs with equal numbers
// of msgs or txs with no messages, so the division works
// TODO edit full.messages t include information about msgs per tx?
uint256 subTreeHeight = merkleTestUtil.calculateTreeHeightFromSize(
full.messages.l2ToL1Messages.length == 0 ? 0 : full.messages.l2ToL1Messages.length / numTxs
);
uint256 outHashTreeHeight = merkleTestUtil.calculateTreeHeightFromSize(numTxsWithPadding);
uint256 numMessagesWithPadding = numTxsWithPadding * Constants.MAX_NEW_L2_TO_L1_MSGS_PER_TX;
uint256 treeHeight = subTreeHeight + outHashTreeHeight;
NaiveMerkle tree = new NaiveMerkle(treeHeight);
for (uint256 i = 0; i < numMessagesWithPadding; i++) {
if (i < full.messages.l2ToL1Messages.length) {
tree.insertLeaf(full.messages.l2ToL1Messages[i]);
} else {
tree.insertLeaf(bytes32(0));
}
}
l2ToL1MessageTreeRoot = tree.computeRoot();
}
(bytes32 root,) = outbox.roots(full.block.decodedHeader.globalVariables.blockNumber);
assertEq(l2ToL1MessageTreeRoot, root, "Invalid l2 to l1 message tree root");
assertEq(rollup.archive(), archive, "Invalid archive");
}
function _populateInbox(address _sender, bytes32 _recipient, bytes32[] memory _contents) internal {
for (uint256 i = 0; i < _contents.length; i++) {
vm.prank(_sender);
inbox.sendL2Message(
DataStructures.L2Actor({actor: _recipient, version: 1}), _contents[i], bytes32(0)
);
}
}
}