Skip to content

Commit

Permalink
refactor: separate creation of the test state reader factory
Browse files Browse the repository at this point in the history
commit-id:a8d6e015
  • Loading branch information
Itay-Tsabary-Starkware committed Jul 4, 2024
1 parent b5fb285 commit f2a5743
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
19 changes: 15 additions & 4 deletions crates/tests-integration/src/integration_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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(),
}
}
20 changes: 4 additions & 16 deletions crates/tests-integration/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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(
Expand Down

0 comments on commit f2a5743

Please sign in to comment.