Skip to content

Commit

Permalink
write genesis config files with hostname:port (#12530) (#13040)
Browse files Browse the repository at this point in the history
cherry-pick #12530 to 1.5 so I
unbreak ci
  • Loading branch information
johnjmartin authored Jul 18, 2023
1 parent e7c5810 commit cc4ad6d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 67 deletions.
33 changes: 33 additions & 0 deletions crates/mysten-network/src/multiaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ impl Multiaddr {
}
Self(new_address)
}

pub fn hostname(&self) -> Option<String> {
for component in self.iter() {
match component {
Protocol::Ip4(ip) => return Some(ip.to_string()),
Protocol::Ip6(ip) => return Some(ip.to_string()),
Protocol::Dns(dns) => return Some(dns.to_string()),
_ => (),
}
}
None
}

pub fn port(&self) -> Option<u16> {
for component in self.iter() {
match component {
Protocol::Udp(port) | Protocol::Tcp(port) => return Some(port),
_ => (),
}
}
None
}
}

impl std::fmt::Display for Multiaddr {
Expand Down Expand Up @@ -299,4 +321,15 @@ mod test {
.to_socket_addr()
.expect_err("DNS is unsupported");
}

#[test]
fn test_get_hostname_port() {
let multi_addr_ip4 = Multiaddr(multiaddr!(Ip4([127, 0, 0, 1]), Tcp(10500u16)));
assert_eq!(Some("127.0.0.1".to_string()), multi_addr_ip4.hostname());
assert_eq!(Some(10500u16), multi_addr_ip4.port());

let multi_addr_dns = Multiaddr(multiaddr!(Dns("mysten.sui"), Tcp(10501u16)));
assert_eq!(Some("mysten.sui".to_string()), multi_addr_dns.hostname());
assert_eq!(Some(10501u16), multi_addr_dns.port());
}
}
25 changes: 22 additions & 3 deletions crates/sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use sui_swarm_config::network_config::NetworkConfig;
use sui_swarm_config::network_config_builder::ConfigBuilder;
use sui_swarm_config::node_config_builder::FullnodeConfigBuilder;
use sui_types::crypto::{SignatureScheme, SuiKeyPair};
use sui_types::multiaddr::Multiaddr;
use tracing::info;

