Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sui): be consistent about sui config directory #20474

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +672 to +673
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this potentially override other settings in the sui_config dir?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, genesis will fail if the folder is not empty, so we're probably safe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is pre-existing, but judging by the error message, genesis will not run if the directory is non-empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I did that messy logic 😅 trying to preserve compatibility between sui-test-validator and sui start. :D

.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