Skip to content
Open
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
49 changes: 49 additions & 0 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,55 @@ fn grandpa_justification_period_bounds(s: &str) -> Result<u32, String> {
clap_num::number_range(s, 1, u32::MAX)
}

impl Cli {
/// Validates CLI parameters before node startup
///
/// This catches configuration errors early, before any services are started.
/// Failing fast prevents silent failures during operation.
pub fn validate(&self) -> Result<(), String> {
// Validate grandpa_justification_period
// Although clap enforces minimum of 1, we double-check for safety
if self.grandpa_justification_period == 0 {
return Err(
"grandpa_justification_period cannot be 0. Must be at least 1.".to_string(),
);
}

// Validate kate_max_cells_size
if self.kate_max_cells_size == 0 {
return Err("kate_max_cells_size cannot be 0. Must be at least 1.".to_string());
}

// Double-check upper bound (clap already enforces this, but defense in depth)
if self.kate_max_cells_size > 10_000 {
return Err(
"kate_max_cells_size cannot exceed 10,000 to prevent resource exhaustion."
.to_string(),
);
}

// Check for dangerous flag combinations
if self.unsafe_da_sync && self.run.validator {
return Err(
"Cannot use --unsafe-da-sync with --validator. \
Data availability checks are required for validators to maintain consensus."
.to_string(),
);
}

// Warn about Kate RPC metrics without Kate RPC enabled
// This is just a warning, not an error, so we log it
if self.kate_rpc_metrics_enabled && !self.kate_rpc_enabled {
log::warn!(
"Kate RPC metrics are enabled (--enable-kate-rpc-metrics) but Kate RPC is disabled. \
Consider enabling Kate RPC with --enable-kate-rpc to make metrics useful."
);
}

Ok(())
}
}

/// Possible subcommands of the main binary.
#[allow(clippy::large_enum_variant)]
#[derive(Debug, clap::Subcommand)]
Expand Down
5 changes: 5 additions & 0 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl SubstrateCli for Cli {
pub fn run() -> Result<()> {
let cli = Cli::from_args();

// Validate configuration before starting any services
// This catches errors early and provides clear feedback to users
cli.validate()
.map_err(|e| sc_cli::Error::Input(e))?;

match &cli.subcommand {
None => {
let runner = cli.create_runner(&cli.run)?;
Expand Down