-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathL1ScrollMessenger.sol
More file actions
326 lines (276 loc) · 12.9 KB
/
L1ScrollMessenger.sol
File metadata and controls
326 lines (276 loc) · 12.9 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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {IScrollChain} from "./rollup/IScrollChain.sol";
import {IL1MessageQueueV1} from "./rollup/IL1MessageQueueV1.sol";
import {IL1MessageQueueV2} from "./rollup/IL1MessageQueueV2.sol";
import {IL1ScrollMessenger} from "./IL1ScrollMessenger.sol";
import {ScrollConstants} from "../libraries/constants/ScrollConstants.sol";
import {IScrollMessenger} from "../libraries/IScrollMessenger.sol";
import {ScrollMessengerBase} from "../libraries/ScrollMessengerBase.sol";
import {WithdrawTrieVerifier} from "../libraries/verifier/WithdrawTrieVerifier.sol";
// solhint-disable avoid-low-level-calls
// solhint-disable not-rely-on-time
// solhint-disable reason-string
/// @title L1ScrollMessenger
/// @notice The `L1ScrollMessenger` contract can:
///
/// 1. send messages from layer 1 to layer 2;
/// 2. relay messages from layer 2 layer 1;
/// 3. replay failed message by replacing the gas limit;
/// 4. drop expired message due to sequencer problems.
///
/// @dev All deposited Ether (including `WETH` deposited throng `L1WETHGateway`) will locked in
/// this contract.
///
/// The messages sent through this contract may failed due to out of gas or some contract errors layer 2. In such case,
/// users can initiate `replayMessage` to retry this message in layer 2. If it is because out of gas, users can provide
/// a larger `gasLimit`. Users need also to pay the cross domain relay fee again.
///
/// The messages sent through this contract may possibly be skipped in layer 2 due to circuit capacity overflow.
/// In such case, users can initiate `dropMessage` to claim refunds. But the cross domain relay fee won't be refunded.
contract L1ScrollMessenger is ScrollMessengerBase, IL1ScrollMessenger {
/**********
* Errors *
**********/
error ErrorForbidToCallMessageQueue();
/*************
* Constants *
*************/
/// @notice The address of Rollup contract.
address public immutable rollup;
/// @notice The address of L1MessageQueueV1 contract.
address public immutable messageQueueV1;
/// @notice The address of L1MessageQueueV2 contract.
address public immutable messageQueueV2;
/// @notice The address of `EnforcedTxGateway`.
address public immutable enforcedTxGateway;
/***********
* Structs *
***********/
struct ReplayState {
// The number of replayed times.
uint128 times;
// The queue index of lastest replayed one. If it is zero, it means the message has not been replayed.
uint128 lastIndex;
}
/*************
* Variables *
*************/
/// @notice Mapping from L1 message hash to the timestamp when the message is sent.
mapping(bytes32 => uint256) public messageSendTimestamp;
/// @notice Mapping from L2 message hash to a boolean value indicating if the message has been successfully executed.
mapping(bytes32 => bool) public isL2MessageExecuted;
/// @notice Mapping from L1 message hash to drop status.
/// @custom:deprecated This is no longer used.
// slither-disable-next-line uninitialized-state
mapping(bytes32 => bool) private __isL1MessageDropped;
/// @dev The storage slot used as Rollup contract, which is deprecated now.
/// @custom:deprecated This is no longer used.
address private __rollup;
/// @dev The storage slot used as L1MessageQueue contract, which is deprecated now.
/// @custom:deprecated This is no longer used.
address private __messageQueue;
/// @dev The maximum number of times each L1 message can be replayed.
/// @custom:deprecated This is no longer used.
uint256 private __maxReplayTimes;
/// @notice Mapping from L1 message hash to replay state.
mapping(bytes32 => ReplayState) public replayStates;
/// @notice Mapping from queue index to previous replay queue index.
///
/// @dev If a message `x` was replayed 3 times with index `q1`, `q2` and `q3`, the
/// value of `prevReplayIndex` and `replayStates` will be `replayStates[hash(x)].lastIndex = q3`,
/// `replayStates[hash(x)].times = 3`, `prevReplayIndex[q3] = q2`, `prevReplayIndex[q2] = q1`,
/// `prevReplayIndex[q1] = x` and `prevReplayIndex[x]=nil`.
///
/// @dev The index `x` that `prevReplayIndex[x]=nil` is used as the termination of the list.
/// Usually we use `0` to represent `nil`, but we cannot distinguish it with the first message
/// with index zero. So a nonzero offset `1` is added to the value of `prevReplayIndex[x]` to
/// avoid such situation.
mapping(uint256 => uint256) public prevReplayIndex;
/***************
* Constructor *
***************/
constructor(
address _counterpart,
address _rollup,
address _messageQueueV1,
address _messageQueueV2,
address _enforcedTxGateway
) ScrollMessengerBase(_counterpart) {
_disableInitializers();
rollup = _rollup;
messageQueueV1 = _messageQueueV1;
messageQueueV2 = _messageQueueV2;
enforcedTxGateway = _enforcedTxGateway;
}
/// @notice Initialize the storage of L1ScrollMessenger.
///
/// @dev The parameters `_counterpart`, `_rollup` and `_messageQueue` are no longer used.
///
/// @param _counterpart The address of L2ScrollMessenger contract in L2.
/// @param _feeVault The address of fee vault, which will be used to collect relayer fee.
/// @param _rollup The address of ScrollChain contract.
/// @param _messageQueue The address of L1MessageQueue contract.
function initialize(
address _counterpart,
address _feeVault,
address _rollup,
address _messageQueue
) public initializer {
ScrollMessengerBase.__ScrollMessengerBase_init(_counterpart, _feeVault);
__rollup = _rollup;
__messageQueue = _messageQueue;
}
/*****************************
* Public Mutating Functions *
*****************************/
/// @inheritdoc IScrollMessenger
function sendMessage(
address _to,
uint256 _value,
bytes memory _message,
uint256 _gasLimit
) external payable override whenNotPaused {
_sendMessage(_to, _value, _message, _gasLimit, _msgSender());
}
/// @inheritdoc IScrollMessenger
function sendMessage(
address _to,
uint256 _value,
bytes calldata _message,
uint256 _gasLimit,
address _refundAddress
) external payable override whenNotPaused {
_sendMessage(_to, _value, _message, _gasLimit, _refundAddress);
}
/// @inheritdoc IL1ScrollMessenger
function relayMessageWithProof(
address _from,
address _to,
uint256 _value,
uint256 _nonce,
bytes memory _message,
L2MessageProof memory _proof
) external override whenNotPaused notInExecution {
bytes32 _xDomainCalldataHash = keccak256(_encodeXDomainCalldata(_from, _to, _value, _nonce, _message));
require(!isL2MessageExecuted[_xDomainCalldataHash], "Message was already successfully executed");
{
require(IScrollChain(rollup).isBatchFinalized(_proof.batchIndex), "Batch is not finalized");
bytes32 _messageRoot = IScrollChain(rollup).withdrawRoots(_proof.batchIndex);
require(
WithdrawTrieVerifier.verifyMerkleProof(_messageRoot, _xDomainCalldataHash, _nonce, _proof.merkleProof),
"Invalid proof"
);
}
// @note check more `_to` address to avoid attack in the future when we add more gateways.
if (_to == messageQueueV1 || _to == messageQueueV2 || _to == enforcedTxGateway) {
revert ErrorForbidToCallMessageQueue();
}
_validateTargetAddress(_to);
// @note This usually will never happen, just in case.
require(_from != xDomainMessageSender, "Invalid message sender");
xDomainMessageSender = _from;
// xDomainMessageSender serves as reentrancy guard (notInExecution modifier).
// slither-disable-next-line reentrancy-eth
(bool success, ) = _to.call{value: _value}(_message);
// reset value to refund gas.
xDomainMessageSender = ScrollConstants.DEFAULT_XDOMAIN_MESSAGE_SENDER;
if (success) {
isL2MessageExecuted[_xDomainCalldataHash] = true;
emit RelayedMessage(_xDomainCalldataHash);
} else {
emit FailedRelayedMessage(_xDomainCalldataHash);
}
}
/// @inheritdoc IL1ScrollMessenger
function replayMessage(
address _from,
address _to,
uint256 _value,
uint256 _messageNonce,
bytes memory _message,
uint32 _newGasLimit,
address _refundAddress
) external payable override whenNotPaused notInExecution {
// We will use a different `queueIndex` for the replaced message. However, the original `queueIndex` or `nonce`
// is encoded in the `_message`. We will check the `xDomainCalldata` on layer 2 to avoid duplicated execution.
// So, only one message will succeed on layer 2. If one of the message is executed successfully, the other one
// will revert with "Message was already successfully executed".
bytes memory _xDomainCalldata = _encodeXDomainCalldata(_from, _to, _value, _messageNonce, _message);
bytes32 _xDomainCalldataHash = keccak256(_xDomainCalldata);
require(messageSendTimestamp[_xDomainCalldataHash] > 0, "Provided message has not been enqueued");
// cannot replay dropped message
require(!__isL1MessageDropped[_xDomainCalldataHash], "Message already dropped");
// compute and deduct the messaging fee to fee vault.
uint256 _fee = IL1MessageQueueV2(messageQueueV2).estimateCrossDomainMessageFee(_newGasLimit);
// charge relayer fee
require(msg.value >= _fee, "Insufficient msg.value for fee");
if (_fee > 0) {
(bool _success, ) = feeVault.call{value: _fee}("");
require(_success, "Failed to deduct the fee");
}
// enqueue the new transaction
uint256 _nextQueueIndex = IL1MessageQueueV2(messageQueueV2).nextCrossDomainMessageIndex();
IL1MessageQueueV2(messageQueueV2).appendCrossDomainMessage(counterpart, _newGasLimit, _xDomainCalldata);
ReplayState memory _replayState = replayStates[_xDomainCalldataHash];
// update the replayed message chain.
unchecked {
if (_replayState.lastIndex == 0) {
// the message has not been replayed before.
prevReplayIndex[_nextQueueIndex] = _messageNonce + 1;
} else {
prevReplayIndex[_nextQueueIndex] = _replayState.lastIndex + 1;
}
}
_replayState.lastIndex = uint128(_nextQueueIndex);
unchecked {
_replayState.times += 1;
}
replayStates[_xDomainCalldataHash] = _replayState;
// refund fee to `_refundAddress`
unchecked {
uint256 _refund = msg.value - _fee;
if (_refund > 0) {
(bool _success, ) = _refundAddress.call{value: _refund}("");
require(_success, "Failed to refund the fee");
}
}
}
/**********************
* Internal Functions *
**********************/
function _sendMessage(
address _to,
uint256 _value,
bytes memory _message,
uint256 _gasLimit,
address _refundAddress
) internal virtual nonReentrant {
// compute the actual cross domain message calldata.
uint256 _messageNonce = IL1MessageQueueV2(messageQueueV2).nextCrossDomainMessageIndex();
bytes memory _xDomainCalldata = _encodeXDomainCalldata(_msgSender(), _to, _value, _messageNonce, _message);
// compute and deduct the messaging fee to fee vault.
uint256 _fee = IL1MessageQueueV2(messageQueueV2).estimateCrossDomainMessageFee(_gasLimit);
require(msg.value >= _fee + _value, "Insufficient msg.value");
if (_fee > 0) {
(bool _success, ) = feeVault.call{value: _fee}("");
require(_success, "Failed to deduct the fee");
}
// append message to L1MessageQueue
IL1MessageQueueV2(messageQueueV2).appendCrossDomainMessage(counterpart, _gasLimit, _xDomainCalldata);
// record the message hash for future use.
bytes32 _xDomainCalldataHash = keccak256(_xDomainCalldata);
// normally this won't happen, since each message has different nonce, but just in case.
require(messageSendTimestamp[_xDomainCalldataHash] == 0, "Duplicated message");
messageSendTimestamp[_xDomainCalldataHash] = block.timestamp;
emit SentMessage(_msgSender(), _to, _value, _messageNonce, _gasLimit, _message);
// refund fee to `_refundAddress`
unchecked {
uint256 _refund = msg.value - _fee - _value;
if (_refund > 0) {
(bool _success, ) = _refundAddress.call{value: _refund}("");
require(_success, "Failed to refund the fee");
}
}
}
}