From fbf27481eef89e70bd343df90d93b2bb26fce7dc Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 22 Jul 2024 14:06:10 +0300 Subject: [PATCH] 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)?) }