matter-labs / foundry-zksync

Fork of Foundry tailored for zkSync environment
Apache License 2.0
299 stars 130 forks source link

feat: add cheatcode to skip zkEVM execution #569

Closed nbaztec closed 2 months ago

nbaztec commented 2 months ago

Motivation

In certain tests the actual test is run in a factory or helper contract. However, since we execute all CREATEs and CALLs on zkEVM, cheatcodes are no longer accessible within these factories.

contract Helper is Test {
    event Added(uint8 indexed sum);

    function exec() public {
        vm.expectEmit();
        emit Added(3);

        Calculator calc = new Calculator();
        uint8 sum = calc.add(1, 2);
        assertEq(3, sum);
    }
}

contract ZkCheatcodeZkVmSkipTest is Test {
    Helper helper;

    function setUp() external {
        helper = new Helper();
        vm.allowCheatcodes(address(helper));
        vm.makePersistent(address(helper));
    }

    function testHelper() external {
        helper.exec();
    }
}

Solution

Adds a cheatcode vm.zkVmSkip() that skips the next CREATE or CALL, and executes it on the EVM instead. This effectively delays the zkEVM execution to the next stack depth.

Additionally, for ease of use, contracts deployed in skip mode are recorded and all calls to them is redirected to EVM instead of zkEVM.

Karrq commented 2 months ago

πŸš€ πŸš€ πŸš€