Skip to content

Commit 1f3a521

Browse files
feat(blockifier): recompile Cairo1 in the CI
1 parent e6f7a3f commit 1f3a521

File tree

3 files changed

+79
-31
lines changed

3 files changed

+79
-31
lines changed

.github/workflows/blockifier_compiled_cairo.yml

+49-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ on:
44
push:
55
branches:
66
- main
7+
- main-v[0-9].**
78
tags:
89
- v[0-9].**
910
paths:
10-
- 'crates/blockifier/feature_contracts/cairo0/**'
11+
- 'crates/blockifier/feature_contracts/**'
12+
- 'crates/blockifier/tests/**'
1113
pull_request:
1214
types:
1315
- opened
1416
- reopened
1517
- synchronize
1618
paths:
17-
- 'crates/blockifier/feature_contracts/cairo0/**'
19+
- 'crates/blockifier/feature_contracts/**'
20+
- 'crates/blockifier/tests/**'
1821

1922
jobs:
2023
verify_cairo_file_dependencies:
@@ -36,6 +39,48 @@ jobs:
3639
LD_LIBRARY_PATH: ${{ env.Python3_ROOT_DIR }}/bin
3740
run: echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" >> $GITHUB_ENV
3841

42+
# Checkout sequencer into a dedicated directory - technical requirement in order to be able to checkout `cairo` in a sibling directory.
43+
- name: checkout sequencer into `sequencer` directory.
44+
uses: actions/checkout@v4
45+
with:
46+
repository: 'starkware-libs/sequencer'
47+
path: 'sequencer'
48+
49+
- name: Read Cairo1 Tag to compile contracts with from Blockifier.
50+
id: read-tag
51+
run: echo "TAG=$(cat sequencer/crates/blockifier/tests/cairo1_compiler_tag.txt)" >> $GITHUB_ENV
52+
53+
- name: Read legacy Cairo1 Tag to compile the legacy contract with.
54+
id: read-legacy-tag
55+
run: echo "LEGACY_TAG=$(cat sequencer/crates/blockifier/tests/legacy_cairo1_compiler_tag.txt)" >> $GITHUB_ENV
56+
57+
- name: checkout cairo1 repo in order to compile cairo1 contracts.
58+
uses: actions/checkout@v4
59+
with:
60+
repository: 'starkware-libs/cairo'
61+
fetch-tags: tags
62+
ref: ${{ env.TAG }}
63+
path: 'cairo'
64+
65+
- run:
66+
cd sequencer &&
67+
pip install -r crates/blockifier/tests/requirements.txt &&
68+
cargo test -p blockifier --test feature_contracts_compatibility_test --features testing -- --include-ignored
69+
70+
# Legacy contract verification.
71+
- name: checkout legacy tag of cairo1 repo in order to compile the legacy contract.
72+
uses: actions/checkout@v4
73+
with:
74+
repository: 'starkware-libs/cairo'
75+
fetch-tags: tags
76+
ref: ${{ env.LEGACY_TAG }}
77+
path: 'cairo'
78+
79+
- name: Verify the legacy contract.
80+
uses: actions-rs/toolchain@master
81+
with:
82+
toolchain: nightly-2023-07-05
83+
- uses: Swatinem/rust-cache@v2
3984
- run:
40-
pip install -r crates/blockifier/tests/requirements.txt;
41-
cargo test verify_feature_contracts -- --include-ignored
85+
cd sequencer &&
86+
LEGACY=1 cargo test -p blockifier --test feature_contracts_compatibility_test --features testing -- --include-ignored

crates/blockifier/src/test_utils/cairo_compile.rs

