diff --git a/src/ORMP.sol b/src/ORMP.sol index 979d8e4..9fa7932 100644 --- a/src/ORMP.sol +++ b/src/ORMP.sol @@ -33,10 +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); + event HashImported(address indexed oracle, bytes32 indexed lookupKey, bytes32 indexed hash); - /// oracle => srcChainId => lookupKey => hash - mapping(address => mapping(uint256 => mapping(bytes32 => bytes32))) public hashLookup; + /// oracle => lookupKey => hash + mapping(address => mapping(bytes32 => bytes32)) public hashLookup; constructor(address dao) Channel(dao) {} @@ -73,12 +73,11 @@ contract ORMP is ReentrancyGuard, Channel { /// @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 importHash(bytes32 lookupKey, bytes32 hash_) external { + hashLookup[msg.sender][lookupKey] = hash_; + emit HashImported(msg.sender, lookupKey, hash_); } function _handleFee( diff --git a/src/Verifier.sol b/src/Verifier.sol index 9863a74..2a5cb02 100644 --- a/src/Verifier.sol +++ b/src/Verifier.sol @@ -22,13 +22,14 @@ import "./interfaces/IVerifier.sol"; abstract contract Verifier is IVerifier { /// @notice Fetch message hash. /// @param chainId The source chain id. + /// @param channel The message channel. /// @param msgIndex The Message index. /// @return Message hash in source chain. - function hashOf(uint256 chainId, uint256 msgIndex) public view virtual returns (bytes32); + function hashOf(uint256 chainId, address channel, uint256 msgIndex) public view virtual returns (bytes32); /// @inheritdoc IVerifier function verifyMessageProof(Message calldata message, bytes calldata) external view returns (bool) { // check oracle's message hash equal relayer's message hash - return hashOf(message.fromChainId, message.index) == hash(message); + return hashOf(message.fromChainId, message.channel, message.index) == hash(message); } } diff --git a/src/eco/Oracle.sol b/src/eco/Oracle.sol index bee4e8d..e042194 100644 --- a/src/eco/Oracle.sol +++ b/src/eco/Oracle.sol @@ -56,12 +56,23 @@ contract Oracle is Verifier { } /// @dev Only could be called by owner. - /// @notice Each channel has a corresponding oracle, and the message root should match with it. /// @param chainId The source chain id. + /// @param channel The message channel. /// @param msgIndex The source chain message index. /// @param msgHash The source chain message hash corresponding to the channel. - function importMessageRoot(uint256 chainId, uint256 msgIndex, bytes32 msgHash) external onlyOwner { - IORMP(PROTOCOL).importHash(chainId, bytes32(msgIndex), msgHash); + function importMessageHash(uint256 chainId, address channel, uint256 msgIndex, bytes32 msgHash) + external + onlyOwner + { + IORMP(PROTOCOL).importHash(_lookupkey(chainId, channel, msgIndex), msgHash); + } + + function hashOf(uint256 chainId, address channel, uint256 msgIndex) public view override returns (bytes32) { + return IORMP(PROTOCOL).hashLookup(address(this), _lookupkey(chainId, channel, msgIndex)); + } + + function _lookupkey(uint256 chainId, address channel, uint256 msgIndex) internal pure returns (bytes32) { + return keccak256(abi.encode(chainId, channel, msgIndex)); } function changeOwner(address newOwner) external onlyOwner { @@ -95,8 +106,4 @@ contract Oracle is Verifier { require(f != 0, "!fee"); return f; } - - function hashOf(uint256 chainId, uint256 msgIndex) public view override returns (bytes32) { - return IORMP(PROTOCOL).hashLookup(address(this), chainId, bytes32(msgIndex)); - } } diff --git a/src/interfaces/IORMP.sol b/src/interfaces/IORMP.sol index c61fb54..654bb91 100644 --- a/src/interfaces/IORMP.sol +++ b/src/interfaces/IORMP.sol @@ -77,15 +77,13 @@ interface IORMP { /// @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; + function importHash(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); + function hashLookup(address oracle, bytes32 lookupKey) external view returns (bytes32); } diff --git a/test/Channel.t.sol b/test/Channel.t.sol index 010e613..0006def 100644 --- a/test/Channel.t.sol +++ b/test/Channel.t.sol @@ -88,7 +88,7 @@ contract ChannelTest is Test, Verifier { } } - function hashOf(uint256, uint256) public view override returns (bytes32) { + function hashOf(uint256, address, uint256) public view override returns (bytes32) { return bytes32(0); } } diff --git a/test/ORMP.m.sol b/test/ORMP.m.sol index fd48bc3..c0be4fe 100644 --- a/test/ORMP.m.sol +++ b/test/ORMP.m.sol @@ -56,12 +56,12 @@ contract ORMPMock is Verifier { function dryrun_recv(bytes memory input) public { require(caller == msg.sender, "!auth"); - Verifier(oracle).hashOf(46, 0); + Verifier(oracle).hashOf(46, self, 0); P memory p = abi.decode(input, (P)); ormp.recv(p.message, p.proof); } - function hashOf(uint256, uint256) public pure override returns (bytes32) { + function hashOf(uint256, address, uint256) public pure override returns (bytes32) { return 0x3871fec397ebd8b84e7780742d8c7a0649097aa54870c8b7e1d5cb027480aad2; } } diff --git a/test/ORMP.t.sol b/test/ORMP.t.sol index fa233ac..8d1d2e0 100644 --- a/test/ORMP.t.sol +++ b/test/ORMP.t.sol @@ -110,7 +110,7 @@ contract ORMPTest is Test, Verifier { return 1; } - function hashOf(uint256, uint256) public view override returns (bytes32) { + function hashOf(uint256, address, uint256) public view override returns (bytes32) { return bytes32(0); } } diff --git a/test/bench/ORMP.b.sol b/test/bench/ORMP.b.sol index 7aa2fe2..7e69457 100644 --- a/test/bench/ORMP.b.sol +++ b/test/bench/ORMP.b.sol @@ -75,7 +75,7 @@ contract ORMPBenchmarkTest is Test { vm.store(address(oracle), bytes32(uint256(0)), bytes32(uint256(uint160(self)))); assertEq(oracle.owner(), self); vm.prank(address(oracle.owner())); - oracle.importMessageRoot(message.fromChainId, blockNumber, root); + oracle.importMessageHash(message.fromChainId, self, blockNumber, root); vm.prank(address(relayer)); ormp.recv(message, ""); diff --git a/test/eco/Oracle.t.sol b/test/eco/Oracle.t.sol index 70047b6..3174e02 100644 --- a/test/eco/Oracle.t.sol +++ b/test/eco/Oracle.t.sol @@ -67,7 +67,7 @@ contract OracleTest is Test { } function test_hashOf() public { - bytes32 r = oracle.hashOf(1, 1); + bytes32 r = oracle.hashOf(1, self, 1); assertEq(r, bytes32(uint256(1))); }