Skip to content

Commit

Permalink
change hash lookup key
Browse files Browse the repository at this point in the history
  • Loading branch information
hujw77 committed Apr 17, 2024
1 parent 6b4ec5b commit 65660d3
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 26 deletions.
13 changes: 6 additions & 7 deletions src/ORMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions src/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 14 additions & 7 deletions src/eco/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
}
6 changes: 2 additions & 4 deletions src/interfaces/IORMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion test/Channel.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/ORMP.m.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion test/ORMP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion test/bench/ORMP.b.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand Down
2 changes: 1 addition & 1 deletion test/eco/Oracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down

0 comments on commit 65660d3

Please sign in to comment.