use std::{collections::HashMap, marker::PhantomData, rc::Rc, sync::Arc};
use hotshot::{
traits::{NodeImplementation, TestableNodeImplementation},
MarketplaceConfig,
};
use hotshot_example_types::storage_types::TestStorage;
use hotshot_types::{
traits::{
network::{AsyncGenerator, ConnectedNetwork},
node_implementation::{NodeType, Versions},
},
HotShotConfig, ValidatorConfig,
};
use super::{test_builder::TestDescription, test_runner::TestRunner};
use crate::test_task::TestTaskStateSeed;
pub type Network<TYPES, I> = Arc<<I as NodeImplementation<TYPES>>::Network>;
pub type Generator<T> = Rc<dyn Fn(u64) -> T>;
pub struct ResourceGenerators<TYPES: NodeType, I: TestableNodeImplementation<TYPES>> {
pub channel_generator: AsyncGenerator<Network<TYPES, I>>,
pub storage: Generator<TestStorage<TYPES>>,
pub hotshot_config: Generator<HotShotConfig<TYPES::SignatureKey>>,
pub validator_config: Generator<ValidatorConfig<TYPES::SignatureKey>>,
pub marketplace_config: Generator<MarketplaceConfig<TYPES, I>>,
}
pub struct TestLauncher<TYPES: NodeType, I: TestableNodeImplementation<TYPES>, V: Versions> {
pub resource_generators: ResourceGenerators<TYPES, I>,
pub metadata: TestDescription<TYPES, I, V>,
pub additional_test_tasks: Vec<Box<dyn TestTaskStateSeed<TYPES, I, V>>>,
}
impl<TYPES: NodeType, I: TestableNodeImplementation<TYPES>, V: Versions> TestLauncher<TYPES, I, V> {
#[must_use]
pub fn launch<N: ConnectedNetwork<TYPES::SignatureKey>>(self) -> TestRunner<TYPES, I, V, N> {
TestRunner::<TYPES, I, V, N> {
launcher: self,
nodes: Vec::new(),
solver_server: None,
late_start: HashMap::new(),
next_node_id: 0,
_pd: PhantomData,
}
}
#[must_use]
pub fn map_hotshot_config(
mut self,
f: impl Fn(&mut HotShotConfig<TYPES::SignatureKey>) + 'static,
) -> Self {
let mut test_config = self.metadata.test_config.clone();
f(&mut test_config);
let hotshot_config_generator = self.resource_generators.hotshot_config.clone();
let hotshot_config: Generator<_> = Rc::new(move |node_id| {
let mut result = (hotshot_config_generator)(node_id);
f(&mut result);
result
});
self.metadata.test_config = test_config;
self.resource_generators.hotshot_config = hotshot_config;
self
}
}