Skip to content

Commit

Permalink
chore: expose compilation args for the compile sierra to casm util
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 3, 2024
1 parent 6a332d4 commit 959f42e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
10 changes: 7 additions & 3 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cairo_lang_starknet_classes::casm_contract_class::{
use starknet_api::core::CompiledClassHash;
use starknet_api::rpc_transaction::RPCDeclareTransaction;
use starknet_api::transaction::Builtin;
use starknet_sierra_compile::compile::compile_sierra_to_casm;
use starknet_sierra_compile::compile::{compile_sierra_to_casm, SierraToCasmCompilationArgs};
use starknet_sierra_compile::errors::CompilationUtilError;
use starknet_sierra_compile::utils::into_contract_class_for_compilation;

Expand All @@ -30,8 +30,12 @@ pub fn compile_contract_class(declare_tx: &RPCDeclareTransaction) -> GatewayResu
into_contract_class_for_compilation(starknet_api_contract_class);

// Compile Sierra to Casm.
let catch_unwind_result =
panic::catch_unwind(|| compile_sierra_to_casm(cairo_lang_contract_class));
let catch_unwind_result = panic::catch_unwind(|| {
compile_sierra_to_casm(
cairo_lang_contract_class,
SierraToCasmCompilationArgs { max_bytecode_size: 1_000_000, ..Default::default() },
)
});
let casm_contract_class = match catch_unwind_result {
Ok(compilation_result) => compilation_result?,
Err(_) => {
Expand Down
23 changes: 14 additions & 9 deletions crates/starknet_sierra_compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ use crate::errors::CompilationUtilError;
#[path = "compile_test.rs"]
pub mod compile_test;
pub struct SierraToCasmCompilationArgs {
list_selector: ListSelector,
add_pythonic_hints: bool,
max_bytecode_size: usize,
pub list_selector: ListSelector,
pub add_pythonic_hints: bool,
pub max_bytecode_size: usize,
}

impl Default for SierraToCasmCompilationArgs {
fn default() -> Self {
Self {
list_selector: ListSelector::DefaultList,
add_pythonic_hints: true,
max_bytecode_size: Default::default(),
}
}
}

/// This function may panic.
pub fn compile_sierra_to_casm(
contract_class: ContractClass,
compilation_args: SierraToCasmCompilationArgs,
) -> Result<CasmContractClass, CompilationUtilError> {
let compilation_args = SierraToCasmCompilationArgs {
list_selector: ListSelector::DefaultList,
add_pythonic_hints: true,
max_bytecode_size: 1000000,
};

contract_class.validate_version_compatible(compilation_args.list_selector)?;

Ok(CasmContractClass::from_contract_class(
Expand Down
15 changes: 11 additions & 4 deletions crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ use std::env;
use std::path::Path;

use assert_matches::assert_matches;
use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError;
use cairo_lang_starknet_classes::allowed_libfuncs::{AllowedLibfuncsError, ListSelector};
use test_utils::{get_absolute_path, FAULTY_ACCOUNT_CLASS_FILE, TEST_FILES_FOLDER};

use crate::compile::{compile_sierra_to_casm, CompilationUtilError};
use crate::compile::{compile_sierra_to_casm, CompilationUtilError, SierraToCasmCompilationArgs};
use crate::test_utils::contract_class_from_file;

const SIERRA_TO_CASM_COMPILATION_ARGS: SierraToCasmCompilationArgs = SierraToCasmCompilationArgs {
list_selector: ListSelector::DefaultList,
add_pythonic_hints: true,
max_bytecode_size: 1_000_000,
};

#[test]
fn test_compile_sierra_to_casm() {
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;

let contract_class = contract_class_from_file(sierra_path);
let casm_contract = compile_sierra_to_casm(contract_class).unwrap();
let casm_contract =
compile_sierra_to_casm(contract_class, SIERRA_TO_CASM_COMPILATION_ARGS).unwrap();
let serialized_casm = serde_json::to_string_pretty(&casm_contract).unwrap().into_bytes();

assert_eq!(serialized_casm.len(), expected_casm_contract_length);
Expand All @@ -31,7 +38,7 @@ fn test_negative_flow_compile_sierra_to_casm() {
// Truncate the sierra program to trigger an error.
contract_class.sierra_program = contract_class.sierra_program[..100].to_vec();

let result = compile_sierra_to_casm(contract_class);
let result = compile_sierra_to_casm(contract_class, SIERRA_TO_CASM_COMPILATION_ARGS);
assert_matches!(
result,
Err(CompilationUtilError::AllowedLibfuncsError(AllowedLibfuncsError::SierraProgramError))
Expand Down

0 comments on commit 959f42e

Please sign in to comment.