|
| 1 | +use account_utils::validator_definitions::ValidatorDefinitions; |
| 2 | +use bls::PublicKey; |
| 3 | +use clap::{App, Arg, ArgMatches}; |
| 4 | +use std::{collections::HashSet, path::PathBuf}; |
| 5 | + |
| 6 | +pub const CMD: &str = "modify"; |
| 7 | +pub const ENABLE: &str = "enable"; |
| 8 | +pub const DISABLE: &str = "disable"; |
| 9 | + |
| 10 | +pub const PUBKEY_FLAG: &str = "pubkey"; |
| 11 | +pub const ALL: &str = "all"; |
| 12 | + |
| 13 | +pub fn cli_app<'a, 'b>() -> App<'a, 'b> { |
| 14 | + App::new(CMD) |
| 15 | + .about("Modify validator status in validator_definitions.yml.") |
| 16 | + .subcommand( |
| 17 | + App::new(ENABLE) |
| 18 | + .about("Enable validator(s) in validator_definitions.yml.") |
| 19 | + .arg( |
| 20 | + Arg::with_name(PUBKEY_FLAG) |
| 21 | + .long(PUBKEY_FLAG) |
| 22 | + .value_name("PUBKEY") |
| 23 | + .help("Validator pubkey to enable") |
| 24 | + .takes_value(true), |
| 25 | + ) |
| 26 | + .arg( |
| 27 | + Arg::with_name(ALL) |
| 28 | + .long(ALL) |
| 29 | + .help("Enable all validators in the validator directory") |
| 30 | + .takes_value(false) |
| 31 | + .conflicts_with(PUBKEY_FLAG), |
| 32 | + ), |
| 33 | + ) |
| 34 | + .subcommand( |
| 35 | + App::new(DISABLE) |
| 36 | + .about("Disable validator(s) in validator_definitions.yml.") |
| 37 | + .arg( |
| 38 | + Arg::with_name(PUBKEY_FLAG) |
| 39 | + .long(PUBKEY_FLAG) |
| 40 | + .value_name("PUBKEY") |
| 41 | + .help("Validator pubkey to disable") |
| 42 | + .takes_value(true), |
| 43 | + ) |
| 44 | + .arg( |
| 45 | + Arg::with_name(ALL) |
| 46 | + .long(ALL) |
| 47 | + .help("Disable all validators in the validator directory") |
| 48 | + .takes_value(false) |
| 49 | + .conflicts_with(PUBKEY_FLAG), |
| 50 | + ), |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), String> { |
| 55 | + // `true` implies we are setting `validator_definition.enabled = true` and |
| 56 | + // vice versa. |
| 57 | + let (enabled, sub_matches) = match matches.subcommand() { |
| 58 | + (ENABLE, Some(sub_matches)) => (true, sub_matches), |
| 59 | + (DISABLE, Some(sub_matches)) => (false, sub_matches), |
| 60 | + (unknown, _) => { |
| 61 | + return Err(format!( |
| 62 | + "{} does not have a {} command. See --help", |
| 63 | + CMD, unknown |
| 64 | + )) |
| 65 | + } |
| 66 | + }; |
| 67 | + let mut defs = ValidatorDefinitions::open(&validator_dir).map_err(|e| { |
| 68 | + format!( |
| 69 | + "No validator definitions found in {:?}: {:?}", |
| 70 | + validator_dir, e |
| 71 | + ) |
| 72 | + })?; |
| 73 | + let pubkeys_to_modify = if sub_matches.is_present(ALL) { |
| 74 | + defs.as_slice() |
| 75 | + .iter() |
| 76 | + .map(|def| def.voting_public_key.clone()) |
| 77 | + .collect::<HashSet<_>>() |
| 78 | + } else { |
| 79 | + let public_key: PublicKey = clap_utils::parse_required(sub_matches, PUBKEY_FLAG)?; |
| 80 | + std::iter::once(public_key).collect::<HashSet<PublicKey>>() |
| 81 | + }; |
| 82 | + |
| 83 | + // Modify required entries from validator_definitions. |
| 84 | + for def in defs.as_mut_slice() { |
| 85 | + if pubkeys_to_modify.contains(&def.voting_public_key) { |
| 86 | + def.enabled = enabled; |
| 87 | + eprintln!( |
| 88 | + "Validator {} {}", |
| 89 | + def.voting_public_key, |
| 90 | + if enabled { "enabled" } else { "disabled" } |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + defs.save(&validator_dir) |
| 96 | + .map_err(|e| format!("Unable to modify validator definitions: {:?}", e))?; |
| 97 | + |
| 98 | + eprintln!("\nSuccessfully modified validator_definitions.yml"); |
| 99 | + Ok(()) |
| 100 | +} |
0 commit comments