Skip to content

Commit 8c3f8a6

Browse files
blockifier_test_utils: wire up on-demand Cairo 1 compilation
Made-with: Cursor
1 parent 35a1029 commit 8c3f8a6

File tree

3 files changed

+118
-316
lines changed

3 files changed

+118
-316
lines changed

.github/workflows/blockifier_compiled_cairo.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ on:
88
- synchronize
99
paths:
1010
- ".github/workflows/blockifier_compiled_cairo.yml"
11-
- "crates/apollo_compile_to_casm/src/constants.rs" # Contains the Cairo1 compiler version.
12-
- "crates/apollo_compile_to_casm/src/allowed_libfuncs.json"
1311
- "crates/apollo_infra_utils/src/cairo0_compiler.rs" # Contains the Cairo0 compiler version.
14-
- "crates/blockifier_test_utils/**"
12+
- "crates/blockifier_test_utils/resources/feature_contracts/cairo0/**"
1513
- "scripts/dependencies.sh"
1614

1715
env:
@@ -39,7 +37,7 @@ jobs:
3937
with:
4038
graphite_token: ${{ secrets.GRAPHITE_CI_OPTIMIZER_TOKEN }}
4139

42-
verify_cairo_file_dependencies:
40+
verify_cairo0_compiled_artifacts:
4341
runs-on: namespace-profile-medium-ubuntu-24-04-amd64
4442
needs: optimize_ci
4543
if: needs.optimize_ci.outputs.skip == 'false'
@@ -60,5 +58,5 @@ jobs:
6058
run: echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" >> $GITHUB_ENV
6159
- run: pip install -r crates/blockifier_test_utils/resources/blockifier-test-utils-requirements.txt
6260

63-
- name: Verify cairo contract recompilation (both cairo versions).
64-
run: cargo test -p blockifier_test_utils --test feature_contracts_compatibility_test -- --include-ignored --nocapture
61+
- name: Verify Cairo 0 compiled artifacts match recompilation.
62+
run: cargo test -p blockifier_test_utils --test feature_contracts_compatibility_test -- --include-ignored verify_feature_contracts_cairo0 --nocapture

crates/blockifier_test_utils/src/contracts.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::cairo_compile::{
2222
LibfuncArg,
2323
};
2424
use crate::cairo_versions::{CairoVersion, RunnableCairo1};
25+
use crate::compile_cache;
2526

2627
#[cfg(test)]
2728
#[path = "contracts_test.rs"]
@@ -409,6 +410,16 @@ impl FeatureContract {
409410
get_raw_contract_class(&self.get_compiled_path())
410411
}
411412

413+
/// Ensures that compiled artifacts are available for this contract.
414+
/// For Cairo 1 non-ERC20 contracts, this triggers on-demand compilation with caching.
415+
/// For Cairo 0 and ERC20 contracts (which use committed artifacts), this is a no-op.
416+
fn ensure_compiled(&self) {
417+
if matches!(self, Self::ERC20(_)) || self.cairo_version().is_cairo0() {
418+
return;
419+
}
420+
compile_cache::ensure_cairo1_compiled(self);
421+
}
422+
412423
fn get_cairo_version_bit(&self) -> u32 {
413424
match self.cairo_version() {
414425
CairoVersion::Cairo0 => 0,
@@ -514,17 +525,16 @@ impl FeatureContract {
514525

515526
pub fn get_sierra_path(&self) -> String {
516527
assert_ne!(self.cairo_version(), CairoVersion::Cairo0);
528+
self.ensure_compiled();
517529
if matches!(self, &Self::ERC20(CairoVersion::Cairo1(_))) {
518530
return ERC20_SIERRA_CONTRACT_PATH.to_string();
519531
}
520532

521-
format!(
522-
"{CAIRO1_FEATURE_CONTRACTS_DIR}/{SIERRA_CONTRACTS_SUBDIR}/{}.sierra.json",
523-
self.get_non_erc20_base_name()
524-
)
533+
compile_cache::cached_sierra_path(self).to_string_lossy().to_string()
525534
}
526535

527536
pub fn get_compiled_path(&self) -> String {
537+
self.ensure_compiled();
528538
// ERC20 is a special case - not in the feature_contracts directory.
529539
if let Self::ERC20(cairo_version) = self {
530540
match cairo_version {
@@ -535,23 +545,21 @@ impl FeatureContract {
535545
}
536546
.into()
537547
} else {
538-
let cairo_version = self.cairo_version();
539-
format!(
540-
"resources/feature_contracts/cairo{}/{}{}.json",
541-
match cairo_version {
542-
CairoVersion::Cairo0 => "0/compiled",
543-
CairoVersion::Cairo1(RunnableCairo1::Casm) => "1/compiled",
544-
#[cfg(feature = "cairo_native")]
545-
CairoVersion::Cairo1(RunnableCairo1::Native) => "1/sierra",
546-
},
547-
self.get_non_erc20_base_name(),
548-
match cairo_version {
549-
CairoVersion::Cairo0 => "_compiled",
550-
CairoVersion::Cairo1(RunnableCairo1::Casm) => ".casm",
551-
#[cfg(feature = "cairo_native")]
552-
CairoVersion::Cairo1(RunnableCairo1::Native) => ".sierra",
548+
match self.cairo_version() {
549+
CairoVersion::Cairo0 => {
550+
format!(
551+
"resources/feature_contracts/cairo0/compiled/{}_compiled.json",
552+
self.get_non_erc20_base_name()
553+
)
553554
}
554-
)
555+
CairoVersion::Cairo1(RunnableCairo1::Casm) => {
556+
compile_cache::cached_compiled_path(self).to_string_lossy().to_string()
557+
}
558+
#[cfg(feature = "cairo_native")]
559+
CairoVersion::Cairo1(RunnableCairo1::Native) => {
560+
compile_cache::cached_sierra_path(self).to_string_lossy().to_string()
561+
}
562+
}
555563
}
556564
}
557565

@@ -697,7 +705,11 @@ impl FeatureContract {
697705
}
698706

699707
pub fn get_raw_contract_class(contract_path: &str) -> String {
700-
let path: PathBuf = [compile_time_cargo_manifest_dir!(), contract_path].iter().collect();
708+
let path: PathBuf = if std::path::Path::new(contract_path).is_absolute() {
709+
PathBuf::from(contract_path)
710+
} else {
711+
[compile_time_cargo_manifest_dir!(), contract_path].iter().collect()
712+
};
701713
fs::read_to_string(&path)
702714
.unwrap_or_else(|e| panic!("Failed to read contract from {path:?}: {e}"))
703715
}

0 commit comments

Comments
 (0)