Skip to content

Commit

Permalink
Let validator reads key pair from network config and remove --address…
Browse files Browse the repository at this point in the history
… and --validator_idx (MystenLabs#1736)

* remove duplicated src/validator.rs

* validator can read network config keypair to decide identity

* fmt

* revert

* fix

* update bench too

Co-authored-by: Lu Zhang <[email protected]>
  • Loading branch information
longbowlu and Lu Zhang authored May 4, 2022
1 parent 231404f commit 532a751
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
2 changes: 0 additions & 2 deletions sui/src/benchmark/validator_preparer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ impl ValidatorPreparer {
let child = Command::new(working_dir.clone().join(VALIDATOR_BINARY_NAME))
.arg("--genesis-config-path")
.arg(GENESIS_CONFIG_NAME)
.arg("--address")
.arg(&self.main_authority_address_hex)
.arg("--force-genesis")
.spawn()
.expect("failed to spawn a validator process");
Expand Down
50 changes: 17 additions & 33 deletions sui/src/bin/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sui::{
sui_commands::{genesis, make_server},
};
use sui_types::{
base_types::{decode_bytes_hex, encode_bytes_hex, SuiAddress},
base_types::{encode_bytes_hex, SuiAddress},
committee::Committee,
};
use tracing::{error, info};
Expand All @@ -34,14 +34,6 @@ struct ValidatorOpt {
#[clap(long)]
pub network_config_path: Option<PathBuf>,

/// Public key/address of the validator to start
#[clap(long, parse(try_from_str = decode_bytes_hex))]
address: Option<SuiAddress>,

/// Index in validator array of validator to start
#[clap(long)]
validator_idx: Option<usize>,

#[clap(long, help = "Specify host:port to listen on")]
listen_address: Option<String>,
}
Expand All @@ -62,38 +54,30 @@ async fn main() -> Result<(), anyhow::Error> {

let network_config_path = sui_config_dir()?.join(SUI_NETWORK_CONFIG);

let genesis_conf: GenesisConfig = PersistedConfig::read(&cfg.genesis_config_path)?;

let authority = if let Some(address) = cfg.address {
// Find the network config for this validator
genesis_conf
.authorities
.iter()
.find(|x| SuiAddress::from(&x.public_key) == address)
.ok_or_else(|| {
anyhow!(
"Network configs must include config for address {}",
address
)
})?
} else if let Some(index) = cfg.validator_idx {
&genesis_conf.authorities[index]
} else {
return Err(anyhow!("Must supply either --address of --validator-idx"));
};

let genesis_conf_copy: GenesisConfig = PersistedConfig::read(&cfg.genesis_config_path)?;

let network_config = match (network_config_path.exists(), cfg.force_genesis) {
(true, false) => PersistedConfig::<NetworkConfig>::read(&network_config_path)?,

// If network.conf is missing, or if --force-genesis is true, we run genesis.
_ => {
let (network_config, _, _) =
genesis(genesis_conf_copy, Some(authority.address)).await?;
let genesis_conf: GenesisConfig = PersistedConfig::read(&cfg.genesis_config_path)?;
let adddress = SuiAddress::from(genesis_conf.key_pair.public_key_bytes());
let (network_config, _, _) = genesis(genesis_conf, Some(adddress)).await?;
network_config
}
};
let public_key_bytes = network_config.key_pair.public_key_bytes();
let address = SuiAddress::from(public_key_bytes);
// Find the network config for this validator
let authority = network_config
.authorities
.iter()
.find(|x| SuiAddress::from(&x.public_key) == address)
.ok_or_else(|| {
anyhow!(
"Keypair (pub key: {:?}) in network config is not in the validator committee",
public_key_bytes,
)
})?;

let listen_address = cfg
.listen_address
Expand Down

0 comments on commit 532a751

Please sign in to comment.