Skip to content

Commit

Permalink
impl Default
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Feb 27, 2025
1 parent 52fa0f2 commit 338c71a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
8 changes: 1 addition & 7 deletions validator/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> {
.subcommand(commands::set_identity::command(default_args))
.subcommand(commands::set_log_filter::command(default_args))
.subcommand(commands::staked_nodes_overrides::command(default_args))
.subcommand(commands::wait_for_restart_window::command(default_args))
.subcommand(commands::wait_for_restart_window::command())
.subcommand(commands::set_public_address::command(default_args));

commands::run::add_args(app, default_args)
Expand Down Expand Up @@ -436,10 +436,6 @@ pub struct DefaultArgs {
pub exit_min_idle_time: String,
pub exit_max_delinquent_stake: String,

// Wait subcommand
pub wait_for_restart_window_min_idle_time: String,
pub wait_for_restart_window_max_delinquent_stake: String,

pub banking_trace_dir_byte_limit: String,

pub wen_restart_path: String,
Expand Down Expand Up @@ -537,8 +533,6 @@ impl DefaultArgs {
rpc_max_request_body_size: MAX_REQUEST_BODY_SIZE.to_string(),
exit_min_idle_time: "10".to_string(),
exit_max_delinquent_stake: "5".to_string(),
wait_for_restart_window_min_idle_time: "10".to_string(),
wait_for_restart_window_max_delinquent_stake: "5".to_string(),
banking_trace_dir_byte_limit: BANKING_TRACE_DIR_DEFAULT_BYTE_LIMIT.to_string(),
wen_restart_path: "wen_restart_progress.proto".to_string(),
thread_args: DefaultThreadArgs::default(),
Expand Down
74 changes: 33 additions & 41 deletions validator/src/commands/wait_for_restart_window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use {
crate::{
admin_rpc_service, cli::DefaultArgs, commands::FromClapArgMatches,
new_spinner_progress_bar, println_name_value,
admin_rpc_service, commands::FromClapArgMatches, new_spinner_progress_bar,
println_name_value,
},
clap::{value_t_or_exit, App, Arg, ArgMatches, SubCommand},
clap::{value_t, App, Arg, ArgMatches, SubCommand},
console::style,
solana_clap_utils::{
input_parsers::pubkey_of,
Expand Down Expand Up @@ -34,19 +34,34 @@ pub struct WaitForRestartWindowArgs {
pub skip_health_check: bool,
}

impl Default for WaitForRestartWindowArgs {
fn default() -> Self {
WaitForRestartWindowArgs {
min_idle_time: 10,
identity: None,
max_delinquent_stake: 5,
skip_new_snapshot_check: false,
skip_health_check: false,
}
}
}

impl FromClapArgMatches for WaitForRestartWindowArgs {
fn from_clap_arg_match(matches: &ArgMatches) -> Result<Self, String> {
let default_args = WaitForRestartWindowArgs::default();
Ok(WaitForRestartWindowArgs {
min_idle_time: value_t_or_exit!(matches, "min_idle_time", usize),
min_idle_time: value_t!(matches, "min_idle_time", usize)
.unwrap_or(default_args.min_idle_time),
identity: pubkey_of(matches, "identity"),
max_delinquent_stake: value_t_or_exit!(matches, "max_delinquent_stake", u8),
max_delinquent_stake: value_t!(matches, "max_delinquent_stake", u8)
.unwrap_or(default_args.max_delinquent_stake),
skip_new_snapshot_check: matches.is_present("skip_new_snapshot_check"),
skip_health_check: matches.is_present("skip_health_check"),
})
}
}

pub(crate) fn command(default_args: &DefaultArgs) -> App<'_, '_> {
pub(crate) fn command<'a>() -> App<'a, 'a> {
SubCommand::with_name(COMMAND)
.about("Monitor the validator for a good time to restart")
.arg(
Expand All @@ -55,7 +70,6 @@ pub(crate) fn command(default_args: &DefaultArgs) -> App<'_, '_> {
.takes_value(true)
.validator(is_parsable::<usize>)
.value_name("MINUTES")
.default_value(&default_args.wait_for_restart_window_min_idle_time)
.help(
"Minimum time that the validator should not be leader before restarting",
),
Expand All @@ -73,7 +87,6 @@ pub(crate) fn command(default_args: &DefaultArgs) -> App<'_, '_> {
.long("max-delinquent-stake")
.takes_value(true)
.validator(is_valid_percentage)
.default_value(&default_args.wait_for_restart_window_max_delinquent_stake)
.value_name("PERCENT")
.help("The maximum delinquent stake % permitted for a restart"),
)
Expand Down Expand Up @@ -365,95 +378,74 @@ mod tests {
#[test]
fn verify_args_struct_by_command_wait_for_restart_window_default() {
verify_args_struct_by_command(
command(&DefaultArgs::default()),
command(),
vec![COMMAND],
WaitForRestartWindowArgs {
min_idle_time: 10,
identity: None,
max_delinquent_stake: 5,
skip_new_snapshot_check: false,
skip_health_check: false,
},
WaitForRestartWindowArgs::default(),
);
}

#[test]
fn verify_args_struct_by_command_wait_for_restart_window_skip_new_snapshot_check() {
verify_args_struct_by_command(
command(&DefaultArgs::default()),
command(),
vec![COMMAND, "--skip-new-snapshot-check"],
WaitForRestartWindowArgs {
min_idle_time: 10,
identity: None,
max_delinquent_stake: 5,
skip_new_snapshot_check: true,
skip_health_check: false,
..WaitForRestartWindowArgs::default()
},
);
}

#[test]
fn verify_args_struct_by_command_wait_for_restart_window_skip_health_check() {
verify_args_struct_by_command(
command(&DefaultArgs::default()),
command(),
vec![COMMAND, "--skip-health-check"],
WaitForRestartWindowArgs {
min_idle_time: 10,
identity: None,
max_delinquent_stake: 5,
skip_new_snapshot_check: false,
skip_health_check: true,
..WaitForRestartWindowArgs::default()
},
);
}

#[test]
fn verify_args_struct_by_command_wait_for_restart_window_min_idle_time() {
verify_args_struct_by_command(
command(&DefaultArgs::default()),
command(),
vec![COMMAND, "--min-idle-time", "60"],
WaitForRestartWindowArgs {
min_idle_time: 60,
identity: None,
max_delinquent_stake: 5,
skip_new_snapshot_check: false,
skip_health_check: false,
..WaitForRestartWindowArgs::default()
},
);
}

#[test]
fn verify_args_struct_by_command_wait_for_restart_window_identity() {
verify_args_struct_by_command(
command(&DefaultArgs::default()),
command(),
vec![
COMMAND,
"--identity",
"ch1do11111111111111111111111111111111111111",
],
WaitForRestartWindowArgs {
min_idle_time: 10,
identity: Some(
Pubkey::from_str("ch1do11111111111111111111111111111111111111").unwrap(),
),
max_delinquent_stake: 5,
skip_new_snapshot_check: false,
skip_health_check: false,
..WaitForRestartWindowArgs::default()
},
);
}

#[test]
fn verify_args_struct_by_command_wait_for_restart_window_max_delinquent_stake() {
verify_args_struct_by_command(
command(&DefaultArgs::default()),
command(),
vec![COMMAND, "--max-delinquent-stake", "10"],
WaitForRestartWindowArgs {
min_idle_time: 10,
identity: None,
max_delinquent_stake: 10,
skip_new_snapshot_check: false,
skip_health_check: false,
..WaitForRestartWindowArgs::default()
},
);
}
Expand Down

0 comments on commit 338c71a

Please sign in to comment.