diff --git a/Cargo.lock b/Cargo.lock index e10e605def8..9a6b1b61bbf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3630,7 +3630,7 @@ dependencies = [ [[package]] name = "mithril-end-to-end" -version = "0.4.37" +version = "0.4.38" dependencies = [ "anyhow", "async-recursion", diff --git a/mithril-test-lab/mithril-end-to-end/Cargo.toml b/mithril-test-lab/mithril-end-to-end/Cargo.toml index c28b58bbadf..7d714f04c8a 100644 --- a/mithril-test-lab/mithril-end-to-end/Cargo.toml +++ b/mithril-test-lab/mithril-end-to-end/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mithril-end-to-end" -version = "0.4.37" +version = "0.4.38" authors = { workspace = true } edition = { workspace = true } documentation = { workspace = true } diff --git a/mithril-test-lab/mithril-end-to-end/src/main.rs b/mithril-test-lab/mithril-end-to-end/src/main.rs index 769f58787e2..08821a68476 100644 --- a/mithril-test-lab/mithril-end-to-end/src/main.rs +++ b/mithril-test-lab/mithril-end-to-end/src/main.rs @@ -11,7 +11,6 @@ use std::{ fs, path::{Path, PathBuf}, sync::Arc, - thread::sleep, time::Duration, }; use tokio_util::sync::CancellationToken; @@ -174,6 +173,11 @@ async fn main() -> StdResult<()> { work_dir.canonicalize().unwrap() } }; + let artifacts_dir = { + let path = work_dir.join("artifacts"); + fs::create_dir(&path).expect("Artifacts dir creation failure"); + path + }; let run_only_mode = args.run_only; let use_p2p_network_mode = args.use_p2p_network; let use_p2p_passive_relays = args.use_p2p_passive_relays; @@ -193,6 +197,7 @@ async fn main() -> StdResult<()> { let mut infrastructure = MithrilInfrastructure::start(&MithrilInfrastructureConfig { server_port, devnet: devnet.clone(), + artifacts_dir, work_dir, bin_dir: args.bin_directory, cardano_node_version: args.cardano_node_version, @@ -224,14 +229,16 @@ async fn main() -> StdResult<()> { }; match runner { - Ok(_) if run_only_mode => run_until_cancelled(devnet).await, + Ok(_) if run_only_mode => run_until_cancelled(infrastructure, devnet).await, Ok(_) => { + infrastructure.stop_nodes().await?; devnet.stop().await?; Ok(()) } Err(error) => { let has_written_logs = infrastructure.tail_logs(40).await; error!("Mithril End to End test in failed: {}", error); + infrastructure.stop_nodes().await?; devnet.stop().await?; has_written_logs?; Err(error) @@ -239,7 +246,10 @@ async fn main() -> StdResult<()> { } } -async fn run_until_cancelled(devnet: Devnet) -> StdResult<()> { +async fn run_until_cancelled( + mut mithril_infrastructure: MithrilInfrastructure, + devnet: Devnet, +) -> StdResult<()> { let cancellation_token = CancellationToken::new(); let cloned_token = cancellation_token.clone(); @@ -247,10 +257,11 @@ async fn run_until_cancelled(devnet: Devnet) -> StdResult<()> { _ = tokio::spawn(async move { while !cloned_token.is_cancelled() { info!("Mithril end to end is running and will remain active until manually stopped..."); - sleep(Duration::from_secs(5)); + tokio::time::sleep(Duration::from_secs(5)).await; } }) => {} _ = tokio::signal::ctrl_c() => { + mithril_infrastructure.stop_nodes().await?; cancellation_token.cancel(); devnet.stop().await?; } diff --git a/mithril-test-lab/mithril-end-to-end/src/mithril/aggregator.rs b/mithril-test-lab/mithril-end-to-end/src/mithril/aggregator.rs index 69eb2be9053..73bf5937c4d 100644 --- a/mithril-test-lab/mithril-end-to-end/src/mithril/aggregator.rs +++ b/mithril-test-lab/mithril-end-to-end/src/mithril/aggregator.rs @@ -6,6 +6,7 @@ use crate::{ use anyhow::{anyhow, Context}; use mithril_common::era::SupportedEra; use mithril_common::{entities, StdResult}; +use slog_scope::info; use std::cmp; use std::collections::HashMap; use std::path::{Path, PathBuf}; @@ -18,6 +19,7 @@ pub struct AggregatorConfig<'a> { pub pool_node: &'a PoolNode, pub cardano_cli_path: &'a Path, pub work_dir: &'a Path, + pub artifacts_dir: &'a Path, pub bin_dir: &'a Path, pub cardano_node_version: &'a str, pub mithril_run_interval: u32, @@ -62,6 +64,10 @@ impl Aggregator { ("URL_SNAPSHOT_MANIFEST", ""), ("SNAPSHOT_STORE_TYPE", "local"), ("SNAPSHOT_UPLOADER_TYPE", "local"), + ( + "SNAPSHOT_DIRECTORY", + aggregator_config.artifacts_dir.to_str().unwrap(), + ), ("NETWORK_MAGIC", &magic_id), ("DATA_STORES_DIRECTORY", "./stores/aggregator"), ( @@ -139,8 +145,11 @@ impl Aggregator { } pub async fn bootstrap_genesis(&mut self) -> StdResult<()> { - let exit_status = self - .command + // Clone the command so we can alter it without affecting the original + let mut command = self.command.clone(); + command.set_log_name("mithril-aggregator-genesis-bootstrap"); + + let exit_status = command .start(&["genesis".to_string(), "bootstrap".to_string()])? .wait() .await @@ -162,10 +171,12 @@ impl Aggregator { pub async fn stop(&mut self) -> StdResult<()> { if let Some(process) = self.process.as_mut() { + info!("Stopping aggregator"); process .kill() .await .with_context(|| "Could not kill aggregator")?; + self.process = None; } Ok(()) } diff --git a/mithril-test-lab/mithril-end-to-end/src/mithril/infrastructure.rs b/mithril-test-lab/mithril-end-to-end/src/mithril/infrastructure.rs index 15b1ee95dad..4e42d300e81 100644 --- a/mithril-test-lab/mithril-end-to-end/src/mithril/infrastructure.rs +++ b/mithril-test-lab/mithril-end-to-end/src/mithril/infrastructure.rs @@ -1,7 +1,4 @@ -use crate::{ - assertions, Aggregator, AggregatorConfig, Client, Devnet, PoolNode, RelayAggregator, - RelayPassive, RelaySigner, Signer, DEVNET_MAGIC_ID, -}; +use anyhow::Context; use mithril_common::chain_observer::{ChainObserver, PallasChainObserver}; use mithril_common::entities::{Epoch, PartyId, ProtocolParameters}; use mithril_common::{CardanoNetwork, StdResult}; @@ -11,12 +8,18 @@ use std::fs; use std::path::PathBuf; use std::sync::Arc; +use crate::{ + assertions, Aggregator, AggregatorConfig, Client, Devnet, PoolNode, RelayAggregator, + RelayPassive, RelaySigner, Signer, DEVNET_MAGIC_ID, +}; + use super::signer::SignerConfig; pub struct MithrilInfrastructureConfig { pub server_port: u64, pub devnet: Devnet, pub work_dir: PathBuf, + pub artifacts_dir: PathBuf, pub bin_dir: PathBuf, pub cardano_node_version: String, pub mithril_run_interval: u32, @@ -30,7 +33,7 @@ pub struct MithrilInfrastructureConfig { } pub struct MithrilInfrastructure { - work_dir: PathBuf, + artifacts_dir: PathBuf, bin_dir: PathBuf, devnet: Devnet, aggregator: Aggregator, @@ -77,8 +80,8 @@ impl MithrilInfrastructure { )); Ok(Self { - work_dir: config.work_dir.to_path_buf(), bin_dir: config.bin_dir.to_path_buf(), + artifacts_dir: config.artifacts_dir.to_path_buf(), devnet: config.devnet.clone(), aggregator, signers, @@ -137,11 +140,22 @@ impl MithrilInfrastructure { pool_node: &PoolNode, chain_observer_type: &str, ) -> StdResult { + let aggregator_artifacts_directory = config.artifacts_dir.join("mithril-aggregator"); + if !aggregator_artifacts_directory.exists() { + fs::create_dir_all(&aggregator_artifacts_directory).with_context(|| { + format!( + "Could not create artifacts directory '{}'", + aggregator_artifacts_directory.display() + ) + })?; + } + let mut aggregator = Aggregator::new(&AggregatorConfig { server_port: config.server_port, pool_node, cardano_cli_path: &config.devnet.cardano_cli_path(), work_dir: &config.work_dir, + artifacts_dir: &aggregator_artifacts_directory, bin_dir: &config.bin_dir, cardano_node_version: &config.cardano_node_version, mithril_run_interval: config.mithril_run_interval, @@ -275,6 +289,18 @@ impl MithrilInfrastructure { Ok(signers) } + pub async fn stop_nodes(&mut self) -> StdResult<()> { + // Note: The aggregator should be stopped *last* since signers depends on it + info!("Stopping Mithril infrastructure"); + for signer in self.signers.as_mut_slice() { + signer.stop().await?; + } + + self.aggregator.stop().await?; + + Ok(()) + } + pub fn devnet(&self) -> &Devnet { &self.devnet } @@ -312,15 +338,16 @@ impl MithrilInfrastructure { } pub fn build_client(&self) -> StdResult { - let work_dir = if self.use_era_specific_work_dir { - let era_work_dir = self.work_dir.join(format!("era.{}", self.current_era)); - if !era_work_dir.exists() { - fs::create_dir(&era_work_dir)?; + let work_dir = { + let mut artifacts_dir = self.artifacts_dir.join("mithril-client"); + if self.use_era_specific_work_dir { + artifacts_dir = artifacts_dir.join(format!("era.{}", self.current_era)); + } + if !artifacts_dir.exists() { + fs::create_dir_all(&artifacts_dir)?; } - era_work_dir - } else { - self.work_dir.clone() + artifacts_dir }; Client::new(self.aggregator.endpoint(), &work_dir, &self.bin_dir) diff --git a/mithril-test-lab/mithril-end-to-end/src/mithril/signer.rs b/mithril-test-lab/mithril-end-to-end/src/mithril/signer.rs index 8fd6dde1070..b91dee9ed66 100644 --- a/mithril-test-lab/mithril-end-to-end/src/mithril/signer.rs +++ b/mithril-test-lab/mithril-end-to-end/src/mithril/signer.rs @@ -1,8 +1,10 @@ use crate::devnet::PoolNode; use crate::utils::MithrilCommand; use crate::{DEVNET_MAGIC_ID, ERA_MARKERS_VERIFICATION_KEY}; +use anyhow::Context; use mithril_common::entities::PartyId; use mithril_common::StdResult; +use slog_scope::info; use std::collections::HashMap; use std::path::Path; use tokio::process::Child; @@ -24,6 +26,7 @@ pub struct SignerConfig<'a> { #[derive(Debug)] pub struct Signer { + name: String, party_id: PartyId, command: MithrilCommand, process: Option, @@ -103,11 +106,11 @@ impl Signer { env, &args, )?; - command.set_log_name( - format!("mithril-signer-{}-{party_id}", signer_config.signer_number).as_str(), - ); + let name = format!("mithril-signer-{}-{party_id}", signer_config.signer_number); + command.set_log_name(&name); Ok(Self { + name, party_id, command, process: None, @@ -119,6 +122,18 @@ impl Signer { Ok(()) } + pub async fn stop(&mut self) -> StdResult<()> { + if let Some(process) = self.process.as_mut() { + let name = self.name.as_str(); + info!("Stopping {name}"); + process + .kill() + .await + .with_context(|| "Could not kill signer")?; + self.process = None; + } + Ok(()) + } pub async fn tail_logs(&self, number_of_line: u64) -> StdResult<()> { self.command .tail_logs( diff --git a/mithril-test-lab/mithril-end-to-end/src/stress_test/aggregator_helpers.rs b/mithril-test-lab/mithril-end-to-end/src/stress_test/aggregator_helpers.rs index ecfac461ee3..0e209954f95 100644 --- a/mithril-test-lab/mithril-end-to-end/src/stress_test/aggregator_helpers.rs +++ b/mithril-test-lab/mithril-end-to-end/src/stress_test/aggregator_helpers.rs @@ -23,6 +23,7 @@ pub async fn bootstrap_aggregator( pool_node: &args.pool_node, cardano_cli_path: &args.cardano_cli_path, work_dir: &args.work_dir, + artifacts_dir: &args.work_dir, bin_dir: &args.bin_dir, cardano_node_version: "1.2.3", mithril_run_interval: 1000,