Skip to content

Commit

Permalink
[feature] #3019: Support JSON5
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Polyakov <[email protected]>
  • Loading branch information
Arjentix authored and appetrosyan committed Dec 23, 2022
1 parent 74f9780 commit 3e1fb30
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 58 deletions.
76 changes: 75 additions & 1 deletion Cargo.lock

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

39 changes: 30 additions & 9 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
clippy::std_instead_of_core,
clippy::std_instead_of_alloc
)]
use std::{panic, path::PathBuf, sync::Arc};
use std::{panic, sync::Arc};

use color_eyre::eyre::{eyre, Result, WrapErr};
use eyre::ContextCompat;
use iroha_actor::{broker::*, prelude::*};
use iroha_config::{
base::proxy::{LoadFromDisk, LoadFromEnv, Override},
iroha::{Configuration, ConfigurationProxy},
path::Path as ConfigPath,
};
use iroha_core::{
block_sync::BlockSynchronizer,
Expand Down Expand Up @@ -50,25 +52,33 @@ pub struct Arguments {
/// Set this flag on the peer that should submit genesis on the network initial start.
pub submit_genesis: bool,
/// Set custom genesis file path. `None` if `submit_genesis` set to `false`.
pub genesis_path: Option<PathBuf>,
pub genesis_path: Option<ConfigPath>,
/// Set custom config file path.
pub config_path: PathBuf,
pub config_path: ConfigPath,
}

const CONFIGURATION_PATH: &str = "config.json";
const GENESIS_PATH: &str = "genesis.json";
const CONFIGURATION_PATH: &str = "config";
const GENESIS_PATH: &str = "genesis";
const SUBMIT_GENESIS: bool = false;

impl Default for Arguments {
fn default() -> Self {
Self {
submit_genesis: SUBMIT_GENESIS,
genesis_path: Some(GENESIS_PATH.into()),
config_path: CONFIGURATION_PATH.into(),
genesis_path: Some(ConfigPath::default(GENESIS_PATH).expect(&format!(
"Default genesis path `{}` has extension, but it should not have one.",
GENESIS_PATH
))),
config_path: ConfigPath::default(CONFIGURATION_PATH).expect(&format!(
"Default config path `{}` has extension, but it should not have one.",
GENESIS_PATH
)),
}
}
}

pub mod config {}

/// Iroha is an [Orchestrator](https://en.wikipedia.org/wiki/Orchestration_%28computing%29) of the
/// system. It configures, coordinates and manages transactions and queries processing, work of consensus and storage.
pub struct Iroha {
Expand Down Expand Up @@ -154,7 +164,13 @@ impl Iroha {
query_judge: QueryJudgeBoxed,
) -> Result<Self> {
let broker = Broker::new();
let file_proxy = ConfigurationProxy::from_path(&args.config_path);
let file_proxy = ConfigurationProxy::from_path(
&args
.config_path
.first_existing_path()
.wrap_err("Configuration file does not exist")?
.as_ref(),
);
let env_proxy = ConfigurationProxy::from_env();
let config = file_proxy.override_with(env_proxy).build()?;

Expand All @@ -170,7 +186,12 @@ impl Iroha {
let genesis = if let Some(genesis_path) = &args.genesis_path {
GenesisNetwork::from_configuration(
args.submit_genesis,
RawGenesisBlock::from_path(genesis_path)?,
RawGenesisBlock::from_path(
genesis_path
.first_existing_path()
.wrap_err("Genesis block file doesn't exist")?
.as_ref(),
)?,
Some(&config.genesis),
&config.sumeragi.transaction_limits,
)
Expand Down
18 changes: 8 additions & 10 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! Iroha peer command-line interface.

use core::str::FromStr;

use eyre::WrapErr as _;
use iroha::Arguments;
use iroha_config::path::Path as ConfigPath;
use iroha_core::prelude::AllowAll;
use iroha_permissions_validators::public_blockchain::default_permissions;

#[tokio::main]
async fn main() -> Result<(), color_eyre::Report> {
let mut args = Arguments::default();
let mut args = iroha::Arguments::default();
if std::env::args().any(|a| is_help(&a)) {
print_help();
return Ok(());
Expand All @@ -23,7 +21,7 @@ async fn main() -> Result<(), color_eyre::Report> {
if std::env::args().any(|a| is_submit(&a)) {
args.submit_genesis = true;
if let Ok(genesis_path) = std::env::var("IROHA2_GENESIS_PATH") {
args.genesis_path = Some(std::path::PathBuf::from_str(&genesis_path)?);
args.genesis_path = Some(ConfigPath::user_provided(&genesis_path)?);
}
} else {
args.genesis_path = None;
Expand All @@ -37,7 +35,7 @@ async fn main() -> Result<(), color_eyre::Report> {
}

if let Ok(config_path) = std::env::var("IROHA2_CONFIG_PATH") {
args.config_path = std::path::PathBuf::from_str(&config_path)?;
args.config_path = ConfigPath::user_provided(&config_path)?;
}
if !args.config_path.exists() {
// Require all the fields defined in default `config.json`
Expand Down Expand Up @@ -85,12 +83,12 @@ fn print_help() {
println!("pass `--version` or `-V` to print version information");
println!();
println!("Iroha 2 is configured via environment variables:");
println!(" IROHA2_CONFIG_PATH is the location of your `config.json`");
println!(" IROHA2_GENESIS_PATH is the location of `genesis.json`");
println!(" IROHA2_CONFIG_PATH is the location of your `config.json` or `config.json5`");
println!(" IROHA2_GENESIS_PATH is the location of `genesis.json` or `genesis.json5`");
println!("If either of these is not provided, Iroha checks the current directory.");
println!(
"Additionally, in case of absence of both IROHA2_CONFIG_PATH and `config.json`
in the current directory, all the variables from `config.json` should be set via the environment
"Additionally, in case of absence of both IROHA2_CONFIG_PATH and `config.json`/`config.json5`
in the current directory, all the variables from `config.json`/`config.json5` should be set via the environment
as follows:"
);
println!(" IROHA_TORII is the torii gateway config");
Expand Down
2 changes: 1 addition & 1 deletion client_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ iroha_config = { version = "=2.0.0-pre-rc.11", path = "../config" }
color-eyre = "0.6.2"
clap = { version = "3.2.16", features = ["derive"] }
dialoguer = { version = "0.10.2", default-features = false }
serde_json = "1.0.83"
json5 = "0.4.1"

[build-dependencies]
anyhow = "1.0.60"
Expand Down
Loading

0 comments on commit 3e1fb30

Please sign in to comment.