generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(evm): refactors Optimism Adapter and adds the Reporter
- Loading branch information
1 parent
74a903a
commit 609e597
Showing
5 changed files
with
59 additions
and
94 deletions.
There are no files selected for viewing
33 changes: 0 additions & 33 deletions
33
packages/evm/contracts/adapters/Optimism/L1CrossDomainMessengerHeaderReporter.sol
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
packages/evm/contracts/adapters/Optimism/L1CrossDomainMessengerMessageRelay.sol
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
packages/evm/contracts/adapters/Optimism/L1CrossDomainMessengerReporter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
44 changes: 19 additions & 25 deletions
44
packages/evm/contracts/adapters/Optimism/L2CrossDomainMessengerAdapter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
File renamed without changes.