Skip to content

Commit

Permalink
chore(starknet_sequencer_node): remove option from client struct (#2659)
Browse files Browse the repository at this point in the history
commit-id:b783812b
  • Loading branch information
Itay-Tsabary-Starkware authored Dec 15, 2024
1 parent 9cae497 commit 06359a5
Showing 1 changed file with 33 additions and 69 deletions.
102 changes: 33 additions & 69 deletions crates/starknet_sequencer_node/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ use crate::config::component_execution_config::ReactiveComponentExecutionMode;
use crate::config::node_config::SequencerNodeConfig;

pub struct SequencerNodeClients {
batcher_client: Option<Client<BatcherRequest, BatcherResponse>>,
mempool_client: Option<Client<MempoolRequest, MempoolResponse>>,
gateway_client: Option<Client<GatewayRequest, GatewayResponse>>,
// TODO (Lev): Change to Option<Box<dyn MemPoolClient>>.
batcher_client: Client<BatcherRequest, BatcherResponse>,
mempool_client: Client<MempoolRequest, MempoolResponse>,
gateway_client: Client<GatewayRequest, GatewayResponse>,
mempool_p2p_propagator_client:
Option<Client<MempoolP2pPropagatorRequest, MempoolP2pPropagatorResponse>>,
state_sync_client: Option<Client<StateSyncRequest, StateSyncResponse>>,
Client<MempoolP2pPropagatorRequest, MempoolP2pPropagatorResponse>,
state_sync_client: Client<StateSyncRequest, StateSyncResponse>,
}

/// 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
///
Expand All @@ -64,8 +61,8 @@ pub struct SequencerNodeClients {
///
/// # Returns
///
/// An Option<Arc<dyn Trait>> containing the available client (local_client or remote_client),
/// wrapped in Arc. If neither client exists, it returns None.
/// An Option<Arc<dyn ClientTrait>> containing the available client (local_client or remote_client),
/// wrapped in Arc. If neither exists, returns None.
///
/// # Example
///
Expand All @@ -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
}};
Expand All @@ -109,10 +100,7 @@ impl SequencerNodeClients {
pub fn get_batcher_local_client(
&self,
) -> Option<LocalComponentClient<BatcherRequest, BatcherResponse>> {
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<SharedMempoolClient> {
Expand All @@ -122,10 +110,7 @@ impl SequencerNodeClients {
pub fn get_mempool_local_client(
&self,
) -> Option<LocalComponentClient<MempoolRequest, MempoolResponse>> {
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<SharedGatewayClient> {
Expand All @@ -135,10 +120,7 @@ impl SequencerNodeClients {
pub fn get_gateway_local_client(
&self,
) -> Option<LocalComponentClient<GatewayRequest, GatewayResponse>> {
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(
Expand All @@ -151,10 +133,7 @@ impl SequencerNodeClients {
&self,
) -> Option<LocalComponentClient<MempoolP2pPropagatorRequest, MempoolP2pPropagatorResponse>>
{
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<SharedStateSyncClient> {
Expand All @@ -164,16 +143,13 @@ impl SequencerNodeClients {
pub fn get_state_sync_local_client(
&self,
) -> Option<LocalComponentClient<StateSyncRequest, StateSyncResponse>> {
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
///
Expand All @@ -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<Client<...>>` 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
///
Expand All @@ -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 {
(
Expand All @@ -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),
}
};
}
Expand All @@ -247,37 +211,37 @@ 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!(
&config.components.mempool_p2p.execution_mode,
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!(
&config.components.state_sync.execution_mode,
LocalStateSyncClient,
RemoteStateSyncClient,
channels.take_state_sync_tx(),
config.components.state_sync.remote_client_config
&config.components.state_sync.remote_client_config
);

SequencerNodeClients {
Expand Down

0 comments on commit 06359a5

Please sign in to comment.