Skip to content

Commit

Permalink
[fix] hyperledger#1640: Generate config.json and genesis.json
Browse files Browse the repository at this point in the history
  • Loading branch information
appetrosyan committed Apr 14, 2022
1 parent 6781520 commit 039e37e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion configs/peer/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"value_type": "Quantity",
"metadata": {},
"mintable": true
"mintable": "Infinitely"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/docs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 34 additions & 3 deletions core/docs/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)]

Expand All @@ -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<RawGenesisBlock> {
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<E: Debug, C: Configurable<Error = E> + Send + Sync + Default> PrintDocs for C {}
Expand Down
12 changes: 7 additions & 5 deletions core/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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]),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions data_model/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl From<GenesisDomain> for Domain {
pub struct NewDomain {
id: <Domain as Identifiable>::Id,
logo: Option<IpfsPath>,
metadata: Metadata,
pub metadata: Metadata,
}

impl PartialOrd for NewDomain {
Expand All @@ -83,7 +83,7 @@ impl Ord for NewDomain {

impl NewDomain {
#[must_use]
fn new(id: <Domain as Identifiable>::Id) -> Self {
pub fn new(id: <Domain as Identifiable>::Id) -> Self {
Self {
id,
logo: None,
Expand Down
2 changes: 2 additions & 0 deletions lints.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ deny = [
'unused_import_braces',
'variant_size_differences',
]

allow = [
'clippy::string_add',
'clippy::as_conversions',
Expand Down Expand Up @@ -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 = []

0 comments on commit 039e37e

Please sign in to comment.