From 54ed0015b07d689d6ae36b6ebf9840e559a94c7c Mon Sep 17 00:00:00 2001 From: echo Date: Wed, 10 Apr 2024 10:53:37 +0800 Subject: [PATCH] Move `ImportedMessageRoot` event to ORMP contract #153 (#155) * fmt * clean * move * fix test --- src/ORMP.sol | 15 +++++++++++++++ src/eco/Oracle.sol | 14 +++++++------- src/interfaces/IORMP.sol | 15 +++++++++++++++ test/eco/Oracle.t.sol | 2 +- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/ORMP.sol b/src/ORMP.sol index 6b01f63..979d8e4 100644 --- a/src/ORMP.sol +++ b/src/ORMP.sol @@ -33,6 +33,10 @@ contract ORMP is ReentrancyGuard, Channel { event MessageAssigned( bytes32 indexed msgHash, address indexed oracle, address indexed relayer, uint256 oracleFee, uint256 relayerFee ); + event HashImported(uint256 indexed srcChainId, address indexed oracle, bytes32 indexed lookupKey, bytes32 hash); + + /// oracle => srcChainId => lookupKey => hash + mapping(address => mapping(uint256 => mapping(bytes32 => bytes32))) public hashLookup; constructor(address dao) Channel(dao) {} @@ -66,6 +70,17 @@ contract ORMP is ReentrancyGuard, Channel { return msgHash; } + /// @dev Import hash by any oracle address. + /// @notice Hash is an abstract of the proof system, it can be a block hash or a message root hash, + /// specifically provided by oracles. + /// @param srcChainId The source chain Id. + /// @param lookupKey The key for loop up hash. + /// @param hash_ The hash to import. + function importHash(uint256 srcChainId, bytes32 lookupKey, bytes32 hash_) external { + hashLookup[msg.sender][srcChainId][lookupKey] = hash_; + emit HashImported(srcChainId, msg.sender, lookupKey, hash_); + } + function _handleFee( address ua, address refund, diff --git a/src/eco/Oracle.sol b/src/eco/Oracle.sol index aee841e..d80becd 100644 --- a/src/eco/Oracle.sol +++ b/src/eco/Oracle.sol @@ -18,19 +18,19 @@ pragma solidity 0.8.17; import "../Verifier.sol"; +import "../interfaces/IORMP.sol"; contract Oracle is Verifier { event SetFee(uint256 indexed chainId, uint256 fee); event SetApproved(address operator, bool approve); event Withdrawal(address indexed to, uint256 amt); event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); - event ImportedMessageRoot(uint256 indexed chainId, uint256 indexed blockHeight, bytes32 messageRoot); + + address public immutable PROTOCOL; address public owner; // chainId => price mapping(uint256 => uint256) public feeOf; - // chainId => blockNumber => messageRoot - mapping(uint256 => mapping(uint256 => bytes32)) rootOf; // operator => isApproved mapping(address => bool) public approvedOf; @@ -44,7 +44,8 @@ contract Oracle is Verifier { _; } - constructor(address dao) { + constructor(address dao, address ormp) { + PROTOCOL = ormp; owner = dao; } @@ -60,8 +61,7 @@ contract Oracle is Verifier { /// @param blockNumber The source chain block number. /// @param messageRoot The source chain message root corresponding to the channel. function importMessageRoot(uint256 chainId, uint256 blockNumber, bytes32 messageRoot) external onlyOwner { - rootOf[chainId][blockNumber] = messageRoot; - emit ImportedMessageRoot(chainId, blockNumber, messageRoot); + IORMP(PROTOCOL).importHash(chainId, bytes32(blockNumber), messageRoot); } function changeOwner(address newOwner) external onlyOwner { @@ -97,6 +97,6 @@ contract Oracle is Verifier { } function merkleRoot(uint256 chainId, uint256 blockNumber) public view override returns (bytes32) { - return rootOf[chainId][blockNumber]; + return IORMP(PROTOCOL).hashLookup(address(this), chainId, bytes32(blockNumber)); } } diff --git a/src/interfaces/IORMP.sol b/src/interfaces/IORMP.sol index 8dbd16a..ae694c4 100644 --- a/src/interfaces/IORMP.sol +++ b/src/interfaces/IORMP.sol @@ -74,4 +74,19 @@ interface IORMP { /// @param msgHash Hash of the checked message. /// @return Return the dispatched result of the checked message. function dones(bytes32 msgHash) external view returns (bool); + + /// @dev Import hash by any oracle address. + /// @notice Hash is an abstract of the proof system, it can be a block hash or a message root hash, + /// specifically provided by oracles. + /// @param srcChainId The source chain Id. + /// @param lookupKey The key for loop up hash. + /// @param hash_ The hash to import. + function importHash(uint256 srcChainId, bytes32 lookupKey, bytes32 hash_) external; + + /// @dev Fetch hash. + /// @param oracle The oracle address. + /// @param srcChainId The source chain Id. + /// @param lookupKey The key for loop up hash. + /// @return Return the hash imported by the oracle. + function hashLookup(address oracle, uint256 srcChainId, bytes32 lookupKey) external view returns (bytes32); } diff --git a/test/eco/Oracle.t.sol b/test/eco/Oracle.t.sol index 3037021..3ca0c32 100644 --- a/test/eco/Oracle.t.sol +++ b/test/eco/Oracle.t.sol @@ -71,7 +71,7 @@ contract OracleTest is Test { assertEq(r, bytes32(uint256(1))); } - function merkleRoot(uint256, uint256) external pure returns (bytes32) { + function hashLookup(address, uint256, bytes32) external pure returns (bytes32) { return bytes32(uint256(1)); } }