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

chore: Move zk tests to different files #775

Merged
merged 7 commits into from
Dec 13, 2024
138 changes: 1 addition & 137 deletions crates/cast/tests/cli/main.rs

Large diffs are not rendered by default.

137 changes: 137 additions & 0 deletions crates/cast/tests/cli/zk.rs

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions crates/forge/tests/cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::utils::generate_large_contract;
use foundry_config::Config;
use foundry_test_utils::{forgetest, snapbox::IntoData, str, util::OutputExt};
use foundry_test_utils::{forgetest, snapbox::IntoData, str};
use globset::Glob;
use regex::Regex;

forgetest_init!(can_parse_build_filters, |prj, cmd| {
prj.clear();
Expand Down Expand Up @@ -166,17 +165,6 @@ forgetest_init!(build_sizes_no_forge_std, |prj, cmd| {
);
});

// tests build output is as expected in zksync mode
forgetest_init!(test_zk_build_sizes, |prj, cmd| {
cmd.args(["build", "--sizes", "--zksync", "--evm-version", "shanghai"]);
let stdout = cmd.assert_success().get_output().stdout_lossy();
let pattern =
Regex::new(r"\|\s*Counter\s*\|\s*800\s*\|\s*800\s*\|\s*450,199\s*\|\s*450,199\s*\|")
.unwrap();

assert!(pattern.is_match(&stdout), "Unexpected size output:\n{stdout}");
});

// tests that skip key in config can be used to skip non-compilable contract
forgetest_init!(test_can_skip_contract, |prj, cmd| {
prj.add_source(
Expand Down
72 changes: 0 additions & 72 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2676,78 +2676,6 @@ Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
);
});

forgetest!(zk_gas_report, |prj, cmd| {
prj.insert_ds_test();
prj.add_source(
"Contracts.sol",
r#"
//SPDX-license-identifier: MIT

import "./test.sol";

contract ContractOne {
int public i;

constructor() {
i = 0;
}

function foo() public{
while(i<5){
i++;
}
}
}

contract ContractOneTest is DSTest {
ContractOne c1;

function setUp() public {
c1 = new ContractOne();
}

function testFoo() public {
c1.foo();
}
}
"#,
)
.unwrap();

prj.write_config(Config {
gas_reports: (vec!["*".to_string()]),
gas_reports_ignore: (vec![]),
..Default::default()
});
let out = cmd.arg("test").arg("--gas-report").assert_success().get_output().stdout_lossy();
cmd.forge_fuse();
let out_zk = cmd
.arg("test")
.arg("--gas-report")
.arg("--zksync")
.assert_success()
.get_output()
.stdout_lossy();

let mut cells = out.split('|');
let deployment_cost: u64 = cells.nth(22).unwrap().trim().parse().unwrap();
let deployment_size: u64 = cells.next().unwrap().trim().parse().unwrap();
let function = cells.nth(12).unwrap().trim();
let gas: u64 = cells.next().unwrap().trim().parse().unwrap();

let mut cells_zk = out_zk.split('|');
let deployment_cost_zk: u64 = cells_zk.nth(22).unwrap().trim().parse().unwrap();
let deployment_size_zk: u64 = cells_zk.next().unwrap().trim().parse().unwrap();
let function_zk = cells_zk.nth(12).unwrap().trim();
let gas_zk: u64 = cells_zk.next().unwrap().trim().parse().unwrap();

assert!(deployment_cost_zk > deployment_cost);
assert!(deployment_size_zk > deployment_size);
assert!(gas_zk > gas);
assert_eq!(function, "foo");
assert_eq!(function_zk, "foo");
});

forgetest_init!(can_use_absolute_imports, |prj, cmd| {
let remapping = prj.paths().libraries[0].join("myDependency");
let config = Config {
Expand Down
3 changes: 3 additions & 0 deletions crates/forge/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ mod verify;
mod verify_bytecode;

mod ext_integration;
mod zk_build;
mod zk_cmd;
mod zk_script;
110 changes: 1 addition & 109 deletions crates/forge/tests/cli/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anvil::{spawn, NodeConfig};
use forge_script_sequence::ScriptSequence;
use foundry_test_utils::{
rpc,
util::{OutputExt, OTHER_SOLC_VERSION, SOLC_VERSION},
util::{OTHER_SOLC_VERSION, SOLC_VERSION},
ScriptOutcome, ScriptTester,
};
use regex::Regex;
Expand Down Expand Up @@ -2037,114 +2037,6 @@ forgetest_async!(can_deploy_library_create2_different_sender, |prj, cmd| {
.await;
});

forgetest_async!(test_zk_can_execute_script_with_arguments, |prj, cmd| {
#[derive(serde::Deserialize, Debug)]
#[allow(dead_code)]
struct ZkTransactions {
transactions: Vec<ZkTransaction>,
}

#[derive(serde::Deserialize, Debug)]
#[allow(dead_code)]
struct ZkTransaction {
zk: Zk,
}

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
struct Zk {
#[serde(default)]
factory_deps: Vec<Vec<u8>>,
}

let node = foundry_test_utils::ZkSyncNode::start();

cmd.args(["init", "--force"]).arg(prj.root());
cmd.assert_success();
cmd.forge_fuse();

prj.add_script(
"Deploy.s.sol",
r#"
pragma solidity ^0.8.18;

import {Script} from "forge-std/Script.sol";

contract Greeter {
string name;
uint256 age;

event Greet(string greet);

function greeting(string memory _name) public returns (string memory) {
name = _name;
string memory greet = string(abi.encodePacked("Hello ", _name));
emit Greet(greet);
return greet;
}

function setAge(uint256 _age) public {
age = _age;
}

function getAge() public view returns (uint256) {
return age;
}
}

contract DeployScript is Script {
Greeter greeter;
string greeting;

function run() external {
// test is using old Vm.sol interface, so we call manually
address(vm).call(abi.encodeWithSignature("zkVm(bool)", true));

vm.startBroadcast();
greeter = new Greeter();
greeter.greeting("john");
greeter.setAge(123);
vm.stopBroadcast();
}
}
"#,
)
.unwrap();

cmd.arg("script").args([
"--zksync",
"DeployScript",
"--broadcast",
"--private-key",
"0x3d3cbc973389cb26f657686445bcc75662b415b656078503592ac8c1abb8810e",
"--chain",
"260",
"--gas-estimate-multiplier",
"310",
"--rpc-url",
node.url().as_str(),
"--slow",
"--evm-version",
"shanghai",
]);

cmd.assert_success()
.get_output()
.stdout_lossy()
.contains("ONCHAIN EXECUTION COMPLETE & SUCCESSFUL");

let run_latest = foundry_common::fs::json_files(prj.root().join("broadcast").as_path())
.find(|file| file.ends_with("run-latest.json"))
.expect("No broadcast artifacts");

let content = foundry_common::fs::read_to_string(run_latest).unwrap();

let transactions: ZkTransactions = serde_json::from_str(&content).unwrap();
let transactions = transactions.transactions;
assert_eq!(transactions.len(), 3);
});

// <https://github.com/foundry-rs/foundry/issues/8993>
forgetest_async!(test_broadcast_raw_create2_deployer, |prj, cmd| {
let (_api, handle) =
Expand Down
34 changes: 0 additions & 34 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,40 +1339,6 @@ Traces:
]]);
});

// Related to: https://github.com/matter-labs/foundry-zksync/issues/478
forgetest_async!(test_zk_can_detect_call_to_empty_contract, |prj, cmd| {
foundry_test_utils::util::initialize(prj.root());

prj.add_test(
"CallEmptyCode.t.sol",
r#"
import "forge-std/Test.sol";

// https://github.com/matter-labs/foundry-zksync/issues/478
contract CallEmptyCode is Test {
// This test should make our EraVM tracer print out an ERROR trace
function testFailDetectEmptyCodeContracts() external {
address mockMe = address(123456789);

vm.mockCall(mockMe, abi.encodeWithSignature("foo()"), abi.encode(42));

(bool success, bytes memory ret) = mockMe.call(abi.encodeWithSignature("bar()"));

require(success, "callMethod failed");
require(keccak256(ret) == keccak256(abi.encode(42)), "return not as expected");
}
}
"#,
)
.unwrap();
cmd.args(["test", "--zksync", "--evm-version", "shanghai", "--mc", "CallEmptyCode"]);

cmd.assert_success()
.get_output()
.stdout_lossy()
.contains("call may fail or behave unexpectedly due to empty code");
});

// tests that `forge test` with a seed produces deterministic random values for uint and addresses.
forgetest_init!(deterministic_randomness_with_seed, |prj, cmd| {
prj.wipe_contracts();
Expand Down
13 changes: 13 additions & 0 deletions crates/forge/tests/cli/zk_build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use foundry_test_utils::util::OutputExt;
use regex::Regex;

// tests build output is as expected in zksync mode
forgetest_init!(test_zk_build_sizes, |prj, cmd| {
cmd.args(["build", "--sizes", "--zksync", "--evm-version", "shanghai"]);
let stdout = cmd.assert_success().get_output().stdout_lossy();
let pattern =
Regex::new(r"\|\s*Counter\s*\|\s*800\s*\|\s*800\s*\|\s*450,199\s*\|\s*450,199\s*\|")
.unwrap();

assert!(pattern.is_match(&stdout), "Unexpected size output:\n{stdout}");
});
Loading
Loading