Skip to content

Commit 200a083

Browse files
elenadimitrovaben-chain
authored andcommitted
Reduce gas costs of deposits (#667)
* Remove messageNonce from BaseCrossDomainMessenger and use CTC queue lenght instead Remove Abs_BaseCrossDomainMessenger and restore dedicated nonce generation in OVM_L2CrossDomainMessenger Fix typo * Remove sentMessages mapping from L1CrossDomainMessenger storage and use the nonce to check for existence of replayed transaction * Refactor out common library function for getting cross domain calldata * Post rebase fixes * Use the queueIndex to check the transaction was enqueued * Fix tests for L1CrossDomainMessenger.replayMessage Also make that test work with an actual CanonicalTransactionChain implementation rather than a smock * Lint fixes * Optimise the resolve calls into the AddressManager lib * Rename the nonce parameter to be clear * Update test name Co-authored-by: ben-chain <[email protected]> * Rename getXDomainCalldata to encodeXDomainCalldata to match the new Lib_CrossDomainUtils Co-authored-by: ben-chain <[email protected]>
1 parent 2e72fd9 commit 200a083

File tree

9 files changed

+302
-202
lines changed

9 files changed

+302
-202
lines changed

packages/contracts/contracts/optimistic-ethereum/OVM/bridge/messaging/Abs_BaseCrossDomainMessenger.sol

Lines changed: 0 additions & 139 deletions
This file was deleted.

packages/contracts/contracts/optimistic-ethereum/OVM/bridge/messaging/OVM_L1CrossDomainMessenger.sol

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ pragma solidity >0.5.0 <0.8.0;
33
pragma experimental ABIEncoderV2;
44

55
/* Library Imports */
6-
import { Lib_OVMCodec } from "../../../libraries/codec/Lib_OVMCodec.sol";
76
import { Lib_AddressResolver } from "../../../libraries/resolver/Lib_AddressResolver.sol";
7+
import { Lib_OVMCodec } from "../../../libraries/codec/Lib_OVMCodec.sol";
88
import { Lib_AddressManager } from "../../../libraries/resolver/Lib_AddressManager.sol";
99
import { Lib_SecureMerkleTrie } from "../../../libraries/trie/Lib_SecureMerkleTrie.sol";
10+
import { Lib_CrossDomainUtils } from "../../../libraries/bridge/Lib_CrossDomainUtils.sol";
1011

1112
/* Interface Imports */
1213
import { iOVM_L1CrossDomainMessenger } from "../../../iOVM/bridge/messaging/iOVM_L1CrossDomainMessenger.sol";
1314
import { iOVM_CanonicalTransactionChain } from "../../../iOVM/chain/iOVM_CanonicalTransactionChain.sol";
1415
import { iOVM_StateCommitmentChain } from "../../../iOVM/chain/iOVM_StateCommitmentChain.sol";
1516

16-
/* Contract Imports */
17-
import { Abs_BaseCrossDomainMessenger } from "./Abs_BaseCrossDomainMessenger.sol";
18-
1917
/* External Imports */
2018
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
2119
import { PausableUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
@@ -32,7 +30,6 @@ import { ReentrancyGuardUpgradeable } from "@openzeppelin/contracts-upgradeable/
3230
*/
3331
contract OVM_L1CrossDomainMessenger is
3432
iOVM_L1CrossDomainMessenger,
35-
Abs_BaseCrossDomainMessenger,
3633
Lib_AddressResolver,
3734
OwnableUpgradeable,
3835
PausableUpgradeable,
@@ -51,11 +48,24 @@ contract OVM_L1CrossDomainMessenger is
5148
bytes32 indexed _xDomainCalldataHash
5249
);
5350

51+
/*************
52+
* Constants *
53+
*************/
54+
55+
// The default x-domain message sender being set to a non-zero value makes
56+
// deployment a bit more expensive, but in exchange the refund on every call to
57+
// `relayMessage` by the L1 and L2 messengers will be higher.
58+
address internal constant DEFAULT_XDOMAIN_SENDER = 0x000000000000000000000000000000000000dEaD;
59+
5460
/**********************
5561
* Contract Variables *
5662
**********************/
5763

5864
mapping (bytes32 => bool) public blockedMessages;
65+
mapping (bytes32 => bool) public relayedMessages;
66+
mapping (bytes32 => bool) public successfulMessages;
67+
68+
address internal xDomainMsgSender = DEFAULT_XDOMAIN_SENDER;
5969

6070
/***************
6171
* Constructor *
@@ -155,6 +165,48 @@ contract OVM_L1CrossDomainMessenger is
155165
emit MessageAllowed(_xDomainCalldataHash);
156166
}
157167

168+
function xDomainMessageSender()
169+
public
170+
override
171+
view
172+
returns (
173+
address
174+
)
175+
{
176+
require(xDomainMsgSender != DEFAULT_XDOMAIN_SENDER, "xDomainMessageSender is not set");
177+
return xDomainMsgSender;
178+
}
179+
180+
/**
181+
* Sends a cross domain message to the target messenger.
182+
* @param _target Target contract address.
183+
* @param _message Message to send to the target.
184+
* @param _gasLimit Gas limit for the provided message.
185+
*/
186+
function sendMessage(
187+
address _target,
188+
bytes memory _message,
189+
uint32 _gasLimit
190+
)
191+
override
192+
public
193+
{
194+
address ovmCanonicalTransactionChain = resolve("OVM_CanonicalTransactionChain");
195+
// Use the CTC queue length as nonce
196+
uint40 nonce = iOVM_CanonicalTransactionChain(ovmCanonicalTransactionChain).getQueueLength();
197+
198+
bytes memory xDomainCalldata = Lib_CrossDomainUtils.encodeXDomainCalldata(
199+
_target,
200+
msg.sender,
201+
_message,
202+
nonce
203+
);
204+
205+
address l2CrossDomainMessenger = resolve("OVM_L2CrossDomainMessenger");
206+
_sendXDomainMessage(ovmCanonicalTransactionChain, l2CrossDomainMessenger, xDomainCalldata, _gasLimit);
207+
emit SentMessage(xDomainCalldata);
208+
}
209+
158210
/**
159211
* Relays a cross domain message to a contract.
160212
* @inheritdoc iOVM_L1CrossDomainMessenger
@@ -172,7 +224,7 @@ contract OVM_L1CrossDomainMessenger is
172224
onlyRelayer
173225
whenNotPaused
174226
{
175-
bytes memory xDomainCalldata = _getXDomainCalldata(
227+
bytes memory xDomainCalldata = Lib_CrossDomainUtils.encodeXDomainCalldata(
176228
_target,
177229
_sender,
178230
_message,
@@ -232,25 +284,40 @@ contract OVM_L1CrossDomainMessenger is
232284
address _target,
233285
address _sender,
234286
bytes memory _message,
235-
uint256 _messageNonce,
287+
uint256 _queueIndex,
236288
uint32 _gasLimit
237289
)
238290
override
239291
public
240292
{
241-
bytes memory xDomainCalldata = _getXDomainCalldata(
242-
_target,
243-
_sender,
244-
_message,
245-
_messageNonce
293+
// Verify that the message is in the queue:
294+
address canonicalTransactionChain = resolve("OVM_CanonicalTransactionChain");
295+
Lib_OVMCodec.QueueElement memory element = iOVM_CanonicalTransactionChain(canonicalTransactionChain).getQueueElement(_queueIndex);
296+
297+
address l2CrossDomainMessenger = resolve("OVM_L2CrossDomainMessenger");
298+
// Compute the transactionHash
299+
bytes32 transactionHash = keccak256(
300+
abi.encode(
301+
address(this),
302+
l2CrossDomainMessenger,
303+
_gasLimit,
304+
_message
305+
)
246306
);
247307

248308
require(
249-
sentMessages[keccak256(xDomainCalldata)] == true,
250-
"Provided message has not already been sent."
309+
transactionHash == element.transactionHash,
310+
"Provided message has not been enqueued."
311+
);
312+
313+
bytes memory xDomainCalldata = Lib_CrossDomainUtils.encodeXDomainCalldata(
314+
_target,
315+
_sender,
316+
_message,
317+
_queueIndex
251318
);
252319

253-
_sendXDomainMessage(xDomainCalldata, _gasLimit);
320+
_sendXDomainMessage(canonicalTransactionChain, l2CrossDomainMessenger, xDomainCalldata, _gasLimit);
254321
}
255322

256323

@@ -364,18 +431,21 @@ contract OVM_L1CrossDomainMessenger is
364431

365432
/**
366433
* Sends a cross domain message.
434+
* @param _canonicalTransactionChain Address of the OVM_CanonicalTransactionChain instance.
435+
* @param _l2CrossDomainMessenger Address of the OVM_L2CrossDomainMessenger instance.
367436
* @param _message Message to send.
368437
* @param _gasLimit OVM gas limit for the message.
369438
*/
370439
function _sendXDomainMessage(
440+
address _canonicalTransactionChain,
441+
address _l2CrossDomainMessenger,
371442
bytes memory _message,
372443
uint256 _gasLimit
373444
)
374-
override
375445
internal
376446
{
377-
iOVM_CanonicalTransactionChain(resolve("OVM_CanonicalTransactionChain")).enqueue(
378-
resolve("OVM_L2CrossDomainMessenger"),
447+
iOVM_CanonicalTransactionChain(_canonicalTransactionChain).enqueue(
448+
_l2CrossDomainMessenger,
379449
_gasLimit,
380450
_message
381451
);

0 commit comments

Comments
 (0)