From 73d32b7cff8a4ef6e665780899f5a938f7f0f70f Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 26 Aug 2024 15:41:33 +0300 Subject: [PATCH] feat: use the command line compiler in gateway --- crates/gateway/build.rs | 2 ++ crates/gateway/src/compilation.rs | 6 ++++++ crates/gateway/src/compilation_test.rs | 11 +++++----- crates/gateway/src/gateway.rs | 2 +- crates/gateway/src/gateway_test.rs | 2 +- .../stateful_transaction_validator_test.rs | 21 +++++++++++-------- crates/tests-integration/build.rs | 3 +++ 7 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 crates/gateway/build.rs create mode 100644 crates/tests-integration/build.rs diff --git a/crates/gateway/build.rs b/crates/gateway/build.rs new file mode 100644 index 00000000000..1024f5d5cb4 --- /dev/null +++ b/crates/gateway/build.rs @@ -0,0 +1,2 @@ +// Sets up the environment variable OUT_DIR. +fn main() {} diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index aab1affc40f..45330c01e7d 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -6,6 +6,7 @@ use starknet_api::contract_class::ClassInfo; use starknet_api::core::CompiledClassHash; use starknet_api::rpc_transaction::RpcDeclareTransaction; use starknet_sierra_compile::cairo_lang_compiler::CairoLangSierraToCasmCompiler; +use starknet_sierra_compile::command_line_compiler::CommandLineCompiler; use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use starknet_sierra_compile::utils::into_contract_class_for_compilation; use starknet_sierra_compile::SierraToCasmCompiler; @@ -24,6 +25,11 @@ pub struct GatewayCompiler { } impl GatewayCompiler { + pub fn new_command_line_compiler(config: SierraToCasmCompilationConfig) -> Self { + Self { sierra_to_casm_compiler: Arc::new(CommandLineCompiler::new(config)) } + } + + // TODO(Arni): Cosider deleting `CairoLangSierraToCasmCompiler`. pub fn new_cairo_lang_compiler(config: SierraToCasmCompilationConfig) -> Self { Self { sierra_to_casm_compiler: Arc::new(CairoLangSierraToCasmCompiler { config }) } } diff --git a/crates/gateway/src/compilation_test.rs b/crates/gateway/src/compilation_test.rs index 8c41913c12f..fd1ac085007 100644 --- a/crates/gateway/src/compilation_test.rs +++ b/crates/gateway/src/compilation_test.rs @@ -19,7 +19,7 @@ use crate::errors::GatewaySpecError; #[fixture] fn gateway_compiler() -> GatewayCompiler { - GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default()) + GatewayCompiler::new_command_line_compiler(SierraToCasmCompilationConfig::default()) } #[fixture] @@ -58,14 +58,15 @@ fn test_compile_contract_class_compiled_class_hash_mismatch( #[rstest] fn test_compile_contract_class_bytecode_size_validation(declare_tx_v3: RpcDeclareTransactionV3) { let gateway_compiler = - GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig { + GatewayCompiler::new_command_line_compiler(SierraToCasmCompilationConfig { max_bytecode_size: 1, }); let result = gateway_compiler.process_declare_tx(&RpcDeclareTransaction::V3(declare_tx_v3)); assert_matches!(result.unwrap_err(), GatewaySpecError::CompilationFailed); - let expected_compilation_error = - CompilationUtilError::CompilationError("Code size limit exceeded.".to_owned()); + let expected_compilation_error = CompilationUtilError::CompilationError( + "Error: Compilation failed.\n\nCaused by:\n Code size limit exceeded.\n".to_owned(), + ); assert!(logs_contain(format!("Compilation failed: {:?}", expected_compilation_error).as_str())); } @@ -84,7 +85,7 @@ fn test_compile_contract_class_bad_sierra( assert_eq!(err, GatewaySpecError::CompilationFailed); let expected_compilation_error = - CompilationUtilError::CompilationError("Invalid Sierra program.".to_owned()); + CompilationUtilError::CompilationError("Error: Invalid Sierra program.\n".to_owned()); assert!(logs_contain(format!("Compilation failed: {:?}", expected_compilation_error).as_str())); } diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index 7a8ca450932..52a0bcd3ca4 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -163,7 +163,7 @@ pub fn create_gateway( mempool_client: SharedMempoolClient, ) -> Gateway { let state_reader_factory = Arc::new(RpcStateReaderFactory { config: rpc_state_reader_config }); - let gateway_compiler = GatewayCompiler::new_cairo_lang_compiler(compiler_config); + let gateway_compiler = GatewayCompiler::new_command_line_compiler(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 0ff0ec93c5d..6c19b9937f8 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -34,7 +34,7 @@ pub fn app_state( stateful_tx_validator: Arc::new(StatefulTransactionValidator { config: StatefulTransactionValidatorConfig::create_for_testing(), }), - gateway_compiler: GatewayCompiler::new_cairo_lang_compiler( + gateway_compiler: GatewayCompiler::new_command_line_compiler( SierraToCasmCompilationConfig::default(), ), state_reader_factory: Arc::new(state_reader_factory), diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index 004691bc089..89ddec92798 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -82,17 +82,20 @@ fn test_stateful_tx_validator( #[case] expected_result: BlockifierStatefulValidatorResult, stateful_validator: StatefulTransactionValidator, ) { - let optional_class_info = match &external_tx { - RpcTransaction::Declare(declare_tx) => Some( - ClassInfo::try_from( - GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default()) + let optional_class_info = + match &external_tx { + RpcTransaction::Declare(declare_tx) => Some( + ClassInfo::try_from( + GatewayCompiler::new_command_line_compiler( + SierraToCasmCompilationConfig::default(), + ) .process_declare_tx(declare_tx) .unwrap(), - ) - .unwrap(), - ), - _ => None, - }; + ) + .unwrap(), + ), + _ => None, + }; let expected_result_as_stateful_transaction_result = expected_result.as_ref().map(|validate_info| *validate_info).map_err(|blockifier_error| { diff --git a/crates/tests-integration/build.rs b/crates/tests-integration/build.rs new file mode 100644 index 00000000000..16e05716f51 --- /dev/null +++ b/crates/tests-integration/build.rs @@ -0,0 +1,3 @@ +// Sets up the environment variable OUT_DIR. This is necessary for for the crate +// starknet_sierra_compile. +fn main() {}