From 33dbd4d56d019c5269e69bb286e9d9dda87c355b Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 8 Jul 2024 09:23:31 +0300 Subject: [PATCH] chore: add the gateway compiler to appstate --- crates/gateway/src/compilation.rs | 1 + crates/gateway/src/config.rs | 2 +- crates/gateway/src/gateway.rs | 9 +++++++-- crates/gateway/src/gateway_test.rs | 20 +++++++++++-------- .../stateful_transaction_validator_test.rs | 11 +++++----- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index 1df6d0d99..18dd58197 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -19,6 +19,7 @@ use crate::utils::is_subsequence; #[path = "compilation_test.rs"] mod compilation_test; +#[derive(Clone)] pub struct GatewayCompiler { #[allow(dead_code)] pub config: GatewayCompilerConfig, diff --git a/crates/gateway/src/config.rs b/crates/gateway/src/config.rs index 389bc4f69..49970fa7a 100644 --- a/crates/gateway/src/config.rs +++ b/crates/gateway/src/config.rs @@ -296,7 +296,7 @@ impl StatefulTransactionValidatorConfig { } } -#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)] +#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, Validate, PartialEq)] pub struct GatewayCompilerConfig {} impl SerializeConfig for GatewayCompilerConfig { diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index a4f611bed..920be0ae0 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -36,6 +36,7 @@ pub struct AppState { pub stateless_tx_validator: StatelessTransactionValidator, pub stateful_tx_validator: Arc, pub state_reader_factory: Arc, + pub gateway_compiler: GatewayCompiler, pub mempool_client: SharedMempoolClient, } @@ -43,6 +44,7 @@ impl Gateway { pub fn new( config: GatewayConfig, state_reader_factory: Arc, + gateway_compiler: GatewayCompiler, mempool_client: SharedMempoolClient, ) -> Self { let app_state = AppState { @@ -53,6 +55,7 @@ impl Gateway { config: config.stateful_tx_validator_config.clone(), }), state_reader_factory, + gateway_compiler, mempool_client, }; Gateway { config, app_state } @@ -93,6 +96,7 @@ async fn add_tx( app_state.stateless_tx_validator, app_state.stateful_tx_validator.as_ref(), app_state.state_reader_factory.as_ref(), + app_state.gateway_compiler, tx, ) }) @@ -113,6 +117,7 @@ fn process_tx( stateless_tx_validator: StatelessTransactionValidator, stateful_tx_validator: &StatefulTransactionValidator, state_reader_factory: &dyn StateReaderFactory, + gateway_compiler: GatewayCompiler, tx: RPCTransaction, ) -> GatewayResult { // TODO(Arni, 1/5/2024): Perform congestion control. @@ -123,7 +128,6 @@ fn process_tx( // Compile Sierra to Casm. let optional_class_info = match &tx { RPCTransaction::Declare(declare_tx) => { - let gateway_compiler = GatewayCompiler { config: Default::default() }; Some(gateway_compiler.compile_contract_class(declare_tx)?) } _ => None, @@ -145,7 +149,8 @@ pub fn create_gateway( client: SharedMempoolClient, ) -> Gateway { let state_reader_factory = Arc::new(RpcStateReaderFactory { config: rpc_state_reader_config }); - Gateway::new(config, state_reader_factory, client) + let gateway_compiler = GatewayCompiler { config: config.compiler_config }; + Gateway::new(config, state_reader_factory, gateway_compiler, client) } #[async_trait] diff --git a/crates/gateway/src/gateway_test.rs b/crates/gateway/src/gateway_test.rs index e4288afe5..5216c8797 100644 --- a/crates/gateway/src/gateway_test.rs +++ b/crates/gateway/src/gateway_test.rs @@ -17,7 +17,9 @@ use starknet_mempool_types::communication::{MempoolClientImpl, MempoolRequestAnd use tokio::sync::mpsc::channel; use tokio::task; -use crate::config::{StatefulTransactionValidatorConfig, StatelessTransactionValidatorConfig}; +use crate::config::{ + GatewayCompilerConfig, StatefulTransactionValidatorConfig, StatelessTransactionValidatorConfig, +}; use crate::gateway::{add_tx, AppState, GatewayCompiler, SharedMempoolClient}; use crate::state_reader_test_utils::{ local_test_state_reader_factory, local_test_state_reader_factory_for_deploy_account, @@ -52,6 +54,7 @@ pub fn app_state( stateful_tx_validator: Arc::new(StatefulTransactionValidator { config: StatefulTransactionValidatorConfig::create_for_testing(), }), + gateway_compiler: GatewayCompiler { config: GatewayCompilerConfig {} }, state_reader_factory: Arc::new(state_reader_factory), mempool_client, } @@ -94,7 +97,7 @@ async fn test_add_tx( let app_state = app_state(mempool_client, state_reader_factory); - let tx_hash = calculate_hash(&tx); + let tx_hash = calculate_hash(&tx, &app_state.gateway_compiler); let response = add_tx(State(app_state), tx.into()).await.into_response(); let status_code = response.status(); @@ -108,13 +111,14 @@ async fn to_bytes(res: Response) -> Bytes { res.into_body().collect().await.unwrap().to_bytes() } -fn calculate_hash(external_tx: &RPCTransaction) -> TransactionHash { +fn calculate_hash( + external_tx: &RPCTransaction, + gateway_compiler: &GatewayCompiler, +) -> TransactionHash { let optional_class_info = match &external_tx { - RPCTransaction::Declare(declare_tx) => Some( - GatewayCompiler { config: Default::default() } - .compile_contract_class(declare_tx) - .unwrap(), - ), + RPCTransaction::Declare(declare_tx) => { + Some(gateway_compiler.compile_contract_class(declare_tx).unwrap()) + } _ => None, }; diff --git a/crates/gateway/src/stateful_transaction_validator_test.rs b/crates/gateway/src/stateful_transaction_validator_test.rs index ffe1df19f..bd1216075 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::transaction::TransactionHash; use starknet_types_core::felt::Felt; use crate::compilation::GatewayCompiler; -use crate::config::StatefulTransactionValidatorConfig; +use crate::config::{GatewayCompilerConfig, StatefulTransactionValidatorConfig}; use crate::errors::{StatefulTransactionValidatorError, StatefulTransactionValidatorResult}; use crate::state_reader_test_utils::{ local_test_state_reader_factory, local_test_state_reader_factory_for_deploy_account, @@ -95,10 +95,11 @@ fn test_stateful_tx_validator( stateful_validator: StatefulTransactionValidator, ) { let optional_class_info = match &external_tx { - RPCTransaction::Declare(declare_tx) => { - let gateway_compiler = GatewayCompiler { config: Default::default() }; - Some(gateway_compiler.compile_contract_class(declare_tx).unwrap()) - } + RPCTransaction::Declare(declare_tx) => Some( + GatewayCompiler { config: GatewayCompilerConfig {} } + .compile_contract_class(declare_tx) + .unwrap(), + ), _ => None, };