Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ZkEvmReceiver #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/ZkEVMReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.0;

interface IZkEVMBridgeMessageReceiver {
function onMessageReceived(address originAddress, uint32 originNetwork, bytes calldata data) external payable;
}

/**
* @title ZkEVMReceiver
* @notice Receive messages to a zkevm chain
*/
abstract contract ZkEVMReceiver is IZkEVMBridgeMessageReceiver {
address public immutable l1Authority;
uint32 public immutable originNetworkId;
address public constant bridge = 0x2a3DD3EB832aF982ec71669E178424b10Dca2EDe;

constructor(address _l1Authority, uint32 _originNetworkId) {
l1Authority = _l1Authority;
originNetworkId = _originNetworkId;
}

modifier onlyCrossChainMessage() {
require(msg.sender == address(this), "Receiver/invalid-sender");
_;
}

function onMessageReceived(address originAddress, uint32 originNetwork, bytes calldata data) external payable virtual {
require(msg.sender == bridge, "Receiver/invalid-sender");
require(originNetwork == originNetworkId, "Receiver/invalid-originNetwork");
require(originAddress == l1Authority, "Receiver/invalid-l1Authority");
(bool success, bytes memory ret) = address(this).call(data);
if (!success) {
assembly {
revert(add(ret, 0x20), mload(ret))
}
}
}
}
58 changes: 44 additions & 14 deletions test/ZkEVMIntegration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,56 @@ pragma solidity >=0.8.0;

import "./IntegrationBase.t.sol";

import { ZkEVMDomain, IBridgeMessageReceiver } from "../src/testing/ZkEVMDomain.sol";

contract ZkevmMessageOrdering is MessageOrdering, IBridgeMessageReceiver {
function onMessageReceived(address /*originAddress*/, uint32 /*originNetwork*/, bytes memory data) external payable {
// call the specific method
(bool success, bytes memory ret) = address(this).call(data);
if (!success) {
assembly {
revert(add(ret, 0x20), mload(ret))
}
}
import { ZkEVMDomain } from "../src/testing/ZkEVMDomain.sol";

import { ZkEVMReceiver } from "../src/ZkEVMReceiver.sol";

contract ZkEVMMessageOrdering is MessageOrdering, ZkEVMReceiver {
constructor(address l1Authority, uint32 networkId) ZkEVMReceiver(l1Authority, networkId) {}

function push(uint256 messageId) public override onlyCrossChainMessage {
super.push(messageId);
}

}

contract ZkEVMIntegrationTest is IntegrationBaseTest {
address l2Authority = makeAddr("l2Authority");
address notL2Authority = makeAddr("notL2Authority");

function test_zkevm() public {
setChain("zkevm", ChainData("ZkEVM", 1101, "https://zkevm-rpc.com"));

checkZkEVMStyle(new ZkEVMDomain(getChain("zkevm"), mainnet));
}

function checkZkEVMStyle(ZkEVMDomain zkevm) public {
function checkZkEVMStyle(ZkEVMDomain zkevm) public {
Domain host = zkevm.hostDomain();

host.selectFork();

ZkevmMessageOrdering moHost = new ZkevmMessageOrdering();
// origin network of the other leg, so 1 for host and 0 for l2
ZkEVMMessageOrdering moHost = new ZkEVMMessageOrdering(l2Authority, 1);

zkevm.selectFork();

ZkevmMessageOrdering moZkevm = new ZkevmMessageOrdering();
ZkEVMMessageOrdering moZkevm = new ZkEVMMessageOrdering(l1Authority, 0);

vm.startPrank(l2Authority);
// Queue up two more L2 -> L1 messages
zkevm.L1_MESSENGER().bridgeMessage(0, address(moHost), true, abi.encodeCall(MessageOrdering.push, (3)));
zkevm.L1_MESSENGER().bridgeMessage(0, address(moHost), true, abi.encodeCall(MessageOrdering.push, (4)));
vm.stopPrank();

assertEq(moZkevm.length(), 0);

host.selectFork();

vm.startPrank(l1Authority);
// Queue up two more L1 -> L2 messages
XChainForwarders.sendMessageZkEVM(address(moZkevm), abi.encodeCall(MessageOrdering.push, (1)));
XChainForwarders.sendMessageZkEVM(address(moZkevm), abi.encodeCall(MessageOrdering.push, (2)));
vm.stopPrank();

assertEq(moHost.length(), 0);

Expand All @@ -62,6 +67,31 @@ contract ZkEVMIntegrationTest is IntegrationBaseTest {
assertEq(moHost.length(), 2);
assertEq(moHost.messages(0), 3);
assertEq(moHost.messages(1), 4);

// Validate the message receiver failure modes
vm.startPrank(notL1Authority);
XChainForwarders.sendMessageZkEVM(address(moZkevm), abi.encodeCall(MessageOrdering.push, (999)));
vm.stopPrank();

vm.expectRevert("Receiver/invalid-l1Authority");
zkevm.relayFromHost(true);
assertEq(moZkevm.length(), 2); // No change

zkevm.selectFork();
vm.expectRevert("Receiver/invalid-sender");
moZkevm.push(999);

// validate message sender is bridge
vm.expectRevert("Receiver/invalid-sender");
moZkevm.onMessageReceived(address(moHost), 0, abi.encodeCall(MessageOrdering.push, (1000)));

// validate origin network is correct
// cannot queue from l1 and relayFromHost because relayFromHost filters
// on the basis of origin network being 1
vm.startPrank(moZkevm.bridge());
vm.expectRevert("Receiver/invalid-originNetwork");
moZkevm.onMessageReceived(address(moZkevm), 2, abi.encodeCall(MessageOrdering.push, (1001)));
vm.stopPrank();
}

}