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

[refactor] #3833, #2373, #3601: Split off Kagami #3841

Merged
merged 18 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
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
77 changes: 32 additions & 45 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ members = [
"tools/kagami",
"tools/kura_inspector",
"tools/parity_scale_decoder",
"tools/swarm",
"tools/wasm_builder_cli",
"version",
"version/derive",
"wasm_codec",
Expand Down
8 changes: 8 additions & 0 deletions default_validator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# `iroha_default_validator`

Use the [Wasm Builder CLI](../tools/wasm_builder_cli) in order to build it:

```bash
cargo run --bin iroha_wasm_builder_cli -- \
build ./default_validator --optimize --outfile ./configs/peer/validator.wasm
```
15 changes: 8 additions & 7 deletions scripts/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ case $1 in
exit 1
};;
"genesis")
cargo run --release --bin kagami -- genesis --compiled-validator-path ./validator.wasm | diff - configs/peer/genesis.json || {
echo 'Please re-generate the genesis with `cargo run --release --bin kagami -- genesis > configs/peer/genesis.json`'
cargo run --release --bin kagami -- genesis --validator-path-in-genesis ./validator.wasm | diff - configs/peer/genesis.json || {
echo 'Please re-generate the genesis with `cargo run --release --bin kagami -- genesis --validator-path-in-genesis ./validator.wasm > configs/peer/genesis.json`'
exit 1
};;
"client")
Expand All @@ -35,24 +35,25 @@ case $1 in
# it is not a default behaviour because Kagami resolves `build` path relative
# to the output file location
temp_file="docker-compose.TMP.yml"
full_cmd="$cmd_base --outfile $temp_file"

eval "$cmd_base $temp_file"
eval "$full_cmd"
diff "$temp_file" "$target" || {
echo "Please re-generate \`$target\` with \`$cmd_base $target\`"
echo "Please re-generate \`$target\` with \`$full_cmd\`"
exit 1
}
}

command_base_for_single() {
echo "cargo run --release --bin kagami -- swarm -p 1 -s Iroha --force file --config-dir ./configs/peer --build ."
echo "cargo run --release --bin iroha_swarm -- -p 1 -s Iroha --force --config-dir ./configs/peer --build ."
}

command_base_for_multiple_local() {
echo "cargo run --release --bin kagami -- swarm -p 4 -s Iroha --force file --config-dir ./configs/peer --build ."
echo "cargo run --release --bin iroha_swarm -- -p 4 -s Iroha --force --config-dir ./configs/peer --build ."
}

command_base_for_default() {
echo "cargo run --release --bin kagami -- swarm -p 4 -s Iroha --force file --config-dir ./configs/peer --image hyperledger/iroha2:dev"
echo "cargo run --release --bin iroha_swarm -- -p 4 -s Iroha --force --config-dir ./configs/peer --image hyperledger/iroha2:dev"
}


Expand Down
15 changes: 0 additions & 15 deletions tools/kagami/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,10 @@ iroha_data_model = { workspace = true }
iroha_schema_gen = { workspace = true }
iroha_primitives = { workspace = true }
iroha_genesis = { workspace = true }
iroha_wasm_builder = { workspace = true }

color-eyre = { workspace = true }
clap = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
derive_more = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_yaml = { workspace = true }
expect-test = { workspace = true }
pathdiff = { workspace = true }
path-absolutize = { workspace = true }
spinoff = { workspace = true, features = ["aesthetic"] }
owo-colors = { workspace = true, features = ["supports-colors"] }
supports-color = { workspace = true }
inquire = { workspace = true }
duct = { workspace = true }
tempfile = { workspace = true }
parity-scale-codec = { workspace = true }

[build-dependencies]
eyre = { workspace = true }
vergen = { workspace = true, features = ["git", "gitoxide"] }
8 changes: 0 additions & 8 deletions tools/kagami/build.rs

This file was deleted.

109 changes: 63 additions & 46 deletions tools/kagami/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ use iroha_data_model::{
metadata::Limits,
parameter::{default::*, ParametersBuilder},
prelude::AssetId,
validator::Validator,
IdBox,
};
use iroha_genesis::{RawGenesisBlock, RawGenesisBlockBuilder, ValidatorMode, ValidatorPath};
use serde_json::json;

use super::*;

const INLINED_VALIDATOR_WARNING: &str = r#"WARN: You're using genesis with inlined validator.
Consider specifying a separate validator file using `--validator-path-in-genesis` instead.
Use `--help` for more information."#;

