Skip to content

Commit

Permalink
[feature] #1643: Wait for peers to commit genesis in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Egor Ivkov <e.o.ivkov@gmail.com>
  • Loading branch information
e-ivkov committed Dec 3, 2021
1 parent 6cd608c commit 03603a3
Show file tree
Hide file tree
Showing 24 changed files with 139 additions and 73 deletions.
1 change: 1 addition & 0 deletions client/tests/integration_tests/add_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use test_network::{Peer as TestPeer, *};
fn client_add_account_with_name_length_more_than_limit_should_not_commit_transaction() -> Result<()>
{
let (_rt, _peer, mut test_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![test_client.clone()], 0);

let pipeline_time = Configuration::pipeline_time();

Expand Down
3 changes: 1 addition & 2 deletions client/tests/integration_tests/add_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ use test_network::{Peer as TestPeer, *};
#[test]
fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() -> Result<()> {
let (_rt, _peer, mut test_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![test_client.clone()], 0);
let pipeline_time = Configuration::pipeline_time();

// Given
thread::sleep(pipeline_time);

let account_id = AccountId::new("alice", "wonderland");
let asset_definition_id = AssetDefinitionId::new("xor", "wonderland");
let create_asset = RegisterBox::new(IdentifiableBox::from(AssetDefinition::new_quantity(
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration_tests/add_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use test_network::{Peer as TestPeer, *};
fn client_add_domain_with_name_length_more_than_limit_should_not_commit_transaction() -> Result<()>
{
let (_rt, _peer, mut test_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![test_client.clone()], 0);
let pipeline_time = Configuration::pipeline_time();

// Given
thread::sleep(pipeline_time);

let normal_domain_name = "sora";
let create_domain = RegisterBox::new(IdentifiableBox::from(Domain::new(normal_domain_name)));
Expand Down
3 changes: 1 addition & 2 deletions client/tests/integration_tests/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_a
) -> Result<()> {
// Given
let (_rt, network, mut iroha_client) = <Network>::start_test_with_runtime(4, 1);
wait_for_genesis_committed(network.clients(), 0);
let pipeline_time = Configuration::pipeline_time();

thread::sleep(pipeline_time * 5);

let create_domain = RegisterBox::new(IdentifiableBox::Domain(Domain::new("domain").into()));
let account_id = AccountId::new("account", "domain");
let create_account = RegisterBox::new(IdentifiableBox::NewAccount(
Expand Down
8 changes: 5 additions & 3 deletions client/tests/integration_tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use test_network::{Peer as TestPeer, *};
#[test]
fn get_config() {
// The underscored variables must not be dropped until end of closure.
let (_dont_drop, _dont_drop_either, cl) = <TestPeer>::start_test_with_runtime();
let (_dont_drop, _dont_drop_either, test_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![test_client.clone()], 0);

let field = cl.get_config_docs(&["torii"]).unwrap().unwrap();
let field = test_client.get_config_docs(&["torii"]).unwrap().unwrap();
assert!(field.contains("IROHA_TORII"));

let cfg: Configuration = serde_json::from_value(cl.get_config_value().unwrap()).unwrap();
let cfg: Configuration =
serde_json::from_value(test_client.get_config_value().unwrap()).unwrap();
let test = Configuration::test();
assert_eq!(cfg.block_sync, test.block_sync);
assert_eq!(cfg.network, test.network);
Expand Down
26 changes: 11 additions & 15 deletions client/tests/integration_tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,23 @@ const PEER_COUNT: usize = 7;

#[test]
fn transaction_event_should_be_sent_to_all_peers_from_all_peers() -> Result<()> {
let asset_definition_id = AssetDefinitionId::new("xor", "wonderland");
let create_asset = RegisterBox::new(IdentifiableBox::AssetDefinition(
AssetDefinition::new_quantity(asset_definition_id).into(),
));
test_with_instruction_and_status(create_asset, true)?;
test_with_instruction_and_status(None, true)?;
let fail = FailBox::new("Failing transaction to test Rejected event.");
test_with_instruction_and_status(fail, false)
test_with_instruction_and_status(Some(fail.into()), false)
}

#[allow(clippy::needless_range_loop)]
#[allow(clippy::needless_range_loop, clippy::needless_pass_by_value)]
fn test_with_instruction_and_status(
instruction: impl Into<Instruction>,
instruction: Option<Instruction>,
should_be_committed: bool,
) -> Result<()> {
let instruction: Instruction = instruction.into();
let (_rt, network, _) = <Network>::start_test_with_runtime(PEER_COUNT.try_into().unwrap(), 1);
wait_for_genesis_committed(network.clients(), 0);

for submitting_peer in 0..PEER_COUNT {
let (_rt, network, _) =
<Network>::start_test_with_runtime(PEER_COUNT.try_into().unwrap(), 1);
let pipeline_time = Configuration::pipeline_time();

// Given
thread::sleep(pipeline_time * 4);

let committed_event_received = Arc::new(RwLock::new([false; PEER_COUNT]));
let validating_event_received = Arc::new(RwLock::new([false; PEER_COUNT]));
let rejected_event_received = Arc::new(RwLock::new([false; PEER_COUNT]));
Expand All @@ -46,8 +40,10 @@ fn test_with_instruction_and_status(
&peers[submitting_peer].api_address,
&peers[submitting_peer].status_address,
);
let transaction = submitter_client
.build_transaction(vec![instruction.clone()], UnlimitedMetadata::new())?;
let transaction = submitter_client.build_transaction(
instruction.clone().into_iter().collect(),
UnlimitedMetadata::new(),
)?;
for receiving_peer in 0..PEER_COUNT {
let committed_event_received_clone = committed_event_received.clone();
let validating_event_received_clone = validating_event_received.clone();
Expand Down
3 changes: 1 addition & 2 deletions client/tests/integration_tests/multiple_blocks_created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ const N_BLOCKS: usize = 510;
fn long_multiple_blocks_created() {
// Given
let (_rt, network, mut iroha_client) = <Network>::start_test_with_runtime(4, 1);
wait_for_genesis_committed(network.clients(), 0);
let pipeline_time = Configuration::pipeline_time();

thread::sleep(pipeline_time * 2);

let create_domain = RegisterBox::new(IdentifiableBox::Domain(Domain::new("domain").into()));
let account_id = AccountId::new("account", "domain");
let create_account = RegisterBox::new(IdentifiableBox::NewAccount(
Expand Down
2 changes: 1 addition & 1 deletion client/tests/integration_tests/multisignature_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use test_network::{Peer as TestPeer, *};
#[test]
fn transaction_signed_by_new_signatory_of_account_should_pass() -> Result<()> {
let (_rt, peer, mut iroha_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![iroha_client.clone()], 0);
let pipeline_time = Configuration::pipeline_time();
thread::sleep(pipeline_time);

// Given
let account_id = AccountId::new("alice", "wonderland");
Expand Down
3 changes: 1 addition & 2 deletions client/tests/integration_tests/multisignature_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use test_network::*;
#[test]
fn multisignature_transactions_should_wait_for_all_signatures() {
let (_rt, network, _) = <Network>::start_test_with_runtime(4, 1);
wait_for_genesis_committed(network.clients(), 0);
let pipeline_time = Configuration::pipeline_time();

thread::sleep(pipeline_time * 3);

let create_domain = RegisterBox::new(IdentifiableBox::Domain(Domain::new("domain").into()));
let account_id = AccountId::new("account", "domain");
let key_pair_1 = KeyPair::generate().expect("Failed to generate KeyPair.");
Expand Down
9 changes: 2 additions & 7 deletions client/tests/integration_tests/offline_peers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![allow(clippy::restriction)]

use std::thread;

use iroha_client::client;
use iroha_core::config::Configuration;
use iroha_data_model::prelude::*;
use test_network::*;
use tokio::runtime::Runtime;
Expand All @@ -12,10 +9,8 @@ use tokio::runtime::Runtime;
fn genesis_block_is_commited_with_some_offline_peers() {
// Given
let rt = Runtime::test();
let (_network, mut iroha_client) = rt.block_on(<Network>::start_test_with_offline(4, 1, 1));
let pipeline_time = Configuration::pipeline_time();

thread::sleep(pipeline_time * 8);
let (network, mut iroha_client) = rt.block_on(<Network>::start_test_with_offline(4, 1, 1));
wait_for_genesis_committed(network.clients(), 1);

//When
let alice_id = AccountId::new("alice", "wonderland");
Expand Down
1 change: 1 addition & 0 deletions client/tests/integration_tests/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use test_network::{Peer as TestPeer, *};
#[test]
fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() {
let (_rt, _peer, mut iroha_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![iroha_client.clone()], 0);
let pipeline_time = Configuration::pipeline_time();

let register = ('a'..'z')
Expand Down
1 change: 1 addition & 0 deletions client/tests/integration_tests/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn permissions_disallow_asset_transfer() {
public_blockchain::default_permissions(),
AllowAll.into(),
));
wait_for_genesis_committed(vec![iroha_client.clone()], 0);
let pipeline_time = Configuration::pipeline_time();

// Given
Expand Down
6 changes: 2 additions & 4 deletions client/tests/integration_tests/restart_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ fn restarted_peer_should_have_the_same_asset_amount() -> Result<()> {
// Given
let rt = Runtime::test();
rt.block_on(peer.start_with_config_permissions_dir(configuration.clone(), AllowAll, &temp_dir));

// Wait for genesis to be committed.
thread::sleep(pipeline_time * 2);
let mut iroha_client = Client::test(&peer.api_address, &peer.status_address);
wait_for_genesis_committed(vec![iroha_client.clone()], 0);

let account_id = AccountId::from_str("alice@wonderland").unwrap();
let asset_definition_id = AssetDefinitionId::from_str("xor#wonderland").unwrap();
let create_asset = RegisterBox::new(IdentifiableBox::AssetDefinition(
AssetDefinition::new_quantity(asset_definition_id.clone()).into(),
));
let mut iroha_client = Client::test(&peer.api_address, &peer.status_address);
iroha_client.submit(create_asset)?;
thread::sleep(pipeline_time * 2);
//When
Expand Down
4 changes: 2 additions & 2 deletions client/tests/integration_tests/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use std::thread;

use iroha_core::config::Configuration;
use iroha_data_model::prelude::*;
use test_network::{Network as TestNetwork, TestConfiguration};
use test_network::{Network as TestNetwork, *};

#[test]
fn test_status() {
const N_PEERS: u64 = 4;
let mut status;

let (rt, network, mut client) = <TestNetwork>::start_test_with_runtime(N_PEERS as u32, 1);
wait_for_genesis_committed(network.clients(), 0);
let pipeline_time = Configuration::pipeline_time();
thread::sleep(pipeline_time * 2);

// Confirm all peers connected
status = client.get_status().unwrap();
Expand Down
4 changes: 1 addition & 3 deletions client/tests/integration_tests/transfer_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ use test_network::{Peer as TestPeer, *};
#[test]
fn client_can_transfer_asset_to_another_account() {
let (_rt, _peer, mut iroha_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![iroha_client.clone()], 0);
let pipeline_time = Configuration::pipeline_time();

// Given
thread::sleep(pipeline_time);

let create_domain = RegisterBox::new(IdentifiableBox::Domain(Domain::new("domain").into()));
let account1_id = AccountId::new("account1", "domain");
let account2_id = AccountId::new("account2", "domain");
Expand Down
4 changes: 2 additions & 2 deletions client/tests/integration_tests/tx_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use test_network::{Peer as TestPeer, *};
#[test]
fn client_has_rejected_and_acepted_txs_should_return_tx_history() {
let (_rt, _peer, mut iroha_client) = <TestPeer>::start_test_with_runtime();
wait_for_genesis_committed(vec![iroha_client.clone()], 0);

let pipeline_time = Configuration::pipeline_time();

// Given
thread::sleep(pipeline_time);

let account_id = AccountId::new("alice", "wonderland");
let asset_definition_id = AssetDefinitionId::new("xor", "wonderland");
let create_asset = RegisterBox::new(IdentifiableBox::AssetDefinition(
Expand Down
5 changes: 2 additions & 3 deletions client/tests/integration_tests/tx_rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use test_network::{Peer as TestPeer, *};
#[test]
fn client_sends_transaction_with_invalid_instruction_should_not_see_any_changes() {
let (_rt, _peer, mut iroha_client) = <TestPeer>::start_test_with_runtime();
let pipeline_time = Configuration::pipeline_time();
wait_for_genesis_committed(vec![iroha_client.clone()], 0);

// Given
thread::sleep(pipeline_time);
let pipeline_time = Configuration::pipeline_time();

//When
let account_id = AccountId::new("alice", "wonderland");
Expand Down
2 changes: 2 additions & 0 deletions client/tests/integration_tests/unregister_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use test_network::*;
fn network_stable_after_add_and_after_remove_peer() -> Result<()> {
// Given a network
let (rt, network, mut genesis_client, pipeline_time, account_id, asset_definition_id) = init()?;
wait_for_genesis_committed(network.clients(), 0);

// When assets are minted
mint(
&asset_definition_id,
Expand Down
31 changes: 20 additions & 11 deletions client/tests/integration_tests/unstable_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

use std::{thread, time::Duration};

use iroha_client::client;
use iroha_client::client::{self, Client};
use iroha_config::logger;
use iroha_core::config::Configuration;
use iroha_data_model::prelude::*;
use iroha_logger::Level;
use test_network::*;
use tokio::runtime::Runtime;

const MAXIMUM_TRANSACTIONS_IN_BLOCK: u32 = 1;
const MAXIMUM_TRANSACTIONS_IN_BLOCK: u32 = 5;

#[test]
fn unstable_network_4_peers_1_fault() {
Expand All @@ -29,7 +31,6 @@ fn unstable_network_4_peers_1_fault() {
}

#[test]
#[ignore = "This test fails after reducing the amount of gossipping"]
fn unstable_network_7_peers_1_fault() {
let n_peers = 7;
let n_transactions = 20;
Expand Down Expand Up @@ -59,16 +60,24 @@ fn unstable_network(
drop(iroha_logger::install_panic_hook());
let rt = Runtime::test();
// Given
let (_network, mut iroha_client) =
rt.block_on(<Network>::start_test_with_offline_and_set_n_shifts(
n_peers,
MAXIMUM_TRANSACTIONS_IN_BLOCK,
n_offline_peers,
u64::from(n_peers),
));
let (network, mut iroha_client) = rt.block_on(async {
let mut configuration = Configuration::test();
configuration.queue.maximum_transactions_in_block = MAXIMUM_TRANSACTIONS_IN_BLOCK;
configuration.sumeragi.n_topology_shifts_before_reshuffle = u64::from(n_peers);
configuration.logger.max_log_level = Level(logger::Level::ERROR).into();
let network =
<Network>::new_with_offline_peers(Some(configuration), n_peers, n_offline_peers)
.await
.expect("Failed to init peers");
let client = Client::test(
&network.genesis.api_address,
&network.genesis.status_address,
);
(network, client)
});
wait_for_genesis_committed(network.clients(), n_offline_peers);

let pipeline_time = Configuration::pipeline_time();
thread::sleep(pipeline_time * n_peers);

let account_id = AccountId::new("alice", "wonderland");
let asset_definition_id = AssetDefinitionId::new("rose", "wonderland");
Expand Down
Loading

0 comments on commit 03603a3

Please sign in to comment.