From 7f90ae32b9acb5f4b0d199acfed2d94fc8f6ac74 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 15 Jul 2024 16:35:07 +0300 Subject: [PATCH 1/2] chore: expose max bytecode size arg for the compile sierra to casm util --- crates/gateway/src/compilation.rs | 5 +++-- crates/starknet_sierra_compile/src/compile.rs | 18 ++++++++++-------- .../src/compile_test.rs | 6 ++++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index c1e17fbfa..23fd561aa 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -57,8 +57,9 @@ impl GatewayCompiler { &self, cairo_lang_contract_class: CairoLangContractClass, ) -> Result { - 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)??; diff --git a/crates/starknet_sierra_compile/src/compile.rs b/crates/starknet_sierra_compile/src/compile.rs index 23ac70ce3..825c2a086 100644 --- a/crates/starknet_sierra_compile/src/compile.rs +++ b/crates/starknet_sierra_compile/src/compile.rs @@ -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 { - 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, )?) } diff --git a/crates/starknet_sierra_compile/src/compile_test.rs b/crates/starknet_sierra_compile/src/compile_test.rs index 14e0b3d13..f2ed1254d 100644 --- a/crates/starknet_sierra_compile/src/compile_test.rs +++ b/crates/starknet_sierra_compile/src/compile_test.rs @@ -8,6 +8,8 @@ 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."); @@ -15,7 +17,7 @@ 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, 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); @@ -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)) From fbf27481eef89e70bd343df90d93b2bb26fce7dc Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 22 Jul 2024 14:06:10 +0300 Subject: [PATCH 2/2] chore: compiler will raise error on casm bytecode size limit --- Cargo.lock | 1 + Cargo.toml | 1 + crates/gateway/Cargo.toml | 1 + crates/gateway/src/compilation.rs | 9 +-------- crates/gateway/src/compilation_test.rs | 10 +++++++++- crates/gateway/src/errors.rs | 5 ----- crates/starknet_sierra_compile/src/compile.rs | 20 ++----------------- 7 files changed, 15 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f175579d..5681eb979 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5449,6 +5449,7 @@ dependencies = [ "async-trait", "axum", "blockifier 0.8.0-rc.0 (git+https://github.com/starkware-libs/blockifier.git?rev=32191d41)", + "cairo-lang-sierra-to-casm", "cairo-lang-starknet-classes", "cairo-vm", "hyper", diff --git a/Cargo.toml b/Cargo.toml index c7530e3b4..b8461c7a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ axum = "0.6.12" bincode = "1.3.3" blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "32191d41" } cairo-lang-sierra = "2.7.0-dev.0" +cairo-lang-sierra-to-casm = "2.7.0-dev.0" cairo-lang-starknet-classes = "2.7.0-dev.0" cairo-lang-utils = "2.7.0-dev.0" cairo-vm = "1.0.0-rc3" diff --git a/crates/gateway/Cargo.toml b/crates/gateway/Cargo.toml index 0d93cbfdb..d98c4bddb 100644 --- a/crates/gateway/Cargo.toml +++ b/crates/gateway/Cargo.toml @@ -15,6 +15,7 @@ testing = [] async-trait.workspace = true axum.workspace = true blockifier= { workspace = true, features = ["testing"] } +cairo-lang-sierra-to-casm.workspace = true cairo-lang-starknet-classes.workspace = true cairo-vm.workspace = true hyper.workspace = true diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index 23fd561aa..fca283b8f 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -70,18 +70,11 @@ impl GatewayCompiler { // the validation of the raw class size. The validation should be linked to the way the class is // saved in Papyrus etc. /// Validates that the Casm class is within size limit. Specifically, this function validates - /// the size of the bytecode and the serialized class. + /// the size of the serialized class. fn validate_casm_class_size( &self, casm_contract_class: &CasmContractClass, ) -> Result<(), GatewayError> { - let bytecode_size = casm_contract_class.bytecode.len(); - if bytecode_size > self.config.max_casm_bytecode_size { - return Err(GatewayError::CasmBytecodeSizeTooLarge { - bytecode_size, - max_bytecode_size: self.config.max_casm_bytecode_size, - }); - } let contract_class_object_size = serde_json::to_string(&casm_contract_class) .expect("Unexpected error serializing Casm contract class.") .len(); diff --git a/crates/gateway/src/compilation_test.rs b/crates/gateway/src/compilation_test.rs index 6f7f3576e..732fc56cc 100644 --- a/crates/gateway/src/compilation_test.rs +++ b/crates/gateway/src/compilation_test.rs @@ -1,6 +1,8 @@ use assert_matches::assert_matches; use blockifier::execution::contract_class::ContractClass; +use cairo_lang_sierra_to_casm::compiler::CompilationError; use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError; +use cairo_lang_starknet_classes::casm_contract_class::StarknetSierraCompilationError; use mempool_test_utils::starknet_api_test_utils::declare_tx as rpc_declare_tx; use rstest::{fixture, rstest}; use starknet_api::core::CompiledClassHash; @@ -52,7 +54,13 @@ fn test_compile_contract_class_bytecode_size_validation(declare_tx: RPCDeclareTr }; let result = gateway_compiler.process_declare_tx(&declare_tx); - assert_matches!(result.unwrap_err(), GatewayError::CasmBytecodeSizeTooLarge { .. }) + assert_matches!( + result.unwrap_err(), + GatewayError::CompilationError(CompilationUtilError::StarknetSierraCompilationError( + StarknetSierraCompilationError::CompilationError(err) + )) + if matches!(err.as_ref(), CompilationError::CodeSizeLimitExceeded) + ) } // TODO(Arni): Redesign this test once the compiler is passed with dependancy injection. diff --git a/crates/gateway/src/errors.rs b/crates/gateway/src/errors.rs index 3a83e97e3..a21ec4ac0 100644 --- a/crates/gateway/src/errors.rs +++ b/crates/gateway/src/errors.rs @@ -19,11 +19,6 @@ use crate::compiler_version::{VersionId, VersionIdError}; /// Errors directed towards the end-user, as a result of gateway requests. #[derive(Debug, Error)] pub enum GatewayError { - #[error( - "Cannot declare Casm contract class with bytecode size of {bytecode_size}; max allowed \ - size: {max_bytecode_size}." - )] - CasmBytecodeSizeTooLarge { bytecode_size: usize, max_bytecode_size: usize }, #[error( "Cannot declare Casm contract class with size of {contract_class_object_size}; max \ allowed size: {max_contract_class_object_size}." diff --git a/crates/starknet_sierra_compile/src/compile.rs b/crates/starknet_sierra_compile/src/compile.rs index 825c2a086..a1a05a24c 100644 --- a/crates/starknet_sierra_compile/src/compile.rs +++ b/crates/starknet_sierra_compile/src/compile.rs @@ -7,29 +7,13 @@ use crate::errors::CompilationUtilError; #[cfg(test)] #[path = "compile_test.rs"] pub mod compile_test; -struct SierraToCasmCompilationArgs { - list_selector: ListSelector, - add_pythonic_hints: bool, -} - -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 { - let compilation_args = SierraToCasmCompilationArgs::default(); - - contract_class.validate_version_compatible(compilation_args.list_selector)?; + contract_class.validate_version_compatible(ListSelector::DefaultList)?; - Ok(CasmContractClass::from_contract_class( - contract_class, - compilation_args.add_pythonic_hints, - max_bytecode_size, - )?) + Ok(CasmContractClass::from_contract_class(contract_class, true, max_bytecode_size)?) }