Skip to content

Commit

Permalink
feat: use the command line compiler in gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 27, 2024
1 parent e3e4d61 commit ebd8ce3
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
3 changes: 3 additions & 0 deletions crates/gateway/build.rs
Original file line number Diff line number Diff line change
@@ -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() {}
6 changes: 6 additions & 0 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 }) }
}
Expand Down
11 changes: 6 additions & 5 deletions crates/gateway/src/compilation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()));
}

Expand All @@ -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()));
}

Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 1 addition & 16 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -82,18 +79,6 @@ fn test_stateful_tx_validator(
#[case] expected_result: BlockifierStatefulValidatorResult<ValidateInfo>,
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() }
Expand All @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions crates/tests-integration/build.rs
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit ebd8ce3

Please sign in to comment.