diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index 5bb911cd18..11b8858825 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -1,10 +1,14 @@ +use std::sync::Arc; + use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractClassV1}; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use cairo_lang_starknet_classes::contract_class::ContractClass as CairoLangContractClass; use starknet_api::core::CompiledClassHash; use starknet_api::rpc_transaction::RpcDeclareTransaction; -use starknet_sierra_compile::compile::SierraToCasmCompiler; +use starknet_sierra_compile::compile::CairoLangCompiler; +use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use starknet_sierra_compile::utils::into_contract_class_for_compilation; +use starknet_sierra_compile::SierraToCasmCompiler; use crate::errors::{GatewayError, GatewayResult}; @@ -15,10 +19,14 @@ mod compilation_test; // TODO(Arni): Pass the compiler with dependancy injection. #[derive(Clone)] pub struct GatewayCompiler { - pub sierra_to_casm_compiler: SierraToCasmCompiler, + pub sierra_to_casm_compiler: Arc, } impl GatewayCompiler { + pub fn new_cairo_lang_compiler(config: SierraToCasmCompilationConfig) -> Self { + Self { sierra_to_casm_compiler: Arc::new(CairoLangCompiler { config }) } + } + /// Formats the contract class for compilation, compiles it, and returns the compiled contract /// class wrapped in a [`ClassInfo`]. /// Assumes the contract class is of a Sierra program which is compiled to Casm. diff --git a/crates/gateway/src/compilation_test.rs b/crates/gateway/src/compilation_test.rs index 2a591df3be..24cb49748d 100644 --- a/crates/gateway/src/compilation_test.rs +++ b/crates/gateway/src/compilation_test.rs @@ -11,7 +11,6 @@ use starknet_api::rpc_transaction::{ RpcDeclareTransactionV3, RpcTransaction, }; -use starknet_sierra_compile::compile::SierraToCasmCompiler; use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use starknet_sierra_compile::errors::CompilationUtilError; @@ -20,7 +19,7 @@ use crate::errors::GatewayError; #[fixture] fn gateway_compiler() -> GatewayCompiler { - GatewayCompiler { sierra_to_casm_compiler: SierraToCasmCompiler { config: Default::default() } } + GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default()) } #[fixture] @@ -53,11 +52,10 @@ fn test_compile_contract_class_compiled_class_hash_mismatch( // TODO(Arni): Redesign this test once the compiler is passed with dependancy injection. #[rstest] fn test_compile_contract_class_bytecode_size_validation(declare_tx_v3: RpcDeclareTransactionV3) { - let gateway_compiler = GatewayCompiler { - sierra_to_casm_compiler: SierraToCasmCompiler { - config: SierraToCasmCompilationConfig { max_bytecode_size: 1 }, - }, - }; + let gateway_compiler = + GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig { + max_bytecode_size: 1, + }); let result = gateway_compiler.process_declare_tx(&RpcDeclareTransaction::V3(declare_tx_v3)); assert_matches!( diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index 326b1141ac..6be26c424b 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -11,7 +11,6 @@ use starknet_api::transaction::TransactionHash; use starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter}; use starknet_mempool_types::communication::SharedMempoolClient; use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput}; -use starknet_sierra_compile::compile::SierraToCasmCompiler; use tracing::{info, instrument}; use crate::compilation::GatewayCompiler; @@ -155,11 +154,10 @@ pub fn create_gateway( mempool_client: SharedMempoolClient, ) -> Gateway { let state_reader_factory = Arc::new(RpcStateReaderFactory { config: rpc_state_reader_config }); - let gateway_compiler = GatewayCompiler { - sierra_to_casm_compiler: SierraToCasmCompiler { - config: config.compiler_config.sierra_to_casm_compiler_config, - }, - }; + let gateway_compiler = GatewayCompiler::new_cairo_lang_compiler( + config.compiler_config.sierra_to_casm_compiler_config, + ); + Gateway::new(config, state_reader_factory, gateway_compiler, mempool_client) } diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index 1f7554b4cc..d73c76ad46 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -13,7 +13,6 @@ use starknet_api::rpc_transaction::RpcTransaction; use starknet_api::transaction::TransactionHash; use starknet_mempool_types::communication::MockMempoolClient; use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput, ThinTransaction}; -use starknet_sierra_compile::compile::SierraToCasmCompiler; use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use crate::compilation::GatewayCompiler; @@ -35,11 +34,9 @@ pub fn app_state( stateful_tx_validator: Arc::new(StatefulTransactionValidator { config: StatefulTransactionValidatorConfig::create_for_testing(), }), - gateway_compiler: GatewayCompiler { - sierra_to_casm_compiler: SierraToCasmCompiler { - config: SierraToCasmCompilationConfig::default(), - }, - }, + gateway_compiler: GatewayCompiler::new_cairo_lang_compiler( + SierraToCasmCompilationConfig::default(), + ), state_reader_factory: Arc::new(state_reader_factory), mempool_client, } diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index 595b0ba251..fff8035dcd 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -19,7 +19,7 @@ use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::felt; use starknet_api::rpc_transaction::RpcTransaction; use starknet_api::transaction::TransactionHash; -use starknet_sierra_compile::compile::SierraToCasmCompiler; +use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use starknet_types_core::felt::Felt; use crate::compilation::GatewayCompiler; @@ -84,11 +84,9 @@ fn test_stateful_tx_validator( ) { let optional_class_info = match &external_tx { RpcTransaction::Declare(declare_tx) => Some( - GatewayCompiler { - sierra_to_casm_compiler: SierraToCasmCompiler { config: Default::default() }, - } - .process_declare_tx(declare_tx) - .unwrap(), + GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default()) + .process_declare_tx(declare_tx) + .unwrap(), ), _ => None, }; diff --git a/crates/starknet_sierra_compile/src/compile.rs b/crates/starknet_sierra_compile/src/compile.rs index 54c9843c2e..3bdd90eda9 100644 --- a/crates/starknet_sierra_compile/src/compile.rs +++ b/crates/starknet_sierra_compile/src/compile.rs @@ -6,18 +6,21 @@ use cairo_lang_starknet_classes::contract_class::ContractClass; use crate::config::SierraToCasmCompilationConfig; use crate::errors::CompilationUtilError; +use crate::SierraToCasmCompiler; #[cfg(test)] #[path = "compile_test.rs"] pub mod compile_test; +/// A compiler that compiles Sierra programs to Casm. Uses the code from the +/// `cairo_lang_starknet_classes` crate. #[derive(Clone)] -pub struct SierraToCasmCompiler { +pub struct CairoLangCompiler { pub config: SierraToCasmCompilationConfig, } -impl SierraToCasmCompiler { - pub fn compile_sierra_to_casm( +impl SierraToCasmCompiler for CairoLangCompiler { + fn compile_sierra_to_casm( &self, contract_class: ContractClass, ) -> Result { @@ -28,7 +31,9 @@ impl SierraToCasmCompiler { Ok(casm_contract_class) } +} +impl CairoLangCompiler { fn compile_sierra_to_casm_inner( &self, contract_class: ContractClass, diff --git a/crates/starknet_sierra_compile/src/compile_test.rs b/crates/starknet_sierra_compile/src/compile_test.rs index bf7ad11a5b..95d811178d 100644 --- a/crates/starknet_sierra_compile/src/compile_test.rs +++ b/crates/starknet_sierra_compile/src/compile_test.rs @@ -6,17 +6,20 @@ use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError; use mempool_test_utils::{get_absolute_path, FAULTY_ACCOUNT_CLASS_FILE, TEST_FILES_FOLDER}; use rstest::{fixture, rstest}; -use crate::compile::{CompilationUtilError, SierraToCasmCompiler}; +use crate::compile::{CairoLangCompiler, CompilationUtilError}; use crate::config::SierraToCasmCompilationConfig; use crate::test_utils::contract_class_from_file; +use crate::SierraToCasmCompiler; #[fixture] -fn compiler() -> SierraToCasmCompiler { - SierraToCasmCompiler { config: SierraToCasmCompilationConfig { max_bytecode_size: 81920 } } +fn compiler() -> impl SierraToCasmCompiler { + CairoLangCompiler { + config: SierraToCasmCompilationConfig { max_bytecode_size: 81920 }, + } } #[rstest] -fn test_compile_sierra_to_casm(compiler: SierraToCasmCompiler) { +fn test_compile_sierra_to_casm(compiler: impl SierraToCasmCompiler) { env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Failed to set current dir."); let sierra_path = Path::new(FAULTY_ACCOUNT_CLASS_FILE); let expected_casm_contract_length = 72304; @@ -30,7 +33,7 @@ fn test_compile_sierra_to_casm(compiler: SierraToCasmCompiler) { // TODO(Arni, 1/5/2024): Add a test for panic result test. #[rstest] -fn test_negative_flow_compile_sierra_to_casm(compiler: SierraToCasmCompiler) { +fn test_negative_flow_compile_sierra_to_casm(compiler: impl SierraToCasmCompiler) { env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Failed to set current dir."); let sierra_path = Path::new(FAULTY_ACCOUNT_CLASS_FILE); diff --git a/crates/starknet_sierra_compile/src/lib.rs b/crates/starknet_sierra_compile/src/lib.rs index 162e53db71..be931e80dc 100644 --- a/crates/starknet_sierra_compile/src/lib.rs +++ b/crates/starknet_sierra_compile/src/lib.rs @@ -1,4 +1,8 @@ //! A lib for compiling Sierra into Casm. +use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; +use cairo_lang_starknet_classes::contract_class::ContractClass; + +use crate::errors::CompilationUtilError; pub mod compile; pub mod config; @@ -7,3 +11,10 @@ pub mod utils; #[cfg(test)] pub mod test_utils; + +pub trait SierraToCasmCompiler: Send + Sync { + fn compile_sierra_to_casm( + &self, + contract_class: ContractClass, + ) -> Result; +}