From 853b8555e7a14ca77fb7ffaad4e7219ccbe94dff 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 | 3 +++ 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 +- .../src/stateful_transaction_validator_test.rs | 17 +---------------- crates/tests-integration/build.rs | 3 +++ 7 files changed, 21 insertions(+), 23 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..2c3d009e87c --- /dev/null +++ b/crates/gateway/build.rs @@ -0,0 +1,3 @@ +// Sets up the environment variable OUT_DIR, which holds the cairo compiler binary. +// The binary is dowloaded to OUT_DIR by the starknet_sierra_compile crate. +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 00158586095..46e09674173 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 afefb0ae9c7..df439401966 100644 --- a/crates/gateway/src/stateful_transaction_validator_test.rs +++ b/crates/gateway/src/stateful_transaction_validator_test.rs @@ -3,7 +3,6 @@ use blockifier::blockifier::stateful_validator::{ StatefulValidatorResult as BlockifierStatefulValidatorResult, }; use blockifier::context::BlockContext; -use blockifier::execution::contract_class::ClassInfo; use blockifier::test_utils::CairoVersion; use blockifier::transaction::errors::{TransactionFeeError, TransactionPreValidationError}; use mempool_test_utils::invoke_tx_args; @@ -23,11 +22,9 @@ use starknet_api::core::{ContractAddress, Nonce, PatriciaKey}; use starknet_api::rpc_transaction::RpcTransaction; use starknet_api::transaction::TransactionHash; use starknet_api::{contract_address, felt, patricia_key}; -use starknet_sierra_compile::config::SierraToCasmCompilationConfig; use starknet_types_core::felt::Felt; use super::ValidateInfo; -use crate::compilation::GatewayCompiler; use crate::config::StatefulTransactionValidatorConfig; use crate::errors::GatewaySpecError; use crate::state_reader::{MockStateReaderFactory, StateReaderFactory}; @@ -82,18 +79,6 @@ fn test_stateful_tx_validator( #[case] expected_result: BlockifierStatefulValidatorResult, stateful_validator: StatefulTransactionValidator, ) { - let optional_class_info = match &rpc_tx { - RpcTransaction::Declare(declare_tx) => Some( - ClassInfo::try_from( - GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default()) - .process_declare_tx(declare_tx) - .unwrap(), - ) - .unwrap(), - ), - _ => None, - }; - let expected_result_as_stateful_transaction_result = expected_result.as_ref().map(|validate_info| *validate_info).map_err(|blockifier_error| { GatewaySpecError::ValidationFailure { data: blockifier_error.to_string() } @@ -103,7 +88,7 @@ fn test_stateful_tx_validator( mock_validator.expect_validate().return_once(|_, _| expected_result.map(|_| ())); mock_validator.expect_get_nonce().returning(|_| Ok(Nonce(Felt::ZERO))); - let result = stateful_validator.run_validate(&rpc_tx, optional_class_info, mock_validator); + let result = stateful_validator.run_validate(&rpc_tx, None, mock_validator); assert_eq!(result, expected_result_as_stateful_transaction_result); } diff --git a/crates/tests-integration/build.rs b/crates/tests-integration/build.rs new file mode 100644 index 00000000000..2c3d009e87c --- /dev/null +++ b/crates/tests-integration/build.rs @@ -0,0 +1,3 @@ +// Sets up the environment variable OUT_DIR, which holds the cairo compiler binary. +// The binary is dowloaded to OUT_DIR by the starknet_sierra_compile crate. +fn main() {}