1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
mod duration;
mod iteration;
use super::ClientLoadConfig;
use assert_fs::{fixture::PathChild, TempDir};
pub use duration::DurationBasedClientLoad;
use indicatif::{ProgressBar, ProgressStyle};
pub use iteration::IterationBasedClientLoad;
use jormungandr_automation::jormungandr::{
    JormungandrBootstrapper, JormungandrProcess, NodeConfigBuilder, Starter, StartupError,
};
use jortestkit::file;
use std::{fs, path::PathBuf, result::Result};

pub fn copy_initial_storage_if_used(
    config: &ClientLoadConfig,
    storage_folder: &str,
    temp_dir: &TempDir,
) {
    if let Some(storage) = config.initial_storage() {
        let client_storage: PathBuf = temp_dir.child(storage_folder).path().into();
        if client_storage.exists() {
            fs::remove_dir_all(&client_storage).expect("cannot remove existing client storage");
        }
        fs::create_dir(&client_storage).expect("cannot create client storage");
        file::copy_folder(storage, &client_storage, true).unwrap()
    }
}

pub fn start_node(
    client_config: &ClientLoadConfig,
    storage_folder_name: &str,
    temp_dir: &TempDir,
) -> Result<JormungandrProcess, StartupError> {
    copy_initial_storage_if_used(client_config, storage_folder_name, temp_dir);

    let node = NodeConfigBuilder::default()
        .with_trusted_peers(vec![client_config.trusted_peer()])
        .with_storage(temp_dir.child(storage_folder_name).to_path_buf())
        .build();

    let config = JormungandrBootstrapper::default_with_config(node)
        .with_block0_hash(*client_config.block0_hash())
        .passive()
        .verbose()
        .build(temp_dir)?;

    Starter::default().config(config).start_async()
}

pub struct ScenarioProgressBar {
    progress_bar: ProgressBar,
}

impl ScenarioProgressBar {
    pub fn new(progress_bar: ProgressBar, prefix: &str) -> Self {
        let spinner_style = ProgressStyle::default_spinner()
            .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ ")
            .template("{prefix:.bold.dim} {spinner} {wide_msg}");
        progress_bar.set_style(spinner_style);
        progress_bar.set_prefix(prefix);
        progress_bar.set_message("initializing...");

        Self { progress_bar }
    }

    pub fn set_progress(&self, progress: &str) {
        self.progress_bar
            .set_message(&format!("bootstrapping... {}", progress));
    }

    pub fn set_error_lines(&self, iter: Vec<String>) {
        for line in iter {
            self.progress_bar.set_message(&format!("Error: {}", line));
        }
    }

    pub fn set_finished(&self) {
        self.progress_bar.set_message("bootstrapped succesfully.");
    }
}