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
use jormungandr_automation::jormungandr::{NodeAlias, Status};
use jormungandr_automation::testing::NamedProcess;
use std::process::Child;
use std::sync::{Arc, Mutex};
use valgrind::{ProxyClient, ValgrindClient, ValgrindSettings};

use super::settings::WalletProxySettings;
/// send query to a running node
pub struct WalletProxyController {
    pub(crate) alias: NodeAlias,
    pub(crate) settings: WalletProxySettings,
    pub(crate) status: Arc<Mutex<Status>>,
    pub(crate) process: Child,
    pub(crate) client: ProxyClient,
    pub(crate) valgrind: ValgrindClient,
}

impl WalletProxyController {
    pub fn new(
        alias: NodeAlias,
        settings: WalletProxySettings,
        status: Arc<Mutex<Status>>,
        process: Child,
    ) -> Result<Self, Error> {
        let address = settings.address();

        let valgrind_settings = ValgrindSettings {
            use_https: false,
            enable_debug: false,
            certificate: None,
            ..Default::default()
        };

        let valgrind = ValgrindClient::new(settings.address(), valgrind_settings)?;
        Ok(Self {
            alias,
            settings,
            status,
            process,
            client: ProxyClient::new(address),
            valgrind,
        })
    }

    pub fn client(&self) -> ValgrindClient {
        self.valgrind.clone()
    }

    pub fn alias(&self) -> &NodeAlias {
        &self.alias
    }

    pub fn status(&self) -> Status {
        // FIXME: this is basically a Clone, but it has to be implemented in
        // jormungandr_automatation, this is only just for the sake of making it compile
        match *self.status.lock().unwrap() {
            Status::Running => Status::Running,
            Status::Starting => Status::Starting,
            Status::Exited(e) => Status::Exited(e),
        }
    }

    pub fn check_running(&self) -> bool {
        self.status() == Status::Running
    }

    pub fn block0(&self) -> Vec<u8> {
        self.client.block0().unwrap()
    }

    pub fn address(&self) -> String {
        self.settings.address()
    }

    pub fn as_named_process(&self) -> NamedProcess {
        NamedProcess::new(self.alias().to_string(), self.process.id() as usize)
    }

    pub fn settings(&self) -> &WalletProxySettings {
        &self.settings
    }

    pub fn shutdown(&mut self) {
        let _ = self.process.kill();
    }
}

impl Drop for WalletProxyController {
    fn drop(&mut self) {
        self.shutdown();
        self.process.wait().unwrap();
    }
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Valgrind(#[from] valgrind::Error),
}