saito_spammer/
config_handler.rs

1use std::io::{Error, ErrorKind};
2
3use figment::providers::{Format, Json};
4use figment::Figment;
5use serde::Deserialize;
6
7use log::{debug, error};
8use saito_core::core::util::configuration::{
9    get_default_issuance_writing_block_interval, BlockchainConfig, Configuration, ConsensusConfig,
10    Endpoint, PeerConfig, Server,
11};
12
13#[derive(Deserialize, Debug, Clone)]
14pub struct Spammer {
15    pub timer_in_milli: u64,
16    pub burst_count: u32,
17    pub tx_size: u64,
18    pub tx_count: u64,
19    pub tx_payment: u64,
20    pub tx_fee: u64,
21    pub stop_after: u64,
22}
23fn get_default_consensus() -> Option<ConsensusConfig> {
24    Some(ConsensusConfig::default())
25}
26#[derive(Deserialize, Debug, Clone)]
27pub struct SpammerConfigs {
28    server: Server,
29    peers: Vec<PeerConfig>,
30    spammer: Spammer,
31    #[serde(skip)]
32    lite: bool,
33    #[serde(default = "get_default_consensus")]
34    consensus: Option<ConsensusConfig>,
35    blockchain: BlockchainConfig,
36}
37
38impl SpammerConfigs {
39    pub fn new() -> SpammerConfigs {
40        SpammerConfigs {
41            server: Server {
42                host: "127.0.0.1".to_string(),
43                port: 0,
44                protocol: "http".to_string(),
45                endpoint: Endpoint {
46                    host: "127.0.0.1".to_string(),
47                    port: 0,
48                    protocol: "http".to_string(),
49                },
50                verification_threads: 4,
51                channel_size: 0,
52                stat_timer_in_ms: 0,
53                reconnection_wait_time: 10000,
54                thread_sleep_time_in_ms: 10,
55                block_fetch_batch_size: 0,
56            },
57            peers: vec![],
58            spammer: Spammer {
59                timer_in_milli: 0,
60                burst_count: 0,
61                tx_size: 0,
62                tx_count: 0,
63                tx_payment: 0,
64                tx_fee: 0,
65                stop_after: 0,
66            },
67            lite: false,
68            consensus: Some(ConsensusConfig::default()),
69            blockchain: BlockchainConfig {
70                last_block_hash: "0000000000000000000000000000000000000000000000000000000000000000"
71                    .to_string(),
72                last_block_id: 0,
73                last_timestamp: 0,
74                genesis_block_id: 0,
75                genesis_timestamp: 0,
76                lowest_acceptable_timestamp: 0,
77                lowest_acceptable_block_hash:
78                    "0000000000000000000000000000000000000000000000000000000000000000".to_string(),
79                lowest_acceptable_block_id: 0,
80                fork_id: "0000000000000000000000000000000000000000000000000000000000000000"
81                    .to_string(),
82                initial_loading_completed: false,
83                issuance_writing_block_interval: get_default_issuance_writing_block_interval(),
84            },
85        }
86    }
87
88    pub fn get_spammer_configs(&self) -> &Spammer {
89        &self.spammer
90    }
91}
92
93impl Configuration for SpammerConfigs {
94    fn get_server_configs(&self) -> Option<&Server> {
95        Some(&self.server)
96    }
97
98    fn get_peer_configs(&self) -> &Vec<PeerConfig> {
99        &self.peers
100    }
101
102    fn get_blockchain_configs(&self) -> &BlockchainConfig {
103        &self.blockchain
104    }
105
106    fn get_block_fetch_url(&self) -> String {
107        let endpoint = &self.get_server_configs().unwrap().endpoint;
108        endpoint.protocol.to_string()
109            + "://"
110            + endpoint.host.as_str()
111            + ":"
112            + endpoint.port.to_string().as_str()
113            + "/block/"
114    }
115
116    fn is_spv_mode(&self) -> bool {
117        false
118    }
119
120    fn is_browser(&self) -> bool {
121        false
122    }
123
124    fn replace(&mut self, config: &dyn Configuration) {
125        self.server = config.get_server_configs().cloned().unwrap();
126        self.peers = config.get_peer_configs().clone();
127        self.lite = config.is_spv_mode();
128        self.consensus = config.get_consensus_config().cloned();
129    }
130
131    fn get_consensus_config(&self) -> Option<&ConsensusConfig> {
132        self.consensus.as_ref()
133    }
134}
135
136pub struct ConfigHandler {}
137
138impl ConfigHandler {
139    pub fn load_configs(config_file_path: String) -> Result<SpammerConfigs, Error> {
140        debug!(
141            "loading configurations from path : {:?} current_dir = {:?}",
142            config_file_path,
143            std::env::current_dir()
144        );
145        // TODO : add prompt with user friendly format
146        let configs = Figment::new()
147            .merge(Json::file(config_file_path))
148            .extract::<SpammerConfigs>();
149
150        if configs.is_err() {
151            error!("{:?}", configs.err().unwrap());
152            return Err(std::io::Error::from(ErrorKind::InvalidInput));
153        }
154
155        Ok(configs.unwrap())
156    }
157}