From 23ee4bd3d8b98d172417ddc8f97171606620fa5c Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 2 Jul 2024 16:44:37 +0300 Subject: [PATCH] chore: expose compilation args for the compile sierra to casm util --- crates/gateway/src/compilation.rs | 10 +++++--- crates/starknet_sierra_compile/src/compile.rs | 23 +++++++++++-------- .../src/compile_test.rs | 15 ++++++++---- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index d52bbd91c..5b7cc1745 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -8,7 +8,7 @@ use cairo_lang_starknet_classes::casm_contract_class::{ }; use starknet_api::core::CompiledClassHash; use starknet_api::rpc_transaction::RPCDeclareTransaction; -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; @@ -29,8 +29,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(_) => { diff --git a/crates/starknet_sierra_compile/src/compile.rs b/crates/starknet_sierra_compile/src/compile.rs index 23ac70ce3..039ad7ad4 100644 --- a/crates/starknet_sierra_compile/src/compile.rs +++ b/crates/starknet_sierra_compile/src/compile.rs @@ -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 { - 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( diff --git a/crates/starknet_sierra_compile/src/compile_test.rs b/crates/starknet_sierra_compile/src/compile_test.rs index 1dec47146..a9064c996 100644 --- a/crates/starknet_sierra_compile/src/compile_test.rs +++ b/crates/starknet_sierra_compile/src/compile_test.rs @@ -2,12 +2,18 @@ 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."); @@ -15,7 +21,8 @@ fn test_compile_sierra_to_casm() { 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); @@ -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))