Skip to content

Commit

Permalink
feat(cairo_native): add batcher compiler struct
Browse files Browse the repository at this point in the history
  • Loading branch information
avi-starkware committed Nov 19, 2024
1 parent fc634d8 commit a25f4bd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/execution/native.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod batcher_compiler;
pub mod contract_class;
pub mod entry_point_execution;
pub mod syscall_handler;
Expand Down
50 changes: 50 additions & 0 deletions crates/blockifier/src/execution/native/batcher_compiler.rs
Original file line number Diff line number Diff line change
@@ -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<SierraContractClass>);

struct BatcherCompiler {
contract_cache_manager: GlobalContractCacheManager,
compilation_request_receiver: Receiver<CompilationRequest>,
command_line_compiler: CommandLineCompiler,
}

impl BatcherCompiler {
pub fn new(
contract_cache_manager: GlobalContractCacheManager,
compilation_request_receiver: Receiver<CompilationRequest>,
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);
}
}
}
}
}

0 comments on commit a25f4bd

Please sign in to comment.