-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathL1ScrollMessengerTest.t.sol
More file actions
244 lines (206 loc) · 9.93 KB
/
Copy pathL1ScrollMessengerTest.t.sol
File metadata and controls
244 lines (206 loc) · 9.93 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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {DSTestPlus} from "solmate/test/utils/DSTestPlus.sol";
import {EnforcedTxGateway} from "../L1/gateways/EnforcedTxGateway.sol";
import {L2GasPriceOracle} from "../L1/rollup/L2GasPriceOracle.sol";
import {L1MessageQueueV2} from "../L1/rollup/L1MessageQueueV2.sol";
import {IScrollChain, ScrollChain} from "../L1/rollup/ScrollChain.sol";
import {Whitelist} from "../L2/predeploys/Whitelist.sol";
import {IL1ScrollMessenger, L1ScrollMessenger} from "../L1/L1ScrollMessenger.sol";
import {L2ScrollMessenger} from "../L2/L2ScrollMessenger.sol";
import {L1GatewayTestBase} from "./L1GatewayTestBase.t.sol";
contract L1ScrollMessengerTest is L1GatewayTestBase {
event OnDropMessageCalled(bytes);
event UpdateMaxReplayTimes(uint256 oldMaxReplayTimes, uint256 newMaxReplayTimes);
function setUp() public {
__L1GatewayTestBase_setUp();
}
function testForbidCallMessageQueueV1FromL2() external {
bytes32 _xDomainCalldataHash = keccak256(
abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(this),
address(messageQueueV1),
0,
0,
new bytes(0)
)
);
prepareL2MessageRoot(_xDomainCalldataHash);
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
hevm.expectRevert(L1ScrollMessenger.ErrorForbidToCallMessageQueue.selector);
l1Messenger.relayMessageWithProof(address(this), address(messageQueueV1), 0, 0, new bytes(0), proof);
}
function testForbidCallMessageQueueV2FromL2() external {
bytes32 _xDomainCalldataHash = keccak256(
abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(this),
address(messageQueueV2),
0,
0,
new bytes(0)
)
);
prepareL2MessageRoot(_xDomainCalldataHash);
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
hevm.expectRevert(L1ScrollMessenger.ErrorForbidToCallMessageQueue.selector);
l1Messenger.relayMessageWithProof(address(this), address(messageQueueV2), 0, 0, new bytes(0), proof);
}
function testForbidCallEnforcedGatewayFromL2() external {
bytes32 _xDomainCalldataHash = keccak256(
abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(this),
address(enforcedTxGateway),
0,
0,
new bytes(0)
)
);
prepareL2MessageRoot(_xDomainCalldataHash);
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
hevm.expectRevert(L1ScrollMessenger.ErrorForbidToCallMessageQueue.selector);
l1Messenger.relayMessageWithProof(address(this), address(enforcedTxGateway), 0, 0, new bytes(0), proof);
}
function testForbidCallSelfFromL2() external {
bytes32 _xDomainCalldataHash = keccak256(
abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(this),
address(l1Messenger),
0,
0,
new bytes(0)
)
);
prepareL2MessageRoot(_xDomainCalldataHash);
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
hevm.expectRevert("Forbid to call self");
l1Messenger.relayMessageWithProof(address(this), address(l1Messenger), 0, 0, new bytes(0), proof);
}
function testSendMessage(uint256 exceedValue, address refundAddress) external {
hevm.assume(refundAddress.code.length == 0);
hevm.assume(uint256(uint160(refundAddress)) > 100); // ignore some precompile contracts
hevm.assume(refundAddress != address(0x000000000000000000636F6e736F6c652e6c6f67)); // ignore console/console2
exceedValue = bound(exceedValue, 1, address(this).balance / 2);
// Insufficient msg.value
hevm.expectRevert("Insufficient msg.value");
l1Messenger.sendMessage(address(0), 1, new bytes(0), defaultGasLimit, refundAddress);
// refund exceed fee
uint256 balanceBefore = refundAddress.balance;
l1Messenger.sendMessage{value: 1 + exceedValue}(address(0), 1, new bytes(0), defaultGasLimit, refundAddress);
assertEq(balanceBefore + exceedValue, refundAddress.balance);
}
function testReplayMessage(uint256 exceedValue, address refundAddress) external {
hevm.assume(refundAddress.code.length == 0);
hevm.assume(uint256(uint160(refundAddress)) > uint256(100)); // ignore some precompile contracts
hevm.assume(refundAddress != feeVault);
hevm.assume(refundAddress != address(0x000000000000000000636F6e736F6c652e6c6f67)); // ignore console/console2
exceedValue = bound(exceedValue, 1, address(this).balance / 2);
// append a message
l1Messenger.sendMessage{value: 100}(address(0), 100, new bytes(0), defaultGasLimit, refundAddress);
// Provided message has not been enqueued
hevm.expectRevert("Provided message has not been enqueued");
l1Messenger.replayMessage(address(this), address(0), 101, 0, new bytes(0), defaultGasLimit, refundAddress);
setL2BaseFee(1);
// Insufficient msg.value
hevm.expectRevert("Insufficient msg.value for fee");
l1Messenger.replayMessage(address(this), address(0), 100, 0, new bytes(0), defaultGasLimit, refundAddress);
uint256 _fee = messageQueueV2.estimateL2BaseFee() * defaultGasLimit;
// refund exceed fee
uint256 balanceBefore = refundAddress.balance;
uint256 feeVaultBefore = feeVault.balance;
l1Messenger.replayMessage{value: _fee + exceedValue}(
address(this),
address(0),
100,
0,
new bytes(0),
defaultGasLimit,
refundAddress
);
assertEq(balanceBefore + exceedValue, refundAddress.balance);
assertEq(feeVaultBefore + _fee, feeVault.balance);
// test replay list
// 1. send a message with nonce 2
// 2. replay 3 times
setL2BaseFee(0);
l1Messenger.sendMessage{value: 100}(address(0), 100, new bytes(0), defaultGasLimit, refundAddress);
bytes32 hash = keccak256(
abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(this),
address(0),
100,
2,
new bytes(0)
)
);
(uint256 _replayTimes, uint256 _lastIndex) = l1Messenger.replayStates(hash);
assertEq(_replayTimes, 0);
assertEq(_lastIndex, 0);
for (uint256 i = 0; i < 3; i++) {
l1Messenger.replayMessage(address(this), address(0), 100, 2, new bytes(0), defaultGasLimit, refundAddress);
(_replayTimes, _lastIndex) = l1Messenger.replayStates(hash);
assertEq(_replayTimes, i + 1);
assertEq(_lastIndex, i + 3);
assertEq(l1Messenger.prevReplayIndex(i + 3), i + 2 + 1);
for (uint256 j = 0; j <= i; j++) {
assertEq(l1Messenger.prevReplayIndex(i + 3 - j), i + 2 - j + 1);
}
}
}
function testSetPause() external {
// not owner, revert
hevm.startPrank(address(1));
hevm.expectRevert("Ownable: caller is not the owner");
l1Messenger.setPause(false);
hevm.stopPrank();
// pause
l1Messenger.setPause(true);
assertBoolEq(true, l1Messenger.paused());
hevm.expectRevert("Pausable: paused");
l1Messenger.sendMessage(address(0), 0, new bytes(0), defaultGasLimit);
hevm.expectRevert("Pausable: paused");
l1Messenger.sendMessage(address(0), 0, new bytes(0), defaultGasLimit, address(0));
hevm.expectRevert("Pausable: paused");
IL1ScrollMessenger.L2MessageProof memory _proof;
l1Messenger.relayMessageWithProof(address(0), address(0), 0, 0, new bytes(0), _proof);
hevm.expectRevert("Pausable: paused");
l1Messenger.replayMessage(address(0), address(0), 0, 0, new bytes(0), 0, address(0));
// unpause
l1Messenger.setPause(false);
assertBoolEq(false, l1Messenger.paused());
}
function testIntrinsicGasLimit() external {
setL2BaseFee(1e9);
uint256 value = 1;
// _xDomainCalldata contains
// 4B function identifier
// 20B sender addr (encoded as 32B)
// 20B target addr (encoded as 32B)
// 32B value
// 32B nonce
// message byte array (32B offset + 32B length + bytes (padding to multiple of 32))
// So the intrinsic gas must be greater than 21000 + 40 * 228 = 30120
uint256 _fee = messageQueueV2.estimateL2BaseFee() * 30120;
l1Messenger.sendMessage{value: _fee + value}(address(0), value, hex"0011220033", 30120);
// insufficient intrinsic gas
hevm.expectRevert(L1MessageQueueV2.ErrorGasLimitBelowIntrinsicGas.selector);
l1Messenger.sendMessage{value: _fee + value}(address(0), 1, hex"0011220033", 30119);
// gas limit exceeds the max value
uint256 gasLimit = 100000000;
_fee = messageQueueV2.estimateL2BaseFee() * gasLimit;
hevm.expectRevert(L1MessageQueueV2.ErrorGasLimitExceeded.selector);
l1Messenger.sendMessage{value: _fee + value}(address(0), value, hex"0011220033", gasLimit);
// update max gas limit
setL2BaseFee(1e9, gasLimit);
_fee = messageQueueV2.estimateL2BaseFee() * gasLimit;
l1Messenger.sendMessage{value: _fee + value}(address(0), value, hex"0011220033", gasLimit);
}
}