From f2a5743ca1a9ef72576ea9f1fe5e247cb0dda5ea Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Thu, 4 Jul 2024 13:43:58 +0300 Subject: [PATCH] refactor: separate creation of the test state reader factory commit-id:a8d6e015 --- .../src/integration_test_utils.rs | 19 ++++++++++++++---- crates/tests-integration/src/state_reader.rs | 20 ++++--------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/crates/tests-integration/src/integration_test_utils.rs b/crates/tests-integration/src/integration_test_utils.rs index d607f76c3..08e6c370c 100644 --- a/crates/tests-integration/src/integration_test_utils.rs +++ b/crates/tests-integration/src/integration_test_utils.rs @@ -6,15 +6,16 @@ use reqwest::{Client, Response}; use starknet_api::rpc_transaction::RPCTransaction; use starknet_api::transaction::TransactionHash; use starknet_gateway::config::{ - GatewayConfig, GatewayNetworkConfig, StatefulTransactionValidatorConfig, + GatewayConfig, GatewayNetworkConfig, RpcStateReaderConfig, StatefulTransactionValidatorConfig, StatelessTransactionValidatorConfig, }; use starknet_gateway::errors::GatewayError; use starknet_gateway::gateway::Gateway; +use starknet_gateway::rpc_state_reader::RpcStateReaderFactory; use starknet_mempool_types::communication::SharedMempoolClient; use test_utils::starknet_api_test_utils::external_tx_to_json; -use crate::state_reader::rpc_test_state_reader_factory; +use crate::state_reader::spawn_test_rpc_state_reader; pub async fn create_gateway( mempool_client: SharedMempoolClient, @@ -37,8 +38,9 @@ pub async fn create_gateway( stateful_tx_validator_config, }; - let state_reader_factory = - Arc::new(rpc_test_state_reader_factory(n_initialized_account_contracts).await); + let rpc_server_addr = spawn_test_rpc_state_reader(n_initialized_account_contracts).await; + let rpc_state_reader_config = spawn_test_rpc_state_reader_config(rpc_server_addr); + let state_reader_factory = Arc::new(RpcStateReaderFactory { config: rpc_state_reader_config }); Gateway::new(gateway_config, state_reader_factory, mempool_client) } @@ -80,3 +82,12 @@ impl GatewayClient { .unwrap() } } + +fn spawn_test_rpc_state_reader_config(rpc_server_addr: SocketAddr) -> RpcStateReaderConfig { + const RPC_SPEC_VERION: &str = "V0_7"; + const JSON_RPC_VERSION: &str = "2.0"; + RpcStateReaderConfig { + url: format!("http://{rpc_server_addr:?}/rpc/{RPC_SPEC_VERION}"), + json_rpc_version: JSON_RPC_VERSION.to_string(), + } +} diff --git a/crates/tests-integration/src/state_reader.rs b/crates/tests-integration/src/state_reader.rs index 81d2e6e41..d3a9f5203 100644 --- a/crates/tests-integration/src/state_reader.rs +++ b/crates/tests-integration/src/state_reader.rs @@ -28,8 +28,6 @@ use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContract use starknet_api::state::{StorageKey, ThinStateDiff}; use starknet_api::{contract_address, felt, patricia_key}; use starknet_client::reader::PendingData; -use starknet_gateway::config::RpcStateReaderConfig; -use starknet_gateway::rpc_state_reader::RpcStateReaderFactory; use starknet_types_core::felt::Felt; use strum::IntoEnumIterator; use tempfile::tempdir; @@ -49,13 +47,10 @@ fn deploy_account_tx_contract_address() -> &'static ContractAddress { /// StateReader for integration tests. /// -/// Creates a papyrus storage reader and Spawns a papyrus rpc server for it. +/// Creates a papyrus storage reader and spawns a papyrus rpc server for it. +/// Returns the address of the rpc server. /// A variable number of identical accounts and test contracts are initialized and funded. -pub async fn rpc_test_state_reader_factory( - n_initialized_account_contracts: u16, -) -> RpcStateReaderFactory { - const RPC_SPEC_VERION: &str = "V0_7"; - const JSON_RPC_VERSION: &str = "2.0"; +pub async fn spawn_test_rpc_state_reader(n_initialized_account_contracts: u16) -> SocketAddr { let block_context = BlockContext::create_for_testing(); let account_contract_cairo0 = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0); let test_contract_cairo0 = FeatureContract::TestContract(CairoVersion::Cairo0); @@ -76,14 +71,7 @@ pub async fn rpc_test_state_reader_factory( ], fund_accounts, ); - let addr = run_papyrus_rpc_server(storage_reader).await; - - RpcStateReaderFactory { - config: RpcStateReaderConfig { - url: format!("http://{addr:?}/rpc/{RPC_SPEC_VERION}"), - json_rpc_version: JSON_RPC_VERSION.to_string(), - }, - } + run_papyrus_rpc_server(storage_reader).await } fn initialize_papyrus_test_state(