Skip to content

Commit

Permalink
feat(evm): adds Yaru tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed Nov 7, 2023
1 parent 6cea859 commit f6a3fc8
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 169 deletions.
1 change: 1 addition & 0 deletions packages/evm/contracts/Yaho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ contract Yaho is IYaho, MessageHashCalculator, MessageIdCalculator {
function _dispatchMessage(uint256 toChainId, address to, bytes calldata data) internal returns (bytes32 messageId) {
bool isHeaderReporter = msg.sender == headerReporter;
address from = isHeaderReporter ? address(0) : msg.sender;
// NOTE: in case of isHeaderReporter = true -> to = address(0)
Message memory message = Message(block.chainid, toChainId, from, to, data);
bytes32 messageHash = calculateMessageHash(message, address(this));
bytes32 salt = keccak256(
Expand Down
5 changes: 3 additions & 2 deletions packages/evm/contracts/Yaru.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract Yaru is IYaru, MessageHashCalculator, MessageIdCalculator, ReentrancyGu
error AlreadyInitialized(uint256 chainId);

/// @param hashi_ Address of the Hashi contract.
/// @param headerVault_ Address of the HeaderVault contract.
constructor(address hashi_, address headerVault_) {
hashi = hashi_;
headerVault = headerVault_;
Expand All @@ -48,15 +49,15 @@ contract Yaru is IYaru, MessageHashCalculator, MessageIdCalculator, ReentrancyGu
executed[messageId] = true;

bytes32 reportedHash = IHashi(hashi).getHash(message.fromChainId, messageId, oracleAdapters);
if (reportedHash != messageHash) revert MessageFailure(messageId, abi.encode(reportedHash, messageHash));
if (reportedHash != messageHash) revert MessageFailure(messageId, abi.encode(keccak256("HashMismatch")));

address to = message.from == address(0) && message.to == address(0) ? headerVault : message.to;
try IJushinki(to).onMessage(message.data, messageId, message.fromChainId, message.from) returns (
bytes memory returnData
) {
returnDatas[i] = returnData;
} catch {
revert MessageFailure(messageId, abi.encode(0));
revert MessageFailure(messageId, abi.encode(keccak256("CallFailed")));
}

emit MessageIdExecuted(message.fromChainId, messageId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ contract HeaderReporter is IHeaderReporter {
/// @param blockNumbers Uint256 array of block number.
/// @param yaho Address of the Yaho contract to call.
/// @param toChainIds The destination chain ids.
/// @param adapters Array of relay adapter addresses to which hashes should be relayed.
/// @param destinationAdapters Array of oracle adapter addresses to receive hashes.
/// @param messageRelays Array of relay addresses to which hashes should be relayed.
/// @param adapters Array of oracle adapter addresses to receive hashes.
function reportHeaders(
uint256[] calldata blockNumbers,
uint256[] calldata toChainIds,
address[] calldata messageRelays,
address[] calldata adapters,
address[] calldata destinationAdapters,
address yaho
) external {
bytes32[] memory blockHeaders = IHeaderStorage(headerStorage).storeBlockHeaders(blockNumbers);
Expand All @@ -36,7 +36,7 @@ contract HeaderReporter is IHeaderReporter {
}
}

IYaho(yaho).dispatchMessagesToAdapters(toChainIds, tos, data, adapters, destinationAdapters);
IYaho(yaho).dispatchMessagesToAdapters(toChainIds, tos, data, messageRelays, adapters);

for (uint256 i = 0; i < blockNumbers.length; ) {
emit HeaderReported(toChainIds[i], blockNumbers[i], blockHeaders[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { IHeaderVault } from "../interfaces/IHeaderVault.sol";

contract HeaderVault is IHeaderVault, Ownable {
mapping(uint256 => mapping(uint256 => bytes32)) private _blockHeaders;
mapping(uint256 => address) _headerReporters;
address public yaru;

error NotYaru(address currentYaru, address expectedYaru);
Expand All @@ -16,28 +15,16 @@ contract HeaderVault is IHeaderVault, Ownable {

modifier onlyYaruAndOnlyHeaderReporter(uint256 fromChainId, address from) {
if (msg.sender != yaru) revert NotYaru(msg.sender, yaru);
address expectedHeaderReporter = _headerReporters[fromChainId];
if (expectedHeaderReporter != from) revert InvalidHeaderReporter(fromChainId, from, expectedHeaderReporter);
if (from != address(0)) revert InvalidHeaderReporter(fromChainId, from, address(0));
_;
}

function disableBlockHeaderReporter(uint256 fromChainId) external onlyOwner {
address headerReporter = _headerReporters[fromChainId];
delete _headerReporters[fromChainId];
emit HeaderReporterDisabled(fromChainId, headerReporter);
}

function enableBlockHeaderReporter(uint256 fromChainId, address headerReporter) external onlyOwner {
_headerReporters[fromChainId] = headerReporter;
emit HeaderReporterEnabled(fromChainId, headerReporter);
}

function getBlockHeader(uint256 chainId, uint256 blockNumber) external view returns (bytes32) {
return _blockHeaders[chainId][blockNumber];
}

function initializeYaru(address yaru_) external onlyOwner {
if (yaru == address(0)) revert YaruAlreadyInitialized(yaru);
if (yaru != address(0)) revert YaruAlreadyInitialized(yaru);
yaru = yaru_;
}

Expand All @@ -55,6 +42,9 @@ contract HeaderVault is IHeaderVault, Ownable {
bytes32 blockHeader = blockHeaders[i];
_blockHeaders[fromChainId][blockNumber] = blockHeader;
emit NewBlock(fromChainId, blockNumber, blockHeader);
unchecked {
++i;
}
}

// TODO: what to return?
Expand Down
4 changes: 0 additions & 4 deletions packages/evm/contracts/interfaces/IHeaderVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ interface IHeaderVault is IJushinki {
event HeaderReporterEnabled(uint256 fromChainId, address headerReporter);
event HeaderReporterDisabled(uint256 fromChainId, address headerReporter);

function disableBlockHeaderReporter(uint256 fromChainId) external;

function enableBlockHeaderReporter(uint256 fromChainId, address headerReporter) external;

function initializeYaru(address yaru_) external;

function getBlockHeader(uint256 chainId, uint256 blockNumber) external view returns (bytes32);
Expand Down
11 changes: 7 additions & 4 deletions packages/evm/contracts/test/PingPong.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ pragma solidity ^0.8.17;
import { IJushinki } from "../interfaces/IJushinki.sol";

contract PingPong is IJushinki {
event Pong(string pong);

uint256 public count;

event Pong(uint256 count);

error PongError();

function ping() public returns (string memory pong) {
count++;
pong = "pong";
emit Pong(pong);
emit Pong(count);
}

function onMessage(bytes calldata, bytes32, uint256, address) external returns (bytes memory) {
function onMessage(bytes calldata data, bytes32, uint256, address) external returns (bytes memory) {
if (uint256(bytes32(data)) == 0) revert PongError();
return abi.encode(ping());
}
}
Loading

0 comments on commit f6a3fc8

Please sign in to comment.