Skip to content

Commit 4d15216

Browse files
feat(blockifier_test_utils): migrate to new cairo recompilation logic
1 parent 970ad50 commit 4d15216

2 files changed

Lines changed: 34 additions & 86 deletions

File tree

crates/blockifier_test_utils/src/cairo_compile.rs

Lines changed: 19 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use apollo_infra_utils::path::project_path;
99
use tempfile::NamedTempFile;
1010
use tracing::info;
1111

12-
use crate::contracts::TagAndToolchain;
13-
1412
const CAIRO0_PIP_REQUIREMENTS_FILE: &str = "tests/requirements.txt";
1513
const CAIRO1_REPO_RELATIVE_PATH_OVERRIDE_ENV_VAR: &str = "CAIRO1_REPO_RELATIVE_PATH";
1614
const DEFAULT_CAIRO1_REPO_RELATIVE_PATH: &str = "../../../cairo";
@@ -93,8 +91,7 @@ fn download_cairo_package(version: &String) {
9391
}
9492

9593
/// Verifies that the Cairo1 package (of the given version) is available.
96-
#[allow(dead_code)]
97-
fn verify_cairo1_package(version: &String, download_if_missing: bool) {
94+
pub fn verify_cairo1_package(version: &String, download_if_missing: bool) {
9895
let cairo_compiler_path = starknet_compile_binary_path(version);
9996
let sierra_compiler_path = starknet_sierra_compile_binary_path(version);
10097
if download_if_missing && (!cairo_compiler_path.exists() || !sierra_compiler_path.exists()) {
@@ -140,68 +137,41 @@ pub fn cairo0_compile(
140137
pub fn cairo1_compile(
141138
path: String,
142139
git_tag_override: Option<String>,
143-
cargo_nightly_arg: Option<String>,
140+
_cargo_nightly_arg: Option<String>,
144141
) -> CompilationArtifacts {
145-
let mut base_compile_args = vec![];
146-
verify_cairo1_compiler_deps(git_tag_override);
147-
148-
let cairo1_compiler_path = local_cairo1_compiler_repo_path();
149-
150-
// Command args common to both compilation phases.
151-
base_compile_args.extend(vec![
152-
"run".into(),
153-
format!("--manifest-path={}/Cargo.toml", cairo1_compiler_path.to_string_lossy()),
154-
"--bin".into(),
155-
]);
156-
// Add additional cargo arg if provided. Should be first arg (base command is `cargo`).
157-
if let Some(nightly_version) = cargo_nightly_arg {
158-
base_compile_args.insert(0, format!("+nightly-{nightly_version}"));
159-
}
142+
let (tag, _cairo_repo_path) = get_tag_and_repo_file_path(git_tag_override);
143+
let download_if_missing = false;
144+
let version = tag.strip_prefix("v").unwrap().to_string();
145+
verify_cairo1_package(&version, download_if_missing);
160146

161-
let sierra_output = starknet_compile(path, &mut base_compile_args);
147+
let sierra_output = starknet_compile(path, &version);
162148

163149
let mut temp_file = NamedTempFile::new().unwrap();
164150
temp_file.write_all(&sierra_output).unwrap();
165151
let temp_path_str = temp_file.into_temp_path();
166152

167153
// Sierra -> CASM.
168-
let casm_output = starknet_sierra_compile(
169-
temp_path_str.to_str().unwrap().to_string(),
170-
&mut base_compile_args,
171-
);
154+
let casm_output =
155+
starknet_sierra_compile(temp_path_str.to_str().unwrap().to_string(), &version);
172156

173157
CompilationArtifacts::Cairo1 { casm: casm_output, sierra: sierra_output }
174158
}
175159

176-
/// Compile Cairo1 Contract into their Sierra version using the compiler version set in the
177-
/// Cargo.toml
178-
pub fn starknet_compile(path: String, base_compile_args: &mut Vec<String>) -> Vec<u8> {
179-
// Cairo -> Sierra.
180-
let mut starknet_compile_commmand = Command::new("cargo");
181-
starknet_compile_commmand.args(base_compile_args.clone());
182-
starknet_compile_commmand.args([
183-
"starknet-compile",
184-
"--",
185-
"--single-file",
186-
&path,
187-
"--allowed-libfuncs-list-name",
188-
"all",
189-
]);
160+
/// Compile Cairo1 Contract into their Sierra version using the given compiler version.
161+
/// Assumes the relevant compiler version is already downloaded.
162+
pub fn starknet_compile(path: String, version: &String) -> Vec<u8> {
163+
let mut starknet_compile_commmand = Command::new(starknet_compile_binary_path(version));
164+
starknet_compile_commmand.args(["--single-file", &path, "--allowed-libfuncs-list-name", "all"]);
190165
let sierra_output = run_and_verify_output(&mut starknet_compile_commmand);
191166

192167
sierra_output.stdout
193168
}
194169

195-
/// Compile Sierra code into CASM.
196-
fn starknet_sierra_compile(path: String, base_compile_args: &mut Vec<String>) -> Vec<u8> {
197-
let mut sierra_compile_command = Command::new("cargo");
198-
sierra_compile_command.args(base_compile_args);
199-
sierra_compile_command.args([
200-
"starknet-sierra-compile",
201-
path.as_str(),
202-
"--allowed-libfuncs-list-name",
203-
"all",
204-
]);
170+
/// Compile Sierra code into CASM using the given compiler version.
171+
/// Assumes the relevant compiler version is already downloaded.
172+
fn starknet_sierra_compile(path: String, version: &String) -> Vec<u8> {
173+
let mut sierra_compile_command = Command::new(starknet_sierra_compile_binary_path(version));
174+
sierra_compile_command.args([path.as_str(), "--allowed-libfuncs-list-name", "all"]);
205175
let casm_output = run_and_verify_output(&mut sierra_compile_command);
206176
casm_output.stdout
207177
}
@@ -250,36 +220,3 @@ fn get_tag_and_repo_file_path(git_tag_override: Option<String>) -> (String, Path
250220

251221
(tag, cairo_repo_path)
252222
}
253-
254-
pub fn prepare_group_tag_compiler_deps(tag_and_toolchain: &TagAndToolchain) {
255-
let (optional_tag, optional_toolchain) = tag_and_toolchain;
256-
257-
// Checkout the required version in the compiler repo.
258-
let (tag, cairo_repo_path) = get_tag_and_repo_file_path(optional_tag.clone());
259-
run_and_verify_output(Command::new("git").args([
260-
"-C",
261-
cairo_repo_path.to_str().unwrap(),
262-
"checkout",
263-
&tag,
264-
]));
265-
266-
// Install the toolchain, if specified.
267-
if let Some(toolchain) = optional_toolchain {
268-
run_and_verify_output(
269-
Command::new("rustup").args(["install", &format!("nightly-{toolchain}")]),
270-
);
271-
}
272-
}
273-
274-
fn verify_cairo1_compiler_deps(git_tag_override: Option<String>) {
275-
let (tag, cairo_repo_path) = get_tag_and_repo_file_path(git_tag_override);
276-
277-
// Verify that the checked out tag is as expected.
278-
run_and_verify_output(Command::new("git").args([
279-
"-C",
280-
cairo_repo_path.to_str().unwrap(),
281-
"rev-parse",
282-
"--verify",
283-
&tag,
284-
]));
285-
}

crates/blockifier_test_utils/tests/feature_contracts_compatibility_test.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::fs;
22

3-
use blockifier_test_utils::cairo_compile::{prepare_group_tag_compiler_deps, CompilationArtifacts};
3+
use blockifier_test_utils::cairo_compile::{
4+
cairo1_compiler_tag,
5+
verify_cairo1_package,
6+
CompilationArtifacts,
7+
};
48
use blockifier_test_utils::cairo_versions::{CairoVersion, RunnableCairo1};
59
use blockifier_test_utils::contracts::{
610
FeatureContract,
@@ -85,7 +89,14 @@ async fn verify_feature_contracts_compatibility(fix: bool, cairo_version: CairoV
8589
for (tag_and_tool_chain, feature_contracts) in
8690
FeatureContract::cairo1_feature_contracts_by_tag()
8791
{
88-
prepare_group_tag_compiler_deps(&tag_and_tool_chain);
92+
let download_if_missing = true;
93+
let version = tag_and_tool_chain
94+
.0
95+
.unwrap_or(cairo1_compiler_tag())
96+
.strip_prefix("v")
97+
.unwrap()
98+
.to_string();
99+
verify_cairo1_package(&version, download_if_missing);
89100

90101
let mut task_set = tokio::task::JoinSet::new();
91102

@@ -97,9 +108,9 @@ async fn verify_feature_contracts_compatibility(fix: bool, cairo_version: CairoV
97108
task_set
98109
.spawn(verify_feature_contracts_compatibility_logic_async(contract, fix));
99110
}
100-
info!("Done spawning tasks for {tag_and_tool_chain:?}. Awaiting them...");
111+
info!("Done spawning tasks for {version:?}. Awaiting them...");
101112
task_set.join_all().await;
102-
info!("Done awaiting tasks for {tag_and_tool_chain:?}.");
113+
info!("Done awaiting tasks for {version:?}.");
103114
}
104115
}
105116
#[cfg(feature = "cairo_native")]

0 commit comments

Comments
 (0)