diff --git a/crates/sui/src/sui_commands.rs b/crates/sui/src/sui_commands.rs index d83350e25bdbd..8a539ade6fbbe 100644 --- a/crates/sui/src/sui_commands.rs +++ b/crates/sui/src/sui_commands.rs @@ -638,41 +638,59 @@ async fn start( } let mut swarm_builder = Swarm::builder(); + // If this is set, then no data will be persisted between runs, and a new genesis will be // generated each run. - if force_regenesis { + let config_dir = if force_regenesis { swarm_builder = swarm_builder.committee_size(NonZeroUsize::new(DEFAULT_NUMBER_OF_AUTHORITIES).unwrap()); let genesis_config = GenesisConfig::custom_genesis(1, 100); swarm_builder = swarm_builder.with_genesis_config(genesis_config); let epoch_duration_ms = epoch_duration_ms.unwrap_or(DEFAULT_EPOCH_DURATION_MS); swarm_builder = swarm_builder.with_epoch_duration_ms(epoch_duration_ms); + tempdir()?.into_path() } else { - if config.is_none() && !sui_config_dir()?.join(SUI_NETWORK_CONFIG).exists() { - genesis(None, None, None, false, epoch_duration_ms, None, false).await.map_err(|_| anyhow!("Cannot run genesis with non-empty Sui config directory: {}.\n\nIf you are trying to run a local network without persisting the data (so a new genesis that is randomly generated and will not be saved once the network is shut down), use --force-regenesis flag.\nIf you are trying to persist the network data and start from a new genesis, use sui genesis --help to see how to generate a new genesis.", sui_config_dir().unwrap().display()))?; - } - - // Load the config of the Sui authority. - // To keep compatibility with sui-test-validator where the user can pass a config - // directory, this checks if the config is a file or a directory - let network_config_path = if let Some(ref config) = config { - if config.is_dir() { - config.join(SUI_NETWORK_CONFIG) - } else if config.is_file() - && config - .extension() - .is_some_and(|ext| (ext == "yml" || ext == "yaml")) + // If the config path looks like a YAML file, it is treated as if it is the network.yaml + // overriding the network.yaml found in the sui config directry. Otherwise it is treated as + // the sui config directory for backwards compatibility with `sui-test-validator`. + let (network_config_path, sui_config_path) = match config { + Some(config) + if config.is_file() + && config + .extension() + .is_some_and(|e| e == "yml" || e == "yaml") => { - config.clone() - } else { - config.join(SUI_NETWORK_CONFIG) + (config, sui_config_dir()?) + } + + Some(config) => (config.join(SUI_NETWORK_CONFIG), config), + + None => { + let sui_config = sui_config_dir()?; + let network_config = sui_config.join(SUI_NETWORK_CONFIG); + + if !network_config.exists() { + genesis(None, None, None, false, epoch_duration_ms, None, false) + .await + .map_err(|_| { + anyhow!( + "Cannot run genesis with non-empty Sui config directory: {}.\n\n\ + If you are trying to run a local network without persisting the \ + data (so a new genesis that is randomly generated and will not be \ + saved once the network is shut down), use --force-regenesis flag.\n\ + If you are trying to persist the network data and start from a new \ + genesis, use sui genesis --help to see how to generate a new \ + genesis.", + sui_config.display(), + ) + })?; + } + + (network_config, sui_config) } - } else { - config - .clone() - .unwrap_or(sui_config_dir()?) - .join(SUI_NETWORK_CONFIG) }; + + // Load the config of the Sui authority. let network_config: NetworkConfig = PersistedConfig::read(&network_config_path).map_err(|err| { err.context(format!( @@ -682,9 +700,11 @@ async fn start( })?; swarm_builder = swarm_builder - .dir(sui_config_dir()?) + .dir(sui_config_path.clone()) .with_network_config(network_config); - } + + sui_config_path + }; let data_ingestion_path = tempdir()?.into_path(); @@ -768,14 +788,6 @@ async fn start( let faucet_address = parse_host_port(input, DEFAULT_FAUCET_PORT) .map_err(|_| anyhow!("Invalid faucet host and port"))?; tracing::info!("Starting the faucet service at {faucet_address}"); - let config_dir = if force_regenesis { - tempdir()?.into_path() - } else { - match config { - Some(config) => config, - None => sui_config_dir()?, - } - }; let host_ip = match faucet_address { SocketAddr::V4(addr) => *addr.ip(),