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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::mjolnir_lib::{
    bootstrap::scenario::{DurationBasedClientLoad, IterationBasedClientLoad},
    MjolnirError,
};
use jormungandr_lib::{crypto::hash::Hash, interfaces::TrustedPeer};
use std::{path::PathBuf, result::Result};

pub struct PassiveBootstrapLoad {
    config: ClientLoadConfig,
}

impl PassiveBootstrapLoad {
    pub fn new(config: ClientLoadConfig) -> Self {
        Self { config }
    }

    pub fn exec(self, scenario: ScenarioType) -> Result<(), MjolnirError> {
        if self.config.pace() < 2 {
            return Err(MjolnirError::PaceTooLow(2));
        }

        match scenario {
            ScenarioType::Duration(duration) => {
                let duration_client_load = DurationBasedClientLoad::new(self.config, duration);
                duration_client_load.run()
            }
            ScenarioType::Iteration(iteration) => {
                IterationBasedClientLoad::new(self.config, iteration).run()
            }
        }
    }
}

#[derive(Debug, Clone)]
pub enum ScenarioType {
    Duration(u64),
    Iteration(u32),
}

#[derive(Debug, Clone)]
pub struct ClientLoadConfig {
    block0_hash: Hash,
    measure: bool,
    count: u32,
    address: String,
    pace: u64,
    initial_storage: Option<PathBuf>,
}

impl ClientLoadConfig {
    pub fn new(
        block0_hash: Hash,
        measure: bool,
        count: u32,
        address: String,
        pace: u64,
        initial_storage: Option<PathBuf>,
    ) -> Self {
        Self {
            block0_hash,
            measure,
            count,
            address,
            pace,
            initial_storage,
        }
    }

    pub fn trusted_peer(&self) -> TrustedPeer {
        TrustedPeer {
            address: self.address.parse().unwrap(),
            id: None,
        }
    }

    pub fn pace(&self) -> u64 {
        self.pace
    }

    pub fn block0_hash(&self) -> &Hash {
        &self.block0_hash
    }

    pub fn measure(&self) -> bool {
        self.measure
    }

    pub fn count(&self) -> u32 {
        self.count
    }

    pub fn initial_storage(&self) -> &Option<PathBuf> {
        &self.initial_storage
    }
}