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
use crate::{args::Args, builder::NetworkBuilder, config::Config, error::Error};
use jormungandr_automation::jormungandr::{JormungandrProcess, NodeAlias};
use std::{collections::HashMap, time::Duration};

pub fn spawn_network(config: Config, args: Args) -> Result<(), Error> {
    let mut topology = config.build_topology();

    println!("Building network...");
    let mut controller = NetworkBuilder::default()
        .apply_config(config.clone())
        .build()?;

    let mut processes: HashMap<NodeAlias, JormungandrProcess> = HashMap::new();

    while !topology.nodes.is_empty() {
        let alias = topology
            .nodes
            .values()
            .find(|n| n.trusted_peers.is_empty())
            .map(|n| n.alias.clone())
            .ok_or(Error::CircularTrust)?;

        let spawn_params = config.node_spawn_params(&alias)?;

        processes.insert(
            alias.clone(),
            controller.spawn(spawn_params.verbose(args.verbose))?,
        );

        topology.nodes.remove(&alias);
        topology.nodes.values_mut().for_each(|n| {
            n.trusted_peers.remove(&alias);
        });

        println!("Node '{}' started", alias);
    }

    let _maybe_explorer = {
        if controller.settings().explorer.is_some() {
            let explorer = Some(controller.spawn_explorer()?);
            println!("explorer started");
            explorer
        } else {
            None
        }
    };

    println!("Network is started");
    loop {
        for node in processes.values() {
            let _result = node.rest().network_stats()?;
        }
        std::thread::sleep(Duration::from_secs(1));
    }
}