From 8a9dc6df9951ed03452e2644b862221b909ecf61 Mon Sep 17 00:00:00 2001 From: yihau Date: Thu, 12 Dec 2024 13:32:12 +0800 Subject: [PATCH] extract repair_whitelist --- validator/src/cli.rs | 45 +------ validator/src/commands/mod.rs | 1 + .../src/commands/repair_whitelist/mod.rs | 117 ++++++++++++++++++ validator/src/main.rs | 69 +---------- 4 files changed, 121 insertions(+), 111 deletions(-) create mode 100644 validator/src/commands/repair_whitelist/mod.rs diff --git a/validator/src/cli.rs b/validator/src/cli.rs index 27e705259a72de..80e603a17b8a34 100644 --- a/validator/src/cli.rs +++ b/validator/src/cli.rs @@ -1738,50 +1738,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> { .subcommand(commands::authorized_voter::command(default_args)) .subcommand(commands::contact_info::command(default_args)) .subcommand(commands::repair_shred_from_peer::command(default_args)) - .subcommand( - SubCommand::with_name("repair-whitelist") - .about("Manage the validator's repair protocol whitelist") - .setting(AppSettings::SubcommandRequiredElseHelp) - .setting(AppSettings::InferSubcommands) - .subcommand( - SubCommand::with_name("get") - .about("Display the validator's repair protocol whitelist") - .arg( - Arg::with_name("output") - .long("output") - .takes_value(true) - .value_name("MODE") - .possible_values(&["json", "json-compact"]) - .help("Output display mode"), - ), - ) - .subcommand( - SubCommand::with_name("set") - .about("Set the validator's repair protocol whitelist") - .setting(AppSettings::ArgRequiredElseHelp) - .arg( - Arg::with_name("whitelist") - .long("whitelist") - .validator(is_pubkey) - .value_name("VALIDATOR IDENTITY") - .multiple(true) - .takes_value(true) - .help("Set the validator's repair protocol whitelist"), - ) - .after_help( - "Note: repair protocol whitelist changes only apply to the currently \ - running validator instance", - ), - ) - .subcommand( - SubCommand::with_name("remove-all") - .about("Clear the validator's repair protocol whitelist") - .after_help( - "Note: repair protocol whitelist changes only apply to the currently \ - running validator instance", - ), - ), - ) + .subcommand(commands::repair_whitelist::command(default_args)) .subcommand( SubCommand::with_name("init").about("Initialize the ledger directory then exit"), ) diff --git a/validator/src/commands/mod.rs b/validator/src/commands/mod.rs index 5169758fd9edd3..ed4245e3040dd6 100644 --- a/validator/src/commands/mod.rs +++ b/validator/src/commands/mod.rs @@ -4,6 +4,7 @@ pub mod exit; pub mod monitor; pub mod plugin; pub mod repair_shred_from_peer; +pub mod repair_whitelist; pub mod set_identity; pub mod set_log_filter; pub mod staked_nodes_overrides; diff --git a/validator/src/commands/repair_whitelist/mod.rs b/validator/src/commands/repair_whitelist/mod.rs new file mode 100644 index 00000000000000..fb40f7dd80bab4 --- /dev/null +++ b/validator/src/commands/repair_whitelist/mod.rs @@ -0,0 +1,117 @@ +use { + crate::{admin_rpc_service, cli::DefaultArgs}, + clap::{values_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand}, + solana_clap_utils::input_validators::is_pubkey, + solana_sdk::pubkey::Pubkey, + std::{collections::HashSet, path::Path, process::exit}, +}; + +pub fn command(_default_args: &DefaultArgs) -> App<'_, '_> { + SubCommand::with_name("repair-whitelist") + .about("Manage the validator's repair protocol whitelist") + .setting(AppSettings::SubcommandRequiredElseHelp) + .setting(AppSettings::InferSubcommands) + .subcommand( + SubCommand::with_name("get") + .about("Display the validator's repair protocol whitelist") + .arg( + Arg::with_name("output") + .long("output") + .takes_value(true) + .value_name("MODE") + .possible_values(&["json", "json-compact"]) + .help("Output display mode"), + ), + ) + .subcommand( + SubCommand::with_name("set") + .about("Set the validator's repair protocol whitelist") + .setting(AppSettings::ArgRequiredElseHelp) + .arg( + Arg::with_name("whitelist") + .long("whitelist") + .validator(is_pubkey) + .value_name("VALIDATOR IDENTITY") + .multiple(true) + .takes_value(true) + .help("Set the validator's repair protocol whitelist"), + ) + .after_help( + "Note: repair protocol whitelist changes only apply to the currently running validator instance", + ), + ) + .subcommand( + SubCommand::with_name("remove-all") + .about("Clear the validator's repair protocol whitelist") + .after_help( + "Note: repair protocol whitelist changes only apply to the currently running validator instance", + ), + ) +} + +pub fn execute(matches: &ArgMatches, ledger_path: &Path) { + match matches.subcommand() { + ("get", Some(subcommand_matches)) => { + let output_mode = subcommand_matches.value_of("output"); + let admin_client = admin_rpc_service::connect(ledger_path); + let repair_whitelist = admin_rpc_service::runtime() + .block_on(async move { admin_client.await?.repair_whitelist().await }) + .unwrap_or_else(|err| { + eprintln!("Repair whitelist query failed: {err}"); + exit(1); + }); + if let Some(mode) = output_mode { + match mode { + "json" => println!( + "{}", + serde_json::to_string_pretty(&repair_whitelist).unwrap() + ), + "json-compact" => { + print!("{}", serde_json::to_string(&repair_whitelist).unwrap()) + } + _ => unreachable!(), + } + } else { + print!("{repair_whitelist}"); + } + } + ("set", Some(subcommand_matches)) => { + let whitelist = if subcommand_matches.is_present("whitelist") { + let validators_set: HashSet<_> = + values_t_or_exit!(subcommand_matches, "whitelist", Pubkey) + .into_iter() + .collect(); + validators_set.into_iter().collect::>() + } else { + return; + }; + set_repair_whitelist(ledger_path, whitelist).unwrap_or_else(|err| { + eprintln!("{err}"); + exit(1); + }); + } + ("remove-all", _) => { + set_repair_whitelist(ledger_path, Vec::default()).unwrap_or_else(|err| { + eprintln!("{err}"); + exit(1); + }); + } + _ => unreachable!(), + } +} + +fn set_repair_whitelist( + ledger_path: &Path, + whitelist: Vec, +) -> Result<(), Box> { + let admin_client = admin_rpc_service::connect(ledger_path); + admin_rpc_service::runtime() + .block_on(async move { admin_client.await?.set_repair_whitelist(whitelist).await }) + .map_err(|err| { + std::io::Error::new( + std::io::ErrorKind::Other, + format!("setRepairWhitelist request failed: {err}"), + ) + })?; + Ok(()) +} diff --git a/validator/src/main.rs b/validator/src/main.rs index 2591881c25abd3..8cd6179e23b43b 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -96,22 +96,6 @@ enum Operation { const MILLIS_PER_SECOND: u64 = 1000; -fn set_repair_whitelist( - ledger_path: &Path, - whitelist: Vec, -) -> Result<(), Box> { - let admin_client = admin_rpc_service::connect(ledger_path); - admin_rpc_service::runtime() - .block_on(async move { admin_client.await?.set_repair_whitelist(whitelist).await }) - .map_err(|err| { - std::io::Error::new( - std::io::ErrorKind::Other, - format!("setRepairWhitelist request failed: {err}"), - ) - })?; - Ok(()) -} - // This function is duplicated in ledger-tool/src/main.rs... fn hardforks_of(matches: &ArgMatches<'_>, name: &str) -> Option> { if matches.is_present(name) { @@ -233,57 +217,8 @@ pub fn main() { return; } ("repair-whitelist", Some(repair_whitelist_subcommand_matches)) => { - match repair_whitelist_subcommand_matches.subcommand() { - ("get", Some(subcommand_matches)) => { - let output_mode = subcommand_matches.value_of("output"); - let admin_client = admin_rpc_service::connect(&ledger_path); - let repair_whitelist = admin_rpc_service::runtime() - .block_on(async move { admin_client.await?.repair_whitelist().await }) - .unwrap_or_else(|err| { - eprintln!("Repair whitelist query failed: {err}"); - exit(1); - }); - if let Some(mode) = output_mode { - match mode { - "json" => println!( - "{}", - serde_json::to_string_pretty(&repair_whitelist).unwrap() - ), - "json-compact" => { - print!("{}", serde_json::to_string(&repair_whitelist).unwrap()) - } - _ => unreachable!(), - } - } else { - print!("{repair_whitelist}"); - } - return; - } - ("set", Some(subcommand_matches)) => { - let whitelist = if subcommand_matches.is_present("whitelist") { - let validators_set: HashSet<_> = - values_t_or_exit!(subcommand_matches, "whitelist", Pubkey) - .into_iter() - .collect(); - validators_set.into_iter().collect::>() - } else { - return; - }; - set_repair_whitelist(&ledger_path, whitelist).unwrap_or_else(|err| { - eprintln!("{err}"); - exit(1); - }); - return; - } - ("remove-all", _) => { - set_repair_whitelist(&ledger_path, Vec::default()).unwrap_or_else(|err| { - eprintln!("{err}"); - exit(1); - }); - return; - } - _ => unreachable!(), - } + commands::repair_whitelist::execute(repair_whitelist_subcommand_matches, &ledger_path); + return; } ("set-public-address", Some(subcommand_matches)) => { let parse_arg_addr = |arg_name: &str, arg_long: &str| -> Option {