-
Notifications
You must be signed in to change notification settings - Fork 18
/
L2MessageQueue.sol
66 lines (50 loc) · 2.41 KB
/
L2MessageQueue.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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {AppendOnlyMerkleTree} from "../../libraries/common/AppendOnlyMerkleTree.sol";
import {OwnableBase} from "../../libraries/common/OwnableBase.sol";
/// @title L2MessageQueue
/// @notice The original idea is from Optimism, see [OVM_L2ToL1MessagePasser](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts/contracts/L2/predeploys/OVM_L2ToL1MessagePasser.sol).
/// The L2 to L1 Message Passer is a utility contract which facilitate an L1 proof of the
/// of a message on L2. The L1 Cross Domain Messenger performs this proof in its
/// _verifyStorageProof function, which verifies the existence of the transaction hash in this
/// contract's `sentMessages` mapping.
contract L2MessageQueue is AppendOnlyMerkleTree, OwnableBase {
/**********
* Events *
**********/
/// @notice Emitted when a new message is added to the merkle tree.
/// @param index The index of the corresponding message.
/// @param messageHash The hash of the corresponding message.
event AppendMessage(uint256 index, bytes32 messageHash);
/*************
* Variables *
*************/
/// @notice The address of L2ScrollMessenger contract.
address public messenger;
/***************
* Constructor *
***************/
constructor(address _owner) {
_transferOwnership(_owner);
}
/// @notice Initialize the state of `L2MessageQueue`
/// @dev You are not allowed to initialize when there are some messages appended.
/// @param _messenger The address of messenger to update.
function initialize(address _messenger) external onlyOwner {
require(nextMessageIndex == 0, "cannot initialize");
_initializeMerkleTree();
messenger = _messenger;
}
/*****************************
* Public Mutating Functions *
*****************************/
/// @notice record the message to merkle tree and compute the new root.
/// @param _messageHash The hash of the new added message.
function appendMessage(bytes32 _messageHash) external returns (bytes32) {
require(msg.sender == messenger, "only messenger");
(uint256 _currentNonce, bytes32 _currentRoot) = _appendMessageHash(_messageHash);
// We can use the event to compute the merkle tree locally.
emit AppendMessage(_currentNonce, _messageHash);
return _currentRoot;
}
}