diff --git a/net/k8s-cluster/src/genesis.rs b/net/k8s-cluster/src/genesis.rs index f03870dc29c38c..59670464cd93c6 100644 --- a/net/k8s-cluster/src/genesis.rs +++ b/net/k8s-cluster/src/genesis.rs @@ -109,17 +109,43 @@ impl<'a> Genesis<'a> { Ok(()) } - pub fn generate_accounts(&mut self, validator_type: &str) -> Result<(), Box> { - let mut accounts = vec!["identity", "vote-account", "stake-account"]; + pub fn generate_accounts( + &mut self, + validator_type: &str, + number_of_accounts: i32 + ) -> Result<(), Box> { let mut filename_prefix = "validator".to_string(); if validator_type == "bootstrap" { filename_prefix = format!("{}-{}", validator_type, filename_prefix); + } else if validator_type == "validator" { + filename_prefix = "validator".to_string(); + } else { + return Err(boxed_error!(format!("Invalid validator type: {}", validator_type))); } + + for i in 0..number_of_accounts { + self.generate_account(validator_type, filename_prefix.as_str(), i)?; + } + + + Ok(()) + } + + fn generate_account(&mut self, validator_type: &str, filename_prefix: &str, i: i32) -> Result<(), Box> { + let account_types = vec!["identity", "vote-account", "stake-account"]; let mut identity: Option = None; let mut vote: Option = None; let mut stake: Option = None; - for account in accounts { - let filename = format!("{}/{}.json", filename_prefix, account); + for account in account_types { + let filename: String; + if validator_type == "bootstrap" { + filename = format!("{}/{}.json", filename_prefix, account); + } else if validator_type == "validator" { + filename = format!("{}-{}-{}.json", filename_prefix, account, i); + } else { + return Err(boxed_error!(format!("Invalid validator type: {}", validator_type))); + } + let outfile = self.config_dir.join(filename); info!("outfile: {:?}", outfile); @@ -160,6 +186,7 @@ impl<'a> Genesis<'a> { let rent = Rent::default(); // vote account + // lamports set a default 500 let default_bootstrap_validator_lamports = &sol_to_lamports(500.0) .max(VoteState::get_rent_exempt_reserve(&rent)) .to_string(); @@ -284,22 +311,6 @@ impl<'a> Genesis<'a> { Ok(()) } - // // convert binary file to base64 - // pub fn setup_config_map( - // &self, - // ) -> Result<(), Box> { - // self.verify_genesis_from_file()?; - // let path = self.config_dir.join("bootstrap-validator").join(DEFAULT_GENESIS_FILE); - // let mut input_content = Vec::new(); - // File::open(path)?.read_to_end(&mut input_content)?; - // let base64_content = general_purpose::STANDARD.encode(input_content); - - // let outpath = self.config_dir.join("bootstrap-validator").join("genesis-config-map.yaml"); - - // Ok(()) - - // } - pub fn load_genesis_to_base64_from_file(&self) -> Result> { let path = self .config_dir @@ -315,12 +326,6 @@ impl<'a> Genesis<'a> { // should be run inside pod pub fn verify_genesis_from_file(&self) -> Result<(), Box> { let path = self.config_dir.join("bootstrap-validator"); - // let loaded_config = GenesisConfig::load(&path); - // let loaded_config = match GenesisConfig::load(&path) { - // Ok(config) => config, - // Err(err) => return Err(boxed_error!(format!("Failed to load genesis config from file! err: {}", err))), - // }; - let loaded_config = GenesisConfig::load(&path)?; info!("hash of loaded genesis: {}", loaded_config.hash()); match &self.genesis_config { diff --git a/net/k8s-cluster/src/main.rs b/net/k8s-cluster/src/main.rs index 9a6e6ba61f47b1..788a56532b416a 100644 --- a/net/k8s-cluster/src/main.rs +++ b/net/k8s-cluster/src/main.rs @@ -1,5 +1,5 @@ use { - clap::{crate_description, crate_name, value_t_or_exit, App, Arg, ArgMatches}, + clap::{crate_description, crate_name, value_t_or_exit, App, Arg, ArgMatches, value_t}, k8s_openapi::{ api::{ apps::v1::{Deployment, DeploymentSpec}, @@ -46,7 +46,11 @@ fn parse_matches() -> ArgMatches<'static> { .long("num-validators") .takes_value(true) .default_value("1") - .help("Number of validator replicas to deploy"), + .help("Number of validator replicas to deploy") + .validator(|s| match s.parse::() { + Ok(n) if n > 0 => Ok(()), + _ => Err(String::from("number_of_validators should be greater than 0")), + }), ) .arg( Arg::with_name("bootstrap_container_name") @@ -211,7 +215,15 @@ async fn main() { return; } } - match genesis.generate_accounts("bootstrap") { + match genesis.generate_accounts("bootstrap", 1) { + Ok(_) => (), + Err(err) => { + error!("generate accounts error! {}", err); + return; + } + } + + match genesis.generate_accounts("validator", setup_config.num_validators) { Ok(_) => (), Err(err) => { error!("generate accounts error! {}", err);