Skip to content

Commit

Permalink
[fix] #3031: Fix the UI/UX of missing configuration parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Petrosyan <a-p-petrosyan@yandex.ru>
  • Loading branch information
appetrosyan committed Jun 5, 2023
1 parent 22ddbd3 commit f668577
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 144 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 74 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#![allow(clippy::print_stdout)]
use std::env;

use color_eyre::eyre::WrapErr as _;
use iroha::style::Styling;
use iroha_config::path::Path as ConfigPath;
use iroha_genesis::{GenesisNetwork, GenesisNetworkTrait as _, RawGenesisBlock};
use owo_colors::OwoColorize as _;

const HELP_ARG: [&str; 2] = ["--help", "-h"];
Expand All @@ -27,6 +29,19 @@ const REQUIRED_ENV_VARS: [(&str, &str); 7] = [
];

#[tokio::main]
/// To make `Iroha` peer work all actors should be started first.
/// After that moment it you can start it with listening to torii events.
///
/// # Side effect
/// - Prints welcome message in the log
///
/// # Errors
/// - Reading genesis from disk
/// - Reading config file
/// - Reading config from `env`
/// - Missing required fields in combined configuration
/// - Telemetry setup
/// - [`Sumeragi`] init
async fn main() -> Result<(), color_eyre::Report> {
let styling = Styling::new();
let mut args = iroha::Arguments::default();
Expand All @@ -43,7 +58,12 @@ async fn main() -> Result<(), color_eyre::Report> {
if env::args().any(|a| SUBMIT_ARG.contains(&a.as_str())) {
args.submit_genesis = true;
if let Ok(genesis_path) = env::var("IROHA2_GENESIS_PATH") {
args.genesis_path = Some(ConfigPath::user_provided(&genesis_path)?);
args.genesis_path =
Some(ConfigPath::user_provided(&genesis_path)
.map_err(|e| {color_eyre::install().expect("CRITICAL"); e})
.wrap_err_with(
||
format!("Could not read `{genesis_path}`, which is required, given you requested `--submit-genesis` on the command line."))?);
}
} else {
args.genesis_path = None;
Expand All @@ -56,6 +76,16 @@ async fn main() -> Result<(), color_eyre::Report> {
.any(|group| group.contains(&arg.as_str())))
{
print_help(&styling)?;
// WORKAROUND for #2212: because of how `color_eyre`
// works, we need to install the hook before creating any
// instance of `eyre::Report`, otherwise the `eyre`
// default reporting hook is going to be installed
// automatically.

// This results in a nasty repetition of the
// `color_eyre::install().unwrap()` pattern, which is the
// lesser of two evils
color_eyre::install().expect("CRITICAL");
eyre::bail!(
"Unrecognised command-line flag `{}`",
arg.style(styling.negative)
Expand All @@ -64,12 +94,19 @@ async fn main() -> Result<(), color_eyre::Report> {
}

if let Ok(config_path) = env::var("IROHA2_CONFIG_PATH") {
args.config_path = ConfigPath::user_provided(&config_path)?;
args.config_path = ConfigPath::user_provided(&config_path)
.map_err(|e| {
color_eyre::install().expect("CRITICAL");
e
})
.wrap_err_with(|| format!("Failed to parse `{config_path}` as configuration path"))?;
}
if !args.config_path.exists() {
// Require all the fields defined in default `config.json`
// to be specified as env vars with their respective prefixes

// TODO: Consider moving these into the
// `iroha::combine_configs` and dependent functions.
for var_name in REQUIRED_ENV_VARS {
// Rather than short circuit and require the person to fix
// the missing env vars one by one, print out the whole
Expand All @@ -84,7 +121,41 @@ async fn main() -> Result<(), color_eyre::Report> {
}
}

<iroha::Iroha>::new(&args).await?.start().await?;
let config = iroha::combine_configs(&args)?;
let telemetry = iroha_logger::init(&config.logger)?;
iroha_logger::info!(
git_commit_sha = env!("VERGEN_GIT_SHA"),
"Hyperledgerいろは2にようこそ!(translation) Welcome to Hyperledger Iroha {}!",
env!("CARGO_PKG_VERSION")
);

let genesis = if let Some(genesis_path) = &args.genesis_path {
GenesisNetwork::from_configuration(
args.submit_genesis,
RawGenesisBlock::from_path(
genesis_path
.first_existing_path()
.ok_or({
color_eyre::install().expect("CRITICAL");
color_eyre::eyre::eyre!("Genesis block file {genesis_path:?} doesn't exist")
})?
.as_ref(),
)?,
Some(&config.genesis),
&config.sumeragi.transaction_limits,
)
.wrap_err("Failed to initialize genesis.")?
} else {
None
};

iroha::Iroha::with_genesis(
genesis,
config,
iroha_actor::broker::Broker::new(),
telemetry,
)
.await?;
Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ serde_json = "1.0.91"
json5 = "0.4.1"
thiserror = "1.0.38"
derive_more = "0.99.17"
cfg-if = "1.0.0"
path-absolutize = "3.0.14"

[dev-dependencies]
proptest = "1.0.0"
Expand Down
1 change: 1 addition & 0 deletions config/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license.workspace = true

[dependencies]
iroha_config_derive = { path = "derive" }
iroha_crypto = { version = "=2.0.0-pre-rc.13", path = "../../crypto" }

serde = { version = "1.0.151", default-features = false, features = ["derive"] }
serde_json = "1.0.91"
Expand Down
8 changes: 7 additions & 1 deletion config/base/derive/src/documented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@ fn impl_get_recursive(ast: &StructWithFields) -> proc_macro2::TokenStream {
quote! {
[stringify!(#ident)] => {
serde_json::to_value(&#l_value)
.map_err(|e| ::iroha_config_base::derive::Error::field_error(stringify!(#ident), e))?
.map_err(
|error|
::iroha_config_base::derive::Error::FieldDeserialization {
field: stringify!(#ident),
error
}
)?
}
#inner_thing2
}
Expand Down
13 changes: 7 additions & 6 deletions config/base/derive/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn impl_load_from_env(ast: &StructWithFields) -> TokenStream {
false
};
let err_ty = quote! { ::iroha_config_base::derive::Error };
let err_variant = quote! { ::iroha_config_base::derive::Error::SerdeError };
let err_variant = quote! { ::iroha_config_base::derive::Error::Json5 };
let inner = if is_string {
quote! { Ok(var) }
} else if as_str_attr {
Expand Down Expand Up @@ -152,8 +152,8 @@ pub fn impl_load_from_disk(ast: &StructWithFields) -> TokenStream {
let proxy_name = &ast.ident;
let disk_trait = quote! { ::iroha_config_base::proxy::LoadFromDisk };
let error_ty = quote! { ::iroha_config_base::derive::Error };
let disk_err_variant = quote! { ::iroha_config_base::derive::Error::DiskError };
let serde_err_variant = quote! { ::iroha_config_base::derive::Error::SerdeError };
let disk_err_variant = quote! { ::iroha_config_base::derive::Error::Disk };
let serde_err_variant = quote! { ::iroha_config_base::derive::Error::Json5 };
let none_proxy = gen_none_fields_proxy(ast);
quote! {
impl #disk_trait for #proxy_name {
Expand Down Expand Up @@ -244,21 +244,22 @@ pub fn impl_build(ast: &StructWithFields) -> TokenStream {
fn gen_none_fields_check(ast: &StructWithFields) -> proc_macro2::TokenStream {
let checked_fields = ast.fields.iter().map(|field| {
let ident = &field.ident;
let err_variant = quote! { ::iroha_config_base::derive::Error::ProxyBuildError };
let missing_field = quote! { ::iroha_config_base::derive::Error::MissingField };
if field.has_inner {
let inner_ty = get_inner_type("Option", &field.ty);
let builder_trait = quote! { ::iroha_config_base::proxy::Builder };
quote! {
#ident: <#inner_ty as #builder_trait>::build(
self.#ident.ok_or(
#err_variant(stringify!(#ident).to_owned())
#missing_field{field: stringify!(#ident), message: ""}
)?
)?
}
} else {
quote! {
#ident: self.#ident.ok_or(
#err_variant(stringify!(#ident).to_owned()))?
#missing_field{field: stringify!(#ident), message: ""}
)?
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion config/base/derive/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod gen {
const _: () = {
use iroha_config_base::view::NoView;
#(
const _: () = assert!(!iroha_config_base::view::IsHasView::<#field_types>::IS_HAS_VIEW, #messages);
const _: () = assert!(!iroha_config_base::view::IsInstanceHasView::<#field_types>::IS_HAS_VIEW, #messages);
)*
};
}
Expand Down
Loading

0 comments on commit f668577

Please sign in to comment.