From 245ad4aede1a4cfcbccfd06df58453335146b1eb Mon Sep 17 00:00:00 2001 From: Aleksandr Petrosyan Date: Thu, 14 Apr 2022 16:14:21 +0400 Subject: [PATCH] [fix] #1640: Generate config.json and genesis.json Signed-off-by: Aleksandr Petrosyan --- Cargo.lock | 1 + configs/peer/genesis.json | 2 +- core/docs/Cargo.toml | 1 + core/docs/src/main.rs | 37 ++++++++++++++++++++++++++++++++++--- core/src/genesis.rs | 12 +++++++----- data_model/src/domain.rs | 4 ++-- lints.toml | 2 ++ 7 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d2ca570c94..5ff4f0b784a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1908,6 +1908,7 @@ dependencies = [ "iroha", "iroha_config", "iroha_core", + "iroha_data_model", "serde_json", ] diff --git a/configs/peer/genesis.json b/configs/peer/genesis.json index 3c0f7cbf670..b8abfa1fe7b 100644 --- a/configs/peer/genesis.json +++ b/configs/peer/genesis.json @@ -54,7 +54,7 @@ }, "value_type": "Quantity", "metadata": {}, - "mintable": true + "mintable": "Infinitely" } } } diff --git a/core/docs/Cargo.toml b/core/docs/Cargo.toml index 7706f36cf9c..383797dbaa7 100644 --- a/core/docs/Cargo.toml +++ b/core/docs/Cargo.toml @@ -18,6 +18,7 @@ default = ["roles", "dex", "telemetry", "dev-telemetry"] [dependencies] iroha_core = { version = "=2.0.0-pre-rc.3", path = "../../core" } iroha_config = { version = "=2.0.0-pre-rc.3", path = "../../config" } +iroha_data_model = { version = "=2.0.0-pre-rc.3", path = "../../data_model" } iroha = { path = "../../cli" } color-eyre = "0.5.11" diff --git a/core/docs/src/main.rs b/core/docs/src/main.rs index 1a4a9bba7a2..764dbc3ba49 100644 --- a/core/docs/src/main.rs +++ b/core/docs/src/main.rs @@ -1,4 +1,5 @@ -//! Cli for generating documentation for iroha configuraion +//! CLI for generating iroha sample configuration and genesis, as well +//! as their documentation. #![allow(clippy::restriction)] @@ -10,12 +11,42 @@ use std::{ use color_eyre::eyre::WrapErr; use iroha::config::Configuration; use iroha_config::Configurable; +use iroha_core::{genesis::{RawGenesisBlock, RawGenesisBlockBuilder}, tx::{AssetDefinition, MintBox}}; use serde_json::{Map, Value}; +// TODO: if we merge #2077 first, we should change this to sync up with the default in docs. +static DEFAULT_PUBLIC_KEY: &str = "ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"; + fn main() -> color_eyre::Result<()> { color_eyre::install().unwrap(); - Configuration::get_markdown(&mut BufWriter::new(stdout())) - .wrap_err("Failed to generate documentation") + if std::env::args().any(|a| is_genesis(&a)) { + writeln!(BufWriter::new(stdout()), "{}", serde_json::to_string_pretty(&generate_default_genesis()?)?)?; + Ok(()) + } else { + Configuration::get_markdown(&mut BufWriter::new(stdout())) + .wrap_err("Failed to generate documentation") + } +} + +fn is_genesis(arg: &str) -> bool { + ["--genesis", "-g"].contains(&arg) +} + +fn generate_default_genesis() -> color_eyre::Result { + let asset_definition = AssetDefinition::quantity("rose#wonderland".parse()?).build(); + let mut result = RawGenesisBlockBuilder::new() + .domain("wonderland".parse()?) + .with_account("alice".parse()?, DEFAULT_PUBLIC_KEY.parse()?) + .with_asset(asset_definition.clone()) + .finish_domain().build(); + let mint = MintBox::new( + iroha_data_model::prelude::Value::U32(13_u32), + iroha_data_model::IdBox::AssetId(iroha_data_model::prelude::AssetId::new( + asset_definition.id().clone(), // Probably redundant clone + "alice@wonderland".parse()? + ))); + result.transactions[0].isi.push(mint.into()); + Ok(result) } impl + Send + Sync + Default> PrintDocs for C {} diff --git a/core/src/genesis.rs b/core/src/genesis.rs index e61b863e61a..4548c0dc2da 100644 --- a/core/src/genesis.rs +++ b/core/src/genesis.rs @@ -7,10 +7,11 @@ use std::{collections::HashSet, fmt::Debug, fs::File, io::BufReader, ops::Deref, use eyre::{eyre, Result, WrapErr}; use iroha_actor::Addr; use iroha_crypto::{KeyPair, PublicKey}; -use iroha_data_model::{asset::AssetDefinition, prelude::*}; +use iroha_data_model::{asset::AssetDefinition, prelude::*, domain::NewDomain}; use iroha_schema::prelude::*; use serde::{Deserialize, Serialize}; use small::SmallVec; +use smallvec::smallvec; use tokio::{time, time::Duration}; pub use self::config::GenesisConfiguration; @@ -257,7 +258,7 @@ impl RawGenesisBlock { /// Create a [`RawGenesisBlock`] with specified [`Domain`] and [`Account`]. pub fn new(account_name: Name, domain_id: DomainId, public_key: PublicKey) -> Self { RawGenesisBlock { - transactions: SmallVec(smallvec::smallvec![GenesisTransaction::new( + transactions: SmallVec(smallvec![GenesisTransaction::new( account_name, domain_id, public_key, @@ -295,7 +296,7 @@ impl GenesisTransaction { /// Create a [`GenesisTransaction`] with the specified [`Domain`] and [`Account`]. pub fn new(account_name: Name, domain_id: DomainId, public_key: PublicKey) -> Self { Self { - isi: SmallVec(smallvec::smallvec![ + isi: SmallVec(smallvec![ RegisterBox::new(Domain::new(domain_id.clone())).into(), RegisterBox::new(Account::new( AccountId::new(account_name, domain_id), @@ -394,9 +395,10 @@ impl RawGenesisBlockBuilder { /// be used to create assets and accounts. pub fn domain(mut self, domain_name: Name) -> RawGenesisDomainBuilder { let domain_id = DomainId::new(domain_name); + let new_domain = NewDomain::new(domain_id.clone()); self.transaction .isi - .push(RegisterBox::new(domain_id.clone()).into()); + .push(Instruction::from(RegisterBox::new(new_domain))); RawGenesisDomainBuilder { transaction: self.transaction, domain_id, @@ -405,7 +407,7 @@ impl RawGenesisBlockBuilder { /// Finish building and produce a `RawGenesisBlock`. pub fn build(self) -> RawGenesisBlock { RawGenesisBlock { - transactions: SmallVec(smallvec::smallvec![self.transaction]), + transactions: SmallVec(smallvec![self.transaction]), } } } diff --git a/data_model/src/domain.rs b/data_model/src/domain.rs index a35884cec27..3515cb70d07 100644 --- a/data_model/src/domain.rs +++ b/data_model/src/domain.rs @@ -64,7 +64,7 @@ impl From for Domain { pub struct NewDomain { id: ::Id, logo: Option, - metadata: Metadata, + pub metadata: Metadata, } impl PartialOrd for NewDomain { @@ -83,7 +83,7 @@ impl Ord for NewDomain { impl NewDomain { #[must_use] - fn new(id: ::Id) -> Self { + pub fn new(id: ::Id) -> Self { Self { id, logo: None, diff --git a/lints.toml b/lints.toml index bd0980b2980..6fef49dcfb3 100644 --- a/lints.toml +++ b/lints.toml @@ -20,6 +20,7 @@ deny = [ 'unused_import_braces', 'variant_size_differences', ] + allow = [ 'clippy::string_add', 'clippy::as_conversions', @@ -60,4 +61,5 @@ allow = [ ## https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn 'clippy::missing_const_for_fn' ] + warn = []