Skip to content

Commit

Permalink
Use relayMessage in OptimismDomain for L2>L1 message (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: telome <>
  • Loading branch information
telome authored Aug 20, 2024
1 parent eab651b commit a99c543
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions src/domains/OptimismDomain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import "forge-std/Vm.sol";

import { RecordedLogs } from "./RecordedLogs.sol";
import { Domain, BridgedDomain } from "./BridgedDomain.sol";
import { stdStorage, StdStorage } from "forge-std/Test.sol";
import { StdChains } from "forge-std/StdChains.sol";

interface MessengerLike {
Expand All @@ -37,14 +38,21 @@ interface MessengerLike {
) external payable;
}

interface L1MessengerLike {
function portal() external returns (address);
}

contract OptimismDomain is BridgedDomain {
using stdStorage for StdStorage;

MessengerLike public immutable l1Messenger;
MessengerLike public immutable l2Messenger;

bytes32 constant SENT_MESSAGE_TOPIC = keccak256("SentMessage(address,address,bytes,uint256,uint256)");
uint160 constant OFFSET = uint160(0x1111000000000000000000000000000000001111);

StdStorage internal stdstore;

uint256 internal lastFromHostLogIndex;
uint256 internal lastToHostLogIndex;

Expand Down Expand Up @@ -84,40 +92,18 @@ contract OptimismDomain is BridgedDomain {

function relayToHost(bool switchToHost) external override {
hostDomain.selectFork();
address portal = L1MessengerLike(address(l1Messenger)).portal();

// Read all L2 -> L1 messages and relay them under Primary fork
// Note: We bypass the L1 messenger relay here because it's easier to not have to generate valid state roots / merkle proofs
Vm.Log[] memory logs = RecordedLogs.getLogs();
for (; lastToHostLogIndex < logs.length; lastToHostLogIndex++) {
Vm.Log memory log = logs[lastToHostLogIndex];
if (log.topics[0] == SENT_MESSAGE_TOPIC && log.emitter == address(l2Messenger)) {
address target = address(uint160(uint256(log.topics[1])));
(address sender, bytes memory message,,) = abi.decode(log.data, (address, bytes, uint256, uint256));
// Set xDomainMessageSender
vm.store(
address(l1Messenger),
bytes32(uint256(204)),
bytes32(uint256(uint160(sender)))
);
vm.startPrank(address(l1Messenger));
(bool success, bytes memory response) = target.call(message);
vm.stopPrank();
vm.store(
address(l1Messenger),
bytes32(uint256(204)),
bytes32(uint256(0))
);
if (!success) {
string memory rmessage;
assembly {
let size := mload(add(response, 0x44))
rmessage := mload(0x40)
mstore(rmessage, size)
mstore(0x40, add(rmessage, and(add(add(size, 0x20), 0x1f), not(0x1f))))
returndatacopy(add(rmessage, 0x20), 0x44, size)
}
revert(rmessage);
}
(address sender, bytes memory message, uint256 nonce, uint256 gasLimit) = abi.decode(log.data, (address, bytes, uint256, uint256));
stdstore.target(portal).sig("l2Sender()").checked_write(address(l2Messenger));
vm.prank(portal);
l1Messenger.relayMessage(nonce, sender, target, 0, gasLimit, message);
}
}

Expand Down

0 comments on commit a99c543

Please sign in to comment.