Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(starknet_sequencer_node): add use_remote_client to the Client struct #3240

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ where
{
local_client: LocalComponentClient<Request, Response>,
remote_client: RemoteComponentClient<Request, Response>,
use_remote_client: bool,
}

impl<Request, Response> Client<Request, Response>
Expand All @@ -39,8 +40,9 @@ where
pub fn new(
local_client: LocalComponentClient<Request, Response>,
remote_client: RemoteComponentClient<Request, Response>,
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<Request, Response> {
Expand All @@ -50,4 +52,8 @@ where
pub fn get_remote_client(&self) -> RemoteComponentClient<Request, Response> {
self.remote_client.clone()
}

pub fn get_use_remote_client(&self) -> bool {
self.use_remote_client
}
}
80 changes: 35 additions & 45 deletions crates/starknet_sequencer_node/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedBatcherClient> {
get_shared_client!(self, batcher_client, execution_mode)
pub fn get_batcher_shared_client(&self) -> Option<SharedBatcherClient> {
get_shared_client!(self, batcher_client)
}

pub fn get_batcher_local_client(
Expand All @@ -121,11 +114,8 @@ impl SequencerNodeClients {
self.batcher_client.get_local_client()
}

pub fn get_mempool_shared_client(
&self,
execution_mode: &ReactiveComponentExecutionMode,
) -> Option<SharedMempoolClient> {
get_shared_client!(self, mempool_client, execution_mode)
pub fn get_mempool_shared_client(&self) -> Option<SharedMempoolClient> {
get_shared_client!(self, mempool_client)
}

pub fn get_mempool_local_client(
Expand All @@ -134,11 +124,8 @@ impl SequencerNodeClients {
self.mempool_client.get_local_client()
}

pub fn get_gateway_shared_client(
&self,
execution_mode: &ReactiveComponentExecutionMode,
) -> Option<SharedGatewayClient> {
get_shared_client!(self, gateway_client, execution_mode)
pub fn get_gateway_shared_client(&self) -> Option<SharedGatewayClient> {
get_shared_client!(self, gateway_client)
}

pub fn get_gateway_local_client(
Expand All @@ -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<SharedL1ProviderClient> {
get_shared_client!(self, l1_provider_client, execution_mode)
pub fn get_l1_provider_shared_client(&self) -> Option<SharedL1ProviderClient> {
get_shared_client!(self, l1_provider_client)
}

pub fn get_mempool_p2p_propagator_shared_client(
&self,

execution_mode: &ReactiveComponentExecutionMode,
) -> Option<SharedMempoolP2pPropagatorClient> {
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(
Expand All @@ -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<SharedStateSyncClient> {
get_shared_client!(self, state_sync_client, execution_mode)
pub fn get_state_sync_shared_client(&self) -> Option<SharedStateSyncClient> {
get_shared_client!(self, state_sync_client)
}

pub fn get_state_sync_local_client(
Expand All @@ -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
///
Expand All @@ -209,25 +190,28 @@ 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 {
(
$local_client_type:ty,
$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),
)
};
}
Expand All @@ -241,45 +225,51 @@ 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!(
LocalMempoolP2pPropagatorClient,
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!(
LocalStateSyncClient,
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!(
LocalL1ProviderClient,
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 {
Expand Down
68 changes: 30 additions & 38 deletions crates/starknet_sequencer_node/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,21 @@ 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))
}
ReactiveComponentExecutionMode::Disabled | ReactiveComponentExecutionMode::Remote => None,
};
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(),
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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());

Expand Down
Loading