diff --git a/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs b/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs index 21ae13f030..45ed4a1934 100644 --- a/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs +++ b/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs @@ -167,7 +167,8 @@ async fn test_mempool_receives_tx_from_other_peer( let (config, mut broadcast_channels, _temp_dir_handles) = setup(&tx_generator, TestIdentifier::MempoolReceivesTxFromOtherPeerTest).await; let (clients, servers) = create_node_modules(&config); - let mempool_client = clients.get_mempool_shared_client().unwrap(); + let mempool_client = + clients.get_mempool_shared_client(&config.components.mempool_p2p.execution_mode).unwrap(); // Build and run the sequencer node. let sequencer_node_future = run_component_servers(servers); let _sequencer_node_handle = tokio::spawn(sequencer_node_future); diff --git a/crates/starknet_sequencer_node/src/clients.rs b/crates/starknet_sequencer_node/src/clients.rs index db5a9ad2f6..3156befa80 100644 --- a/crates/starknet_sequencer_node/src/clients.rs +++ b/crates/starknet_sequencer_node/src/clients.rs @@ -54,50 +54,75 @@ pub struct SequencerNodeClients { } /// A macro to retrieve a shared client wrapped in an `Arc`. The returned client is either the local -/// or the remote, as at most one of them exists. If neither, it returns `None`. +/// or the remote, based on the provided execution mode. If the execution mode is `Disabled` or +/// neither client exists, it returns `None`. /// /// # Arguments /// /// * `$self` - The `self` reference to the struct that contains the client field. /// * `$client_field` - The field name (within `self`) representing the client, which has both /// `local_client` and `remote_client` as options. +/// * `$execution_mode` - A reference to the `ReactiveComponentExecutionMode` that determines which +/// client to return (`local_client` or `remote_client`). /// /// # Returns /// /// An `Option>` containing the available client (local_client or -/// remote_client), wrapped in Arc. If neither exists, returns None. +/// remote_client), wrapped in `Arc`. If the execution mode is `Disabled` or neither client exists, +/// returns `None`. /// /// # Example /// /// ```rust,ignore -/// // Assuming `SequencerNodeClients` struct has fields `batcher_client` and `mempool_client. +/// // Assuming `SequencerNodeClients` struct has fields `batcher_client` and `mempool_client`. /// impl SequencerNodeClients { -/// pub fn get_batcher_shared_client(&self) -> Option> { -/// get_shared_client!(self, batcher_client) +/// pub fn get_batcher_shared_client( +/// &self, +/// execution_mode: &ReactiveComponentExecutionMode, +/// ) -> Option> { +/// get_shared_client!(self, batcher_client, execution_mode) /// } /// -/// pub fn get_mempool_shared_client(&self) -> Option> { -/// get_shared_client!(self, mempool_client) +/// pub fn get_mempool_shared_client( +/// &self, +/// execution_mode: &ReactiveComponentExecutionMode, +/// ) -> Option> { +/// get_shared_client!(self, mempool_client, execution_mode) /// } /// } /// ``` #[macro_export] macro_rules! get_shared_client { - ($self:ident, $client_field:ident) => {{ + ($self:ident, $client_field:ident, $execution_mode:expr) => {{ let client = &$self.$client_field; - if let Some(local_client) = client.get_local_client() { - return Some(Arc::new(local_client)); - } else if let Some(remote_client) = client.get_remote_client() { - return Some(Arc::new(remote_client)); + match &$execution_mode { + ReactiveComponentExecutionMode::Disabled => None, + ReactiveComponentExecutionMode::Remote => { + if let Some(remote_client) = client.get_remote_client() { + Some(Arc::new(remote_client)) + } else { + None + } + } + ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled + | ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled => { + if let Some(local_client) = client.get_local_client() { + Some(Arc::new(local_client)) + } else { + None + } + } } - None }}; } // TODO(Nadin): Refactor getters to remove code duplication. impl SequencerNodeClients { - pub fn get_batcher_shared_client(&self) -> Option { - get_shared_client!(self, batcher_client) + pub fn get_batcher_shared_client( + &self, + execution_mode: &ReactiveComponentExecutionMode, + ) -> Option { + get_shared_client!(self, batcher_client, execution_mode) } pub fn get_batcher_local_client( @@ -106,8 +131,11 @@ impl SequencerNodeClients { self.batcher_client.get_local_client() } - pub fn get_mempool_shared_client(&self) -> Option { - get_shared_client!(self, mempool_client) + pub fn get_mempool_shared_client( + &self, + execution_mode: &ReactiveComponentExecutionMode, + ) -> Option { + get_shared_client!(self, mempool_client, execution_mode) } pub fn get_mempool_local_client( @@ -116,8 +144,11 @@ impl SequencerNodeClients { self.mempool_client.get_local_client() } - pub fn get_gateway_shared_client(&self) -> Option { - get_shared_client!(self, gateway_client) + pub fn get_gateway_shared_client( + &self, + execution_mode: &ReactiveComponentExecutionMode, + ) -> Option { + get_shared_client!(self, gateway_client, execution_mode) } pub fn get_gateway_local_client( @@ -132,14 +163,19 @@ impl SequencerNodeClients { self.l1_provider_client.get_local_client() } - pub fn get_l1_provider_shared_client(&self) -> Option { - get_shared_client!(self, l1_provider_client) + pub fn get_l1_provider_shared_client( + &self, + execution_mode: &ReactiveComponentExecutionMode, + ) -> Option { + get_shared_client!(self, l1_provider_client, execution_mode) } pub fn get_mempool_p2p_propagator_shared_client( &self, + + execution_mode: &ReactiveComponentExecutionMode, ) -> Option { - get_shared_client!(self, mempool_p2p_propagator_client) + get_shared_client!(self, mempool_p2p_propagator_client, execution_mode) } pub fn get_mempool_p2p_propagator_local_client( @@ -149,8 +185,11 @@ impl SequencerNodeClients { self.mempool_p2p_propagator_client.get_local_client() } - pub fn get_state_sync_shared_client(&self) -> Option { - get_shared_client!(self, state_sync_client) + pub fn get_state_sync_shared_client( + &self, + execution_mode: &ReactiveComponentExecutionMode, + ) -> Option { + get_shared_client!(self, state_sync_client, execution_mode) } pub fn get_state_sync_local_client( diff --git a/crates/starknet_sequencer_node/src/components.rs b/crates/starknet_sequencer_node/src/components.rs index 8e335965aa..b41349f399 100644 --- a/crates/starknet_sequencer_node/src/components.rs +++ b/crates/starknet_sequencer_node/src/components.rs @@ -47,10 +47,11 @@ pub fn create_node_components( let batcher = match config.components.batcher.execution_mode { ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled | ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => { - let mempool_client = - clients.get_mempool_shared_client().expect("Mempool Client should be available"); + let mempool_client = clients + .get_mempool_shared_client(&config.components.mempool.execution_mode) + .expect("Mempool Client should be available"); let l1_provider_client = clients - .get_l1_provider_shared_client() + .get_l1_provider_shared_client(&config.components.l1_provider.execution_mode) .expect("L1 Provider Client should be available"); Some(create_batcher(config.batcher_config.clone(), mempool_client, l1_provider_client)) } @@ -58,10 +59,11 @@ pub fn create_node_components( }; let consensus_manager = match config.components.consensus_manager.execution_mode { ActiveComponentExecutionMode::Enabled => { - let batcher_client = - clients.get_batcher_shared_client().expect("Batcher Client should be available"); + let batcher_client = clients + .get_batcher_shared_client(&config.components.batcher.execution_mode) + .expect("Batcher Client should be available"); let state_sync_client = clients - .get_state_sync_shared_client() + .get_state_sync_shared_client(&config.components.state_sync.execution_mode) .expect("State Sync Client should be available"); Some(ConsensusManager::new( config.consensus_manager_config.clone(), @@ -74,10 +76,11 @@ pub fn create_node_components( let gateway = match config.components.gateway.execution_mode { ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled | ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => { - let mempool_client = - clients.get_mempool_shared_client().expect("Mempool Client should be available"); + let mempool_client = clients + .get_mempool_shared_client(&config.components.mempool.execution_mode) + .expect("Mempool Client should be available"); let state_sync_client = clients - .get_state_sync_shared_client() + .get_state_sync_shared_client(&config.components.state_sync.execution_mode) .expect("State Sync Client should be available"); Some(create_gateway( @@ -91,37 +94,40 @@ pub fn create_node_components( }; let http_server = match config.components.http_server.execution_mode { ActiveComponentExecutionMode::Enabled => { - let gateway_client = - clients.get_gateway_shared_client().expect("Gateway Client should be available"); + let gateway_client = clients + .get_gateway_shared_client(&config.components.gateway.execution_mode) + .expect("Gateway Client should be available"); Some(create_http_server(config.http_server_config.clone(), gateway_client)) } ActiveComponentExecutionMode::Disabled => None, }; - let (mempool_p2p_propagator, mempool_p2p_runner) = match config - .components - .mempool_p2p - .execution_mode - { - ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled - | ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => { - let gateway_client = - clients.get_gateway_shared_client().expect("Gateway Client should be available"); - let (mempool_p2p_propagator, mempool_p2p_runner) = - create_p2p_propagator_and_runner(config.mempool_p2p_config.clone(), gateway_client); - (Some(mempool_p2p_propagator), Some(mempool_p2p_runner)) - } - ReactiveComponentExecutionMode::Disabled | ReactiveComponentExecutionMode::Remote => { - (None, None) - } - }; + let (mempool_p2p_propagator, mempool_p2p_runner) = + match config.components.mempool_p2p.execution_mode { + ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled + | ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => { + let gateway_client = clients + .get_gateway_shared_client(&config.components.gateway.execution_mode) + .expect("Gateway Client should be available"); + let (mempool_p2p_propagator, mempool_p2p_runner) = create_p2p_propagator_and_runner( + config.mempool_p2p_config.clone(), + gateway_client, + ); + (Some(mempool_p2p_propagator), Some(mempool_p2p_runner)) + } + ReactiveComponentExecutionMode::Disabled | ReactiveComponentExecutionMode::Remote => { + (None, None) + } + }; let mempool = match config.components.mempool.execution_mode { ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled | ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => { let mempool_p2p_propagator_client = clients - .get_mempool_p2p_propagator_shared_client() + .get_mempool_p2p_propagator_shared_client( + &config.components.mempool_p2p.execution_mode, + ) .expect("Propagator Client should be available"); let mempool = create_mempool(mempool_p2p_propagator_client); Some(mempool) @@ -159,7 +165,9 @@ pub fn create_node_components( let l1_scraper = match config.components.l1_scraper.execution_mode { ActiveComponentExecutionMode::Enabled => { - let l1_provider_client = clients.get_l1_provider_shared_client().unwrap(); + let l1_provider_client = clients + .get_l1_provider_shared_client(&config.components.l1_provider.execution_mode) + .unwrap(); let l1_scraper_config = config.l1_scraper_config.clone(); let base_layer = EthereumBaseLayerContract::new(config.base_layer_config.clone());