#[derive(Parser, Debug, Clone)]
#[clap(group = ArgGroup::new("validator").required(true))]
pub struct Args {
/// If this option provided validator will be inlined in the genesis.
#[clap(long, group = "validator")]
inlined_validator: bool,
/// If this option provided validator won't be included in the genesis and only path to the validator will be included.
/// Path is either absolute path to validator or relative to genesis location.
/// Validator can be generated using `kagami validator` command.
#[clap(long, group = "validator")]
compiled_validator_path: Option<PathBuf>,
/// Reads the validator from the file at <PATH> (relative to CWD)
/// and includes the content into the genesis.
///
/// WARN: This approach can lead to reproducibility issues, as WASM builds are currently not
/// guaranteed to be reproducible. Additionally, inlining the validator bloats the genesis JSON
/// and makes it less readable. Consider specifying a separate validator file
/// using `--validator-path-in-genesis` instead. For more details, refer to
/// the related PR: https://github.com/hyperledger/iroha/pull/3434
#[clap(long, group = "validator", value_name = "PATH")]
inline_validator_from_file: Option<PathBuf>,
/// Specifies the <PATH> that will be directly inserted into the genesis JSON as-is.
#[clap(long, group = "validator", value_name = "PATH")]
validator_path_in_genesis: Option<PathBuf>,
#[clap(subcommand)]
mode: Option<Mode>,
}
Expand Down Expand Up @@ -58,46 +66,69 @@ pub enum Mode {

impl<T: Write> RunArgs<T> for Args {
fn run(self, writer: &mut BufWriter<T>) -> Outcome {
if self.inlined_validator {
eprintln!("WARN: You're using genesis with inlined validator.");
eprintln!(
"Consider providing validator in separate file `--compiled-validator-path PATH`."
);
eprintln!("Use `--help` to get more information.");
}
let validator_path = self.compiled_validator_path;
let genesis = match self.mode.unwrap_or_default() {
Mode::Default => generate_default(validator_path),
let Self {
inline_validator_from_file,
validator_path_in_genesis,
mode,
} = self;

let validator: ValidatorMode =
match (inline_validator_from_file, validator_path_in_genesis) {
(Some(path), None) => {
eprintln!("{INLINED_VALIDATOR_WARNING}");
ParsedValidatorArgs::Inline(path)
}
(None, Some(path)) => ParsedValidatorArgs::Path(path),
_ => unreachable!("clap invariant"),
}
.try_into()?;

let genesis = match mode.unwrap_or_default() {
Mode::Default => generate_default(validator),
Mode::Synthetic {
domains,
accounts_per_domain,
assets_per_domain,
} => generate_synthetic(
validator_path,
domains,
accounts_per_domain,
assets_per_domain,
),
} => generate_synthetic(validator, domains, accounts_per_domain, assets_per_domain),
}?;
writeln!(writer, "{}", serde_json::to_string_pretty(&genesis)?)
.wrap_err("Failed to write serialized genesis to the buffer.")
}
}

enum ParsedValidatorArgs {
Inline(PathBuf),
Path(PathBuf),
}

impl TryFrom<ParsedValidatorArgs> for ValidatorMode {
type Error = color_eyre::Report;

fn try_from(value: ParsedValidatorArgs) -> Result<Self, Self::Error> {
let mode = match value {
ParsedValidatorArgs::Path(path) => ValidatorMode::Path(ValidatorPath(path)),
ParsedValidatorArgs::Inline(path) => {
let validator = ValidatorMode::Path(ValidatorPath(path.clone()))
.try_into()
.wrap_err_with(|| {
format!("Failed to read the validator located at {}", path.display())
})?;
ValidatorMode::Inline(validator)
}
};
Ok(mode)
}
}

#[allow(clippy::too_many_lines)]
pub fn generate_default(validator_path: Option<PathBuf>) -> color_eyre::Result<RawGenesisBlock> {
pub fn generate_default(validator: ValidatorMode) -> color_eyre::Result<RawGenesisBlock> {
let mut meta = Metadata::new();
meta.insert_with_limits(
"key".parse()?,
"value".to_owned().into(),
Limits::new(1024, 1024),
)?;

let validator = match validator_path {
Some(validator_path) => ValidatorMode::Path(ValidatorPath(validator_path)),
None => ValidatorMode::Inline(construct_validator()?),
};

let mut genesis = RawGenesisBlockBuilder::new()
.domain_with_metadata("wonderland".parse()?, meta.clone())
.account_with_metadata(
Expand Down Expand Up @@ -177,26 +208,12 @@ pub fn generate_default(validator_path: Option<PathBuf>) -> color_eyre::Result<R
Ok(genesis)
}

fn construct_validator() -> color_eyre::Result<Validator> {
let temp_dir = tempfile::tempdir()
.wrap_err("Failed to generate a tempdir for validator sources")?
.into_path();
let path = super::validator::compute_validator_path(temp_dir)?;
let wasm_blob = super::validator::construct_validator(path)?;
Ok(Validator::new(WasmSmartContract::from_compiled(wasm_blob)))
}

fn generate_synthetic(
validator_path: Option<PathBuf>,
validator: ValidatorMode,
domains: u64,
accounts_per_domain: u64,
assets_per_domain: u64,
) -> color_eyre::Result<RawGenesisBlock> {
let validator = match validator_path {
Some(validator_path) => ValidatorMode::Path(ValidatorPath(validator_path)),
None => ValidatorMode::Inline(construct_validator()?),
};

// Add default `Domain` and `Account` to still be able to query
let mut builder = RawGenesisBlockBuilder::new()
.domain("wonderland".parse()?)
Expand Down
Loading