#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -468,7 +469,10 @@ async fn genesis(
let mut ssfn_nodes = vec![];
if let Some(ssfn_info) = ssfn_info {
for (i, ssfn) in ssfn_info.into_iter().enumerate() {
let path = sui_config_dir.join(sui_config::ssfn_config_file(i));
let path = sui_config_dir.join(multiaddr_to_filename(
ssfn.p2p_address.clone(),
sui_config::ssfn_config_file(i),
));
// join base fullnode config with each SsfnGenesisConfig entry
let ssfn_config = FullnodeConfigBuilder::new()
.with_config_directory(FULL_NODE_DB_PATH.into())
Expand Down Expand Up @@ -501,7 +505,10 @@ async fn genesis(
.into_iter()
.enumerate()
{
let path = sui_config_dir.join(sui_config::validator_config_file(i));
let path = sui_config_dir.join(multiaddr_to_filename(
validator.network_address.clone(),
sui_config::validator_config_file(i),
));
let mut val_p2p = validator.p2p_config.clone();
val_p2p.seed_peers = ssfn_seed_peers.clone();
validator.p2p_config = val_p2p;
Expand All @@ -513,7 +520,10 @@ async fn genesis(
.into_iter()
.enumerate()
{
let path = sui_config_dir.join(sui_config::validator_config_file(i));
let path = sui_config_dir.join(multiaddr_to_filename(
validator.network_address.clone(),
sui_config::validator_config_file(i),
));
validator.save(path)?;
}
}
Expand Down Expand Up @@ -640,3 +650,12 @@ fn read_line() -> Result<String, anyhow::Error> {
io::stdin().read_line(&mut s)?;
Ok(s.trim_end().to_string())
}

fn multiaddr_to_filename(address: Multiaddr, default: String) -> String {
if let Some(hostname) = address.hostname() {
if let Some(port) = address.port() {
return format!("{}-{}.yaml", hostname, port);
}
}
default
}
65 changes: 1 addition & 64 deletions crates/sui/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use sui::{
client_commands::{SuiClientCommandResult, SuiClientCommands},
sui_commands::SuiCommand,
};
use sui_config::{Config, NodeConfig, SUI_BENCHMARK_GENESIS_GAS_KEYSTORE_FILENAME};
use sui_config::{
PersistedConfig, SUI_CLIENT_CONFIG, SUI_FULLNODE_CONFIG, SUI_GENESIS_FILENAME,
SUI_KEYSTORE_FILENAME, SUI_NETWORK_CONFIG,
Expand All @@ -31,7 +30,7 @@ use sui_json_rpc_types::{
OwnedObjectRef, SuiObjectData, SuiObjectDataFilter, SuiObjectDataOptions, SuiObjectResponse,
SuiObjectResponseQuery, SuiTransactionBlockEffects, SuiTransactionBlockEffectsAPI,
};
use sui_keys::keystore::{AccountKeystore, FileBasedKeystore};
use sui_keys::keystore::AccountKeystore;
use sui_macros::sim_test;
use sui_move_build::{BuildConfig, SuiPackageHooks};
use sui_sdk::sui_client_config::SuiClientConfig;
Expand Down Expand Up @@ -119,68 +118,6 @@ async fn test_genesis() -> Result<(), anyhow::Error> {
Ok(())
}

#[sim_test]
async fn test_genesis_for_benchmarks() -> Result<(), anyhow::Error> {
let temp_dir = tempfile::tempdir()?;
let working_dir = temp_dir.path();

// Make some (meaningless) ip addresses for the committee.
let benchmark_ips = vec![
"1.1.1.1".into(),
"1.1.1.2".into(),
"1.1.1.3".into(),
"1.1.1.4".into(),
"1.1.1.5".into(),
];
let committee_size = benchmark_ips.len();

// Print all the genesis and config files.
SuiCommand::Genesis {
working_dir: Some(working_dir.to_path_buf()),
write_config: None,
force: false,
from_config: None,
epoch_duration_ms: None,
benchmark_ips: Some(benchmark_ips.clone()),
with_faucet: false,
}
.execute()
.await?;

// Get all the newly created file names.
let files = read_dir(working_dir)?
.flat_map(|r| r.map(|file| file.file_name().to_str().unwrap().to_owned()))
.collect::<Vec<_>>();

// We expect one file per validator (the validator's configs) as well as various network
// and client configuration files. We particularly care about the genesis blob, the keystore
// containing the key of the initial gas objects, and the validators' configuration files.
assert!(files.contains(&SUI_GENESIS_FILENAME.to_string()));
assert!(files.contains(&SUI_BENCHMARK_GENESIS_GAS_KEYSTORE_FILENAME.to_string()));
for i in 0..committee_size {
assert!(files.contains(&sui_config::validator_config_file(i)));
}

// Check the network config and ensure each validator boots on the specified ip address.
for (i, expected_ip) in benchmark_ips.into_iter().enumerate() {
let config_path = &working_dir.join(sui_config::validator_config_file(i));
let config = NodeConfig::load(config_path)?;
let socket_address = config.network_address.to_socket_addr().unwrap();
assert_eq!(expected_ip, socket_address.ip().to_string());
}

// Ensure the keystore containing the genesis gas objects is created as expected.
let path = working_dir.join(SUI_BENCHMARK_GENESIS_GAS_KEYSTORE_FILENAME);
let keystore = FileBasedKeystore::new(&path)?;
let expected_gas_key = GenesisConfig::benchmark_gas_key();
let expected_gas_address = SuiAddress::from(&expected_gas_key.public());
let stored_gas_key = keystore.get_key(&expected_gas_address)?;
assert_eq!(&expected_gas_key, stored_gas_key);

temp_dir.close()?;
Ok(())
}

#[tokio::test]
async fn test_addresses_command() -> Result<(), anyhow::Error> {
let test_cluster = TestClusterBuilder::new().build().await;
Expand Down

0 comments on commit cc4ad6d

Please sign in to comment.