Skip to content

Commit 6725d67

Browse files
feat(blockifier_test_utils): migrate to new cairo recompilation logic
1 parent 91c458f commit 6725d67

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";
@@ -81,8 +79,7 @@ fn cairo1_package_exists(version: &String) -> bool {
8179

8280
/// Verifies that the Cairo1 package (of the given version) is available.
8381
/// Attempts to download it if not.
84-
#[allow(dead_code)]
85-
async fn verify_cairo1_package(version: &String) {
82+
pub async fn verify_cairo1_package(version: &String) {
8683
if !cairo1_package_exists(version) {
8784
download_cairo_package(version).await;
8885
}
@@ -125,68 +122,40 @@ pub fn cairo0_compile(
125122
pub fn cairo1_compile(
126123
path: String,
127124
git_tag_override: Option<String>,
128-
cargo_nightly_arg: Option<String>,
125+
_cargo_nightly_arg: Option<String>,
129126
) -> CompilationArtifacts {
130-
let mut base_compile_args = vec![];
131-
verify_cairo1_compiler_deps(git_tag_override);
132-
133-
let cairo1_compiler_path = local_cairo1_compiler_repo_path();
134-
135-
// Command args common to both compilation phases.
136-
base_compile_args.extend(vec![
137-
"run".into(),
138-
format!("--manifest-path={}/Cargo.toml", cairo1_compiler_path.to_string_lossy()),
139-
"--bin".into(),
140-
]);
141-
// Add additional cargo arg if provided. Should be first arg (base command is `cargo`).
142-
if let Some(nightly_version) = cargo_nightly_arg {
143-
base_compile_args.insert(0, format!("+nightly-{nightly_version}"));
144-
}
127+
let (tag, _cairo_repo_path) = get_tag_and_repo_file_path(git_tag_override);
128+
let version = tag.strip_prefix("v").unwrap().to_string();
129+
assert!(cairo1_package_exists(&version));
145130

146-
let sierra_output = starknet_compile(path, &mut base_compile_args);
131+
let sierra_output = starknet_compile(path, &version);
147132

148133
let mut temp_file = NamedTempFile::new().unwrap();
149134
temp_file.write_all(&sierra_output).unwrap();
150135
let temp_path_str = temp_file.into_temp_path();
151136

152137
// Sierra -> CASM.
153-
let casm_output = starknet_sierra_compile(
154-
temp_path_str.to_str().unwrap().to_string(),
155-
&mut base_compile_args,
156-
);
138+
let casm_output =
139+
starknet_sierra_compile(temp_path_str.to_str().unwrap().to_string(), &version);
157140

158141
CompilationArtifacts::Cairo1 { casm: casm_output, sierra: sierra_output }
159142
}
160143

161-
/// Compile Cairo1 Contract into their Sierra version using the compiler version set in the
162-
/// Cargo.toml
163-
pub fn starknet_compile(path: String, base_compile_args: &mut [String]) -> Vec<u8> {
164-
// Cairo -> Sierra.
165-
let mut starknet_compile_commmand = Command::new("cargo");
166-
starknet_compile_commmand.args(base_compile_args.to_owned());
167-
starknet_compile_commmand.args([
168-
"starknet-compile",
169-
"--",
170-
"--single-file",
171-
&path,
172-
"--allowed-libfuncs-list-name",
173-
"all",
174-
]);
144+
/// Compile Cairo1 Contract into their Sierra version using the given compiler version.
145+
/// Assumes the relevant compiler version is already downloaded.
146+
pub fn starknet_compile(path: String, version: &String) -> Vec<u8> {
147+
let mut starknet_compile_commmand = Command::new(starknet_compile_binary_path(version));
148+
starknet_compile_commmand.args(["--single-file", &path, "--allowed-libfuncs-list-name", "all"]);
175149
let sierra_output = run_and_verify_output(&mut starknet_compile_commmand);
176150

177151
sierra_output.stdout
178152
}
179153

180-
/// Compile Sierra code into CASM.
181-
fn starknet_sierra_compile(path: String, base_compile_args: &mut Vec<String>) -> Vec<u8> {
182-
let mut sierra_compile_command = Command::new("cargo");
183-
sierra_compile_command.args(base_compile_args);
184-
sierra_compile_command.args([
185-
"starknet-sierra-compile",
186-
&path,
187-
"--allowed-libfuncs-list-name",
188-
"all",
189-
]);
154+
/// Compile Sierra code into CASM using the given compiler version.
155+
/// Assumes the relevant compiler version is already downloaded.
156+
fn starknet_sierra_compile(path: String, version: &String) -> Vec<u8> {
157+
let mut sierra_compile_command = Command::new(starknet_sierra_compile_binary_path(version));
158+
sierra_compile_command.args([&path, "--allowed-libfuncs-list-name", "all"]);
190159
let casm_output = run_and_verify_output(&mut sierra_compile_command);
191160
casm_output.stdout
192161
}
@@ -235,36 +204,3 @@ fn get_tag_and_repo_file_path(git_tag_override: Option<String>) -> (String, Path
235204

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

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)