Skip to content

Commit c26200e

Browse files
fix(blockifier_test_utils): address PR review feedback
Restore CONTRACT_FILTER env var support for both Cairo 0 and Cairo 1 test entrypoints, restore TODO(Dori) parallelization comment, document get_raw_contract_class path handling, and keep verify_contract_casm_hashes at its original position to minimize diff. Made-with: Cursor
1 parent 31102d7 commit c26200e

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

crates/blockifier_test_utils/src/contracts.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,9 @@ impl FeatureContract {
704704
}
705705
}
706706

707+
/// Reads the raw JSON content of a contract class from disk.
708+
/// Accepts both absolute paths (e.g. from the compilation cache) and crate-relative paths
709+
/// (e.g. `resources/feature_contracts/...` resolved against the crate's manifest directory).
707710
pub fn get_raw_contract_class(contract_path: &str) -> String {
708711
let path: PathBuf = if std::path::Path::new(contract_path).is_absolute() {
709712
PathBuf::from(contract_path)

crates/blockifier_test_utils/tests/feature_contracts_compatibility_test.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@ fn verify_cairo0_contract(contract: &FeatureContract, fix: bool) {
6262
assert_eq!(contract.get_compiled_class_hash(&HashVersion::V2), CompiledClassHash::default());
6363
}
6464

65-
// ======================== Cairo 1 compilation & hash verification ========================
66-
67-
fn verify_contract_casm_hashes(contract: &FeatureContract) {
68-
let casm_json = contract.get_raw_class();
69-
let casm: CasmContractClass =
70-
serde_json::from_str(&casm_json).expect("Failed to deserialize CASM contract class");
71-
let (casm_hash_v1, casm_hash_v2) = contract.get_compiled_class_hashes_constants();
72-
casm_hash_v1.assert_eq(&format!("0x{:x}", casm.hash(&HashVersion::V1).0));
73-
casm_hash_v2.assert_eq(&format!("0x{:x}", casm.hash(&HashVersion::V2).0));
74-
}
75-
7665
// ======================== Enum <-> filesystem consistency ========================
7766

7867
/// For Cairo 0: verifies that each .cairo file has a matching _compiled.json and a matching
@@ -138,15 +127,33 @@ fn verify_feature_contracts_match_enum(
138127
}
139128
}
140129

130+
// Verified that the constant casm hashes V1 and V2 in the FeatureContract enum are correct.
131+
// In case of a mismatch, run: `env UPDATE_EXPECT=1 cargo test -p blockifier_test_utils --test
132+
// feature_contracts_compatibility_test -- verify_feature_contracts_cairo1`
133+
fn verify_contract_casm_hashes(contract: &FeatureContract) {
134+
let casm_json = contract.get_raw_class();
135+
let casm: CasmContractClass =
136+
serde_json::from_str(&casm_json).expect("Failed to deserialize CASM contract class");
137+
let (casm_hash_v1, casm_hash_v2) = contract.get_compiled_class_hashes_constants();
138+
casm_hash_v1.assert_eq(&format!("0x{:x}", casm.hash(&HashVersion::V1).0));
139+
casm_hash_v2.assert_eq(&format!("0x{:x}", casm.hash(&HashVersion::V2).0));
140+
}
141+
141142
// ======================== Test entrypoints ========================
142143

143144
#[ignore]
144145
#[traced_test]
145146
#[tokio::test(flavor = "multi_thread")]
146147
async fn verify_feature_contracts_cairo0() {
147148
let fix = std::env::var("FIX_FEATURE_TEST").is_ok();
149+
let contract_filter = std::env::var("CONTRACT_FILTER").ok();
150+
let matches_filter = |contract: &FeatureContract| -> bool {
151+
contract_filter.as_deref().is_none_or(|filter| format!("{contract:?}").contains(filter))
152+
};
153+
// TODO(Dori, 1/10/2024): Parallelize Cairo0 recompilation.
148154
for contract in FeatureContract::all_feature_contracts()
149155
.filter(|c| c.cairo_version() == CairoVersion::Cairo0)
156+
.filter(|c| matches_filter(c))
150157
{
151158
verify_cairo0_contract(&contract, fix);
152159
}
@@ -158,7 +165,13 @@ async fn verify_feature_contracts_cairo0() {
158165
/// feature_contracts_compatibility_test -- verify_feature_contracts_cairo1`
159166
#[test]
160167
fn verify_feature_contracts_cairo1() {
161-
for contract in FeatureContract::all_cairo1_casm_feature_contracts() {
168+
let contract_filter = std::env::var("CONTRACT_FILTER").ok();
169+
let matches_filter = |contract: &FeatureContract| -> bool {
170+
contract_filter.as_deref().is_none_or(|filter| format!("{contract:?}").contains(filter))
171+
};
172+
for contract in
173+
FeatureContract::all_cairo1_casm_feature_contracts().filter(|c| matches_filter(c))
174+
{
162175
verify_contract_casm_hashes(&contract);
163176
}
164177
// ERC20 uses committed artifacts; just verify its hashes.

0 commit comments

Comments
 (0)