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
#![allow(dead_code)]
use crate::jormungandr::{
    starter::{JormungandrParams, NodeBlock0},
    FaketimeConfig, LeadershipMode,
};
use jormungandr_lib::crypto::hash::Hash;
use std::{
    path::{Path, PathBuf},
    process::Command,
};

pub struct CommandBuilder {
    bin: PathBuf,
    config: Option<PathBuf>,
    genesis_block: Option<NodeBlock0>,
    secret: Option<PathBuf>,
    log_file: Option<PathBuf>,
    rewards_history: bool,
    faketime: Option<FaketimeConfig>,
}

impl CommandBuilder {
    pub fn new(bin: &Path) -> Self {
        CommandBuilder {
            bin: bin.to_path_buf(),
            config: None,
            genesis_block: None,
            secret: None,
            log_file: None,
            rewards_history: false,
            faketime: None,
        }
    }

    pub fn config(mut self, path: &Path) -> Self {
        self.config = Some(path.to_path_buf());
        self
    }

    pub fn faketime(mut self, faketime: FaketimeConfig) -> Self {
        self.faketime = Some(faketime);
        self
    }

    pub fn genesis_block_hash(mut self, hash: Hash) -> Self {
        self.genesis_block = Some(NodeBlock0::Hash(hash));
        self
    }

    pub fn genesis_block_path(mut self, path: &Path) -> Self {
        self.genesis_block = Some(NodeBlock0::File(path.to_path_buf()));
        self
    }

    pub fn leader_with_secret(mut self, secret: &Path) -> Self {
        self.secret = Some(secret.to_path_buf());
        self
    }

    pub fn stderr_to_log_file(mut self, path: &Path) -> Self {
        self.log_file = Some(path.to_path_buf());
        self
    }

    pub fn rewards_history(mut self, report: bool) -> Self {
        self.rewards_history = report;
        self
    }

    pub fn command(self) -> Command {
        let mut command = if let Some(faketime) = &self.faketime {
            let mut cmd = Command::new("faketime");
            cmd.args(["-f", &format!("{:+}s", faketime.offset)]);
            cmd.arg(self.bin);
            cmd
        } else {
            Command::new(self.bin)
        };

        if let Some(secret_path) = self.secret {
            command.arg("--secret").arg(secret_path);
        }

        if self.rewards_history {
            command.arg("--rewards-report-all");
        }

        let config_path = self
            .config
            .expect("configuration file path needs to be set");
        command.arg("--config").arg(config_path);

        if let Some(node_block0) = &self.genesis_block {
            match node_block0 {
                NodeBlock0::Hash(hash) => {
                    command.arg("--genesis-block-hash").arg(hash.to_string());
                }
                NodeBlock0::File(path) => {
                    command.arg("--genesis-block").arg(path);
                }
            }
        } else {
            panic!("one of the genesis block options needs to be specified")
        }

        command.stderr(std::process::Stdio::piped());
        command.stdout(std::process::Stdio::piped());

        command
    }
}

pub fn get_command(
    params: &JormungandrParams,
    bin_path: impl AsRef<Path>,
    leadership_mode: LeadershipMode,
) -> Command {
    let bin_path = bin_path.as_ref();
    let node_config_path = params.node_config_path();
    let secret_path = params.secret_path();
    let builder = CommandBuilder::new(bin_path).config(&node_config_path);

    let builder = match (leadership_mode, params.genesis()) {
        (LeadershipMode::Passive, NodeBlock0::Hash(hash)) => builder.genesis_block_hash(*hash),
        (LeadershipMode::Leader, NodeBlock0::File(block_path)) => {
            builder.genesis_block_path(block_path).leader_with_secret(
                secret_path
                    .as_ref()
                    .expect("no secrets defined for leader node"),
            )
        }
        (LeadershipMode::Leader, NodeBlock0::Hash(block_hash)) => {
            builder.genesis_block_hash(*block_hash).leader_with_secret(
                secret_path
                    .as_ref()
                    .expect("no secrets defined for leader node"),
            )
        }
        (LeadershipMode::Passive, NodeBlock0::File(block_path)) => {
            builder.genesis_block_path(block_path)
        }
    };
    builder.rewards_history(params.rewards_history()).command()
}