From a25f4bd0b2d1a528d47a0a4e0631c7d98a6d9269 Mon Sep 17 00:00:00 2001 From: Avi Cohen Date: Tue, 19 Nov 2024 17:36:12 +0200 Subject: [PATCH] feat(cairo_native): add batcher compiler struct --- Cargo.lock | 11 ++++ Cargo.toml | 1 + crates/blockifier/Cargo.toml | 4 +- crates/blockifier/src/execution/native.rs | 1 + .../src/execution/native/batcher_compiler.rs | 50 +++++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 crates/blockifier/src/execution/native/batcher_compiler.rs diff --git a/Cargo.lock b/Cargo.lock index 9b8a3d36189..c195d65a016 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1496,6 +1496,7 @@ dependencies = [ "cairo-native", "cairo-vm", "criterion", + "crossbeam-channel", "derive_more 0.99.18", "glob", "indexmap 2.6.0", @@ -1519,6 +1520,7 @@ dependencies = [ "sha2", "starknet-types-core", "starknet_api", + "starknet_sierra_compile", "strum 0.25.0", "strum_macros 0.25.3", "tempfile", @@ -2854,6 +2856,15 @@ dependencies = [ "itertools 0.10.5", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-deque" version = "0.8.5" diff --git a/Cargo.toml b/Cargo.toml index cac5e602ee0..956b9fc71e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,6 +110,7 @@ clap = "4.5.4" colored = "2.1.0" const_format = "0.2.30" criterion = "0.5.1" +crossbeam-channel = "0.5.13" deadqueue = "0.2.4" defaultmap = "0.5.0" derive_more = "0.99.17" diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index a61aa48d800..3c0c0ab0ac2 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -10,7 +10,7 @@ description = "The transaction-executing component in the Starknet sequencer." workspace = true [features] -cairo_native = ["dep:cairo-native"] +cairo_native = ["crossbeam-channel", "dep:cairo-native", "starknet_sierra_compile"] jemalloc = ["dep:tikv-jemallocator"] reexecution = ["transaction_serde"] testing = ["rand", "rstest", "starknet_api/testing"] @@ -31,6 +31,7 @@ cairo-lang-sierra.workspace = true cairo-lang-starknet-classes.workspace = true cairo-native = { workspace = true, optional = true } cairo-vm.workspace = true +crossbeam-channel = { workspace = true, optional = true } derive_more.workspace = true indexmap.workspace = true itertools.workspace = true @@ -51,6 +52,7 @@ serde_json = { workspace = true, features = ["arbitrary_precision"] } sha2.workspace = true starknet-types-core.workspace = true starknet_api.workspace = true +starknet_sierra_compile = { workspace = true, optional = true } strum.workspace = true strum_macros.workspace = true tempfile.workspace = true diff --git a/crates/blockifier/src/execution/native.rs b/crates/blockifier/src/execution/native.rs index 8b24ff59652..4ac545b4d83 100644 --- a/crates/blockifier/src/execution/native.rs +++ b/crates/blockifier/src/execution/native.rs @@ -1,3 +1,4 @@ +pub mod batcher_compiler; pub mod contract_class; pub mod entry_point_execution; pub mod syscall_handler; diff --git a/crates/blockifier/src/execution/native/batcher_compiler.rs b/crates/blockifier/src/execution/native/batcher_compiler.rs new file mode 100644 index 00000000000..631ccaa8575 --- /dev/null +++ b/crates/blockifier/src/execution/native/batcher_compiler.rs @@ -0,0 +1,50 @@ +use std::sync::Arc; + +use crossbeam_channel::Receiver; +use starknet_api::core::ClassHash; +use starknet_api::state::ContractClass as SierraContractClass; +use starknet_sierra_compile::command_line_compiler::CommandLineCompiler; +use starknet_sierra_compile::SierraToNativeCompiler; + +use crate::execution::native::contract_class::NativeContractClassV1; +use crate::state::global_cache::GlobalContractCacheManager; + +type CompilationRequest = (ClassHash, Arc); + +struct BatcherCompiler { + contract_cache_manager: GlobalContractCacheManager, + compilation_request_receiver: Receiver, + command_line_compiler: CommandLineCompiler, +} + +impl BatcherCompiler { + pub fn new( + contract_cache_manager: GlobalContractCacheManager, + compilation_request_receiver: Receiver, + command_line_compiler: CommandLineCompiler, + ) -> Self { + Self { contract_cache_manager, compilation_request_receiver, command_line_compiler } + } + pub fn run(&self) { + for (class_hash, sierra_contract_class) in self.compilation_request_receiver.iter() { + if self.contract_cache_manager.get_native_contract_executor(&class_hash).is_some() { + // Skip the compilation if the contract class is already compiled. + continue; + } + // TODO(Avi): Convert `sierra_contract_class` to + // `cairo_lang_starknet_classes::contract_class::ContractClass` + let compilation_result = + self.command_line_compiler.compile_to_native(sierra_contract_class.into()); + match compilation_result { + Ok(contract_executor) => { + self.contract_cache_manager + .set_native_contract_executor(class_hash, Some(contract_executor)); + } + Err(err) => { + eprintln!("Error compiling contract class: {}", err); + self.contract_cache_manager.set_native_contract_executor(class_hash, None); + } + } + } + } +}