From 4bf529f7fa3337e27f1973d8412db18af6bc5dd1 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 5 Aug 2024 08:59:07 +0300 Subject: [PATCH] refactor: define sierra to casm compiler trait --- crates/gateway/src/compilation.rs | 12 ++++++++++-- crates/gateway/src/compilation_test.rs | 12 +++++------- crates/gateway/src/gateway.rs | 10 ++++------ crates/gateway/src/gateway_test.rs | 9 +++------ .../src/stateful_transaction_validator_test.rs | 10 ++++------ crates/starknet_sierra_compile/src/compile.rs | 11 ++++++++--- crates/starknet_sierra_compile/src/compile_test.rs | 11 ++++++----- crates/starknet_sierra_compile/src/lib.rs | 11 +++++++++++ 8 files changed, 51 insertions(+), 35 deletions(-) diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index 2463b0cd0ff..bc6ef96a1ca 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::CairoLangSierraToCasmCompiler; +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(CairoLangSierraToCasmCompiler { 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 2a591df3be3..24cb49748da 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 326b1141ac8..6be26c424bf 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 1f7554b4ccb..d73c76ad464 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 595b0ba2511..fff8035dcd1 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 b07e281e276..7565b29afa5 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 CairoLangSierraToCasmCompiler { pub config: SierraToCasmCompilationConfig, } -impl SierraToCasmCompiler { - pub fn compile( +impl SierraToCasmCompiler for CairoLangSierraToCasmCompiler { + fn compile( &self, contract_class: ContractClass, ) -> Result { @@ -27,7 +30,9 @@ impl SierraToCasmCompiler { Ok(casm_contract_class) } +} +impl CairoLangSierraToCasmCompiler { fn compile_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 bea5973a3e8..30afaa24e7e 100644 --- a/crates/starknet_sierra_compile/src/compile_test.rs +++ b/crates/starknet_sierra_compile/src/compile_test.rs @@ -6,17 +6,18 @@ 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::{CairoLangSierraToCasmCompiler, CompilationUtilError}; use crate::config::SierraToCasmCompilationConfig; use crate::test_utils::contract_class_from_file; +use crate::SierraToCasmCompiler; #[fixture] -fn compiler() -> SierraToCasmCompiler { - SierraToCasmCompiler { config: SierraToCasmCompilationConfig::default() } +fn compiler() -> impl SierraToCasmCompiler { + CairoLangSierraToCasmCompiler { config: SierraToCasmCompilationConfig::default() } } #[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 +31,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 162e53db719..d17a287aeb3 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( + &self, + contract_class: ContractClass, + ) -> Result; +}