/
testing_utils.rs
122 lines (108 loc) · 3.56 KB
/
testing_utils.rs
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
use std::net::SocketAddr;
use std::panic;
use std::path::PathBuf;
use std::process::Output;
use std::str::FromStr;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use client::Client;
use configs::chain_spec::{read_or_default_chain_spec, ChainSpec};
use configs::network::get_peer_id_from_seed;
use configs::ClientConfig;
use configs::NetworkConfig;
use configs::RPCConfig;
use primitives::network::PeerInfo;
use primitives::signature::SecretKey;
use primitives::test_utils::get_key_pair_from_seed;
use crate::start_from_client;
const TMP_DIR: &str = "../../tmp/testnet";
pub fn configure_chain_spec() -> ChainSpec {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("../configs/res/testnet_chain.json");
read_or_default_chain_spec(&Some(d))
}
pub struct Node {
pub client: Arc<Client>,
pub node_info: PeerInfo,
pub client_cfg: ClientConfig,
pub network_cfg: NetworkConfig,
pub rpc_cfg: RPCConfig,
pub secret_key: SecretKey,
}
impl Node {
pub fn new(
name: &str,
account_id: &str,
peer_id_seed: u32,
addr: &str,
rpc_port: u16,
boot_nodes: Vec<PeerInfo>,
chain_spec: ChainSpec,
) -> Self {
let node_info = PeerInfo {
account_id: Some(String::from(account_id)),
id: get_peer_id_from_seed(peer_id_seed),
addr: SocketAddr::from_str(addr).unwrap(),
};
let mut base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
base_path.push(TMP_DIR);
base_path.push(name);
if base_path.exists() {
std::fs::remove_dir_all(base_path.clone()).unwrap();
}
let client_cfg = ClientConfig {
base_path,
account_id: String::from(account_id),
public_key: None,
chain_spec,
log_level: log::LevelFilter::Info,
};
let network_cfg = NetworkConfig {
listen_addr: node_info.addr,
peer_id: node_info.id,
boot_nodes,
reconnect_delay: Duration::from_millis(50),
gossip_interval: Duration::from_millis(50),
tx_gossip_interval: Duration::from_millis(50),
gossip_sample_size: 10,
};
let rpc_cfg = RPCConfig { rpc_port };
let client = Arc::new(Client::new(&client_cfg));
let (_, secret_key) = get_key_pair_from_seed(account_id);
Node { client, node_info, client_cfg, network_cfg, rpc_cfg, secret_key }
}
pub fn start(&self) {
let client = self.client.clone();
let account_id = self.client_cfg.account_id.clone();
let network_cfg = self.network_cfg.clone();
let rpc_cfg = self.rpc_cfg.clone();
thread::spawn(|| {
start_from_client(client, Some(account_id), network_cfg, rpc_cfg);
});
thread::sleep(Duration::from_secs(1));
}
}
pub fn check_result(output: Output) -> Result<String, String> {
let mut result = String::from_utf8_lossy(output.stdout.as_slice());
if !output.status.success() {
if result.is_empty() {
result = String::from_utf8_lossy(output.stderr.as_slice());
}
return Err(result.to_owned().to_string());
}
Ok(result.to_owned().to_string())
}
pub fn wait<F>(f: F, check_interval_ms: u64, max_wait_ms: u64)
where
F: Fn() -> bool,
{
let mut ms_slept = 0;
while !f() {
thread::sleep(Duration::from_millis(check_interval_ms));
ms_slept += check_interval_ms;
if ms_slept > max_wait_ms {
panic!("Timed out waiting for the condition");
}
}
}