Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-iroha#2614: Config finalisation, proxy entrypoint, kagami docgen

Signed-off-by: Ilia Churin <[email protected]>
  • Loading branch information
ilchu committed Sep 6, 2022
1 parent 0017471 commit e47e093
Show file tree
Hide file tree
Showing 29 changed files with 361 additions and 309 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.

13 changes: 7 additions & 6 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use std::{panic, path::PathBuf, sync::Arc};

use color_eyre::eyre::{eyre, Result, WrapErr};
use iroha_actor::{broker::*, prelude::*};
use iroha_config::iroha::Configuration;
use iroha_config::{
base::proxy::{Builder, LoadFromDisk, LoadFromEnv},
iroha::{Configuration, ConfigurationProxy},
};
use iroha_core::{
block_sync::{BlockSynchronizer, BlockSynchronizerTrait},
genesis::{GenesisNetwork, GenesisNetworkTrait, RawGenesisBlock},
Expand Down Expand Up @@ -124,11 +127,9 @@ where
query_judge: QueryJudgeBoxed,
) -> Result<Self> {
let broker = Broker::new();
let mut config = match Configuration::from_path(&args.config_path) {
Ok(config) => config,
Err(_) => Configuration::default(),
};
config.load_environment()?;
let mut proxy = ConfigurationProxy::from_path(&args.config_path)?;
proxy.load_environment()?;
let config = proxy.build()?;

let telemetry = iroha_logger::init(&config.logger)?;
iroha_logger::info!("Hyperledgerいろは2にようこそ!");
Expand Down
92 changes: 39 additions & 53 deletions cli/src/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
use std::{collections::HashSet, str::FromStr};

use iroha_config::{
block_sync::Configuration as BlockSyncConfiguration,
genesis::Configuration as GenesisConfiguration,
iroha::Configuration,
kura::Configuration as KuraConfiguration,
queue::Configuration as QueueConfiguration,
sumeragi::{Configuration as SumeragiConfiguration, TrustedPeers},
torii::{Configuration as ToriiConfiguration, DEFAULT_TORII_P2P_ADDR},
wasm::Configuration as WasmConfiguration,
wsv::Configuration as WsvConfiguration,
iroha::{Configuration, ConfigurationProxy},
sumeragi::TrustedPeers,
torii::{uri::DEFAULT_API_URL, DEFAULT_TORII_P2P_ADDR, DEFAULT_TORII_TELEMETRY_URL},
};
use iroha_crypto::{KeyPair, PublicKey};
use iroha_data_model::peer::Id as PeerId;
Expand Down Expand Up @@ -65,49 +59,41 @@ pub fn get_config(trusted_peers: HashSet<PeerId>, key_pair: Option<KeyPair>) ->
.into(),
};
iroha_logger::info!(?public_key);
Configuration {
public_key: public_key.clone(),
private_key: private_key.clone(),
kura: KuraConfiguration {
init_mode: iroha_config::kura::Mode::Strict,
block_store_path: "./blocks".into(),
..KuraConfiguration::default()
},
sumeragi: SumeragiConfiguration {
key_pair: KeyPair::new(public_key.clone(), private_key.clone())
.expect("Key pair mismatch"),
peer_id: PeerId::new(DEFAULT_TORII_P2P_ADDR, &public_key),
trusted_peers: TrustedPeers {
peers: trusted_peers,
},
gossip_period_ms: 500,
..SumeragiConfiguration::default()
},
torii: ToriiConfiguration {
max_transaction_size: 0x8000,
..ToriiConfiguration::default()
},
block_sync: BlockSyncConfiguration {
block_batch_size: 1,
gossip_period_ms: 5000,
..BlockSyncConfiguration::default()
},
queue: QueueConfiguration {
maximum_transactions_in_block: 2,
..QueueConfiguration::default()
},
genesis: GenesisConfiguration {
account_public_key: public_key,
account_private_key: Some(private_key),
..GenesisConfiguration::default()
},
wsv: WsvConfiguration {
wasm_runtime_config: WasmConfiguration {
fuel_limit: 10_000_000,
..WasmConfiguration::default()
},
..WsvConfiguration::default()
},
..Configuration::default()
let mut proxy = ConfigurationProxy::default();
proxy.public_key = Some(public_key.clone());
proxy.private_key = Some(private_key.clone());
if let Some(sumeragi_proxy) = &mut proxy.sumeragi {
sumeragi_proxy.key_pair =
Some(KeyPair::new(public_key.clone(), private_key.clone()).expect("Key pair mismatch"));
sumeragi_proxy.peer_id = Some(PeerId::new(DEFAULT_TORII_P2P_ADDR, &public_key));
sumeragi_proxy.trusted_peers = Some(TrustedPeers {
peers: trusted_peers,
});
sumeragi_proxy.gossip_period_ms = Some(500);
}
if let Some(torii_proxy) = &mut proxy.torii {
torii_proxy.api_url = Some(DEFAULT_API_URL.to_owned());
torii_proxy.p2p_addr = Some(DEFAULT_TORII_P2P_ADDR.to_owned());
torii_proxy.telemetry_url = Some(DEFAULT_TORII_TELEMETRY_URL.to_owned());
torii_proxy.max_transaction_size = Some(0x8000);
}
if let Some(block_sync_proxy) = &mut proxy.block_sync {
block_sync_proxy.block_batch_size = Some(1);
block_sync_proxy.gossip_period_ms = Some(500);
}
if let Some(queue_proxy) = &mut proxy.queue {
queue_proxy.maximum_transactions_in_block = Some(2);
}
if let Some(genesis_proxy) = &mut proxy.genesis {
genesis_proxy.account_public_key = Some(public_key);
genesis_proxy.account_private_key = Some(Some(private_key));
}
if let Some(wsv_proxy) = &mut proxy.wsv {
if let Some(wasm_proxy) = &mut wsv_proxy.wasm_runtime_config {
wasm_proxy.fuel_limit = Some(10_000_000);
}
}
proxy
.build()
.expect("Should build as all required fields were provided")
}
23 changes: 12 additions & 11 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Crate contains iroha client which talks to iroha network via http
use iroha_config::client::Configuration;

