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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use cli::client_proxy::ClientProxy;
use diem_client::BlockingClient;
use diem_config::config::NodeConfig;
use diem_temppath::TempPath;
use diem_types::{chain_id::ChainId, waypoint::Waypoint};
use nix::{
    sys::signal::{kill, SIGKILL},
    unistd::Pid,
};
use std::{fmt, fs::File, io::Write, path::PathBuf, process::Stdio, time::Duration};

#[derive(Debug)]
pub struct NodeInfo {
    pub chain_id: ChainId,
    pub json_rpc: String,
    pub root_key_path: PathBuf,
    pub waypoint: Waypoint,
    pub local_node_info: Option<LocalNodeInfo>,
}

#[derive(Debug)]
pub struct LocalNodeInfo {
    pub log_path: PathBuf,
    pub config_path: TempPath,
    node_pid: Pid,
}

fn diem_root_folder() -> PathBuf {
    let mut diem_root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    diem_root_dir.pop();
    diem_root_dir.pop();
    diem_root_dir.pop();
    diem_root_dir
}

fn wait_till_healthy(json_rpc: &str) {
    let client = BlockingClient::new(json_rpc);
    loop {
        if client.get_metadata().is_ok() {
            break;
        }
        std::thread::sleep(Duration::from_millis(10));
    }
}

impl NodeInfo {
    pub fn new_local() -> Self {
        let config_temp_path = diem_temppath::TempPath::new();
        let config_path = config_temp_path.as_ref().to_path_buf();
        std::fs::DirBuilder::new()
            .recursive(true)
            .create(&config_path)
            .unwrap();
        let config_path = config_path.canonicalize().unwrap();

        // Build a single validator network
        let template = NodeConfig::default_for_validator();
        let builder = diem_genesis_tool::validator_builder::ValidatorBuilder::new(
            &config_path,
            diem_framework_releases::current_module_blobs().to_vec(),
        )
        .template(template)
        .randomize_first_validator_ports(true);
        let (root_keys, _genesis, genesis_waypoint, validators) =
            builder.build(rand::rngs::OsRng).unwrap();

        let diem_root_key_path = config_path.join("mint.key");
        let serialized_keys = bcs::to_bytes(&root_keys.root_key).unwrap();
        let mut key_file = std::fs::File::create(&diem_root_key_path).unwrap();
        key_file.write_all(&serialized_keys).unwrap();

        let mut log_file = config_path;
        log_file.push("validator.log");
        let log = File::create(log_file.as_path()).unwrap();

        let child_pid = std::process::Command::new("cargo")
            .args(&[
                "run",
                "--bin",
                "diem-node",
                "--",
                "--config",
                validators[0].config_path().to_str().unwrap(),
            ])
            .current_dir(diem_root_folder())
            .stderr(log)
            .stdout(Stdio::null())
            .spawn()
            .unwrap()
            .id();

        let config = NodeConfig::load(validators[0].config_path()).unwrap();
        let json_rpc = format!("http://localhost:{}", config.json_rpc.address.port());

        wait_till_healthy(json_rpc.as_str());

        NodeInfo {
            chain_id: ChainId::test(),
            json_rpc,
            root_key_path: diem_root_key_path,
            waypoint: genesis_waypoint,
            local_node_info: Some(LocalNodeInfo {
                log_path: log_file,
                config_path: config_temp_path,
                node_pid: Pid::from_raw(child_pid as i32),
            }),
        }
    }

    pub fn get_client(&self) -> ClientProxy {
        let root_key = self.root_key_path.to_str().unwrap();
        ClientProxy::new(
            self.chain_id,
            self.json_rpc.as_str(),
            root_key,
            root_key,
            root_key,
            false,
            None,
            None,
            self.waypoint,
            false,
        )
        .expect("Failed to spawn a client from the NodeInfo")
    }
}

impl Drop for LocalNodeInfo {
    fn drop(&mut self) {
        kill(self.node_pid, Some(SIGKILL)).unwrap();
    }
}

impl fmt::Display for NodeInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "json-rpc endpoint: {}", self.json_rpc)?;
        writeln!(f, "waypoint: {}", self.waypoint)?;
        writeln!(f, "chain-id: {}", self.chain_id)?;
        writeln!(f, "root-key path: {:?}", self.root_key_path)?;
        if let Some(info) = &self.local_node_info {
            writeln!(f, "config path: {:?}", info.config_path.path())?;
            writeln!(f, "log path: {:?}", info.log_path)?;
        }
        Ok(())
    }
}