Skip to content

Commit

Permalink
fix(sui): be consistent about sui config directory
Browse files Browse the repository at this point in the history
## Description
When a `--config` is supplied and it is a directory, we treat it as the
sui config directory in some but not all cases. This changes makes it so
that if `--config` does not look like a YAML file, then it is treated
like it should be the config directory, and that location is used
everywhere the config directory is expected.

## Test plan

Manually tested running `sui start`, `sui start --config ...`.
  • Loading branch information
amnn committed Nov 29, 2024
1 parent b806afe commit d2e3cda
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions crates/sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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();

Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit d2e3cda

Please sign in to comment.