From 4c211a6016ec51040611a070e058a85d4e94f296 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Tue, 30 Jul 2024 12:13:26 +0300 Subject: [PATCH] feat(blockifier): recompile Cairo1 in the CI --- .../workflows/blockifier_compiled_cairo.yml | 40 ++++++++++-- .../src/test_utils/cairo_compile.rs | 1 - .../feature_contracts_compatibility_test.rs | 63 ++++++++++--------- 3 files changed, 71 insertions(+), 33 deletions(-) diff --git a/.github/workflows/blockifier_compiled_cairo.yml b/.github/workflows/blockifier_compiled_cairo.yml index c4111b1118d..cdba1e0360d 100644 --- a/.github/workflows/blockifier_compiled_cairo.yml +++ b/.github/workflows/blockifier_compiled_cairo.yml @@ -4,17 +4,22 @@ on: push: branches: - main + - main-v[0-9].** tags: - v[0-9].** paths: - - 'crates/blockifier/feature_contracts/cairo0/**' + - '.github/workflows/blockifier_compiled_cairo.yml' + - 'crates/blockifier/feature_contracts/**' + - 'crates/blockifier/tests/**' pull_request: types: - opened - reopened - synchronize paths: - - 'crates/blockifier/feature_contracts/cairo0/**' + - '.github/workflows/blockifier_compiled_cairo.yml' + - 'crates/blockifier/feature_contracts/**' + - 'crates/blockifier/tests/**' jobs: verify_cairo_file_dependencies: @@ -36,6 +41,33 @@ jobs: LD_LIBRARY_PATH: ${{ env.Python3_ROOT_DIR }}/bin run: echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" >> $GITHUB_ENV + # Checkout sequencer into a dedicated directory - technical requirement in order to be able to checkout `cairo` in a sibling directory. + - name: checkout sequencer into `sequencer` directory. + uses: actions/checkout@v4 + with: + repository: 'starkware-libs/sequencer' + path: 'sequencer' + + - name: checkout cairo1 repo in order to compile cairo1 contracts. + uses: actions/checkout@v4 + with: + repository: 'starkware-libs/cairo' + fetch-depth: 0 + fetch-tags: true + path: 'cairo' + + - name: Verify cairo contract recompilation (non-legacy, both cairo versions). + - run: + cd sequencer && + pip install -r crates/blockifier/tests/requirements.txt && + cargo test -p blockifier --test feature_contracts_compatibility_test --features testing -- --include-ignored + + # Legacy contract verification. + - name: Verify the legacy contract. + uses: actions-rs/toolchain@master + with: + toolchain: nightly-2023-07-05 + - uses: Swatinem/rust-cache@v2 - run: - pip install -r crates/blockifier/tests/requirements.txt; - cargo test verify_feature_contracts -- --include-ignored + cd sequencer && + LEGACY=1 cargo test -p blockifier --test feature_contracts_compatibility_test --features testing -- --include-ignored diff --git a/crates/blockifier/src/test_utils/cairo_compile.rs b/crates/blockifier/src/test_utils/cairo_compile.rs index 49f32545054..d6ecb9e6f14 100644 --- a/crates/blockifier/src/test_utils/cairo_compile.rs +++ b/crates/blockifier/src/test_utils/cairo_compile.rs @@ -186,7 +186,6 @@ fn verify_cairo1_compiler_deps(git_tag_override: Option) { // Checkout the required version in the compiler repo. run_and_verify_output(Command::new("git").args([ "-C", - // TODO(Dori, 1/6/2024): Handle CI case (repo path will be different). cairo_repo_path.to_str().unwrap(), "checkout", &tag, diff --git a/crates/blockifier/tests/feature_contracts_compatibility_test.rs b/crates/blockifier/tests/feature_contracts_compatibility_test.rs index bb856a0fc73..c36b3678249 100644 --- a/crates/blockifier/tests/feature_contracts_compatibility_test.rs +++ b/crates/blockifier/tests/feature_contracts_compatibility_test.rs @@ -18,33 +18,24 @@ const FIX_COMMAND: &str = "FIX_FEATURE_TEST=1 cargo test -- --ignored"; // ``` // Then, run the FIX_COMMAND above. -// This test currently doesn't support Cairo1 contracts. To fix them you'll need to compile them one -// by one: -// 1. Clone the [cairo repo](https://github.com/starkware-libs/cairo). -// 2. Checkout the commit defined in [the root Cargo.toml](../../../../Cargo.toml). -// 3. From within the compiler repo root directory, run: -// ``` -// PREFIX=~/workspace/blockifier/crates/blockifier/feature_contracts/cairo1 -// CONTRACT_NAME= -// cargo run --release --bin starknet-compile -- --single-file \ -// $PREFIX/$CONTRACT_NAME.cairo \ -// $PREFIX/compiled/$CONTRACT_NAME.sierra.json -// cargo run --release --bin starknet-sierra-compile \ -// $PREFIX/compiled/$CONTRACT_NAME.sierra.json \ -// $PREFIX/compiled/$CONTRACT_NAME.casm.json -// ``` -// TODO(Gilad, 1/1/2024): New year's resolution: support Cairo1 in the test. +// To test Cairo1 feature contracts, first clone the Cairo repo and checkout the required tag. +// The repo should be located next to the sequencer repo: +// / +// - sequencer/ +// - cairo/ +// Then, run the FIX_COMMAND above. // Checks that: // 1. `TEST_CONTRACTS` dir exists and contains only `.cairo` files and the subdirectory // `COMPILED_CONTRACTS_SUBDIR`. // 2. for each `X.cairo` file in `TEST_CONTRACTS` there exists an `X_compiled.json` file in // `COMPILED_CONTRACTS_SUBDIR` which equals `starknet-compile-deprecated X.cairo --no_debug_info`. -fn verify_feature_contracts_compatibility(fix: bool, cairo_version: CairoVersion) { +fn verify_feature_contracts_compatibility( + fix: bool, + contracts_to_test: impl Iterator, +) { // TODO(Dori, 1/10/2024): Parallelize this test. - for contract in FeatureContract::all_feature_contracts() - .filter(|contract| contract.cairo_version() == cairo_version) - { + for contract in contracts_to_test { // Compare output of cairo-file on file with existing compiled file. let expected_compiled_output = contract.compile(); let existing_compiled_path = contract.get_compiled_path(); @@ -125,17 +116,33 @@ fn verify_feature_contracts_match_enum() { assert_eq!(compiled_paths_from_enum, compiled_paths_on_filesystem); } +/// Tests if the env var is set. If a value is provided, also tests that the env var value is equal +/// to the provided value. +fn is_env_var_set(env_var: &str, expected_value: Option) -> bool { + match std::env::var(env_var) { + Ok(value) => expected_value.map_or(true, |expected_value| value == expected_value), + Err(_) => false, + } +} + #[rstest] #[ignore] fn verify_feature_contracts( #[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion, ) { - // TODO(Dori, 1/9/2024): Support Cairo1 contracts in the CI and remove this `if` statement. - if std::env::var("CI").unwrap_or("false".into()) == "true" - && matches!(cairo_version, CairoVersion::Cairo1) - { - return; - } - let fix_features = std::env::var("FIX_FEATURE_TEST").is_ok(); - verify_feature_contracts_compatibility(fix_features, cairo_version) + let fix_features = is_env_var_set("FIX_FEATURE_TEST", None); + let legacy_mode = is_env_var_set("LEGACY", None); + let ci_mode = is_env_var_set("CI", Some("true".into())); + + let contracts_to_test = FeatureContract::all_feature_contracts().filter(|contract| { + // If called with LEGACY environment variable set, only test legacy contracts (from CI + // or not). + // If tested from the CI *without* the LEGACY environment variable set, test only + // non-legacy contracts of the respective cairo version. + // If not tested from CI, test all contracts of the requested cairo version. + contract.cairo_version() == cairo_version + && (if legacy_mode { contract.is_legacy() } else { !ci_mode || !contract.is_legacy() }) + }); + + verify_feature_contracts_compatibility(fix_features, contracts_to_test) }