/
L1MessageService.sol
216 lines (182 loc) · 6.97 KB
/
L1MessageService.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.19;
import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import { IMessageService } from "../../interfaces/IMessageService.sol";
import { IGenericErrors } from "../../interfaces/IGenericErrors.sol";
import { PauseManager } from "../lib/PauseManager.sol";
import { RateLimiter } from "../lib/RateLimiter.sol";
import { L1MessageManager } from "./L1MessageManager.sol";
/**
* @title Contract to manage cross-chain messaging on L1.
* @author ConsenSys Software Inc.
*/
abstract contract L1MessageService is
Initializable,
RateLimiter,
L1MessageManager,
PauseManager,
IMessageService,
IGenericErrors
{
// @dev This is initialised to save user cost with existing slot.
uint256 public nextMessageNumber;
address private _messageSender;
// Keep free storage slots for future implementation updates to avoid storage collision.
uint256[50] private __gap;
// @dev adding these should not affect storage as they are constants and are store in bytecode
uint256 private constant REFUND_OVERHEAD_IN_GAS = 40000;
/**
* @notice Initialises underlying message service dependencies.
* @dev _messageSender is initialised to a non-zero value for gas efficiency on claiming.
* @param _limitManagerAddress The address owning the rate limiting management role.
* @param _pauseManagerAddress The address owning the pause management role.
* @param _rateLimitPeriod The period to rate limit against.
* @param _rateLimitAmount The limit allowed for withdrawing the period.
**/
function __MessageService_init(
address _limitManagerAddress,
address _pauseManagerAddress,
uint256 _rateLimitPeriod,
uint256 _rateLimitAmount
) internal onlyInitializing {
if (_limitManagerAddress == address(0)) {
revert ZeroAddressNotAllowed();
}
if (_pauseManagerAddress == address(0)) {
revert ZeroAddressNotAllowed();
}
__ERC165_init();
__Context_init();
__AccessControl_init();
__RateLimiter_init(_rateLimitPeriod, _rateLimitAmount);
_grantRole(RATE_LIMIT_SETTER_ROLE, _limitManagerAddress);
_grantRole(PAUSE_MANAGER_ROLE, _pauseManagerAddress);
nextMessageNumber = 1;
_messageSender = address(123456789);
}
/**
* @notice Adds a message for sending cross-chain and emits MessageSent.
* @dev The message number is preset (nextMessageNumber) and only incremented at the end if successful for the next caller.
* @dev This function should be called with a msg.value = _value + _fee. The fee will be paid on the destination chain.
* @param _to The address the message is intended for.
* @param _fee The fee being paid for the message delivery.
* @param _calldata The calldata to pass to the recipient.
**/
function sendMessage(
address _to,
uint256 _fee,
bytes calldata _calldata
) external payable whenTypeNotPaused(L1_L2_PAUSE_TYPE) whenTypeNotPaused(GENERAL_PAUSE_TYPE) {
if (_to == address(0)) {
revert ZeroAddressNotAllowed();
}
if (_fee > msg.value) {
revert ValueSentTooLow();
}
uint256 messageNumber = nextMessageNumber;
uint256 valueSent = msg.value - _fee;
bytes32 messageHash = keccak256(abi.encode(msg.sender, _to, _fee, valueSent, messageNumber, _calldata));
// @dev Status check and revert is in the message manager
_addL1L2MessageHash(messageHash);
nextMessageNumber++;
emit MessageSent(msg.sender, _to, _fee, valueSent, messageNumber, _calldata, messageHash);
}
/**
* @notice Claims and delivers a cross-chain message.
* @dev _feeRecipient can be set to address(0) to receive as msg.sender.
* @dev _messageSender is set temporarily when claiming and reset post. Used in sender().
* @dev _messageSender is reset to address(123456789) to be more gas efficient.
* @param _from The address of the original sender.
* @param _to The address the message is intended for.
* @param _fee The fee being paid for the message delivery.
* @param _value The value to be transferred to the destination address.
* @param _feeRecipient The recipient for the fee.
* @param _calldata The calldata to pass to the recipient.
* @param _nonce The unique auto generated nonce used when sending the message.
**/
function claimMessage(
address _from,
address _to,
uint256 _fee,
uint256 _value,
address payable _feeRecipient,
bytes calldata _calldata,
uint256 _nonce
) external distributeFees(_fee, _to, _calldata, _feeRecipient) {
_requireTypeNotPaused(L2_L1_PAUSE_TYPE);
_requireTypeNotPaused(GENERAL_PAUSE_TYPE);
bytes32 messageHash = keccak256(abi.encode(_from, _to, _fee, _value, _nonce, _calldata));
// @dev Status check and revert is in the message manager.
_updateL2L1MessageStatusToClaimed(messageHash);
_addUsedAmount(_fee + _value);
_messageSender = _from;
(bool callSuccess, bytes memory returnData) = _to.call{ value: _value }(_calldata);
if (!callSuccess) {
if (returnData.length > 0) {
assembly {
let data_size := mload(returnData)
revert(add(32, returnData), data_size)
}
} else {
revert MessageSendingFailed(_to);
}
}
_messageSender = address(123456789);
emit MessageClaimed(messageHash);
}
/**
* @notice Claims and delivers a cross-chain message.
* @dev _messageSender is set temporarily when claiming.
**/
function sender() external view returns (address) {
return _messageSender;
}
/**
* @notice Function to receive funds for liquidity purposes.
**/
receive() external payable virtual {}
/**
* @notice The unspent fee is refunded if applicable.
* @param _feeInWei The fee paid for delivery in Wei.
* @param _to The recipient of the message and gas refund.
* @param _calldata The calldata of the message.
**/
modifier distributeFees(
uint256 _feeInWei,
address _to,
bytes calldata _calldata,
address _feeRecipient
) {
//pre-execution
uint256 startingGas = gasleft();
_;
//post-execution
// we have a fee
if (_feeInWei > 0) {
// default postman fee
uint256 deliveryFee = _feeInWei;
// do we have empty calldata?
if (_calldata.length == 0) {
bool isDestinationEOA;
assembly {
isDestinationEOA := iszero(extcodesize(_to))
}
// are we calling an EOA
if (isDestinationEOA) {
// initial + cost to call and refund minus gasleft
deliveryFee = (startingGas + REFUND_OVERHEAD_IN_GAS - gasleft()) * tx.gasprice;
if (_feeInWei > deliveryFee) {
_to.call{ value: (_feeInWei - deliveryFee) }("");
} else {
deliveryFee = _feeInWei;
}
}
}
address feeReceiver = _feeRecipient == address(0) ? msg.sender : _feeRecipient;
(bool callSuccess, ) = feeReceiver.call{ value: deliveryFee }("");
if (!callSuccess) {
revert FeePaymentFailed(feeReceiver);
}
}
}
}