Skip to content

Commit

Permalink
feat: use the starknet-sierra-compile downloaded binary to compile
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 27, 2024
1 parent 3635605 commit e3e4d61
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/starknet_sierra_compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde.workspace = true
serde_json.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
tempfile.workspace = true
thiserror.workspace = true
validator.workspace = true

Expand Down
12 changes: 4 additions & 8 deletions crates/starknet_sierra_compile/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;
use std::fs;
use std::process::Command;
use std::{env, fs};

include!("src/build_utils.rs");

fn main() {
println!("cargo:rerun-if-changed=../../Cargo.lock");
Expand All @@ -14,12 +15,7 @@ fn main() {
/// compile Sierra to Casm. In this crate (`starknet_sierra_compile`), the binary is executed as a
/// subprocess whenever Sierra compilation is required.
fn download_cairo() {
let out_dir = env::var("OUT_DIR").expect("Failed to get the OUT_DIR environment variable");
// Navigate from this crate's build folder to reach the `target/BUILD_FLAVOR` directory.
let target_dir = Path::new(&out_dir)
.ancestors()
.nth(3)
.expect("Failed to navigate up three levels from OUT_DIR");
let target_dir = get_traget_build_flavor_dir();
let cairo_folder_dir = target_dir.join("cairo");

if cairo_folder_dir.exists() {
Expand Down
13 changes: 13 additions & 0 deletions crates/starknet_sierra_compile/src/build_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// Get the crate's `OUT_DIR` and navigates up to reach the `target/BUILD_FLAVOR` directory.
/// This directory is shared accross all crates in this project.
pub fn get_traget_build_flavor_dir() -> &'static std::path::Path {
let out_dir = std::env::var("OUT_DIR").expect("Failed to get the OUT_DIR environment variable");
// Navigate from this crate's build folder to reach the `target/BUILD_FLAVOR` directory.
Box::leak(
std::path::Path::new(&out_dir)
.ancestors()
.nth(3)
.expect("Failed to navigate up three levels from OUT_DIR")
.into(),
)
}
77 changes: 77 additions & 0 deletions crates/starknet_sierra_compile/src/command_line_compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::io::Write;
use std::process::Command;
use std::sync::OnceLock;

use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::contract_class::ContractClass;
use tempfile::NamedTempFile;

use crate::build_utils::get_traget_build_flavor_dir;
use crate::config::SierraToCasmCompilationConfig;
use crate::errors::CompilationUtilError;
use crate::SierraToCasmCompiler;

#[derive(Clone)]
pub struct CommandLineCompiler {
pub config: SierraToCasmCompilationConfig,
path_to_starknet_sierra_compile_binary: &'static str,
}

impl CommandLineCompiler {
pub fn new(config: SierraToCasmCompilationConfig) -> Self {
Self { config, path_to_starknet_sierra_compile_binary: get_compiler_path() }
}
}

impl SierraToCasmCompiler for CommandLineCompiler {
fn compile(
&self,
contract_class: ContractClass,
) -> Result<CasmContractClass, CompilationUtilError> {
// Create a temporary file to store the Sierra contract class.
let serialized_contract_class = serde_json::to_string(&contract_class)?;

let mut temp_file = NamedTempFile::new()?;
temp_file.write_all(serialized_contract_class.as_bytes())?;
let temp_file_path = temp_file.path().to_str().ok_or(
CompilationUtilError::UnexpectedError("Failed to get temporary file path".to_owned()),
)?;

// Set the parameters for the compile process.
let mut command = Command::new(self.path_to_starknet_sierra_compile_binary);
command.args([
temp_file_path,
"--add-pythonic-hints",
"--max-bytecode-size",
&self.config.max_bytecode_size.to_string(),
]);

// Run the compile process.
let compile_output = command.output()?;

if !compile_output.status.success() {
let stderr_output = String::from_utf8(compile_output.stderr)
.unwrap_or("Failed to get stderr output".into());
return Err(CompilationUtilError::CompilationError(stderr_output));
};

Ok(serde_json::from_slice::<CasmContractClass>(&compile_output.stdout)?)
}
}

static COMPILER_PATH: OnceLock<&str> = OnceLock::new();
fn get_compiler_path() -> &'static str {
COMPILER_PATH.get_or_init(|| {
let target_dir = get_traget_build_flavor_dir().join("cairo");

let binary_name = "starknet-sierra-compile";
Box::leak(
target_dir
.join("bin")
.join(binary_name)
.to_str()
.expect("Failed to get compiler path")
.into(),
)
})
}
22 changes: 16 additions & 6 deletions crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ use std::path::Path;

use assert_matches::assert_matches;
use mempool_test_utils::{get_absolute_path, FAULTY_ACCOUNT_CLASS_FILE, TEST_FILES_FOLDER};
use rstest::{fixture, rstest};
use rstest::rstest;

use crate::cairo_lang_compiler::CairoLangSierraToCasmCompiler;
use crate::command_line_compiler::CommandLineCompiler;
use crate::config::SierraToCasmCompilationConfig;
use crate::errors::CompilationUtilError;
use crate::test_utils::contract_class_from_file;
use crate::SierraToCasmCompiler;

#[fixture]
fn compiler() -> impl SierraToCasmCompiler {
CairoLangSierraToCasmCompiler { config: SierraToCasmCompilationConfig::default() }
const SIERRA_TO_CASM_COMPILATION_CONFIG: SierraToCasmCompilationConfig =
SierraToCasmCompilationConfig { max_bytecode_size: 81920 };

const CAIRO_LANG_COMPILER: CairoLangSierraToCasmCompiler =
CairoLangSierraToCasmCompiler { config: SIERRA_TO_CASM_COMPILATION_CONFIG };

fn commnad_line_compiler() -> CommandLineCompiler {
CommandLineCompiler::new(SIERRA_TO_CASM_COMPILATION_CONFIG)
}

// TODO: use the other compiler as well.
#[rstest]
fn test_compile_sierra_to_casm(compiler: impl SierraToCasmCompiler) {
#[case::cairo_lang_compiler(&CAIRO_LANG_COMPILER)]
#[case::command_line_compiler(&commnad_line_compiler())]
fn test_compile_sierra_to_casm(#[case] compiler: &impl SierraToCasmCompiler) {
env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Failed to set current dir.");
let sierra_path = Path::new(FAULTY_ACCOUNT_CLASS_FILE);
let expected_casm_contract_length = 72304;
Expand All @@ -32,7 +40,9 @@ fn test_compile_sierra_to_casm(compiler: impl SierraToCasmCompiler) {

// TODO(Arni, 1/5/2024): Add a test for panic result test.
#[rstest]
fn test_negative_flow_compile_sierra_to_casm(compiler: impl SierraToCasmCompiler) {
#[case::cairo_lang_compiler(&CAIRO_LANG_COMPILER)]
#[case::command_line_compiler(&commnad_line_compiler())]
fn test_negative_flow_compile_sierra_to_casm(#[case] compiler: &impl SierraToCasmCompiler) {
env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Failed to set current dir.");
let sierra_path = Path::new(FAULTY_ACCOUNT_CLASS_FILE);

Expand Down
12 changes: 12 additions & 0 deletions crates/starknet_sierra_compile/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ impl From<StarknetSierraCompilationError> for CompilationUtilError {
CompilationUtilError::CompilationError(error.to_string())
}
}

impl From<serde_json::Error> for CompilationUtilError {
fn from(error: serde_json::Error) -> Self {
CompilationUtilError::UnexpectedError(error.to_string())
}
}

impl From<std::io::Error> for CompilationUtilError {
fn from(error: std::io::Error) -> Self {
CompilationUtilError::UnexpectedError(error.to_string())
}
}
2 changes: 2 additions & 0 deletions crates/starknet_sierra_compile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use cairo_lang_starknet_classes::contract_class::ContractClass;

use crate::errors::CompilationUtilError;

pub mod build_utils;
pub mod cairo_lang_compiler;
pub mod command_line_compiler;
pub mod config;
pub mod errors;
pub mod utils;
Expand Down

0 comments on commit e3e4d61

Please sign in to comment.