diff --git a/crates/starknet_sequencer_node/src/clients.rs b/crates/starknet_sequencer_node/src/clients.rs index 336bc32aec..7f2811bfb3 100644 --- a/crates/starknet_sequencer_node/src/clients.rs +++ b/crates/starknet_sequencer_node/src/clients.rs @@ -42,19 +42,16 @@ use crate::config::component_execution_config::ReactiveComponentExecutionMode; use crate::config::node_config::SequencerNodeConfig; pub struct SequencerNodeClients { - batcher_client: Option>, - mempool_client: Option>, - gateway_client: Option>, - // TODO (Lev): Change to Option>. + batcher_client: Client, + mempool_client: Client, + gateway_client: Client, mempool_p2p_propagator_client: - Option>, - state_sync_client: Option>, + Client, + state_sync_client: Client, } -/// A macro to retrieve a shared client (either local or remote) from a specified field in a struct, -/// returning it wrapped in an `Arc`. This macro simplifies access to a client by checking if a -/// This macro simplifies client access by checking the specified client field and returning the -/// existing client, either local_client or remote_client. Only one will exist at a time. +/// 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`. /// /// # Arguments /// @@ -64,8 +61,8 @@ pub struct SequencerNodeClients { /// /// # Returns /// -/// An Option> containing the available client (local_client or remote_client), -/// wrapped in Arc. If neither client exists, it returns None. +/// An Option> containing the available client (local_client or remote_client), +/// wrapped in Arc. If neither exists, returns None. /// /// # Example /// @@ -81,20 +78,14 @@ pub struct SequencerNodeClients { /// } /// } /// ``` -/// -/// In this example, `get_shared_client!` checks if `batcher_client` has a local or remote client -/// available. If a local client exists, it returns `Some(Arc::new(local_client))`; otherwise, -/// it checks for a remote client and returns `Some(Arc::new(remote_client))` if available. -/// If neither client is available, it returns `None`. #[macro_export] macro_rules! get_shared_client { ($self:ident, $client_field:ident) => {{ - if let Some(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)); - } + 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)); } None }}; @@ -109,10 +100,7 @@ impl SequencerNodeClients { pub fn get_batcher_local_client( &self, ) -> Option> { - match &self.batcher_client { - Some(client) => client.get_local_client(), - None => None, - } + self.batcher_client.get_local_client() } pub fn get_mempool_shared_client(&self) -> Option { @@ -122,10 +110,7 @@ impl SequencerNodeClients { pub fn get_mempool_local_client( &self, ) -> Option> { - match &self.mempool_client { - Some(client) => client.get_local_client(), - None => None, - } + self.mempool_client.get_local_client() } pub fn get_gateway_shared_client(&self) -> Option { @@ -135,10 +120,7 @@ impl SequencerNodeClients { pub fn get_gateway_local_client( &self, ) -> Option> { - match &self.gateway_client { - Some(client) => client.get_local_client(), - None => None, - } + self.gateway_client.get_local_client() } pub fn get_mempool_p2p_propagator_shared_client( @@ -151,10 +133,7 @@ impl SequencerNodeClients { &self, ) -> Option> { - match &self.mempool_p2p_propagator_client { - Some(client) => client.get_local_client(), - None => None, - } + self.mempool_p2p_propagator_client.get_local_client() } pub fn get_state_sync_shared_client(&self) -> Option { @@ -164,16 +143,13 @@ impl SequencerNodeClients { pub fn get_state_sync_local_client( &self, ) -> Option> { - match &self.state_sync_client { - Some(client) => client.get_local_client(), - None => None, - } + self.state_sync_client.get_local_client() } } -/// A macro for creating a component client, determined by the component's execution mode. Returns a -/// `Client` containing either a local client if the component is run locally, or a remote client if -/// the execution mode is Remote. Returns None if the execution mode is Disabled. +/// A macro for creating a component client fitting the component's execution mode. Returns a +/// `Client` containing: a local client if the component is run locally, a remote client if +/// the component is run remotely, and neither if the component is disabled. /// /// # Arguments /// @@ -184,14 +160,7 @@ impl SequencerNodeClients { /// * $remote_client_type - The type for the remote client to create, e.g., RemoteBatcherClient. The /// client type should have a function $remote_client_type::new(config). /// * $channel_expr - Sender side for the local client. -/// * $remote_client_config - Configuration for the remote client, passed as Some(config) when -/// available. -/// -/// # Returns -/// -/// An `Option>` containing either a local or remote client based on the execution mode -/// (LocalExecutionWithRemoteDisabled / LocalExecutionWithRemoteEnabled for local clients, Remote -/// for remote clients), or None if the execution mode is Disabled. +/// * $remote_client_config - Configuration for the remote client, passed as Option(config). /// /// # Example /// @@ -206,11 +175,6 @@ impl SequencerNodeClients { /// channels.take_batcher_tx(), /// config.components.batcher.remote_client_config /// ); -/// -/// match batcher_client { -/// Some(client) => println!("Client created: {:?}", client), -/// None => println!("Client not created because the execution mode is disabled."), -/// } /// ``` macro_rules! create_client { ( @@ -224,16 +188,16 @@ macro_rules! create_client { ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled | ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => { let local_client = Some(<$local_client_type>::new($channel_expr)); - Some(Client::new(local_client, None)) + Client::new(local_client, None) } ReactiveComponentExecutionMode::Remote => match $remote_client_config { - Some(ref config) => { + Some(config) => { let remote_client = Some(<$remote_client_type>::new(config.clone())); - Some(Client::new(None, remote_client)) + Client::new(None, remote_client) } - None => None, + None => panic!("Remote client configuration is missing."), }, - ReactiveComponentExecutionMode::Disabled => None, + ReactiveComponentExecutionMode::Disabled => Client::new(None, None), } }; } @@ -247,21 +211,21 @@ pub fn create_node_clients( LocalBatcherClient, RemoteBatcherClient, channels.take_batcher_tx(), - config.components.batcher.remote_client_config + &config.components.batcher.remote_client_config ); let mempool_client = create_client!( &config.components.mempool.execution_mode, LocalMempoolClient, RemoteMempoolClient, channels.take_mempool_tx(), - config.components.mempool.remote_client_config + &config.components.mempool.remote_client_config ); let gateway_client = create_client!( &config.components.gateway.execution_mode, LocalGatewayClient, RemoteGatewayClient, channels.take_gateway_tx(), - config.components.gateway.remote_client_config + &config.components.gateway.remote_client_config ); let mempool_p2p_propagator_client = create_client!( @@ -269,7 +233,7 @@ pub fn create_node_clients( LocalMempoolP2pPropagatorClient, RemoteMempoolP2pPropagatorClient, channels.take_mempool_p2p_propagator_tx(), - config.components.mempool_p2p.remote_client_config + &config.components.mempool_p2p.remote_client_config ); let state_sync_client = create_client!( @@ -277,7 +241,7 @@ pub fn create_node_clients( LocalStateSyncClient, RemoteStateSyncClient, channels.take_state_sync_tx(), - config.components.state_sync.remote_client_config + &config.components.state_sync.remote_client_config ); SequencerNodeClients {