From cfa79adc92a51f74d5c4a4e1eb1f2d53417d9b0c Mon Sep 17 00:00:00 2001 From: nadin-Starkware Date: Sun, 12 Jan 2025 12:06:40 +0200 Subject: [PATCH] chore(starknet_sequencer_node): add use_remote_client to the Client struct commit-id:b4390bbe --- .../tests/mempool_p2p_flow_test.rs | 3 +- .../src/component_client/definitions.rs | 8 +- crates/starknet_sequencer_node/src/clients.rs | 80 ++++++++----------- .../starknet_sequencer_node/src/components.rs | 68 +++++++--------- 4 files changed, 73 insertions(+), 86 deletions(-) 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 19ba1f04d5..2483669c6d 100644 --- a/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs +++ b/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs @@ -166,8 +166,7 @@ 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(&config.components.mempool_p2p.execution_mode).unwrap(); + let mempool_client = clients.get_mempool_shared_client().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_infra/src/component_client/definitions.rs b/crates/starknet_sequencer_infra/src/component_client/definitions.rs index 390048c064..9e7e4c1e06 100644 --- a/crates/starknet_sequencer_infra/src/component_client/definitions.rs +++ b/crates/starknet_sequencer_infra/src/component_client/definitions.rs @@ -29,6 +29,7 @@ where { local_client: LocalComponentClient, remote_client: RemoteComponentClient, + use_remote_client: bool, } impl Client @@ -39,8 +40,9 @@ where pub fn new( local_client: LocalComponentClient, remote_client: RemoteComponentClient, + use_remote_client: bool, ) -> Self { - Self { local_client, remote_client } + Self { local_client, remote_client, use_remote_client } } pub fn get_local_client(&self) -> LocalComponentClient { @@ -50,4 +52,8 @@ where pub fn get_remote_client(&self) -> RemoteComponentClient { self.remote_client.clone() } + + pub fn get_use_remote_client(&self) -> bool { + self.use_remote_client + } } diff --git a/crates/starknet_sequencer_node/src/clients.rs b/crates/starknet_sequencer_node/src/clients.rs index 73b21f0571..d587e4c09f 100644 --- a/crates/starknet_sequencer_node/src/clients.rs +++ b/crates/starknet_sequencer_node/src/clients.rs @@ -93,26 +93,19 @@ pub struct SequencerNodeClients { /// ``` #[macro_export] macro_rules! get_shared_client { - ($self:ident, $client_field:ident, $execution_mode:expr) => {{ + ($self:ident, $client_field:ident) => {{ let client = &$self.$client_field; - match &$execution_mode { - ReactiveComponentExecutionMode::Disabled => None, - ReactiveComponentExecutionMode::Remote => Some(Arc::new(client.get_remote_client())), - ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled - | ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled => { - Some(Arc::new(client.get_local_client())) - } + match client.get_use_remote_client() { + true => Some(Arc::new(client.get_remote_client())), + false => Some(Arc::new(client.get_local_client())), } }}; } // TODO(Nadin): Refactor getters to remove code duplication. impl SequencerNodeClients { - pub fn get_batcher_shared_client( - &self, - execution_mode: &ReactiveComponentExecutionMode, - ) -> Option { - get_shared_client!(self, batcher_client, execution_mode) + pub fn get_batcher_shared_client(&self) -> Option { + get_shared_client!(self, batcher_client) } pub fn get_batcher_local_client( @@ -121,11 +114,8 @@ impl SequencerNodeClients { self.batcher_client.get_local_client() } - pub fn get_mempool_shared_client( - &self, - execution_mode: &ReactiveComponentExecutionMode, - ) -> Option { - get_shared_client!(self, mempool_client, execution_mode) + pub fn get_mempool_shared_client(&self) -> Option { + get_shared_client!(self, mempool_client) } pub fn get_mempool_local_client( @@ -134,11 +124,8 @@ impl SequencerNodeClients { self.mempool_client.get_local_client() } - pub fn get_gateway_shared_client( - &self, - execution_mode: &ReactiveComponentExecutionMode, - ) -> Option { - get_shared_client!(self, gateway_client, execution_mode) + pub fn get_gateway_shared_client(&self) -> Option { + get_shared_client!(self, gateway_client) } pub fn get_gateway_local_client( @@ -153,19 +140,14 @@ impl SequencerNodeClients { self.l1_provider_client.get_local_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_l1_provider_shared_client(&self) -> Option { + get_shared_client!(self, l1_provider_client) } pub fn get_mempool_p2p_propagator_shared_client( &self, - - execution_mode: &ReactiveComponentExecutionMode, ) -> Option { - get_shared_client!(self, mempool_p2p_propagator_client, execution_mode) + get_shared_client!(self, mempool_p2p_propagator_client) } pub fn get_mempool_p2p_propagator_local_client( @@ -174,11 +156,8 @@ impl SequencerNodeClients { self.mempool_p2p_propagator_client.get_local_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_shared_client(&self) -> Option { + get_shared_client!(self, state_sync_client) } pub fn get_state_sync_local_client( @@ -198,6 +177,8 @@ impl SequencerNodeClients { /// 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 Option(config). +/// * $execution_mode - Determines whether to enable the remote client +/// (ReactiveComponentExecutionMode::Remote) or to use the local client. /// /// # Example /// @@ -209,13 +190,14 @@ impl SequencerNodeClients { /// LocalBatcherClient, /// RemoteBatcherClient, /// channels.take_batcher_tx(), -/// config.components.batcher.remote_client_config +/// config.components.batcher.remote_client_config, +/// ReactiveComponentExecutionMode::Remote /// ); /// ``` /// /// This macro returns an instance of `Client` with both local and remote clients initialized. /// The local client uses the provided channel, and the remote client uses the configuration and -/// socket. +/// socket. The use_remote_client field is set based on the value of $execution_mode. #[macro_export] macro_rules! create_client { ( @@ -223,11 +205,13 @@ macro_rules! create_client { $remote_client_type:ty, $channel_expr:expr, $remote_client_config:expr, - $socket:expr + $socket:expr, + $execution_mode:expr ) => { Client::new( <$local_client_type>::new($channel_expr), <$remote_client_type>::new($remote_client_config.clone(), $socket), + matches!($execution_mode, ReactiveComponentExecutionMode::Remote), ) }; } @@ -241,21 +225,24 @@ pub fn create_node_clients( RemoteBatcherClient, channels.take_batcher_tx(), &config.components.batcher.remote_client_config, - config.components.batcher.socket + config.components.batcher.socket, + config.components.batcher.execution_mode ); let mempool_client = create_client!( LocalMempoolClient, RemoteMempoolClient, channels.take_mempool_tx(), &config.components.mempool.remote_client_config, - config.components.mempool.socket + config.components.mempool.socket, + config.components.mempool.execution_mode ); let gateway_client = create_client!( LocalGatewayClient, RemoteGatewayClient, channels.take_gateway_tx(), &config.components.gateway.remote_client_config, - config.components.gateway.socket + config.components.gateway.socket, + config.components.gateway.execution_mode ); let mempool_p2p_propagator_client = create_client!( @@ -263,7 +250,8 @@ pub fn create_node_clients( RemoteMempoolP2pPropagatorClient, channels.take_mempool_p2p_propagator_tx(), &config.components.mempool_p2p.remote_client_config, - config.components.mempool_p2p.socket + config.components.mempool_p2p.socket, + config.components.mempool_p2p.execution_mode ); let state_sync_client = create_client!( @@ -271,7 +259,8 @@ pub fn create_node_clients( RemoteStateSyncClient, channels.take_state_sync_tx(), &config.components.state_sync.remote_client_config, - config.components.state_sync.socket + config.components.state_sync.socket, + config.components.state_sync.execution_mode ); let l1_provider_client = create_client!( @@ -279,7 +268,8 @@ pub fn create_node_clients( RemoteL1ProviderClient, channels.take_l1_provider_tx(), &config.components.l1_provider.remote_client_config, - config.components.l1_provider.socket + config.components.l1_provider.socket, + config.components.l1_provider.execution_mode ); SequencerNodeClients { diff --git a/crates/starknet_sequencer_node/src/components.rs b/crates/starknet_sequencer_node/src/components.rs index b41349f399..8e335965aa 100644 --- a/crates/starknet_sequencer_node/src/components.rs +++ b/crates/starknet_sequencer_node/src/components.rs @@ -47,11 +47,10 @@ 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(&config.components.mempool.execution_mode) - .expect("Mempool Client should be available"); + let mempool_client = + clients.get_mempool_shared_client().expect("Mempool Client should be available"); let l1_provider_client = clients - .get_l1_provider_shared_client(&config.components.l1_provider.execution_mode) + .get_l1_provider_shared_client() .expect("L1 Provider Client should be available"); Some(create_batcher(config.batcher_config.clone(), mempool_client, l1_provider_client)) } @@ -59,11 +58,10 @@ 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(&config.components.batcher.execution_mode) - .expect("Batcher Client should be available"); + let batcher_client = + clients.get_batcher_shared_client().expect("Batcher Client should be available"); let state_sync_client = clients - .get_state_sync_shared_client(&config.components.state_sync.execution_mode) + .get_state_sync_shared_client() .expect("State Sync Client should be available"); Some(ConsensusManager::new( config.consensus_manager_config.clone(), @@ -76,11 +74,10 @@ 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(&config.components.mempool.execution_mode) - .expect("Mempool Client should be available"); + let mempool_client = + clients.get_mempool_shared_client().expect("Mempool Client should be available"); let state_sync_client = clients - .get_state_sync_shared_client(&config.components.state_sync.execution_mode) + .get_state_sync_shared_client() .expect("State Sync Client should be available"); Some(create_gateway( @@ -94,40 +91,37 @@ 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(&config.components.gateway.execution_mode) - .expect("Gateway Client should be available"); + let gateway_client = + clients.get_gateway_shared_client().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(&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_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 = match config.components.mempool.execution_mode { ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled | ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => { let mempool_p2p_propagator_client = clients - .get_mempool_p2p_propagator_shared_client( - &config.components.mempool_p2p.execution_mode, - ) + .get_mempool_p2p_propagator_shared_client() .expect("Propagator Client should be available"); let mempool = create_mempool(mempool_p2p_propagator_client); Some(mempool) @@ -165,9 +159,7 @@ 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(&config.components.l1_provider.execution_mode) - .unwrap(); + let l1_provider_client = clients.get_l1_provider_shared_client().unwrap(); let l1_scraper_config = config.l1_scraper_config.clone(); let base_layer = EthereumBaseLayerContract::new(config.base_layer_config.clone());