Skip to content

Commit a5cc203

Browse files
feat(blockifier_test_utils): migrate to new cairo recompilation logic
1 parent fc85104 commit a5cc203

2 files changed

Lines changed: 32 additions & 86 deletions

File tree

crates/blockifier_test_utils/src/cairo_compile.rs

Lines changed: 18 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";
@@ -87,8 +85,7 @@ fn cairo1_package_exists(version: &String) -> bool {
8785

8886
/// Verifies that the Cairo1 package (of the given version) is available.
8987
/// Attempts to download it if not.
90-
#[allow(dead_code)]
91-
async fn verify_cairo1_package(version: &String) {
88+
pub async fn verify_cairo1_package(version: &String) {
9289
if !cairo1_package_exists(version) {
9390
download_cairo_package(version).await;
9491
}
@@ -131,68 +128,40 @@ pub fn cairo0_compile(
131128
pub fn cairo1_compile(
132129
path: String,
133130
git_tag_override: Option<String>,
134-
cargo_nightly_arg: Option<String>,
131+
_cargo_nightly_arg: Option<String>,
135132
) -> CompilationArtifacts {
136-
let mut base_compile_args = vec![];
137-
verify_cairo1_compiler_deps(git_tag_override);
138-
139-
let cairo1_compiler_path = local_cairo1_compiler_repo_path();
140-
141-
// Command args common to both compilation phases.
142-
base_compile_args.extend(vec![
143-
"run".into(),
144-
format!("--manifest-path={}/Cargo.toml", cairo1_compiler_path.to_string_lossy()),
145-
"--bin".into(),
146-
]);
147-
// Add additional cargo arg if provided. Should be first arg (base command is `cargo`).
148-
if let Some(nightly_version) = cargo_nightly_arg {
149-
base_compile_args.insert(0, format!("+nightly-{nightly_version}"));
150-
}
133+
let (tag, _cairo_repo_path) = get_tag_and_repo_file_path(git_tag_override);
134+
let version = tag.strip_prefix("v").unwrap().to_string();
135+
assert!(cairo1_package_exists(&version));
151136

152-
let sierra_output = starknet_compile(path, &mut base_compile_args);
137+
let sierra_output = starknet_compile(path, &version);
153138

154139
let mut temp_file = NamedTempFile::new().unwrap();
155140
temp_file.write_all(&sierra_output).unwrap();
156141
let temp_path_str = temp_file.into_temp_path();
157142

158143
// Sierra -> CASM.
159-
let casm_output = starknet_sierra_compile(
160-
temp_path_str.to_str().unwrap().to_string(),
161-
&mut base_compile_args,
162-
);
144+
let casm_output =
145+
starknet_sierra_compile(temp_path_str.to_str().unwrap().to_string(), &version);
163146

164147
CompilationArtifacts::Cairo1 { casm: casm_output, sierra: sierra_output }
165148
}
166149

167-
/// Compile Cairo1 Contract into their Sierra version using the compiler version set in the
168-
/// Cargo.toml
169-
pub fn starknet_compile(path: String, base_compile_args: &mut [String]) -> Vec<u8> {
170-
// Cairo -> Sierra.
171-
let mut starknet_compile_commmand = Command::new("cargo");
172-
starknet_compile_commmand.args(base_compile_args.to_owned());
173-
starknet_compile_commmand.args([
174-
"starknet-compile",
175-
"--",
176-
"--single-file",
177-
&path,
178-
"--allowed-libfuncs-list-name",
179-
"all",
180-
]);
150+
/// Compile Cairo1 Contract into their Sierra version using the given compiler version.
151+
/// Assumes the relevant compiler version is already downloaded.
152+
pub fn starknet_compile(path: String, version: &String) -> Vec<u8> {
153+
let mut starknet_compile_commmand = Command::new(starknet_compile_binary_path(version));
154+
starknet_compile_commmand.args(["--single-file", &path, "--allowed-libfuncs-list-name", "all"]);
181155
let sierra_output = run_and_verify_output(&mut starknet_compile_commmand);
182156

183157
sierra_output.stdout
184158
}
185159

186-
/// Compile Sierra code into CASM.
187-
fn starknet_sierra_compile(path: String, base_compile_args: &mut Vec<String>) -> Vec<u8> {
188-
let mut sierra_compile_command = Command::new("cargo");
189-
sierra_compile_command.args(base_compile_args);
190-
sierra_compile_command.args([
191-
"starknet-sierra-compile",
192-
&path,
193-
"--allowed-libfuncs-list-name",
194-
"all",
195-
]);
160+
/// Compile Sierra code into CASM using the given compiler version.
161+
/// Assumes the relevant compiler version is already downloaded.
162+
fn starknet_sierra_compile(path: String, version: &String) -> Vec<u8> {
163+
let mut sierra_compile_command = Command::new(starknet_sierra_compile_binary_path(version));
164+
sierra_compile_command.args([&path, "--allowed-libfuncs-list-name", "all"]);
196165
let casm_output = run_and_verify_output(&mut sierra_compile_command);
197166
casm_output.stdout
198167
}
@@ -241,36 +210,3 @@ fn get_tag_and_repo_file_path(git_tag_override: Option<String>) -> (String, Path
241210

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

crates/blockifier_test_utils/tests/feature_contracts_compatibility_test.rs

Lines changed: 14 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,13 @@ 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 version = tag_and_tool_chain
93+
.0
94+
.unwrap_or(cairo1_compiler_tag())
95+
.strip_prefix("v")
96+
.unwrap()
97+
.to_string();
98+
verify_cairo1_package(&version).await;
8999

90100
let mut task_set = tokio::task::JoinSet::new();
91101

@@ -97,9 +107,9 @@ async fn verify_feature_contracts_compatibility(fix: bool, cairo_version: CairoV
97107
task_set
98108
.spawn(verify_feature_contracts_compatibility_logic_async(contract, fix));
99109
}
100-
info!("Done spawning tasks for {tag_and_tool_chain:?}. Awaiting them...");
110+
info!("Done spawning tasks for {version:?}. Awaiting them...");
101111
task_set.join_all().await;
102-
info!("Done awaiting tasks for {tag_and_tool_chain:?}.");
112+
info!("Done awaiting tasks for {version:?}.");
103113
}
104114
}
105115
#[cfg(feature = "cairo_native")]

0 commit comments

Comments
 (0)