/// Module with iroha client itself
pub mod client;
/// Module with general communication primitives like an HTTP request builder.
Expand All @@ -10,21 +8,24 @@ mod http_default;

/// Module containing sample configurations for tests and benchmarks.
pub mod samples {
use iroha_config::torii::uri;
use iroha_config::{base::proxy::Builder, torii::uri};
use iroha_crypto::KeyPair;

use super::Configuration;
use iroha_config::client::{Configuration, ConfigurationProxy};

/// Get sample client configuration.
#[allow(clippy::expect_used)]
pub fn get_client_config(key_pair: &KeyPair) -> Configuration {
let (public_key, private_key) = key_pair.clone().into();
Configuration {
public_key,
private_key,
account_id: "alice@wonderland".parse().expect("Should not fail."),
torii_api_url: iroha_primitives::small::SmallStr::from_str(uri::DEFAULT_API_URL),
..Configuration::default()
}
let mut proxy = ConfigurationProxy::default();
proxy.public_key = Some(public_key);
proxy.private_key = Some(private_key);
proxy.torii_api_url = Some(iroha_primitives::small::SmallStr::from_str(
uri::DEFAULT_API_URL,
));
proxy.account_id = Some("alice@wonderland".parse().expect("Should not fail."));
proxy
.build()
.expect("Should build as all required fields were provided")
}
}
53 changes: 44 additions & 9 deletions config/base/derive/src/documented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro2::Span;
use quote::quote;
use syn::{Lit, LitStr, Meta};

use super::utils::StructWithFields;
use super::utils::{get_inner_type, StructWithFields};

