-
Notifications
You must be signed in to change notification settings - Fork 1
/
EthereumToOptimismExecutor.sol
109 lines (88 loc) · 3.63 KB
/
EthereumToOptimismExecutor.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;
import { ICrossDomainMessenger } from "../vendor/optimism/ICrossDomainMessenger.sol";
import { IMessageDispatcher } from "../interfaces/IMessageDispatcher.sol";
import {
ISingleMessageExecutor,
IBatchMessageExecutor
} from "../interfaces/extensions/IBatchMessageExecutor.sol";
import { MessageLib } from "../libraries/MessageLib.sol";
/**
* @title MessageExecutorOptimism contract
* @notice The MessageExecutorOptimism contract executes messages from the Ethereum chain.
* These messages are sent by the `MessageDispatcherOptimism` contract which lives on the Ethereum chain.
*/
contract MessageExecutorOptimism is IBatchMessageExecutor {
/* ============ Variables ============ */
/// @notice Address of the Optimism cross domain messenger on the Optimism chain.
ICrossDomainMessenger public immutable crossDomainMessenger;
/// @notice Address of the dispatcher contract on the Ethereum chain.
IMessageDispatcher public dispatcher;
/**
* @notice Mapping to uniquely identify the messages that were executed
* messageId => boolean
* @dev Ensure that messages cannot be replayed once they have been executed.
*/
mapping(bytes32 => bool) public executed;
/* ============ Constructor ============ */
/**
* @notice MessageExecutorOptimism constructor.
* @param _crossDomainMessenger Address of the Optimism cross domain messenger on the Optimism chain
*/
constructor(ICrossDomainMessenger _crossDomainMessenger) {
require(address(_crossDomainMessenger) != address(0), "Executor/CDM-not-zero-address");
crossDomainMessenger = _crossDomainMessenger;
}
/* ============ External Functions ============ */
/// @inheritdoc ISingleMessageExecutor
function executeMessage(
address _to,
bytes calldata _data,
bytes32 _messageId,
uint256 _fromChainId,
address _from
) external {
IMessageDispatcher _dispatcher = dispatcher;
_isAuthorized(_dispatcher);
bool _executedMessageId = executed[_messageId];
executed[_messageId] = true;
MessageLib.executeMessage(_to, _data, _messageId, _fromChainId, _from, _executedMessageId);
emit MessageIdExecuted(_fromChainId, _messageId);
}
/// @inheritdoc IBatchMessageExecutor
function executeMessageBatch(
MessageLib.Message[] calldata _messages,
bytes32 _messageId,
uint256 _fromChainId,
address _from
) external {
IMessageDispatcher _dispatcher = dispatcher;
_isAuthorized(_dispatcher);
bool _executedMessageId = executed[_messageId];
executed[_messageId] = true;
MessageLib.executeMessageBatch(_messages, _messageId, _fromChainId, _from, _executedMessageId);
emit MessageIdExecuted(_fromChainId, _messageId);
}
/**
* @notice Set dispatcher contract address.
* @dev Will revert if it has already been set.
* @param _dispatcher Address of the dispatcher contract on the Ethereum chain
*/
function setDispatcher(IMessageDispatcher _dispatcher) external {
require(address(dispatcher) == address(0), "Executor/dispatcher-already-set");
dispatcher = _dispatcher;
}
/* ============ Internal Functions ============ */
/**
* @notice Check if sender is authorized to message `executeMessageBatch`.
* @param _dispatcher Address of the dispatcher on the Ethereum chain
*/
function _isAuthorized(IMessageDispatcher _dispatcher) internal view {
ICrossDomainMessenger _crossDomainMessenger = crossDomainMessenger;
require(
msg.sender == address(_crossDomainMessenger) &&
_crossDomainMessenger.xDomainMessageSender() == address(_dispatcher),
"Executor/sender-unauthorized"
);
}
}