Skip to content

Commit

Permalink
chore: expose max bytecode size arg for the compile sierra to casm util
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 23, 2024
1 parent 05697a3 commit 7830a4a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
5 changes: 3 additions & 2 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ impl GatewayCompiler {
&self,
cairo_lang_contract_class: CairoLangContractClass,
) -> Result<CasmContractClass, GatewayError> {
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, self.config.max_casm_bytecode_size)
});
let casm_contract_class =
catch_unwind_result.map_err(|_| CompilationUtilError::CompilationPanic)??;

Expand Down
18 changes: 10 additions & 8 deletions crates/starknet_sierra_compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ use crate::errors::CompilationUtilError;
#[cfg(test)]
#[path = "compile_test.rs"]
pub mod compile_test;
pub struct SierraToCasmCompilationArgs {
struct SierraToCasmCompilationArgs {
list_selector: ListSelector,
add_pythonic_hints: bool,
max_bytecode_size: usize,
}

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

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

contract_class.validate_version_compatible(compilation_args.list_selector)?;

Ok(CasmContractClass::from_contract_class(
contract_class,
compilation_args.add_pythonic_hints,
compilation_args.max_bytecode_size,
max_bytecode_size,
)?)
}
6 changes: 4 additions & 2 deletions crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use mempool_test_utils::{get_absolute_path, FAULTY_ACCOUNT_CLASS_FILE, TEST_FILE
use crate::compile::{compile_sierra_to_casm, CompilationUtilError};
use crate::test_utils::contract_class_from_file;

const MAX_BYTECODE_SIZE: usize = 81920;

#[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, MAX_BYTECODE_SIZE).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 +33,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, MAX_BYTECODE_SIZE);
assert_matches!(
result,
Err(CompilationUtilError::AllowedLibfuncsError(AllowedLibfuncsError::SierraProgramError))
Expand Down

0 comments on commit 7830a4a

Please sign in to comment.