Skip to content

Commit

Permalink
Merge pull request solana-labs#16 from gregcusack/solana-k8s-cluster-…
Browse files Browse the repository at this point in the history
…genesis

add in validator accounts into genesis
  • Loading branch information
Greg Cusack authored Aug 31, 2023
2 parents d9850f2 + b8da2dd commit da8ee55
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
57 changes: 31 additions & 26 deletions net/k8s-cluster/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,43 @@ impl<'a> Genesis<'a> {
Ok(())
}

pub fn generate_accounts(&mut self, validator_type: &str) -> Result<(), Box<dyn Error>> {
let mut accounts = vec!["identity", "vote-account", "stake-account"];
pub fn generate_accounts(
&mut self,
validator_type: &str,
number_of_accounts: i32
) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
let account_types = vec!["identity", "vote-account", "stake-account"];
let mut identity: Option<Keypair> = None;
let mut vote: Option<Keypair> = None;
let mut stake: Option<Keypair> = 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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -284,22 +311,6 @@ impl<'a> Genesis<'a> {
Ok(())
}

// // convert binary file to base64
// pub fn setup_config_map(
// &self,
// ) -> Result<(), Box<dyn Error>> {
// 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<String, Box<dyn Error>> {
let path = self
.config_dir
Expand All @@ -315,12 +326,6 @@ impl<'a> Genesis<'a> {
// should be run inside pod
pub fn verify_genesis_from_file(&self) -> Result<(), Box<dyn Error>> {
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 {
Expand Down
18 changes: 15 additions & 3 deletions net/k8s-cluster/src/main.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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::<i32>() {
Ok(n) if n > 0 => Ok(()),
_ => Err(String::from("number_of_validators should be greater than 0")),
}),
)
.arg(
Arg::with_name("bootstrap_container_name")
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit da8ee55

Please sign in to comment.