From 22d27642024fb7f9a1b685c1264e0df14ab14291 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Tue, 7 May 2024 13:11:03 +0300 Subject: [PATCH] ci: support Cairo1 recompile in CI Signed-off-by: Dori Medini --- .github/workflows/compiled_cairo.yml | 54 +++++++++++++++++-- .../src/test_utils/cairo_compile.rs | 1 - crates/blockifier/src/test_utils/contracts.rs | 10 ++-- .../feature_contracts_compatibility_test.rs | 52 +++++++++--------- 4 files changed, 83 insertions(+), 34 deletions(-) diff --git a/.github/workflows/compiled_cairo.yml b/.github/workflows/compiled_cairo.yml index 747c6349cf..4e8961d6c1 100644 --- a/.github/workflows/compiled_cairo.yml +++ b/.github/workflows/compiled_cairo.yml @@ -1,4 +1,4 @@ -name: CI +name: Verify Cairo File Dependencies on: push: @@ -14,13 +14,40 @@ on: - synchronize paths: - 'crates/blockifier/feature_contracts/cairo0/**' + - 'crates/blockifier/feature_contracts/cairo1/**' + - 'crates/blockifier/tests/feature_contracts_compatibility_test.rs' jobs: verify_cairo_file_dependencies: runs-on: ubuntu-latest + steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@master + # Checkout blockifier into a dedicated directory - technical requirement in order to be able to checkout `cairo` in a sibling directory. + - name: checkout blockifier into `blockifier` directory. + uses: actions/checkout@v4 + with: + repository: 'starkware-libs/blockifier' + path: 'blockifier' + + - name: Read Cairo1 Tag to compile contracts with from Blockifier. + id: read-tag + # GITHUB_ENV is a variable github allocates for dynamic stuff inside workflow like our usage, name not customizable. + run: echo "TAG=$(cat blockifier/crates/blockifier/tests/cairo1_compiler_tag.txt)" >> $GITHUB_ENV + + - name: Read legacy Cairo1 Tag to compile the legacy contract with. + id: read-legacy-tag + # GITHUB_ENV is a variable github allocates for dynamic stuff inside workflow like our usage, name not customizable. + run: echo "LEGACY_TAG=$(cat blockifier/crates/blockifier/tests/legacy_cairo1_compiler_tag.txt)" >> $GITHUB_ENV + + - name: checkout cairo1 repo in order to compile cairo1 contracts. + uses: actions/checkout@v4 + with: + repository: 'starkware-libs/cairo' + fetch-tags: tags + ref: ${{ env.TAG }} + path: 'cairo' + + - uses: actions-rs/toolchain@master with: components: rustfmt toolchain: nightly-2024-01-12 @@ -30,5 +57,24 @@ jobs: python-version: '3.9' cache: 'pip' - run: - pip install -r crates/blockifier/tests/requirements.txt; + cd blockifier && + pip install -r crates/blockifier/tests/requirements.txt && cargo test verify_feature_contracts -- --include-ignored + + - name: checkout legacy tag of cairo1 repo in order to compile the legacy contract. + uses: actions/checkout@v4 + with: + repository: 'starkware-libs/cairo' + fetch-tags: tags + ref: ${{ env.LEGACY_TAG }} + path: 'cairo' + + - name: Verify the legacy contract. + uses: actions-rs/toolchain@master + with: + components: rustfmt + toolchain: nightly-2024-01-12 + - uses: Swatinem/rust-cache@v2 + - run: + cd blockifier && + LEGACY=1 cargo test verify_feature_contracts -- --include-ignored diff --git a/crates/blockifier/src/test_utils/cairo_compile.rs b/crates/blockifier/src/test_utils/cairo_compile.rs index be66c009a7..2fee3815b6 100644 --- a/crates/blockifier/src/test_utils/cairo_compile.rs +++ b/crates/blockifier/src/test_utils/cairo_compile.rs @@ -169,7 +169,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/src/test_utils/contracts.rs b/crates/blockifier/src/test_utils/contracts.rs index 30de54d111..77f975858d 100644 --- a/crates/blockifier/src/test_utils/contracts.rs +++ b/crates/blockifier/src/test_utils/contracts.rs @@ -105,6 +105,10 @@ impl FeatureContract { } } + pub fn is_legacy(&self) -> bool { + matches!(self, Self::LegacyTestContract) + } + /// Unique integer representing each unique contract. Used to derive "class hash" and "address". fn get_integer_base(self) -> u32 { self.get_cairo_version_bit() @@ -191,10 +195,8 @@ impl FeatureContract { cairo0_compile(self.get_source_path(), extra_arg, false) } CairoVersion::Cairo1 => { - let tag_override = match self { - Self::LegacyTestContract => Some(LEGACY_CONTRACT_COMPILER_TAG.into()), - _ => None, - }; + let tag_override = + if self.is_legacy() { Some(LEGACY_CONTRACT_COMPILER_TAG.into()) } else { None }; cairo1_compile(self.get_source_path(), tag_override) } } diff --git a/crates/blockifier/tests/feature_contracts_compatibility_test.rs b/crates/blockifier/tests/feature_contracts_compatibility_test.rs index 34546d3572..98de58bc4b 100644 --- a/crates/blockifier/tests/feature_contracts_compatibility_test.rs +++ b/crates/blockifier/tests/feature_contracts_compatibility_test.rs @@ -19,32 +19,23 @@ 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 blockifier repo: +// / +// - blockifier/ +// - 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) { - for contract in FeatureContract::all_feature_contracts() - .filter(|contract| contract.cairo_version() == cairo_version) - { +fn verify_feature_contracts_compatibility( + fix: bool, + contracts_to_test: impl Iterator, +) { + 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(); @@ -140,10 +131,21 @@ fn verify_feature_contracts_match_enum() { fn verify_feature_contracts( #[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion, ) { - if std::env::var("CI").is_ok() && matches!(cairo_version, CairoVersion::Cairo1) { - // TODO(Dori, 1/6/2024): Support Cairo1 contracts in the CI and remove this `if` statement. - return; - } let fix_features = std::env::var("FIX_FEATURE_TEST").is_ok(); - verify_feature_contracts_compatibility(fix_features, cairo_version) + + 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 std::env::var("LEGACY").is_ok() { + contract.is_legacy() + } else { + !std::env::var("CI").is_ok() || !contract.is_legacy() + }) + }); + + verify_feature_contracts_compatibility(fix_features, contracts_to_test) }