-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ fn verify_cairo1_compiler_deps(git_tag_override: Option<String>) {
180180
// Checkout the required version in the compiler repo.
181181
run_and_verify_output(Command::new("git").args([
182182
"-C",
183-
// TODO(Dori, 1/6/2024): Handle CI case (repo path will be different).
184183
cairo_repo_path.to_str().unwrap(),
185184
"checkout",
186185
&tag,

crates/blockifier/tests/feature_contracts_compatibility_test.rs

+30-26
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,24 @@ const FIX_COMMAND: &str = "FIX_FEATURE_TEST=1 cargo test -- --ignored";
1919
// ```
2020
// Then, run the FIX_COMMAND above.
2121

22-
// This test currently doesn't support Cairo1 contracts. To fix them you'll need to compile them one
23-
// by one:
24-
// 1. Clone the [cairo repo](https://github.com/starkware-libs/cairo).
25-
// 2. Checkout the commit defined in [the root Cargo.toml](../../../../Cargo.toml).
26-
// 3. From within the compiler repo root directory, run:
27-
// ```
28-
// PREFIX=~/workspace/blockifier/crates/blockifier/feature_contracts/cairo1
29-
// CONTRACT_NAME=<contract_base_filename>
30-
// cargo run --release --bin starknet-compile -- --single-file \
31-
// $PREFIX/$CONTRACT_NAME.cairo \
32-
// $PREFIX/compiled/$CONTRACT_NAME.sierra.json
33-
// cargo run --release --bin starknet-sierra-compile \
34-
// $PREFIX/compiled/$CONTRACT_NAME.sierra.json \
35-
// $PREFIX/compiled/$CONTRACT_NAME.casm.json
36-
// ```
37-
// TODO(Gilad, 1/1/2024): New year's resolution: support Cairo1 in the test.
22+
// To test Cairo1 feature contracts, first clone the Cairo repo and checkout the required tag.
23+
// The repo should be located next to the sequencer repo:
24+
// <WORKSPACE_DIR>/
25+
// - sequencer/
26+
// - cairo/
27+
// Then, run the FIX_COMMAND above.
3828

3929
// Checks that:
4030
// 1. `TEST_CONTRACTS` dir exists and contains only `.cairo` files and the subdirectory
4131
// `COMPILED_CONTRACTS_SUBDIR`.
4232
// 2. for each `X.cairo` file in `TEST_CONTRACTS` there exists an `X_compiled.json` file in
4333
// `COMPILED_CONTRACTS_SUBDIR` which equals `starknet-compile-deprecated X.cairo --no_debug_info`.
44-
fn verify_feature_contracts_compatibility(fix: bool, cairo_version: CairoVersion) {
34+
fn verify_feature_contracts_compatibility(
35+
fix: bool,
36+
contracts_to_test: impl Iterator<Item = FeatureContract>,
37+
) {
4538
// TODO(Dori, 1/10/2024): Parallelize this test.
46-
for contract in FeatureContract::all_feature_contracts()
47-
.filter(|contract| contract.cairo_version() == cairo_version)
48-
{
39+
for contract in contracts_to_test {
4940
// Compare output of cairo-file on file with existing compiled file.
5041
let expected_compiled_output = contract.compile();
5142
let existing_compiled_path = contract.get_compiled_path();
@@ -136,15 +127,28 @@ fn verify_feature_contracts_match_enum() {
136127
assert_eq!(compiled_paths_from_enum, compiled_paths_on_filesystem);
137128
}
138129

130+
fn is_env_var_set(env_var: &str) -> bool {
131+
std::env::var(env_var).is_ok()
132+
}
133+
139134
#[rstest]
140135
#[ignore]
141136
fn verify_feature_contracts(
142137
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
143138
) {
144-
// TODO(Dori, 1/9/2024): Support Cairo1 contracts in the CI and remove this `if` statement.
145-
if std::env::var("CI").is_ok() && matches!(cairo_version, CairoVersion::Cairo1) {
146-
return;
147-
}
148-
let fix_features = std::env::var("FIX_FEATURE_TEST").is_ok();
149-
verify_feature_contracts_compatibility(fix_features, cairo_version)
139+
let fix_features = is_env_var_set("FIX_FEATURE_TEST");
140+
let legacy_mode = is_env_var_set("LEGACY");
141+
let ci_mode = is_env_var_set("CI");
142+
143+
let contracts_to_test = FeatureContract::all_feature_contracts().filter(|contract| {
144+
// If called with LEGACY environment variable set, only test legacy contracts (from CI
145+
// or not).
146+
// If tested from the CI *without* the LEGACY environment variable set, test only
147+
// non-legacy contracts of the respective cairo version.
148+
// If not tested from CI, test all contracts of the requested cairo version.
149+
contract.cairo_version() == cairo_version
150+
&& (if legacy_mode { contract.is_legacy() } else { !ci_mode || !contract.is_legacy() })
151+
});
152+
153+
verify_feature_contracts_compatibility(fix_features, contracts_to_test)
150154
}

0 commit comments

Comments
 (0)