From aa71cff70384d70ccc21c3c9e28662f2749f382b Mon Sep 17 00:00:00 2001 From: avi-starkware Date: Tue, 17 Dec 2024 11:28:26 +0200 Subject: [PATCH] chore(cairo_native): allow disabling native compilation in the contract class manager (#2703) --- .../src/state/contract_class_manager.rs | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/blockifier/src/state/contract_class_manager.rs b/crates/blockifier/src/state/contract_class_manager.rs index 6fcac067c1..a6c734fae3 100644 --- a/crates/blockifier/src/state/contract_class_manager.rs +++ b/crates/blockifier/src/state/contract_class_manager.rs @@ -9,6 +9,7 @@ use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use starknet_sierra_compile::utils::into_contract_class_for_compilation; use starknet_sierra_compile::SierraToNativeCompiler; +use crate::blockifier::config::ContractClassManagerConfig; use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass}; use crate::execution::native::contract_class::NativeCompiledClassV1; use crate::state::global_cache::{CachedCairoNative, ContractCaches}; @@ -21,15 +22,17 @@ const CHANNEL_SIZE: usize = 1000; /// * `class_hash` - used to identify the contract class in the cache. /// * `sierra_contract_class` - the sierra contract class to be compiled. /// * `casm_compiled_class` - stored in [`NativeCompiledClassV1`] to allow fallback to cairo_vm -/// execution in case of unxecpected failure during native execution. +/// execution in case of unexpected failure during native execution. type CompilationRequest = (ClassHash, Arc, CompiledClassV1); /// Manages the global cache of contract classes and handles sierra-to-native compilation requests. struct ContractClassManager { - // The global cache of contract classes: casm, sierra, and native. + config: ContractClassManagerConfig, + /// The global cache of contract classes: casm, sierra, and native. contract_caches: Arc, - // The sending half of the compilation request channel. - sender: SyncSender, + /// The sending half of the compilation request channel. Set to `None` if native compilation is + /// disabled. + sender: Option>, } #[allow(dead_code)] @@ -37,9 +40,13 @@ impl ContractClassManager { /// Creates a new contract class manager and spawns a thread that listens for compilation /// requests and processes them (a.k.a. the compilation worker). /// Returns the contract class manager. - pub fn start(contract_caches: ContractCaches) -> ContractClassManager { + pub fn start(config: ContractClassManagerConfig) -> ContractClassManager { // TODO(Avi, 15/12/2024): Add the size of the channel to the config. - let contract_caches = Arc::new(contract_caches); + let contract_caches = Arc::new(ContractCaches::new(config.contract_cache_size)); + if !config.run_cairo_native { + // Native compilation is disabled - no need to start the compilation worker. + return ContractClassManager { config, contract_caches, sender: None }; + } let (sender, receiver) = sync_channel(CHANNEL_SIZE); let compiler_config = SierraToCasmCompilationConfig::default(); let compiler = CommandLineCompiler::new(compiler_config); @@ -51,15 +58,17 @@ impl ContractClassManager { move || run_compilation_worker(contract_caches, receiver, compiler) }); - ContractClassManager { contract_caches, sender } + ContractClassManager { config, contract_caches, sender: Some(sender) } } /// Sends a compilation request to the compilation worker. Does not block the sender. Logs an - /// error is the channel is full. + /// error if the channel is full. pub fn send_compilation_request(&self, request: CompilationRequest) { + assert!(!self.config.run_cairo_native, "Native compilation is disabled."); + let sender = self.sender.as_ref().expect("Compilation channel not available."); self.cache_request_contracts(&request); // TODO(Avi, 15/12/2024): Check for duplicated requests. - self.sender.try_send(request).unwrap_or_else(|err| match err { + sender.try_send(request).unwrap_or_else(|err| match err { TrySendError::Full((class_hash, _, _)) => { error!( "Compilation request channel is full (size: {}). Compilation request for \ @@ -88,6 +97,11 @@ impl ContractClassManager { self.contract_caches.get_casm(class_hash) } + /// Sets the casm compiled class for the given class hash in the cache. + pub fn set_casm(&self, class_hash: ClassHash, compiled_class: RunnableCompiledClass) { + self.contract_caches.set_casm(class_hash, compiled_class); + } + /// Caches the sierra and casm contract classes of a compilation request. fn cache_request_contracts(&self, request: &CompilationRequest) { let (class_hash, sierra, casm) = request.clone();