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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use super::{
    super::VitSettings,
    vit_station::{
        dump_settings_to_file, BootstrapCommandBuilder, DbGenerator,
        Error as VitStationControllerError, RestClient, ValidVotePlanParameters,
        ValidVotingTemplateGenerator, VitStationController, VIT_CONFIG,
    },
    wallet_proxy::{Error as WalletProxyError, WalletProxyController, WalletProxySpawnParams},
};
use crate::builders::ArchiveConfiguration;
use crate::mode::standard::settings::{VIT_STATION, VIT_STATION_ARCHIVE};
use crate::mode::standard::ExplorerController;
use crate::Result;
use assert_fs::fixture::PathChild;
use chain_impl_mockchain::testing::scenario::template::VotePlanDef;
use hersir::builder::ControllerError;
use hersir::config::{
    BlockchainConfiguration, CommitteeTemplate, ExplorerTemplate, SpawnParams, VotePlanTemplate,
    WalletTemplate,
};
use hersir::{
    builder::{
        NetworkBuilder, NodeAlias, NodeSetting, Settings, Topology, Wallet as WalletSettings,
    },
    config::SessionSettings,
};
use jormungandr_automation::jormungandr::{JormungandrProcess, Status, TestingDirectory};
use std::{
    path::PathBuf,
    process::Command,
    sync::{Arc, Mutex},
};
use thiserror::Error;
use thor::{Wallet, WalletAlias};

#[derive(Default)]
pub struct VitControllerBuilder {
    committees: Vec<CommitteeTemplate>,
    controller_builder: NetworkBuilder,
    archive_conf: Option<ArchiveConfiguration>,
}

impl VitControllerBuilder {
    pub fn new() -> Self {
        Self {
            committees: Vec::new(),
            controller_builder: NetworkBuilder::default(),
            archive_conf: None,
        }
    }

    pub(crate) fn archive_conf(mut self, archive_conf: ArchiveConfiguration) -> Self {
        self.archive_conf = Some(archive_conf);
        self
    }

    pub(crate) fn committee(mut self, committee: CommitteeTemplate) -> Self {
        self.committees.push(committee);
        self
    }

    pub fn topology(mut self, topology: Topology) -> Self {
        self.controller_builder = self.controller_builder.topology(topology);
        self
    }

    pub fn blockchain(mut self, blockchain: BlockchainConfiguration) -> Self {
        self.controller_builder = self.controller_builder.blockchain_config(blockchain);
        self
    }

    pub fn wallets(mut self, wallet_templates: Vec<WalletTemplate>) -> Self {
        self.controller_builder = self.controller_builder.wallet_templates(wallet_templates);
        self
    }

    pub fn wallet(mut self, wallet_template: WalletTemplate) -> Self {
        self.controller_builder = self.controller_builder.wallet_template(wallet_template);
        self
    }

    pub fn vote_plans(mut self, vote_plans: Vec<VotePlanTemplate>) -> Self {
        self.controller_builder = self.controller_builder.vote_plan_templates(vote_plans);
        self
    }
    pub fn explorer(mut self, explorer: ExplorerTemplate) -> Self {
        self.controller_builder = self.controller_builder.explorer(Some(explorer));
        self
    }

    pub fn build(
        self,
        mut session_settings: SessionSettings,
    ) -> std::result::Result<VitController, Error> {
        let controller = self
            .controller_builder
            .committees(self.committees)
            .session_settings(session_settings.clone())
            .build()?;

        Ok(VitController::new(
            VitSettings::new(&mut session_settings, self.archive_conf),
            controller,
        ))
    }
}

#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    Controller(#[from] ControllerError),
    #[error("cannot bootstrap vit station server. health checkpoint is rejecting request")]
    CannotBootstrap,
    #[error("cannot bootstrap explorer. health checkpoint is rejecting request")]
    CannotBootstrapExplorer,
    #[error("cannot get wallet with alias {alias}, either does not exist or controller does not have any control over it")]
    CannotGetWallet { alias: String },
}

