Skip to content

Commit

Permalink
Removal of arguments from test_network crate
Browse files Browse the repository at this point in the history
Signed-off-by: i1i1 <[email protected]>
  • Loading branch information
i1i1 committed Sep 8, 2021
1 parent 298466b commit 115b716
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 65 deletions.
10 changes: 5 additions & 5 deletions iroha/src/init.rs
Original file line number Diff line number Diff line change
@@ -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<String, Domain> {
pub fn domains(configuration: &Configuration) -> Result<BTreeMap<String, Domain>> {
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())
}
44 changes: 33 additions & 11 deletions iroha/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K::World>,
query_validator: IsQueryAllowedBoxed<K::World>,
broker: Broker,
) -> Result<Self> {
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<G>,
config: Configuration,
instruction_validator: IsInstructionAllowedBoxed<K::World>,
query_validator: IsQueryAllowedBoxed<K::World>,
broker: Broker,
) -> Result<Self> {
// TODO: use channel for prometheus/telemetry endpoint
#[allow(unused)]
Expand All @@ -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(),
),
));
Expand All @@ -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)
Expand All @@ -201,7 +223,7 @@ where
Arc::clone(&wsv),
instruction_validator,
Arc::clone(&query_validator),
genesis_network,
genesis,
queue.clone(),
broker.clone(),
network_addr.clone(),
Expand Down
5 changes: 4 additions & 1 deletion iroha/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
53 changes: 31 additions & 22 deletions iroha/test_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Self>;
}

impl<G: GenesisNetworkTrait> TestGenesis for G {
fn test(submit_genesis: bool) -> Option<Self> {
let cfg = Configuration::test();
G::from_configuration(
submit_genesis,
GENESIS_PATH,
&cfg.genesis_configuration,
cfg.sumeragi_configuration.max_instruction_number,
)
.expect("Failed to")
}
}

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -381,8 +390,8 @@ where
let (sender, reciever) = std::sync::mpsc::sync_channel(1);
let handle = task::spawn(
async move {
let mut iroha = <Iroha<W, G, Q, S, K, B>>::with_broker_and_config(
&test_arguments(true),
let mut iroha = <Iroha<W, G, Q, S, K, B>>::with_genesis(
G::test(true),
configuration,
permissions.into(),
AllowAll.into(),
Expand All @@ -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<G>,
instruction_validator: impl Into<IsInstructionAllowedBoxed<W>> + Send + 'static,
query_validator: impl Into<IsQueryAllowedBoxed<W>> + Send + 'static,
) {
Expand All @@ -426,8 +435,8 @@ where
let join_handle = tokio::spawn(
async move {
let _temp_dir = temp_dir;
let mut iroha = <Iroha<W, G, Q, S, K, B>>::with_broker_and_config(
&args,
let mut iroha = <Iroha<W, G, Q, S, K, B>>::with_genesis(
genesis,
configuration,
instruction_validator.into(),
query_validator.into(),
Expand All @@ -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<G>, 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<G>) {
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
Expand Down Expand Up @@ -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,
)
Expand Down
38 changes: 21 additions & 17 deletions iroha_client/benches/torii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
21 changes: 12 additions & 9 deletions iroha_dsl/tests/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 115b716

Please sign in to comment.