-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathSparta.t.sol
297 lines (242 loc) · 10.4 KB
/
Sparta.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 Aztec Labs.
pragma solidity >=0.8.18;
import {DecoderBase} from "../decoders/Base.sol";
import {DataStructures} from "../../src/core/libraries/DataStructures.sol";
import {Constants} from "../../src/core/libraries/ConstantsGen.sol";
import {SignatureLib} from "../../src/core/sequencer_selection/SignatureLib.sol";
import {MessageHashUtils} from "@oz/utils/cryptography/MessageHashUtils.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 {Leonidas} from "../../src/core/sequencer_selection/Leonidas.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";
import {IFeeJuicePortal} from "../../src/core/interfaces/IFeeJuicePortal.sol";
/**
* We are using the same blocks as from Rollup.t.sol.
* The tests in this file is testing the sequencer selection
*/
contract SpartaTest is DecoderBase {
using MessageHashUtils for bytes32;
Registry internal registry;
Inbox internal inbox;
Outbox internal outbox;
Rollup internal rollup;
MerkleTestUtil internal merkleTestUtil;
TxsDecoderHelper internal txsHelper;
PortalERC20 internal portalERC20;
AvailabilityOracle internal availabilityOracle;
mapping(address validator => uint256 privateKey) internal privateKeys;
SignatureLib.Signature internal emptySignature;
/**
* @notice Set up the contracts needed for the tests with time aligned to the provided block name
*/
modifier setup(uint256 _validatorCount) {
string memory _name = "mixed_block_1";
{
Leonidas leonidas = new Leonidas(address(1));
DecoderBase.Full memory full = load(_name);
uint256 slotNumber = full.block.decodedHeader.globalVariables.slotNumber;
uint256 initialTime =
full.block.decodedHeader.globalVariables.timestamp - slotNumber * leonidas.SLOT_DURATION();
vm.warp(initialTime);
}
address[] memory initialValidators = new address[](_validatorCount);
for (uint256 i = 1; i < _validatorCount + 1; i++) {
uint256 privateKey = uint256(keccak256(abi.encode("validator", i)));
address validator = vm.addr(privateKey);
privateKeys[validator] = privateKey;
initialValidators[i - 1] = validator;
}
registry = new Registry(address(this));
availabilityOracle = new AvailabilityOracle();
portalERC20 = new PortalERC20();
rollup = new Rollup(
registry,
availabilityOracle,
IFeeJuicePortal(address(0)),
bytes32(0),
address(this),
initialValidators
);
inbox = Inbox(address(rollup.INBOX()));
outbox = Outbox(address(rollup.OUTBOX()));
registry.upgrade(address(rollup));
merkleTestUtil = new MerkleTestUtil();
txsHelper = new TxsDecoderHelper();
_;
}
mapping(address => bool) internal _seenValidators;
mapping(address => bool) internal _seenCommittee;
function testInitialCommitteMatch() public setup(4) {
address[] memory validators = rollup.getValidators();
address[] memory committee = rollup.getCurrentEpochCommittee();
assertEq(rollup.getCurrentEpoch(), 0);
assertEq(validators.length, 4, "Invalid validator set size");
assertEq(committee.length, 4, "invalid committee set size");
for (uint256 i = 0; i < validators.length; i++) {
_seenValidators[validators[i]] = true;
}
for (uint256 i = 0; i < committee.length; i++) {
assertTrue(_seenValidators[committee[i]]);
assertFalse(_seenCommittee[committee[i]]);
_seenCommittee[committee[i]] = true;
}
}
function testProposerForNonSetupEpoch(uint8 _epochsToJump) public setup(4) {
uint256 pre = rollup.getCurrentEpoch();
vm.warp(
block.timestamp + uint256(_epochsToJump) * rollup.EPOCH_DURATION() * rollup.SLOT_DURATION()
);
uint256 post = rollup.getCurrentEpoch();
assertEq(pre + _epochsToJump, post, "Invalid epoch");
address expectedProposer = rollup.getCurrentProposer();
// Add a validator which will also setup the epoch
rollup.addValidator(address(0xdead));
address actualProposer = rollup.getCurrentProposer();
assertEq(expectedProposer, actualProposer, "Invalid proposer");
}
function testValidatorSetLargerThanCommittee(bool _insufficientSigs) public setup(100) {
assertGt(rollup.getValidators().length, rollup.TARGET_COMMITTEE_SIZE(), "Not enough validators");
uint256 committeSize = rollup.TARGET_COMMITTEE_SIZE() * 2 / 3 + (_insufficientSigs ? 0 : 1);
_testBlock("mixed_block_1", _insufficientSigs, committeSize, false);
assertEq(
rollup.getEpochCommittee(rollup.getCurrentEpoch()).length,
rollup.TARGET_COMMITTEE_SIZE(),
"Invalid committee size"
);
}
function testHappyPath() public setup(4) {
_testBlock("mixed_block_1", false, 3, false);
_testBlock("mixed_block_2", false, 3, false);
}
function testInvalidProposer() public setup(4) {
_testBlock("mixed_block_1", true, 3, true);
}
function testInsufficientSigs() public setup(4) {
_testBlock("mixed_block_1", true, 2, false);
}
struct StructToAvoidDeepStacks {
uint256 needed;
address proposer;
bool shouldRevert;
}
function _testBlock(
string memory _name,
bool _expectRevert,
uint256 _signatureCount,
bool _invalidaProposer
) internal {
DecoderBase.Full memory full = load(_name);
bytes memory header = full.block.header;
bytes32 archive = full.block.archive;
bytes memory body = full.block.body;
StructToAvoidDeepStacks memory ree;
// We jump to the time of the block. (unless it is in the past)
vm.warp(max(block.timestamp, full.block.decodedHeader.globalVariables.timestamp));
_populateInbox(full.populate.sender, full.populate.recipient, full.populate.l1ToL2Content);
availabilityOracle.publish(body);
ree.proposer = rollup.getCurrentProposer();
ree.shouldRevert = false;
rollup.setupEpoch();
bytes32[] memory txHashes = new bytes32[](0);
if (_signatureCount > 0 && ree.proposer != address(0)) {
address[] memory validators = rollup.getEpochCommittee(rollup.getCurrentEpoch());
ree.needed = validators.length * 2 / 3 + 1;
SignatureLib.Signature[] memory signatures = new SignatureLib.Signature[](_signatureCount);
bytes32 digest = keccak256(abi.encode(archive, txHashes));
for (uint256 i = 0; i < _signatureCount; i++) {
signatures[i] = createSignature(validators[i], digest);
}
if (_expectRevert) {
ree.shouldRevert = true;
if (_signatureCount < ree.needed) {
vm.expectRevert(
abi.encodeWithSelector(
Errors.Leonidas__InsufficientAttestationsProvided.selector,
ree.needed,
_signatureCount
)
);
}
// @todo Handle SignatureLib__InvalidSignature case
// @todo Handle Leonidas__InsufficientAttestations case
}
if (_expectRevert && _invalidaProposer) {
address realProposer = ree.proposer;
ree.proposer = address(uint160(uint256(keccak256(abi.encode("invalid", ree.proposer)))));
vm.expectRevert(
abi.encodeWithSelector(
Errors.Leonidas__InvalidProposer.selector, realProposer, ree.proposer
)
);
ree.shouldRevert = true;
}
vm.prank(ree.proposer);
rollup.propose(header, archive, bytes32(0), txHashes, signatures);
if (ree.shouldRevert) {
return;
}
} else {
rollup.propose(header, archive, bytes32(0));
}
assertEq(_expectRevert, ree.shouldRevert, "Does not match revert expectation");
bytes32 l2ToL1MessageTreeRoot;
{
uint32 numTxs = full.block.numTxs;
// 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
// 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 to 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(numTxs);
uint256 numMessagesWithPadding = numTxs * Constants.MAX_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.getRootData(full.block.decodedHeader.globalVariables.blockNumber);
// If we are trying to read a block beyond the proven chain, we should see "nothing".
if (rollup.provenBlockCount() > full.block.decodedHeader.globalVariables.blockNumber) {
assertEq(l2ToL1MessageTreeRoot, root, "Invalid l2 to l1 message tree root");
} else {
assertEq(root, bytes32(0), "Invalid outbox 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)
);
}
}
function createSignature(address _signer, bytes32 _digest)
internal
view
returns (SignatureLib.Signature memory)
{
uint256 privateKey = privateKeys[_signer];
bytes32 digestForSig = _digest.toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, digestForSig);
return SignatureLib.Signature({isEmpty: false, v: v, r: r, s: s});
}
}