Skip to content

Commit

Permalink
refactor(evm): refactors Optimism Adapter and adds the Reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed Jan 24, 2024
1 parent 74a903a commit 609e597
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 94 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.17;

import { Reporter } from "../Reporter.sol";
import { IOracleAdapter } from "../../interfaces/IOracleAdapter.sol";
import { ICrossDomainMessenger } from "./interfaces/ICrossDomainMessenger.sol";

contract L1CrossDomainMessengerReporter is Reporter {
string public constant PROVIDER = "l1-cross-domain-messenger-reporter";
// The first 1.92 million gas on L2 is free. See here:
// https://community.optimism.io/docs/developers/bridge/messaging/#for-l1-%E2%87%92-l2-transactions
uint32 internal constant GAS_LIMIT = 1_920_000;

ICrossDomainMessenger public immutable L1_CROSS_DOMAIN_MESSENGER;
uint256 public immutable TO_CHAIN_ID;

error InvalidToChainId(uint256 chainId, uint256 expectedChainId);

constructor(
address headerStorage,
address yaho,
address l1CrossDomainMessenger,
uint256 toChainId
) Reporter(headerStorage, yaho) {
L1_CROSS_DOMAIN_MESSENGER = ICrossDomainMessenger(l1CrossDomainMessenger);
TO_CHAIN_ID = toChainId;
}

function _dispatch(
uint256 toChainId,
address adapter,
uint256[] memory ids,
bytes32[] memory hashes
) internal override returns (bytes32) {
if (toChainId != TO_CHAIN_ID) revert InvalidToChainId(toChainId, TO_CHAIN_ID);
bytes memory message = abi.encodeWithSignature("storeHashes(uint256[],bytes32[])", ids, hashes);
L1_CROSS_DOMAIN_MESSENGER.sendMessage(adapter, message, GAS_LIMIT);
return bytes32(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.17;

import { ICrossDomainMessenger } from "./ICrossDomainMessenger.sol";
import { ICrossDomainMessenger } from "./interfaces/ICrossDomainMessenger.sol";
import { OracleAdapter } from "../OracleAdapter.sol";
import { BlockHashOracleAdapter } from "../BlockHashOracleAdapter.sol";

contract L2CrossDomainMessengerAdapter is OracleAdapter, BlockHashOracleAdapter {
ICrossDomainMessenger public immutable l2CrossDomainMessenger;
address public immutable reporter;
uint256 public immutable chainId;
string public constant PROVIDER = "amb";

error ArrayLengthMissmatch(address emitter);
error UnauthorizedHashReporter(address emitter, address reporter);
error UnauthorizedL2CrossDomainMessenger(address emitter, address sender);
ICrossDomainMessenger public immutable L2_CROSS_DOMAIN_MESSENGER;
address public immutable REPORTER;
uint256 public immutable SOURCE_CHAIN_ID;

constructor(ICrossDomainMessenger l2CrossDomainMessenger_, address reporter_, uint256 chainId_) {
l2CrossDomainMessenger = l2CrossDomainMessenger_;
reporter = reporter_;
chainId = chainId_;
error ArrayLengthMissmatch();
error UnauthorizedHashReporter(address sender, address expectedSender);
error UnauthorizedL2CrossDomainMessenger(address domainMessageSender, address expectedDomainMessageSender);

constructor(ICrossDomainMessenger l2CrossDomainMessenger_, address reporter, uint256 sourceChainId) {
L2_CROSS_DOMAIN_MESSENGER = ICrossDomainMessenger(l2CrossDomainMessenger_);
REPORTER = reporter;
SOURCE_CHAIN_ID = sourceChainId;
}

/// @dev Check that the l2CrossDomainMessenger and xDomainMessageSender are valid.
modifier onlyValid() {
if (msg.sender != address(l2CrossDomainMessenger))
revert UnauthorizedL2CrossDomainMessenger(address(this), msg.sender);
if (l2CrossDomainMessenger.xDomainMessageSender() != reporter)
revert UnauthorizedHashReporter(address(this), reporter);
if (msg.sender != address(L2_CROSS_DOMAIN_MESSENGER))
revert UnauthorizedL2CrossDomainMessenger(msg.sender, address(L2_CROSS_DOMAIN_MESSENGER));
address xDomainMessageSender = L2_CROSS_DOMAIN_MESSENGER.xDomainMessageSender();
if (xDomainMessageSender != REPORTER) revert UnauthorizedHashReporter(xDomainMessageSender, REPORTER);
_;
}

/// @dev Stores the hashes for a given array of ids.
/// @param ids Array of ids number for which to set the hashes.
/// @param hashes Array of hashes to set for the given ids.
/// @notice Only callable by `l2CrossDomainMessenger` with a message passed from `reporter`.
/// @notice Will revert if given array lengths do not match.
function storeHashes(uint256[] memory ids, bytes32[] memory hashes) external onlyValid {
if (ids.length != hashes.length) revert ArrayLengthMissmatch(address(this));
for (uint256 i = 0; i < ids.length; i++) {
_storeHash(chainId, ids[i], hashes[i]);
}
if (ids.length != hashes.length) revert ArrayLengthMissmatch();
_storeHashes(SOURCE_CHAIN_ID, ids, hashes);
}
}

0 comments on commit 609e597

Please sign in to comment.