Skip to content

Commit

Permalink
refactor: define sierra to casm compiler trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 6, 2024
1 parent bb73e86 commit 6bc1920
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 35 deletions.
12 changes: 10 additions & 2 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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<dyn SierraToCasmCompiler>,
}

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.
Expand Down
12 changes: 5 additions & 7 deletions crates/gateway/src/compilation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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]
Expand Down Expand Up @@ -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!(
Expand Down
10 changes: 4 additions & 6 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 3 additions & 6 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
}
Expand Down
10 changes: 4 additions & 6 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
Expand Down
11 changes: 8 additions & 3 deletions crates/starknet_sierra_compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CasmContractClass, CompilationUtilError> {
Expand All @@ -28,7 +31,9 @@ impl SierraToCasmCompiler {

Ok(casm_contract_class)
}
}

impl CairoLangCompiler {
fn compile_sierra_to_casm_inner(
&self,
contract_class: ContractClass,
Expand Down
13 changes: 8 additions & 5 deletions crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
11 changes: 11 additions & 0 deletions crates/starknet_sierra_compile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CasmContractClass, CompilationUtilError>;
}

0 comments on commit 6bc1920

Please sign in to comment.