-
Notifications
You must be signed in to change notification settings - Fork 1
/
EthereumToArbitrumDispatcher.sol
438 lines (381 loc) · 14.5 KB
/
EthereumToArbitrumDispatcher.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;
import { IInbox } from "@arbitrum/nitro-contracts/src/bridge/IInbox.sol";
import { IMessageExecutor } from "../interfaces/IMessageExecutor.sol";
import {
IMessageDispatcher,
ISingleMessageDispatcher,
IBatchMessageDispatcher,
IMessageDispatcherArbitrum
} from "../interfaces/extensions/IMessageDispatcherArbitrum.sol";
import { MessageLib } from "../libraries/MessageLib.sol";
/**
* @title MessageDispatcherArbitrum contract
* @notice The MessageDispatcherArbitrum contract allows a user or contract to send messages from Ethereum to Arbitrum.
* It lives on the Ethereum chain and communicates with the `MessageExecutorArbitrum` contract on the Arbitrum chain.
*/
contract MessageDispatcherArbitrum is IMessageDispatcherArbitrum {
/* ============ Variables ============ */
/// @notice Address of the Arbitrum inbox on the Ethereum chain.
IInbox public immutable inbox;
/// @notice Address of the executor contract on the Arbitrum chain.
IMessageExecutor internal executor;
/// @notice Nonce used to compute unique `messageId`s.
uint256 internal nonce;
/// @notice ID of the chain receiving the dispatched messages. i.e.: 42161 for Mainnet, 421613 for Goerli.
uint256 internal immutable toChainId;
/**
* @notice Hash of transactions that were dispatched in `dispatchMessage` or `dispatchMessageBatch`.
* txHash => boolean
* @dev Ensure that messages passed to `processMessage` and `processMessageBatch` have been dispatched first.
*/
mapping(bytes32 => bool) public dispatched;
/* ============ Constructor ============ */
/**
* @notice MessageDispatcher constructor.
* @param _inbox Address of the Arbitrum inbox on Ethereum
* @param _toChainId ID of the chain receiving the dispatched messages
*/
constructor(IInbox _inbox, uint256 _toChainId) {
require(address(_inbox) != address(0), "Dispatcher/inbox-not-zero-adrs");
require(_toChainId != 0, "Dispatcher/chainId-not-zero");
inbox = _inbox;
toChainId = _toChainId;
}
/* ============ External Functions ============ */
/// @inheritdoc IMessageDispatcher
function dispatchMessage(
uint256 _toChainId,
address _to,
bytes calldata _data
) external returns (bytes32) {
_checkToChainId(_toChainId);
bytes32 _messageId = _computeMessageId(msg.sender, _to, _data);
dispatched[_getMessageTxHash(_messageId, msg.sender, _to, _data)] = true;
emit MessageDispatched(_messageId, msg.sender, _toChainId, _to, _data);
return _messageId;
}
/// @inheritdoc IBatchMessageDispatcher
function dispatchMessageBatch(
uint256 _toChainId,
MessageLib.Message[] calldata _messages
) external returns (bytes32) {
_checkToChainId(_toChainId);
bytes32 _messageId = _computeMessageBatchId(msg.sender, _messages);
dispatched[_getMessageBatchTxHash(_messageId, msg.sender, _messages)] = true;
emit MessageBatchDispatched(_messageId, msg.sender, _toChainId, _messages);
return _messageId;
}
/**
* @inheritdoc IMessageDispatcherArbitrum
* @dev The transaction hash must match the one stored in the `dispatched` mapping.
* @dev `_refundAddress` is passed as `_callValueRefundAddress`, this address can cancel the retryable ticket.
* @dev We store `_message` in memory to avoid a stack too deep error.
*/
function processMessage(
bytes32 _messageId,
address _from,
address _to,
bytes calldata _data,
address _refundAddress,
uint256 _gasLimit,
uint256 _maxSubmissionCost,
uint256 _gasPriceBid
) external payable returns (uint256) {
require(
dispatched[_getMessageTxHash(_messageId, _from, _to, _data)],
"Dispatcher/msg-not-dispatched"
);
address _executorAddress = address(executor);
_checkProcessParams(_executorAddress, _refundAddress);
bytes memory _message = MessageLib.encodeMessage(_to, _data, _messageId, block.chainid, _from);
uint256 _ticketId = _createRetryableTicket(
_executorAddress,
_maxSubmissionCost,
_refundAddress,
_refundAddress,
_gasLimit,
_gasPriceBid,
_message
);
emit MessageProcessed(_messageId, msg.sender, _ticketId);
return _ticketId;
}
/**
* @inheritdoc IMessageDispatcherArbitrum
* @dev The transaction hash must match the one stored in the `dispatched` mapping.
* @dev `_refundAddress` is passed as `_callValueRefundAddress`, this address can cancel the retryable ticket.
* @dev We store `_messageBatch` in memory to avoid a stack too deep error.
*/
function processMessageBatch(
bytes32 _messageId,
MessageLib.Message[] calldata _messages,
address _from,
address _refundAddress,
uint256 _gasLimit,
uint256 _maxSubmissionCost,
uint256 _gasPriceBid
) external payable returns (uint256) {
require(
dispatched[_getMessageBatchTxHash(_messageId, _from, _messages)],
"Dispatcher/msges-not-dispatched"
);
address _executorAddress = address(executor);
_checkProcessParams(_executorAddress, _refundAddress);
bytes memory _messageBatch = MessageLib.encodeMessageBatch(
_messages,
_messageId,
block.chainid,
_from
);
uint256 _ticketId = _createRetryableTicket(
_executorAddress,
_maxSubmissionCost,
_refundAddress,
_refundAddress,
_gasLimit,
_gasPriceBid,
_messageBatch
);
emit MessageBatchProcessed(_messageId, msg.sender, _ticketId);
return _ticketId;
}
/**
* @inheritdoc IMessageDispatcherArbitrum
* @dev `_refundAddress` is passed as `_callValueRefundAddress`, this address can cancel the retryable ticket.
* @dev We store `_message` in memory to avoid a stack too deep error.
*/
function dispatchAndProcessMessage(
uint256 _toChainId,
address _to,
bytes calldata _data,
address _refundAddress,
uint256 _gasLimit,
uint256 _maxSubmissionCost,
uint256 _gasPriceBid
) external payable returns (bytes32 messageId, uint256 ticketId) {
address _executorAddress = address(executor);
_checkProcessParams(_executorAddress, _refundAddress);
_checkToChainId(_toChainId);
messageId = _computeMessageId(msg.sender, _to, _data);
bytes memory _message = MessageLib.encodeMessage(
_to,
_data,
messageId,
block.chainid,
msg.sender
);
ticketId = _createRetryableTicket(
_executorAddress,
_maxSubmissionCost,
_refundAddress,
_refundAddress,
_gasLimit,
_gasPriceBid,
_message
);
emit MessageDispatched(messageId, msg.sender, _toChainId, _to, _data);
emit MessageProcessed(messageId, msg.sender, ticketId);
}
/**
* @inheritdoc IMessageDispatcherArbitrum
* @dev `_refundAddress` is passed as `_callValueRefundAddress`, this address can cancel the retryable ticket.
* @dev We store `_messageBatch` in memory to avoid a stack too deep error.
*/
function dispatchAndProcessMessageBatch(
uint256 _toChainId,
MessageLib.Message[] calldata _messages,
address _refundAddress,
uint256 _gasLimit,
uint256 _maxSubmissionCost,
uint256 _gasPriceBid
) external payable returns (bytes32 messageId, uint256 ticketId) {
address _executorAddress = address(executor);
_checkProcessParams(_executorAddress, _refundAddress);
_checkToChainId(_toChainId);
messageId = _computeMessageBatchId(msg.sender, _messages);
bytes memory _messageBatch = MessageLib.encodeMessageBatch(
_messages,
messageId,
block.chainid,
msg.sender
);
ticketId = _createRetryableTicket(
_executorAddress,
_maxSubmissionCost,
_refundAddress,
_refundAddress,
_gasLimit,
_gasPriceBid,
_messageBatch
);
emit MessageBatchDispatched(messageId, msg.sender, _toChainId, _messages);
emit MessageBatchProcessed(messageId, msg.sender, ticketId);
}
/**
* @notice Set executor contract address.
* @dev Will revert if it has already been set.
* @param _executor Address of the executor contract on the Arbitrum chain
*/
function setExecutor(IMessageExecutor _executor) external {
require(address(executor) == address(0), "Dispatcher/executor-already-set");
executor = _executor;
}
/**
* @notice Get transaction hash for a single message.
* @dev The transaction hash is used to ensure that only messages that were dispatched are processed.
* @param _messageId ID uniquely identifying the message that was dispatched
* @param _from Address who dispatched the message
* @param _to Address that will receive the message
* @param _data Data that was dispatched
* @return bytes32 Transaction hash
*/
function getMessageTxHash(
bytes32 _messageId,
address _from,
address _to,
bytes calldata _data
) external view returns (bytes32) {
return _getMessageTxHash(_messageId, _from, _to, _data);
}
/**
* @notice Get transaction hash for a batch of messages.
* @dev The transaction hash is used to ensure that only messages that were dispatched are processed.
* @param _messageId ID uniquely identifying the messages that were dispatched
* @param _from Address who dispatched the messages
* @param _messages Array of messages that were dispatched
* @return bytes32 Transaction hash
*/
function getMessageBatchTxHash(
bytes32 _messageId,
address _from,
MessageLib.Message[] calldata _messages
) external view returns (bytes32) {
return _getMessageBatchTxHash(_messageId, _from, _messages);
}
/// @inheritdoc ISingleMessageDispatcher
function getMessageExecutorAddress(uint256 _toChainId) external view returns (address) {
_checkToChainId(_toChainId);
return address(executor);
}
/* ============ Internal Functions ============ */
/**
* @notice Get transaction hash for a single message.
* @dev The transaction hash is used to ensure that only messages that were dispatched are processed.
* @param _messageId ID uniquely identifying the message that was dispatched
* @param _from Address who dispatched the message
* @param _to Address that will receive the message
* @param _data Data that was dispatched
* @return bytes32 Transaction hash
*/
function _getMessageTxHash(
bytes32 _messageId,
address _from,
address _to,
bytes memory _data
) internal view returns (bytes32) {
return keccak256(abi.encode(address(this), _messageId, _from, _to, _data));
}
/**
* @notice Get transaction hash for a batch of messages.
* @dev The transaction hash is used to ensure that only messages that were dispatched are processed.
* @param _messageId ID uniquely identifying the messages that were dispatched
* @param _from Address who dispatched the messages
* @param _messages Array of messages that were dispatched
* @return bytes32 Transaction hash
*/
function _getMessageBatchTxHash(
bytes32 _messageId,
address _from,
MessageLib.Message[] memory _messages
) internal view returns (bytes32) {
return keccak256(abi.encode(address(this), _messageId, _from, _messages));
}
/**
* @notice Check toChainId to ensure messages can be dispatched to this chain.
* @dev Will revert if `_toChainId` is not supported.
* @param _toChainId ID of the chain receiving the message
*/
function _checkToChainId(uint256 _toChainId) internal view {
require(_toChainId == toChainId, "Dispatcher/chainId-not-supported");
}
/**
* @notice Check process parameters to ensure messages can be dispatched.
* @dev Will revert if `executor` is not set.
* @dev Will revert if `_refund` is address zero.
* @param _executor Address of the executor contract on the Optimism chain
* @param _refund Address that will receive the `excessFeeRefund` amount if any
*/
function _checkProcessParams(address _executor, address _refund) internal pure {
require(_executor != address(0), "Dispatcher/executor-not-set");
require(_refund != address(0), "Dispatcher/refund-not-zero-adrs");
}
/**
* @notice Helper to increment nonce.
* @return uint256 Incremented nonce
*/
function _incrementNonce() internal returns (uint256) {
unchecked {
nonce++;
}
return nonce;
}
/**
* @notice Compute message ID.
* @param _from Address that dispatched the message
* @param _to Address that will receive the message
* @param _data Data to dispatch
*/
function _computeMessageId(
address _from,
address _to,
bytes calldata _data
) internal returns (bytes32) {
return MessageLib.computeMessageId(_incrementNonce(), _from, _to, _data);
}
/**
* @notice Compute message batch ID.
* @param _from Address that dispatched the batch of messages
* @param _messages Array of Message to dispatch
*/
function _computeMessageBatchId(
address _from,
MessageLib.Message[] memory _messages
) internal returns (bytes32) {
return MessageLib.computeMessageBatchId(_incrementNonce(), _from, _messages);
}
/**
* @notice Put a message in the L2 inbox that can be reexecuted for some fixed amount of time if it reverts
* @dev all msg.value will be deposited to `_callValueRefundAddress` on L2
* @dev `_gasLimit` and `_gasPriceBid` should not be set to 1 as that is used to trigger the RetryableData error
* @param _to Destination L2 contract address
* @param _maxSubmissionCost Max gas deducted from user's L2 balance to cover base submission fee
* @param _excessFeeRefundAddress `_gasLimit` x `_gasPriceBid` - execution cost gets credited here on L2 balance
* @param _callValueRefundAddress l2Callvalue gets credited here on L2 if retryable txn times out or gets cancelled
* @param _gasLimit Max gas deducted from user's L2 balance to cover L2 execution. Should not be set to 1 (magic value used to trigger the RetryableData error)
* @param _gasPriceBid Price bid for L2 execution. Should not be set to 1 (magic value used to trigger the RetryableData error)
* @param _data ABI encoded data of L2 message
* @return uint256 Unique message number of the retryable transaction
*/
function _createRetryableTicket(
address _to,
uint256 _maxSubmissionCost,
address _excessFeeRefundAddress,
address _callValueRefundAddress,
uint256 _gasLimit,
uint256 _gasPriceBid,
bytes memory _data
) internal returns (uint256) {
return
inbox.createRetryableTicket{ value: msg.value }(
_to,
0, // l2CallValue
_maxSubmissionCost,
_excessFeeRefundAddress,
_callValueRefundAddress,
_gasLimit,
_gasPriceBid,
_data
);
}
}