Skip to content

Commit 08e5606

Browse files
chore(blockifier_test_utils): extract logic into starknet_sierra_compile
1 parent 64193f0 commit 08e5606

1 file changed

Lines changed: 33 additions & 34 deletions

File tree

crates/blockifier_test_utils/src/cairo_compile.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -135,55 +135,40 @@ pub fn cairo1_compile(
135135
git_tag_override: Option<String>,
136136
cargo_nightly_arg: Option<String>,
137137
) -> CompilationArtifacts {
138-
let mut base_compile_args = vec![];
139-
140-
let sierra_output =
141-
starknet_compile(path, git_tag_override, cargo_nightly_arg, &mut base_compile_args);
142-
143-
let mut temp_file = NamedTempFile::new().unwrap();
144-
temp_file.write_all(&sierra_output).unwrap();
145-
let temp_path_str = temp_file.into_temp_path();
146-
147-
// Sierra -> CASM.
148-
let mut sierra_compile_command = Command::new("cargo");
149-
sierra_compile_command.args(base_compile_args);
150-
sierra_compile_command.args([
151-
"starknet-sierra-compile",
152-
temp_path_str.to_str().unwrap(),
153-
"--allowed-libfuncs-list-name",
154-
"all",
155-
]);
156-
let casm_output = run_and_verify_output(&mut sierra_compile_command);
157-
158-
CompilationArtifacts::Cairo1 { casm: casm_output.stdout, sierra: sierra_output }
159-
}
160-
161-
/// Compile Cairo1 Contract into their Sierra version using the compiler version set in the
162-
/// Cargo.toml
163-
pub fn starknet_compile(
164-
path: String,
165-
git_tag_override: Option<String>,
166-
cargo_nightly_arg: Option<String>,
167-
base_compile_args: &mut Vec<String>,
168-
) -> Vec<u8> {
169138
verify_cairo1_compiler_deps(git_tag_override);
170139

171140
let cairo1_compiler_path = local_cairo1_compiler_repo_path();
172141

173142
// Command args common to both compilation phases.
174-
base_compile_args.extend(vec![
143+
let mut base_compile_args = vec![
175144
"run".into(),
176145
format!("--manifest-path={}/Cargo.toml", cairo1_compiler_path.to_string_lossy()),
177146
"--bin".into(),
178-
]);
147+
];
179148
// Add additional cargo arg if provided. Should be first arg (base command is `cargo`).
180149
if let Some(nightly_version) = cargo_nightly_arg {
181150
base_compile_args.insert(0, format!("+nightly-{nightly_version}"));
182151
}
183152

153+
let sierra_output = starknet_compile(path, &base_compile_args);
154+
155+
let mut temp_file = NamedTempFile::new().unwrap();
156+
temp_file.write_all(&sierra_output).unwrap();
157+
let temp_path_str = temp_file.into_temp_path();
158+
159+
// Sierra -> CASM.
160+
let casm_output =
161+
starknet_sierra_compile(temp_path_str.to_str().unwrap().to_string(), &base_compile_args);
162+
163+
CompilationArtifacts::Cairo1 { casm: casm_output, sierra: sierra_output }
164+
}
165+
166+
/// Compiles Cairo1 Contract into their Sierra version using the compiler version set in the
167+
/// Cargo.toml
168+
pub fn starknet_compile(path: String, base_compile_args: &[String]) -> Vec<u8> {
184169
// Cairo -> Sierra.
185170
let mut starknet_compile_commmand = Command::new("cargo");
186-
starknet_compile_commmand.args(base_compile_args.clone());
171+
starknet_compile_commmand.args(base_compile_args.to_owned());
187172
starknet_compile_commmand.args([
188173
"starknet-compile",
189174
"--",
@@ -197,6 +182,20 @@ pub fn starknet_compile(
197182
sierra_output.stdout
198183
}
199184

185+
/// Compiles Sierra code into CASM.
186+
fn starknet_sierra_compile(path: String, base_compile_args: &[String]) -> Vec<u8> {
187+
let mut sierra_compile_command = Command::new("cargo");
188+
sierra_compile_command.args(base_compile_args);
189+
sierra_compile_command.args([
190+
"starknet-sierra-compile",
191+
&path,
192+
"--allowed-libfuncs-list-name",
193+
"all",
194+
]);
195+
let casm_output = run_and_verify_output(&mut sierra_compile_command);
196+
casm_output.stdout
197+
}
198+
200199
/// Verifies that the required dependencies are available before compiling; panics if unavailable.
201200
fn verify_cairo0_compiler_deps() {
202201
// Python compiler. Verify correct version.

0 commit comments

Comments
 (0)