From 9226e47bf5c3195394713dbb1d128fd46deb5dbd Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 30 Nov 2019 21:10:12 +1100 Subject: [PATCH] Move files about in beacon sim --- tests/beacon_chain_sim/src/checks.rs | 57 +++++++++++++++++ ...{simulated_network.rs => local_network.rs} | 0 tests/beacon_chain_sim/src/main.rs | 61 +++---------------- 3 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 tests/beacon_chain_sim/src/checks.rs rename tests/beacon_chain_sim/src/{simulated_network.rs => local_network.rs} (100%) diff --git a/tests/beacon_chain_sim/src/checks.rs b/tests/beacon_chain_sim/src/checks.rs new file mode 100644 index 0000000000..8ed3da7e09 --- /dev/null +++ b/tests/beacon_chain_sim/src/checks.rs @@ -0,0 +1,57 @@ +use crate::local_network::LocalNetwork; +use futures::{stream, Future, IntoFuture, Stream}; +use std::time::{Duration, Instant}; +use tokio::timer::Delay; +use types::{Epoch, EthSpec}; + +pub fn verify_first_finalization( + network: LocalNetwork, + slot_duration: Duration, +) -> impl Future { + epoch_delay(Epoch::new(4), slot_duration, E::slots_per_epoch()) + .and_then(|()| verify_all_finalized_at(network, Epoch::new(2))) +} + +/// Delays for `epochs`, plus half a slot extra. +fn epoch_delay( + epochs: Epoch, + slot_duration: Duration, + slots_per_epoch: u64, +) -> impl Future { + let duration = slot_duration * (epochs.as_u64() * slots_per_epoch) as u32 + slot_duration / 2; + + Delay::new(Instant::now() + duration).map_err(|e| format!("Epoch delay failed: {:?}", e)) +} + +fn verify_all_finalized_at( + network: LocalNetwork, + epoch: Epoch, +) -> impl Future { + network + .remote_nodes() + .into_future() + .and_then(|remote_nodes| { + stream::unfold(remote_nodes.into_iter(), |mut iter| { + iter.next().map(|remote_node| { + remote_node + .http + .beacon() + .get_head() + .map(|head| head.finalized_slot.epoch(E::slots_per_epoch())) + .map(|epoch| (epoch, iter)) + .map_err(|e| format!("Get head via http failed: {:?}", e)) + }) + }) + .collect() + }) + .and_then(move |epochs| { + if epochs.iter().any(|node_epoch| *node_epoch != epoch) { + Err(format!( + "Nodes are not finalized at epoch {}. Finalized epochs: {:?}", + epoch, epochs + )) + } else { + Ok(()) + } + }) +} diff --git a/tests/beacon_chain_sim/src/simulated_network.rs b/tests/beacon_chain_sim/src/local_network.rs similarity index 100% rename from tests/beacon_chain_sim/src/simulated_network.rs rename to tests/beacon_chain_sim/src/local_network.rs diff --git a/tests/beacon_chain_sim/src/main.rs b/tests/beacon_chain_sim/src/main.rs index ad475be8c2..206a22b6fb 100644 --- a/tests/beacon_chain_sim/src/main.rs +++ b/tests/beacon_chain_sim/src/main.rs @@ -1,14 +1,14 @@ -mod simulated_network; +mod checks; +mod local_network; -use futures::{future, stream, Future, IntoFuture, Stream}; +use futures::{future, stream, Future, Stream}; +use local_network::LocalNetwork; use node_test_rig::{ environment::EnvironmentBuilder, testing_client_config, ClientGenesis, LocalBeaconNode, LocalValidatorClient, ProductionClient, ValidatorConfig, }; -use simulated_network::LocalNetwork; -use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; -use tokio::timer::Delay; -use types::{Epoch, EthSpec, MinimalEthSpec}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use types::MinimalEthSpec; pub type E = MinimalEthSpec; pub type BeaconNode = LocalBeaconNode>; @@ -83,53 +83,10 @@ fn async_sim( .map(|_| ()) }) .and_then(move |_| { - epoch_delay(Epoch::new(4), slot_duration, E::slots_per_epoch()) - .and_then(|()| verify_all_finalized_at(network_3, Epoch::new(2))) + let network = network_3; + + checks::verify_first_finalization(network, slot_duration) }); env.runtime().block_on(future) } - -/// Delays for `epochs`, plus half a slot extra. -fn epoch_delay( - epochs: Epoch, - slot_duration: Duration, - slots_per_epoch: u64, -) -> impl Future { - let duration = slot_duration * (epochs.as_u64() * slots_per_epoch) as u32 + slot_duration / 2; - - Delay::new(Instant::now() + duration).map_err(|e| format!("Epoch delay failed: {:?}", e)) -} - -fn verify_all_finalized_at( - network: LocalNetwork, - epoch: Epoch, -) -> impl Future { - network - .remote_nodes() - .into_future() - .and_then(|remote_nodes| { - stream::unfold(remote_nodes.into_iter(), |mut iter| { - iter.next().map(|remote_node| { - remote_node - .http - .beacon() - .get_head() - .map(|head| head.finalized_slot.epoch(E::slots_per_epoch())) - .map(|epoch| (epoch, iter)) - .map_err(|e| format!("Get head via http failed: {:?}", e)) - }) - }) - .collect() - }) - .and_then(move |epochs| { - if epochs.iter().any(|node_epoch| *node_epoch != epoch) { - Err(format!( - "Nodes are not finalized at epoch {}. Finalized epochs: {:?}", - epoch, epochs - )) - } else { - Ok(()) - } - }) -}