#[derive(Clone)]
pub struct VitController {
    vit_settings: VitSettings,
    hersir_controller: hersir::controller::Controller,
}

impl VitController {
    pub fn new(
        vit_settings: VitSettings,
        hersir_controller: hersir::controller::Controller,
    ) -> Self {
        Self {
            vit_settings,
            hersir_controller,
        }
    }

    pub fn vit_settings(&self) -> &VitSettings {
        &self.vit_settings
    }

    pub fn hersir_controller(&self) -> hersir::controller::Controller {
        self.hersir_controller.clone()
    }

    pub fn wallet(&mut self, wallet: &str) -> Result<Wallet> {
        self.hersir_controller
            .controlled_wallet(wallet)
            .ok_or(Error::CannotGetWallet {
                alias: wallet.to_owned(),
            })
            .map_err(Into::into)
    }

    pub fn spawn_node(&mut self, spawn_params: SpawnParams) -> Result<JormungandrProcess> {
        self.hersir_controller
            .spawn(spawn_params)
            .map_err(Into::into)
    }

    pub fn settings(&self) -> Settings {
        self.hersir_controller.settings().clone()
    }

    pub fn block0_file(&self) -> PathBuf {
        self.hersir_controller.block0_file()
    }

    pub fn defined_nodes(&self) -> Vec<(&NodeAlias, &NodeSetting)> {
        self.hersir_controller.defined_nodes().collect()
    }

    pub fn controlled_wallets(&self) -> Vec<Wallet> {
        self.hersir_controller.controlled_wallets()
    }

    pub fn defined_wallets(&self) -> Vec<(WalletAlias, &WalletSettings)> {
        self.hersir_controller.defined_wallets().collect()
    }

    pub fn defined_vote_plan(&self, alias: &str) -> Result<VotePlanDef> {
        self.hersir_controller
            .defined_vote_plan(alias)
            .map_err(Into::into)
    }

    pub fn defined_vote_plans(&self) -> Vec<VotePlanDef> {
        self.hersir_controller.defined_vote_plans()
    }

    pub fn working_directory(&self) -> &TestingDirectory {
        self.hersir_controller.working_directory()
    }

    pub fn spawn_vit_station_archive(&self, version: String) -> Result<VitStationController> {
        let settings = self
            .vit_settings
            .vit_stations
            .get(VIT_STATION_ARCHIVE)
            .ok_or(VitStationControllerError::NoVitStationArchiverDefinedInSettings)?;

        let mut command_builder =
            BootstrapCommandBuilder::new(PathBuf::from("vit-servicing-station-server"));

        let mut command = command_builder
            .db_url(&settings.db_url)
            .block0_paths(
                settings
                    .block0_paths
                    .clone()
                    .map(|p| p.to_str().unwrap().to_string()),
            )
            .service_version(version)
            .build();

        println!("Starting vit-servicing-station-archiver: {:?}", command);

        let controller = VitStationController {
            alias: VIT_STATION_ARCHIVE.to_string(),
            rest_client: RestClient::from(settings),
            process: command.spawn().unwrap(),
            settings: settings.clone(),
            status: Arc::new(Mutex::new(Status::Running)),
        };

        wait_for_bootstrap(&controller)?;

        Ok(controller)
    }

