Skip to content

Commit

Permalink
chore: flatten execution mode enum (#1742)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadin-Starkware authored Nov 3, 2024
1 parent 7474f08 commit ff604e9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 112 deletions.
42 changes: 21 additions & 21 deletions config/mempool/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@
"privacy": "Public",
"value": 81920
},
"components.batcher.execution_mode.LocalExecution.enable_remote_connection": {
"description": "Specifies whether the component, when running locally, allows remote connections.",
"components.batcher.execution_mode": {
"description": "The component execution mode.",
"privacy": "Public",
"value": false
"value": "LocalExecutionWithRemoteDisabled"
},
"components.batcher.local_server_config.#is_none": {
"description": "Flag for an optional field.",
Expand Down Expand Up @@ -254,10 +254,10 @@
"privacy": "Public",
"value": "0.0.0.0:8080"
},
"components.consensus_manager.execution_mode.LocalExecution.enable_remote_connection": {
"description": "Specifies whether the component, when running locally, allows remote connections.",
"components.consensus_manager.execution_mode": {
"description": "The component execution mode.",
"privacy": "Public",
"value": false
"value": "LocalExecutionWithRemoteDisabled"
},
"components.consensus_manager.local_server_config.#is_none": {
"description": "Flag for an optional field.",
Expand Down Expand Up @@ -304,10 +304,10 @@
"privacy": "Public",
"value": "0.0.0.0:8080"
},
"components.gateway.execution_mode.LocalExecution.enable_remote_connection": {
"description": "Specifies whether the component, when running locally, allows remote connections.",
"components.gateway.execution_mode": {
"description": "The component execution mode.",
"privacy": "Public",
"value": false
"value": "LocalExecutionWithRemoteDisabled"
},
"components.gateway.local_server_config.#is_none": {
"description": "Flag for an optional field.",
Expand Down Expand Up @@ -354,10 +354,10 @@
"privacy": "Public",
"value": "0.0.0.0:8080"
},
"components.http_server.execution_mode.LocalExecution.enable_remote_connection": {
"description": "Specifies whether the component, when running locally, allows remote connections.",
"components.http_server.execution_mode": {
"description": "The component execution mode.",
"privacy": "Public",
"value": true
"value": "LocalExecutionWithRemoteEnabled"
},
"components.http_server.local_server_config.#is_none": {
"description": "Flag for an optional field.",
Expand Down Expand Up @@ -404,10 +404,10 @@
"privacy": "Public",
"value": "0.0.0.0:8080"
},
"components.mempool.execution_mode.LocalExecution.enable_remote_connection": {
"description": "Specifies whether the component, when running locally, allows remote connections.",
"components.mempool.execution_mode": {
"description": "The component execution mode.",
"privacy": "Public",
"value": false
"value": "LocalExecutionWithRemoteDisabled"
},
"components.mempool.local_server_config.#is_none": {
"description": "Flag for an optional field.",
Expand Down Expand Up @@ -454,10 +454,10 @@
"privacy": "Public",
"value": "0.0.0.0:8080"
},
"components.mempool_p2p.execution_mode.LocalExecution.enable_remote_connection": {
"description": "Specifies whether the component, when running locally, allows remote connections.",
"components.mempool_p2p.execution_mode": {
"description": "The component execution mode.",
"privacy": "Public",
"value": false
"value": "LocalExecutionWithRemoteDisabled"
},
"components.mempool_p2p.local_server_config.#is_none": {
"description": "Flag for an optional field.",
Expand Down Expand Up @@ -504,10 +504,10 @@
"privacy": "Public",
"value": "0.0.0.0:8080"
},
"components.monitoring_endpoint.execution_mode.LocalExecution.enable_remote_connection": {
"description": "Specifies whether the component, when running locally, allows remote connections.",
"components.monitoring_endpoint.execution_mode": {
"description": "The component execution mode.",
"privacy": "Public",
"value": true
"value": "LocalExecutionWithRemoteEnabled"
},
"components.monitoring_endpoint.local_server_config.#is_none": {
"description": "Flag for an optional field.",
Expand Down
18 changes: 10 additions & 8 deletions crates/mempool_node/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,35 @@ pub fn create_node_clients(
) -> SequencerNodeClients {
let batcher_client: Option<SharedBatcherClient> = match config.components.batcher.execution_mode
{
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
Some(Arc::new(LocalBatcherClient::new(channels.take_batcher_tx())))
}
ComponentExecutionMode::Disabled => None,
};
let mempool_client: Option<SharedMempoolClient> = match config.components.mempool.execution_mode
{
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
Some(Arc::new(LocalMempoolClient::new(channels.take_mempool_tx())))
}
ComponentExecutionMode::Disabled => None,
};
let gateway_client: Option<SharedGatewayClient> = match config.components.gateway.execution_mode
{
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
Some(Arc::new(LocalGatewayClient::new(channels.take_gateway_tx())))
}
ComponentExecutionMode::Disabled => None,
};

let mempool_p2p_propagator_client: Option<SharedMempoolP2pPropagatorClient> =
match config.components.mempool.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
Some(Arc::new(LocalMempoolP2pPropagatorClient::new(
channels.take_mempool_p2p_propagator_tx(),
)))
}
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => Some(Arc::new(
LocalMempoolP2pPropagatorClient::new(channels.take_mempool_p2p_propagator_tx()),
)),
ComponentExecutionMode::Disabled => None,
};
SequencerNodeClients {
Expand Down
22 changes: 14 additions & 8 deletions crates/mempool_node/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,26 @@ pub fn create_node_components(
clients: &SequencerNodeClients,
) -> SequencerNodeComponents {
let batcher = match config.components.batcher.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
let mempool_client =
clients.get_mempool_client().expect("Mempool Client should be available");
Some(create_batcher(config.batcher_config.clone(), mempool_client))
}
ComponentExecutionMode::Disabled => None,
};
let consensus_manager = match config.components.consensus_manager.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
let batcher_client =
clients.get_batcher_client().expect("Batcher Client should be available");
Some(ConsensusManager::new(config.consensus_manager_config.clone(), batcher_client))
}
ComponentExecutionMode::Disabled => None,
};
let gateway = match config.components.gateway.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
let mempool_client =
clients.get_mempool_client().expect("Mempool Client should be available");

Expand All @@ -61,7 +64,8 @@ pub fn create_node_components(
ComponentExecutionMode::Disabled => None,
};
let http_server = match config.components.http_server.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
let gateway_client =
clients.get_gateway_client().expect("Gateway Client should be available");

Expand All @@ -72,7 +76,8 @@ pub fn create_node_components(

let (mempool_p2p_propagator, mempool_p2p_runner) =
match config.components.mempool_p2p.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
let gateway_client =
clients.get_gateway_client().expect("Gateway Client should be available");
let (mempool_p2p_propagator, mempool_p2p_runner) = create_p2p_propagator_and_runner(
Expand All @@ -85,7 +90,8 @@ pub fn create_node_components(
};

let mempool = match config.components.mempool.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: _ } => {
ComponentExecutionMode::LocalExecutionWithRemoteDisabled
| ComponentExecutionMode::LocalExecutionWithRemoteEnabled => {
let mempool_p2p_propagator_client = clients
.get_mempool_p2p_propagator_client()
.expect("Propagator Client should be available");
Expand All @@ -96,10 +102,10 @@ pub fn create_node_components(
};

let monitoring_endpoint = match config.components.monitoring_endpoint.execution_mode {
ComponentExecutionMode::LocalExecution { enable_remote_connection: true } => Some(
ComponentExecutionMode::LocalExecutionWithRemoteEnabled => Some(
create_monitoring_endpoint(config.monitoring_endpoint_config.clone(), VERSION_FULL),
),
ComponentExecutionMode::LocalExecution { enable_remote_connection: false } => None,
ComponentExecutionMode::LocalExecutionWithRemoteDisabled => None,
ComponentExecutionMode::Disabled => None,
};

Expand Down
85 changes: 20 additions & 65 deletions crates/mempool_node/src/config/component_execution_config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use std::collections::BTreeMap;

use papyrus_config::dumping::{
append_sub_config_name,
ser_optional_sub_config,
ser_param,
SerializeConfig,
};
use papyrus_config::dumping::{ser_optional_sub_config, ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use starknet_sequencer_infra::component_definitions::{
Expand All @@ -18,30 +13,10 @@ use validator::{Validate, ValidationError};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum ComponentExecutionMode {
Disabled,
LocalExecution { enable_remote_connection: bool },
LocalExecutionWithRemoteEnabled,
LocalExecutionWithRemoteDisabled,
}

impl ComponentExecutionMode {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
match self {
ComponentExecutionMode::Disabled => BTreeMap::from_iter([ser_param(
"Disabled",
&"Disabled",
"The component is disabled.",
ParamPrivacyInput::Public,
)]),
ComponentExecutionMode::LocalExecution { enable_remote_connection } => {
BTreeMap::from_iter([ser_param(
"LocalExecution.enable_remote_connection",
enable_remote_connection,
"Specifies whether the component, when running locally, allows remote \
connections.",
ParamPrivacyInput::Public,
)])
}
}
}
}
// TODO(Lev/Tsabary): When papyrus_config will support it, change to include communication config in
// the enum.

Expand All @@ -57,8 +32,14 @@ pub struct ComponentExecutionConfig {

impl SerializeConfig for ComponentExecutionConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
let members = BTreeMap::from_iter([ser_param(
"execution_mode",
&self.execution_mode,
"The component execution mode.",
ParamPrivacyInput::Public,
)]);
vec![
append_sub_config_name(self.execution_mode.dump(), "execution_mode"),
members,
ser_optional_sub_config(&self.local_server_config, "local_server_config"),
ser_optional_sub_config(&self.remote_client_config, "remote_client_config"),
ser_optional_sub_config(&self.remote_server_config, "remote_server_config"),
Expand All @@ -72,9 +53,7 @@ impl SerializeConfig for ComponentExecutionConfig {
impl Default for ComponentExecutionConfig {
fn default() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: false,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteDisabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: None,
Expand All @@ -86,9 +65,7 @@ impl Default for ComponentExecutionConfig {
impl ComponentExecutionConfig {
pub fn gateway_default_config() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: false,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteDisabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: None,
Expand All @@ -100,9 +77,7 @@ impl ComponentExecutionConfig {
// a workaround I've set the local one, but this should be addressed.
pub fn http_server_default_config() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: true,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteEnabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: Some(RemoteServerConfig::default()),
Expand All @@ -114,9 +89,7 @@ impl ComponentExecutionConfig {
// one of them is set. As a workaround I've set the local one, but this should be addressed.
pub fn monitoring_endpoint_default_config() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: true,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteEnabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: Some(RemoteServerConfig::default()),
Expand All @@ -125,9 +98,7 @@ impl ComponentExecutionConfig {

pub fn mempool_default_config() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: false,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteDisabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: None,
Expand All @@ -136,9 +107,7 @@ impl ComponentExecutionConfig {

pub fn batcher_default_config() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: false,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteDisabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: None,
Expand All @@ -147,9 +116,7 @@ impl ComponentExecutionConfig {

pub fn consensus_manager_default_config() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: false,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteDisabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: None,
Expand All @@ -158,9 +125,7 @@ impl ComponentExecutionConfig {

pub fn mempool_p2p_default_config() -> Self {
Self {
execution_mode: ComponentExecutionMode::LocalExecution {
enable_remote_connection: false,
},
execution_mode: ComponentExecutionMode::LocalExecutionWithRemoteDisabled,
local_server_config: Some(LocalServerConfig::default()),
remote_client_config: None,
remote_server_config: None,
Expand All @@ -178,18 +143,8 @@ pub fn validate_single_component_config(
component_config.remote_server_config.is_some(),
) {
(ComponentExecutionMode::Disabled, false, false, false) => Ok(()),
(
ComponentExecutionMode::LocalExecution { enable_remote_connection: true },
true,
false,
true,
) => Ok(()),
(
ComponentExecutionMode::LocalExecution { enable_remote_connection: false },
true,
false,
false,
) => Ok(()),
(ComponentExecutionMode::LocalExecutionWithRemoteEnabled, true, false, true) => Ok(()),
(ComponentExecutionMode::LocalExecutionWithRemoteDisabled, true, false, false) => Ok(()),
_ => {
let mut error = ValidationError::new("Invalid component execution configuration.");
error.message = Some("Ensure settings align with the chosen execution mode.".into());
Expand Down
4 changes: 2 additions & 2 deletions crates/mempool_node/src/config/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use crate::config::{
};

const LOCAL_EXECUTION_MODE: ComponentExecutionMode =
ComponentExecutionMode::LocalExecution { enable_remote_connection: false };
ComponentExecutionMode::LocalExecutionWithRemoteDisabled;
const ENABLE_REMOTE_CONNECTION_MODE: ComponentExecutionMode =
ComponentExecutionMode::LocalExecution { enable_remote_connection: true };
ComponentExecutionMode::LocalExecutionWithRemoteEnabled;

/// Test the validation of the struct ComponentExecutionConfig.
/// Validates that execution mode of the component and the local/remote config are at sync.
Expand Down
Loading

0 comments on commit ff604e9

Please sign in to comment.