Skip to content

Commit 6d15594

Browse files
apollo_infra: add component config validations
1 parent 0001539 commit 6d15594

File tree

3 files changed

+92
-29
lines changed

3 files changed

+92
-29
lines changed

crates/apollo_node/src/config/component_execution_config.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,54 @@ fn validate_max_concurrency(max_concurrency: usize) -> Result<(), ValidationErro
254254
Ok(())
255255
} else {
256256
Err(create_validation_error(
257-
format!("Invalid max_concurrency: {}", max_concurrency),
257+
format!("Invalid max_concurrency: {max_concurrency}"),
258258
"Invalid max concurrency",
259259
"Ensure the max concurrency is greater than 0.",
260260
))
261261
}
262262
}
263263

264+
fn check_expectation(
265+
field: &'static str,
266+
has: bool,
267+
required: bool,
268+
execution_mode: &ReactiveComponentExecutionMode,
269+
) -> Result<(), ValidationError> {
270+
if required && !has {
271+
return Err(create_validation_error(
272+
format!("{field} config is required when execution mode is {execution_mode:?}."),
273+
"Missing expected server config.",
274+
"Ensure the server config is set.",
275+
));
276+
}
277+
if !required && has {
278+
return Err(create_validation_error(
279+
format!("{field} config should not be set when execution mode is {execution_mode:?}."),
280+
"Unexpected server config.",
281+
"Ensure the server config is not set.",
282+
));
283+
}
284+
Ok(())
285+
}
286+
264287
fn validate_reactive_component_execution_config(
265288
component_config: &ReactiveComponentExecutionConfig,
266289
) -> Result<(), ValidationError> {
290+
// Validate the execution mode matches presence/absence of local and remote server configs.
291+
let has_local = component_config.local_server_config.is_some();
292+
let has_remote = component_config.remote_client_config.is_some();
293+
294+
// Expected local and remote server configs expected values based on the execution mode.
295+
let (local_req, remote_req) = match &component_config.execution_mode {
296+
ReactiveComponentExecutionMode::Disabled => (false, false),
297+
ReactiveComponentExecutionMode::Remote => (false, true),
298+
ReactiveComponentExecutionMode::LocalExecutionWithRemoteDisabled
299+
| ReactiveComponentExecutionMode::LocalExecutionWithRemoteEnabled => (true, false),
300+
};
301+
check_expectation("local server", has_local, local_req, &component_config.execution_mode)?;
302+
check_expectation("remote client", has_remote, remote_req, &component_config.execution_mode)?;
303+
304+
// Validate the execution mode matches socket validity.
267305
match (&component_config.execution_mode, component_config.is_valid_socket()) {
268306
(ReactiveComponentExecutionMode::Disabled, _) => Ok(()),
269307
(ReactiveComponentExecutionMode::Remote, true)

crates/apollo_node/src/config/config_test.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,48 @@ const VALID_PORT: u16 = 8080;
3737
#[rstest]
3838
#[case::local(
3939
ReactiveComponentExecutionMode::Disabled,
40-
LocalServerConfig::default(),
41-
RemoteClientConfig::default(),
40+
None,
41+
None,
4242
VALID_URL,
4343
VALID_IP,
4444
VALID_PORT
4545
)]
4646
#[case::local(
4747
ReactiveComponentExecutionMode::Remote,
48-
LocalServerConfig::default(),
49-
RemoteClientConfig::default(),
48+
None,
49+
Some(RemoteClientConfig::default()),
5050
VALID_URL,
5151
VALID_IP,
5252
VALID_PORT
5353
)]
5454
#[case::local(
5555
LOCAL_EXECUTION_MODE,
56-
LocalServerConfig::default(),
57-
RemoteClientConfig::default(),
56+
Some(LocalServerConfig::default()),
57+
None,
5858
VALID_URL,
5959
VALID_IP,
6060
VALID_PORT
6161
)]
6262
#[case::remote(
6363
ENABLE_REMOTE_CONNECTION_MODE,
64-
LocalServerConfig::default(),
65-
RemoteClientConfig::default(),
64+
Some(LocalServerConfig::default()),
65+
None,
6666
VALID_URL,
6767
VALID_IP,
6868
VALID_PORT
6969
)]
7070
fn valid_component_execution_config(
7171
#[case] execution_mode: ReactiveComponentExecutionMode,
72-
#[case] local_server_config: LocalServerConfig,
73-
#[case] remote_client_config: RemoteClientConfig,
72+
#[case] local_server_config: Option<LocalServerConfig>,
73+
#[case] remote_client_config: Option<RemoteClientConfig>,
7474
#[case] url: &str,
7575
#[case] ip: IpAddr,
7676
#[case] port: u16,
7777
) {
7878
let component_exe_config = ReactiveComponentExecutionConfig {
7979
execution_mode,
80-
local_server_config: Some(local_server_config),
81-
remote_client_config: Some(remote_client_config),
80+
local_server_config,
81+
remote_client_config,
8282
max_concurrency: 1,
8383
url: url.to_string(),
8484
ip,

crates/apollo_node/src/config/node_config.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ impl Default for SequencerNodeConfig {
278278
}
279279
}
280280

281-
macro_rules! ensure_component_config_matches_mode {
281+
macro_rules! validate_component_config_is_set_iff_running_locally {
282282
($self:ident, $component_field:ident, $config_field:ident) => {{
283+
// The component config should be set iff its running locally.
283284
if $self.components.$component_field.is_running_locally() != $self.$config_field.is_some() {
284285
let execution_mode = &$self.components.$component_field.execution_mode;
285286
let component_config_availability =
@@ -314,33 +315,57 @@ impl SequencerNodeConfig {
314315

315316
fn cross_member_validations(&self) -> Result<(), ConfigError> {
316317
// TODO(Tsabary): should be based on iteration of `ComponentConfig` fields.
317-
ensure_component_config_matches_mode!(self, batcher, batcher_config);
318-
ensure_component_config_matches_mode!(self, class_manager, class_manager_config);
319-
ensure_component_config_matches_mode!(self, gateway, gateway_config);
320-
ensure_component_config_matches_mode!(
318+
validate_component_config_is_set_iff_running_locally!(self, batcher, batcher_config);
319+
validate_component_config_is_set_iff_running_locally!(
320+
self,
321+
class_manager,
322+
class_manager_config
323+
);
324+
validate_component_config_is_set_iff_running_locally!(self, gateway, gateway_config);
325+
validate_component_config_is_set_iff_running_locally!(
321326
self,
322327
l1_endpoint_monitor,
323328
l1_endpoint_monitor_config
324329
);
325-
ensure_component_config_matches_mode!(self, l1_provider, l1_provider_config);
326-
ensure_component_config_matches_mode!(
330+
validate_component_config_is_set_iff_running_locally!(
331+
self,
332+
l1_provider,
333+
l1_provider_config
334+
);
335+
validate_component_config_is_set_iff_running_locally!(
327336
self,
328337
l1_gas_price_provider,
329338
l1_gas_price_provider_config
330339
);
331-
ensure_component_config_matches_mode!(self, mempool, mempool_config);
332-
ensure_component_config_matches_mode!(self, mempool_p2p, mempool_p2p_config);
333-
ensure_component_config_matches_mode!(self, sierra_compiler, sierra_compiler_config);
334-
ensure_component_config_matches_mode!(self, state_sync, state_sync_config);
335-
ensure_component_config_matches_mode!(self, consensus_manager, consensus_manager_config);
336-
ensure_component_config_matches_mode!(self, http_server, http_server_config);
337-
ensure_component_config_matches_mode!(self, l1_scraper, l1_scraper_config);
338-
ensure_component_config_matches_mode!(
340+
validate_component_config_is_set_iff_running_locally!(self, mempool, mempool_config);
341+
validate_component_config_is_set_iff_running_locally!(
342+
self,
343+
mempool_p2p,
344+
mempool_p2p_config
345+
);
346+
validate_component_config_is_set_iff_running_locally!(
347+
self,
348+
sierra_compiler,
349+
sierra_compiler_config
350+
);
351+
validate_component_config_is_set_iff_running_locally!(self, state_sync, state_sync_config);
352+
validate_component_config_is_set_iff_running_locally!(
353+
self,
354+
consensus_manager,
355+
consensus_manager_config
356+
);
357+
validate_component_config_is_set_iff_running_locally!(
358+
self,
359+
http_server,
360+
http_server_config
361+
);
362+
validate_component_config_is_set_iff_running_locally!(self, l1_scraper, l1_scraper_config);
363+
validate_component_config_is_set_iff_running_locally!(
339364
self,
340365
l1_gas_price_scraper,
341366
l1_gas_price_scraper_config
342367
);
343-
ensure_component_config_matches_mode!(
368+
validate_component_config_is_set_iff_running_locally!(
344369
self,
345370
monitoring_endpoint,
346371
monitoring_endpoint_config

0 commit comments

Comments
 (0)