pub fn impl_documented(ast: &StructWithFields) -> TokenStream {
let name = &ast.ident;
Expand Down Expand Up @@ -47,17 +47,30 @@ fn impl_get_doc_recursive(docs: Vec<LitStr>, ast: &StructWithFields) -> proc_mac
.zip(docs)
.map(|(field, documentation)| {
let inner_thing = field.has_inner;
let option_thing = field.has_option;
let ty = &field.ty;
let ident = &field.ident;
if inner_thing {
let documented_trait = quote! { iroha_config_base::proxy::Documented };
if inner_thing && option_thing {
let inner_ty = get_inner_type("Option", &field.ty);
quote! {
[stringify!(#ident)] => {
let curr_doc = #documentation;
let inner_docs = <#ty as ::iroha_config_base::proxy::Documented>::get_inner_docs();
let inner_docs = <#inner_ty as #documented_trait>::get_inner_docs();
let total_docs = format!("{}\n\nHas following fields:\n\n{}\n", curr_doc, inner_docs);
Some(total_docs)
},
[stringify!(#ident), rest @ ..] => <#ty as ::iroha_config_base::proxy::Documented>::get_doc_recursive(rest)?,
[stringify!(#ident), rest @ ..] => <#inner_ty as #documented_trait>::get_doc_recursive(rest)?,
}
} else if inner_thing {
quote! {
[stringify!(#ident)] => {
let curr_doc = #documentation;
let inner_docs = <#ty as #documented_trait>::get_inner_docs();
let total_docs = format!("{}\n\nHas following fields:\n\n{}\n", curr_doc, inner_docs);
Some(total_docs)
},
[stringify!(#ident), rest @ ..] => <#ty as #documented_trait>::get_doc_recursive(rest)?,
}
} else {
quote! { [stringify!(#ident)] => Some(#documentation.to_owned()), }
Expand All @@ -84,10 +97,17 @@ fn impl_get_doc_recursive(docs: Vec<LitStr>, ast: &StructWithFields) -> proc_mac
fn impl_get_inner_docs(docs: Vec<LitStr>, ast: &StructWithFields) -> proc_macro2::TokenStream {
let inserts = ast.fields.iter().zip(docs).map(|(field, documentation)| {
let inner_thing = field.has_inner;
let option_thing = field.has_option;
let ty = &field.ty;
let ident = &field.ident;
let doc = if inner_thing {
quote! { <#ty as ::iroha_config_base::proxy::Documented>::get_inner_docs().as_str() }
let documented_trait = quote! { iroha_config_base::proxy::Documented };
let doc = if inner_thing && option_thing {
let inner_ty = get_inner_type("Option", &field.ty);
quote! {
<#inner_ty as #documented_trait>::get_inner_docs().as_str()
}
} else if inner_thing {
quote! { <#ty as #documented_trait>::get_inner_docs().as_str() }
} else {
quote! { #documentation.into() }
};
Expand All @@ -114,8 +134,13 @@ fn impl_get_docs(docs: Vec<LitStr>, ast: &StructWithFields) -> proc_macro2::Toke
let ident = &field.ident;
let ty = &field.ty;
let inner_thing = field.has_inner;
let doc = if inner_thing {
quote! { <#ty as ::iroha_config_base::proxy::Documented>::get_docs().into() }
let option_thing = field.has_option;
let documented_trait = quote! { iroha_config_base::proxy::Documented };
let doc = if inner_thing && option_thing {
let inner_ty = get_inner_type("Option", &field.ty);
quote! { <#inner_ty as #documented_trait>::get_docs().into() }
} else if inner_thing {
quote! { <#ty as #documented_trait>::get_docs().into() }
} else {
quote! { #documentation.into() }
};
Expand Down Expand Up @@ -153,9 +178,19 @@ fn impl_get_recursive(ast: &StructWithFields) -> proc_macro2::TokenStream {
.iter()
.map(|field | {
let inner_thing = field.has_inner;
let option_thing = field.has_option;
// if ast.ident == "ConfigurationProxy" {println!("FIELD: {}, INNER: {}, OPTION: {}", field.ident, field.has_inner, field.has_option);}
let ident = &field.ident;
let l_value = &field.lvalue_read;
let inner_thing2 = if inner_thing {
let inner_thing2 = if inner_thing && option_thing {
let inner_ty = get_inner_type("Option", &field.ty);
let documented_trait = quote! { iroha_config_base::proxy::Documented };
quote! {
[stringify!(#ident), rest @ ..] => {
<#inner_ty as #documented_trait>::get_recursive(#l_value.as_ref().expect("Should be instantiated"), rest)?
},
}
} else if inner_thing {
quote! {
[stringify!(#ident), rest @ ..] => {
#l_value.get_recursive(rest)?
Expand Down
18 changes: 12 additions & 6 deletions config/base/derive/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ pub fn impl_proxy(ast: StructWithFields) -> TokenStream {
let disk_derive = quote! { ::iroha_config_base::derive::LoadFromDisk };
let builder_derive = quote! { ::iroha_config_base::derive::Builder };
let combine_derive = quote! { ::iroha_config_base::derive::Combine };
let documented_derive = quote! { ::iroha_config_base::derive::Documented };
quote! {
/// Proxy configuration structure to be used as an intermediate
/// for config loading
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize,
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize,
#builder_derive,
#loadenv_derive,
#disk_derive,
#combine_derive)]
#combine_derive,
#documented_derive
)]
#[builder(parent = #parent_ty)]
#proxy_struct

}
.into()
}
Expand Down Expand Up @@ -155,13 +159,15 @@ fn gen_proxy_struct(mut ast: StructWithFields) -> StructWithFields {
Option<#ty>
};
field.has_option = true;
// Also remove `#[serde(default = ..)]` if present
// as it breaks proxy deserialization
utils::remove_attr(&mut field.attrs, "serde");
});
ast.ident = format_ident!("{}Proxy", ast.ident);
// Removing as `..Proxy` has its own doc
// Removing struct-level docs as `..Proxy` has its own doc,
// but not the field documentation as they stay the same
utils::remove_attr(&mut ast.attrs, "doc");
// Remove #[serde(default) as we have a default impl.
// Also remove `#[serde(default = ..)]` if present
// as it breaks proxy deserialization
utils::remove_attr_struct(&mut ast, "serde");
ast
}

Expand Down
30 changes: 3 additions & 27 deletions config/base/derive/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ pub fn impl_view(ast: StructWithFields) -> TokenStream {
let original = original_struct(ast.clone());
let view = view_struct(ast);
let impl_from = impl_from(&original, &view);
let impl_default = impl_default(&original, &view);
let impl_has_view = impl_has_view(&original);
let assertions = assertions(&view);
let out = quote! {
#original
#impl_has_view
#view
#impl_from
#impl_default
#assertions
};
out.into()
Expand Down Expand Up @@ -64,7 +62,8 @@ mod gen {
if let NestedMeta::Meta(Meta::Path(path)) = nested {
// TODO: check here
if path.is_ident("Default")
|| path.is_ident("Documented")
|| path.is_ident("LoadFromEnv")
|| path.is_ident("Builder")
|| path.is_ident("Proxy")
{
return false;
Expand All @@ -88,6 +87,7 @@ mod gen {
remove_attr_struct(&mut ast, "view");
remove_attr_struct(&mut ast, "config");
remove_attr_struct(&mut ast, "builder");
remove_attr_struct(&mut ast, "proxy");
ast.ident = format_ident!("{}View", ast.ident);
ast
}
Expand Down Expand Up @@ -128,30 +128,6 @@ mod gen {
}
}

pub fn impl_default(
original: &StructWithFields,
view: &StructWithFields,
) -> proc_macro2::TokenStream {
let StructWithFields {
ident: original_ident,
..
} = original;
let StructWithFields {
generics,
ident: view_ident,
..
} = view;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

quote! {
impl #impl_generics core::default::Default for #view_ident #ty_generics #where_clause {
fn default() -> Self {
core::convert::From::<_>::from(<#original_ident as core::default::Default>::default())
}
}
}
}

pub fn impl_has_view(original: &StructWithFields) -> proc_macro2::TokenStream {
let StructWithFields {
generics,
Expand Down
Loading

0 comments on commit e47e093

Please sign in to comment.