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

chore(cli): Improve error message for attempting genesis with non-empty config dir #4987

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
39 changes: 21 additions & 18 deletions crates/iota/src/iota_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,7 @@ async fn start(
}

// Resolve the configuration directory.
let config = config_dir.clone().map_or_else(iota_config_dir, Ok)?;
let network_config_path = config.clone().join(IOTA_NETWORK_CONFIG);
let config_path = config_dir.clone().map_or_else(iota_config_dir, Ok)?;

let mut swarm_builder = Swarm::builder();

Expand Down Expand Up @@ -711,12 +710,13 @@ async fn start(
let epoch_duration_ms = epoch_duration_ms.unwrap_or(DEFAULT_EPOCH_DURATION_MS);
swarm_builder = swarm_builder.with_epoch_duration_ms(epoch_duration_ms);
} else {
let network_config_path = config_path.join(IOTA_NETWORK_CONFIG);
// Auto genesis if no configuration exists in the configuration directory.
if !network_config_path.exists() {
genesis(
None,
None,
Some(config.clone()),
Some(config_path.clone()),
false,
epoch_duration_ms,
None,
Expand All @@ -726,7 +726,12 @@ async fn start(
remote_migration_snapshots,
delegator,
)
.await?;
.await
.map_err(|e| anyhow!("{e}: {}. \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 iota genesis --help \
to see how to generate a new genesis.", config_path.display()))?;
} else if committee_size.is_some() {
eprintln!(
"{}",
Expand All @@ -749,7 +754,7 @@ async fn start(
network_config_path
))
})?;
let genesis_path = config.join(IOTA_GENESIS_FILENAME);
let genesis_path = config_path.join(IOTA_GENESIS_FILENAME);
let genesis = iota_config::genesis::Genesis::load(genesis_path)?;
let network_config = NetworkConfig {
validator_configs,
Expand All @@ -758,7 +763,7 @@ async fn start(
};

swarm_builder = swarm_builder
.dir(config)
.dir(config_path)
.with_network_config(network_config);
}

Expand Down Expand Up @@ -949,11 +954,7 @@ async fn genesis(
// create default IOTA config dir if not specified
// on the command line and if it does not exist
// yet
None => {
let config_path = iota_config_dir()?;
fs::create_dir_all(&config_path)?;
Copy link
Member

Choose a reason for hiding this comment

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

Why remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because iota_config_dir() already does it.

config_path
}
None => iota_config_dir()?,
};

// if IOTA config dir is not empty then either clean it
Expand Down Expand Up @@ -983,28 +984,30 @@ async fn genesis(
fs::remove_dir_all(path)
}
.map_err(|err| {
anyhow!(err).context(format!("Cannot remove file {:?}", file.path()))
anyhow!(err)
.context(format!("Cannot remove file {}", file.path().display()))
})?;
}
}
} else {
fs::remove_dir_all(iota_config_dir).map_err(|err| {
anyhow!(err).context(format!(
"Cannot remove IOTA config dir {:?}",
iota_config_dir
"Cannot remove IOTA config dir {}",
iota_config_dir.display()
))
})?;
fs::create_dir(iota_config_dir).map_err(|err| {
anyhow!(err).context(format!(
"Cannot create IOTA config dir {:?}",
iota_config_dir
"Cannot create IOTA config dir {}",
iota_config_dir.display()
))
})?;
}
} else if files.len() != 2 || !client_path.exists() || !keystore_path.exists() {
bail!(
"cannot run genesis with non-empty IOTA config directory {}, please use the --force/-f option to remove the existing configuration",
iota_config_dir.to_str().unwrap()
"Cannot run genesis with non-empty IOTA config directory {}. \n
Please use the --force/-f option to remove the existing configuration",
iota_config_dir.display()
);
}
}
Expand Down
Loading