From 115b7165f5284ae7d7ad85952c5bed49b2bf7a15 Mon Sep 17 00:00:00 2001 From: i1i1 Date: Wed, 8 Sep 2021 10:48:00 +0300 Subject: [PATCH] Removal of arguments from test_network crate Signed-off-by: i1i1 --- iroha/src/init.rs | 10 +++---- iroha/src/lib.rs | 44 +++++++++++++++++++++-------- iroha/src/tx.rs | 5 +++- iroha/test_network/src/lib.rs | 53 ++++++++++++++++++++--------------- iroha_client/benches/torii.rs | 38 ++++++++++++++----------- iroha_dsl/tests/dsl.rs | 21 ++++++++------ 6 files changed, 106 insertions(+), 65 deletions(-) diff --git a/iroha/src/init.rs b/iroha/src/init.rs index bcb5c9226aa..e7f79f57d57 100644 --- a/iroha/src/init.rs +++ b/iroha/src/init.rs @@ -1,20 +1,20 @@ use std::collections::BTreeMap; use iroha_data_model::prelude::*; +use iroha_error::{error, Result}; use crate::config::Configuration; /// Returns the a map of a form `domain_name -> domain`, for initial domains. -#[allow(clippy::expect_used)] -pub fn domains(configuration: &Configuration) -> BTreeMap { +pub fn domains(configuration: &Configuration) -> Result> { let key = configuration .genesis_configuration .genesis_account_public_key .clone() - .expect("Genesis account public key is not specified."); - std::iter::once(( + .ok_or_else(|| error!("Genesis account public key is not specified."))?; + Ok(std::iter::once(( GENESIS_DOMAIN_NAME.to_owned(), Domain::from(GenesisDomain::new(key)), )) - .collect() + .collect()) } diff --git a/iroha/src/lib.rs b/iroha/src/lib.rs index 32d7cbf0597..b8774ef9c3d 100644 --- a/iroha/src/lib.rs +++ b/iroha/src/lib.rs @@ -142,13 +142,43 @@ where /// - Reading genesis from disk /// - Reading telemetry configs and setuping telemetry /// - Initialization of sumeragi - #[allow(clippy::non_ascii_literal)] pub async fn with_broker_and_config( args: &Arguments, config: Configuration, instruction_validator: IsInstructionAllowedBoxed, query_validator: IsQueryAllowedBoxed, broker: Broker, + ) -> Result { + let genesis = G::from_configuration( + args.submit_genesis, + &args.genesis_path, + &config.genesis_configuration, + config.torii_configuration.torii_max_instruction_number, + ) + .wrap_err("Failed to initialize genesis.")?; + + Self::with_genesis( + genesis, + config, + instruction_validator, + query_validator, + broker, + ) + .await + } + + /// Creates Iroha with specified broker, config, and genesis + /// # Errors + /// Can fail if fails: + /// - Reading telemetry configs and setuping telemetry + /// - Initialization of sumeragi + #[allow(clippy::non_ascii_literal)] + pub async fn with_genesis( + genesis: Option, + config: Configuration, + instruction_validator: IsInstructionAllowedBoxed, + query_validator: IsQueryAllowedBoxed, + broker: Broker, ) -> Result { // TODO: use channel for prometheus/telemetry endpoint #[allow(unused)] @@ -167,7 +197,7 @@ where let wsv = Arc::new(WorldStateView::from_config( config.wsv_configuration, W::with( - init::domains(&config), + init::domains(&config).wrap_err("Failed to get initial domains")?, config.sumeragi_configuration.trusted_peers.peers.clone(), ), )); @@ -180,14 +210,6 @@ where .await .expect_running(); - let genesis_network = G::from_configuration( - args.submit_genesis, - &args.genesis_path, - &config.genesis_configuration, - config.torii_configuration.torii_max_instruction_number, - ) - .wrap_err("Failed to initialize genesis.")?; - #[cfg(feature = "telemetry")] if let Some(telemetry) = telemetry { telemetry::start(&config.telemetry, telemetry) @@ -201,7 +223,7 @@ where Arc::clone(&wsv), instruction_validator, Arc::clone(&query_validator), - genesis_network, + genesis, queue.clone(), broker.clone(), network_addr.clone(), diff --git a/iroha/src/tx.rs b/iroha/src/tx.rs index 9a7202227c0..3097641ff8e 100644 --- a/iroha/src/tx.rs +++ b/iroha/src/tx.rs @@ -553,7 +553,10 @@ mod tests { let accepted_tx_hash = accepted_tx.hash(); let valid_tx_hash = accepted_tx .validate( - &WorldStateView::new(World::with(init::domains(&config), BTreeSet::new())), + &WorldStateView::new(World::with( + init::domains(&config).unwrap(), + BTreeSet::new(), + )), &AllowAll.into(), &AllowAll.into(), true, diff --git a/iroha/test_network/src/lib.rs b/iroha/test_network/src/lib.rs index 7fc7b29b807..de2a95f847b 100644 --- a/iroha/test_network/src/lib.rs +++ b/iroha/test_network/src/lib.rs @@ -25,7 +25,7 @@ use iroha::{ sumeragi::{config::SumeragiConfiguration, Sumeragi, SumeragiTrait}, torii::config::ToriiConfiguration, wsv::{World, WorldTrait}, - Arguments, Iroha, + Iroha, }; use iroha_actor::{broker::*, prelude::*}; use iroha_client::{client::Client, config::Configuration as ClientConfiguration}; @@ -114,12 +114,20 @@ pub const CONFIGURATION_PATH: &str = "tests/test_config.json"; pub const CLIENT_CONFIGURATION_PATH: &str = "tests/test_client_config.json"; pub const GENESIS_PATH: &str = "tests/genesis.json"; -pub fn test_arguments(submit_genesis: bool) -> Arguments { - Arguments { - submit_genesis, - genesis_path: GENESIS_PATH.into(), - config_path: CONFIGURATION_PATH.into(), - ..Arguments::default() +pub trait TestGenesis: Sized { + fn test(submit_genesis: bool) -> Option; +} + +impl TestGenesis for G { + fn test(submit_genesis: bool) -> Option { + let cfg = Configuration::test(); + G::from_configuration( + submit_genesis, + GENESIS_PATH, + &cfg.genesis_configuration, + cfg.sumeragi_configuration.max_instruction_number, + ) + .expect("Failed to") } } @@ -215,7 +223,8 @@ where config.sumeragi_configuration.trusted_peers.peers = self.peers().map(|peer| &peer.id).cloned().collect(); config.sumeragi_configuration.max_faulty_peers = (self.peers.len() / 3) as u32; - peer.start_with_config(test_arguments(false), config).await; + peer.start_with_config(GenesisNetwork::test(false), config) + .await; time::sleep(Configuration::pipeline_time() * 2).await; let add_peer = RegisterBox::new(IdentifiableBox::Peer( DataModelPeer::new(peer.id.clone()).into(), @@ -252,10 +261,10 @@ where let mut futures = Vec::new(); - futures.push(genesis.start_with_config(test_arguments(true), configuration.clone())); + futures.push(genesis.start_with_config(G::test(true), configuration.clone())); for peer in peers.iter_mut().choose_multiple(rng, online_peers as usize) { - futures.push(peer.start_with_config(test_arguments(false), configuration.clone())); + futures.push(peer.start_with_config(G::test(false), configuration.clone())); } futures::future::join_all(futures).await; @@ -381,8 +390,8 @@ where let (sender, reciever) = std::sync::mpsc::sync_channel(1); let handle = task::spawn( async move { - let mut iroha = >::with_broker_and_config( - &test_arguments(true), + let mut iroha = >::with_genesis( + G::test(true), configuration, permissions.into(), AllowAll.into(), @@ -404,8 +413,8 @@ where /// Starts peer with config and permissions pub async fn start_with_config_permissions( &mut self, - args: Arguments, configuration: Configuration, + genesis: Option, instruction_validator: impl Into> + Send + 'static, query_validator: impl Into> + Send + 'static, ) { @@ -426,8 +435,8 @@ where let join_handle = tokio::spawn( async move { let _temp_dir = temp_dir; - let mut iroha = >::with_broker_and_config( - &args, + let mut iroha = >::with_genesis( + genesis, configuration, instruction_validator.into(), query_validator.into(), @@ -448,19 +457,19 @@ where } /// Starts peer with config - pub async fn start_with_config(&mut self, args: Arguments, configuration: Configuration) { - self.start_with_config_permissions(args, configuration, AllowAll, AllowAll) + pub async fn start_with_config(&mut self, genesis: Option, configuration: Configuration) { + self.start_with_config_permissions(configuration, genesis, AllowAll, AllowAll) .await; } /// Starts peer with config - pub async fn start_with_args(&mut self, args: Arguments) { - self.start_with_config(args, Configuration::test()).await; + pub async fn start_with_genesis(&mut self, genesis: Option) { + self.start_with_config(genesis, Configuration::test()).await; } /// Starts peer - pub async fn start(&mut self) { - self.start_with_args(Arguments::default()).await; + pub async fn start(&mut self, submit_genesis: bool) { + self.start_with_genesis(G::test(submit_genesis)).await; } /// Creates peer @@ -515,8 +524,8 @@ where configuration.sumeragi_configuration.trusted_peers.peers = std::iter::once(peer.id.clone()).collect(); peer.start_with_config_permissions( - test_arguments(true), configuration.clone(), + G::test(true), instruction_validator, query_validator, ) diff --git a/iroha_client/benches/torii.rs b/iroha_client/benches/torii.rs index 7ec4b0548ba..87235c1ab17 100644 --- a/iroha_client/benches/torii.rs +++ b/iroha_client/benches/torii.rs @@ -3,7 +3,11 @@ use std::thread; use criterion::{criterion_group, criterion_main, Criterion, Throughput}; -use iroha::{config::Configuration, prelude::*, Arguments}; +use iroha::{ + config::Configuration, + genesis::{GenesisNetwork, GenesisNetworkTrait}, + prelude::*, +}; use iroha_client::{ client::{asset, Client}, config::Configuration as ClientConfiguration, @@ -24,15 +28,15 @@ fn query_requests(criterion: &mut Criterion) { let rt = Runtime::test(); configuration.sumeragi_configuration.trusted_peers.peers = std::iter::once(peer.id.clone()).collect(); + let genesis = GenesisNetwork::from_configuration( + true, + GENESIS_PATH, + &configuration.genesis_configuration, + configuration.sumeragi_configuration.max_instruction_number, + ) + .unwrap(); - rt.block_on(peer.start_with_config( - Arguments { - submit_genesis: true, - genesis_path: GENESIS_PATH.into(), - ..Arguments::default() - }, - configuration, - )); + rt.block_on(peer.start_with_config(genesis, configuration)); thread::sleep(std::time::Duration::from_millis(50)); let mut group = criterion.benchmark_group("query-reqeuests"); @@ -108,14 +112,14 @@ fn instruction_submits(criterion: &mut Criterion) { configuration.sumeragi_configuration.trusted_peers.peers = std::iter::once(peer.id.clone()).collect(); - rt.block_on(peer.start_with_config( - Arguments { - submit_genesis: true, - genesis_path: GENESIS_PATH.into(), - ..Arguments::default() - }, - configuration, - )); + let genesis = GenesisNetwork::from_configuration( + true, + GENESIS_PATH, + &configuration.genesis_configuration, + configuration.sumeragi_configuration.max_instruction_number, + ) + .unwrap(); + rt.block_on(peer.start_with_config(genesis, configuration)); thread::sleep(std::time::Duration::from_millis(50)); let mut group = criterion.benchmark_group("instruction-requests"); diff --git a/iroha_dsl/tests/dsl.rs b/iroha_dsl/tests/dsl.rs index 7aed3190408..59b8188bf04 100644 --- a/iroha_dsl/tests/dsl.rs +++ b/iroha_dsl/tests/dsl.rs @@ -2,7 +2,10 @@ use std::{thread, time::Duration}; -use iroha::{config::Configuration, Arguments}; +use iroha::{ + config::Configuration, + genesis::{GenesisNetwork, GenesisNetworkTrait}, +}; use iroha_client::{client::Client, config::Configuration as ClientConfiguration}; use iroha_dsl::prelude::*; use test_network::{Peer as TestPeer, TestRuntime}; @@ -112,15 +115,15 @@ fn find_rate_and_make_exchange_isi_should_succeed() { Duration::from_millis(configuration.sumeragi_configuration.pipeline_time_ms()); // Given + let genesis = GenesisNetwork::from_configuration( + true, + GENESIS_PATH, + &configuration.genesis_configuration, + configuration.sumeragi_configuration.max_instruction_number, + ) + .unwrap(); let rt = Runtime::test(); - rt.block_on(peer.start_with_config( - Arguments { - submit_genesis: true, - genesis_path: GENESIS_PATH.into(), - ..Arguments::default() - }, - configuration, - )); + rt.block_on(peer.start_with_config(genesis, configuration)); thread::sleep(pipeline_time); let mut configuration = ClientConfiguration::from_path(CLIENT_CONFIGURATION_PATH)