    //TODO: move to vit station builder
    pub fn spawn_vit_station(
        &self,
        vote_plan_parameters: ValidVotePlanParameters,
        template_generator: &mut dyn ValidVotingTemplateGenerator,
        version: String,
    ) -> Result<VitStationController> {
        let settings = self
            .vit_settings
            .vit_stations
            .get(VIT_STATION)
            .ok_or(VitStationControllerError::NoVitStationDefinedInSettings)?;

        let working_directory = self.hersir_controller.working_directory().path();

        let dir = working_directory.join(VIT_STATION);
        std::fs::DirBuilder::new().recursive(true).create(&dir)?;

        let config_file = dir.join(VIT_CONFIG);
        dump_settings_to_file(config_file.to_str().unwrap(), settings).unwrap();

        let db_url = DbGenerator::new(vote_plan_parameters).build(template_generator)?;

        let mut command_builder =
            BootstrapCommandBuilder::new(PathBuf::from("vit-servicing-station-server"));
        let mut command = command_builder
            .in_settings_file(&config_file)
            .db_url(db_url)
            .service_version(version)
            .block0_path(Some(
                self.hersir_controller
                    .block0_file()
                    .to_str()
                    .unwrap()
                    .to_string(),
            ))
            .build();

        println!("Starting vit-servicing-station: {:?}", command);

        let controller = VitStationController {
            alias: VIT_STATION.to_string(),
            rest_client: RestClient::from(settings),
            process: command.spawn().unwrap(),
            settings: settings.clone(),
            status: Arc::new(Mutex::new(Status::Running)),
        };

        wait_for_bootstrap(&controller)?;

        Ok(controller)
    }

    pub fn spawn_explorer(&mut self) -> Result<ExplorerController> {
        let explorer = self.hersir_controller.spawn_explorer()?;
        if !explorer.wait_to_be_up(1, 3) {
            Err(Error::CannotBootstrapExplorer)?
        } else {
            Ok(ExplorerController::new("explorer".to_string(), explorer))
        }
    }

    //TODO: move to wallet builder
    pub fn spawn_wallet_proxy_custom(
        &self,
        params: &mut WalletProxySpawnParams,
    ) -> Result<WalletProxyController> {
        let node_alias = params.alias.clone();

        let (alias, settings) = self
            .vit_settings()
            .wallet_proxies
            .iter()
            .next()
            .ok_or(WalletProxyError::NoWalletProxiesDefinedInSettings)?;
        let node_setting =
            if let Some(node_setting) = self.hersir_controller.settings().nodes.get(&node_alias) {
                node_setting.clone()
            } else {
                return Err(crate::error::Error::ProxyNotFound {
                    alias: node_alias.to_string(),
                });
            };

        let mut settings_overriden = settings.clone();
        params.override_settings(&mut settings_overriden);

        let block0_file = self.hersir_controller.block0_file();
        let working_directory = self.hersir_controller.working_directory();

        let dir = working_directory.child(alias);
        std::fs::DirBuilder::new().recursive(true).create(dir)?;

        settings_overriden.node_backend_address = Some(node_setting.config.rest.listen);

        let mut command = Command::new("valgrind");
        command
            .arg("--address")
            .arg(settings_overriden.base_address().to_string())
            .arg("--vit-address")
            .arg(&settings_overriden.base_vit_address().to_string())
            .arg("--node-address")
            .arg(
                &settings_overriden
                    .base_node_backend_address()
                    .unwrap()
                    .to_string(),
            )
            .arg("--block0")
            .arg(block0_file.as_path().to_str().unwrap());

        println!("proxy: {:?}", command);

        if let valgrind::Protocol::Https(certs) = &params.protocol {
            command
                .arg("--cert")
                .arg(&certs.cert_path)
                .arg("--key")
                .arg(&certs.key_path);
        }

        WalletProxyController::new(
            alias.into(),
            settings.clone(),
            Arc::new(Mutex::new(Status::Running)),
            command.spawn().unwrap(),
        )
        .map_err(Into::into)
    }

    //TODO: move to wallet builder
    pub fn spawn_wallet_proxy(&self, alias: &str) -> Result<WalletProxyController> {
        self.spawn_wallet_proxy_custom(&mut WalletProxySpawnParams::new(alias))
    }
}

fn wait_for_bootstrap(controller: &VitStationController) -> std::result::Result<(), Error> {
    std::thread::sleep(std::time::Duration::from_secs(5));

    for _ in 0..5 {
        if controller.check_running() {
            return Ok(());
        }
        std::thread::sleep(std::time::Duration::from_secs(1));
    }
    Err(Error::CannotBootstrap)
}