-
Notifications
You must be signed in to change notification settings - Fork 11
/
spawner.rs
164 lines (149 loc) Β· 5.71 KB
/
spawner.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::path::PathBuf;
use provider::{
constants::LOCALHOST,
types::{SpawnNodeOptions, TransferedFile},
DynNamespace,
};
use support::fs::FileSystem;
use crate::{
generators,
network::node::NetworkNode,
network_spec::{node::NodeSpec, parachain::ParachainSpec},
ScopedFilesystem, ZombieRole,
};
#[derive(Clone)]
pub struct SpawnNodeCtx<'a, T: FileSystem> {
/// Relaychain id, from the chain-spec (e.g rococo_local_testnet)
pub(crate) chain_id: &'a str,
// Parachain id, from the chain-spec (e.g local_testnet)
pub(crate) parachain_id: Option<&'a str>,
/// Relaychain chain name (e.g rococo-local)
pub(crate) chain: &'a str,
/// Role of the node in the network
pub(crate) role: ZombieRole,
/// Ref to the namespace
pub(crate) ns: &'a DynNamespace,
/// Ref to an scoped filesystem (encapsulate fs actions inside the ns directory)
pub(crate) scoped_fs: &'a ScopedFilesystem<'a, T>,
/// Ref to a parachain (used to spawn collators)
pub(crate) parachain: Option<&'a ParachainSpec>,
/// The string represenation of the bootnode addres to pass to nodes
pub(crate) bootnodes_addr: &'a Vec<String>,
}
pub async fn spawn_node<'a, T>(
node: &NodeSpec,
mut files_to_inject: Vec<TransferedFile>,
ctx: &SpawnNodeCtx<'a, T>,
) -> Result<NetworkNode, anyhow::Error>
where
T: FileSystem,
{
let mut created_paths = vec![];
// Create and inject the keystore IFF
// - The node is validator in the relaychain
// - The node is collator (encoded as validator) and the parachain is cumulus_based
// (parachain_id) should be set then.
if node.is_validator && (ctx.parachain.is_none() || ctx.parachain_id.is_some()) {
// Generate keystore for node
let node_files_path = if let Some(para) = ctx.parachain {
para.id.to_string()
} else {
node.name.clone()
};
let key_filenames =
generators::generate_node_keystore(&node.accounts, &node_files_path, ctx.scoped_fs)
.await
.unwrap();
// Paths returned are relative to the base dir, we need to convert into
// fullpaths to inject them in the nodes.
let remote_keystore_chain_id = if let Some(id) = ctx.parachain_id {
id
} else {
ctx.chain_id
};
for key_filename in key_filenames {
let f = TransferedFile {
local_path: PathBuf::from(format!(
"{}/{}/{}",
ctx.ns.base_dir().to_string_lossy(),
node_files_path,
key_filename.to_string_lossy()
)),
remote_path: PathBuf::from(format!(
"/data/chains/{}/keystore/{}",
remote_keystore_chain_id,
key_filename.to_string_lossy()
)),
};
files_to_inject.push(f);
}
created_paths.push(PathBuf::from(format!(
"/data/chains/{}/keystore",
remote_keystore_chain_id
)));
}
let base_dir = format!("{}/{}", ctx.ns.base_dir().to_string_lossy(), &node.name);
let cfg_path = format!("{}/cfg", &base_dir);
let data_path = format!("{}/data", &base_dir);
let relay_data_path = format!("{}/relay-data", &base_dir);
let gen_opts = generators::GenCmdOptions {
relay_chain_name: ctx.chain,
cfg_path: &cfg_path, // TODO: get from provider/ns
data_path: &data_path, // TODO: get from provider
relay_data_path: &relay_data_path, // TODO: get from provider
use_wrapper: false, // TODO: get from provider
bootnode_addr: ctx.bootnodes_addr.clone(),
};
let (cmd, args) = match ctx.role {
// Collator should be `non-cumulus` one (e.g adder/undying)
ZombieRole::Node | ZombieRole::Collator => {
let maybe_para_id = ctx.parachain.map(|para| para.id);
generators::generate_node_command(node, gen_opts, maybe_para_id)
},
ZombieRole::CumulusCollator => {
let para = ctx.parachain.expect("parachain must be part of the context, this is a bug");
let full_p2p = generators::generate_node_port(None)?;
generators::generate_node_command_cumulus(node, gen_opts, para.id, full_p2p.0)
}
_ => unreachable!()
// TODO: do we need those?
// ZombieRole::Bootnode => todo!(),
// ZombieRole::Companion => todo!(),
};
println!("\n");
println!("π {}, spawning.... with command:", node.name);
println!("{cmd} {}", args.join(" "));
let spawn_ops = SpawnNodeOptions {
name: node.name.clone(),
command: cmd,
args,
env: node
.env
.iter()
.map(|env| (env.name.clone(), env.value.clone()))
.collect(),
injected_files: files_to_inject,
created_paths,
};
// Drops the port parking listeners before spawn
node.p2p_port.drop_listener();
node.rpc_port.drop_listener();
node.prometheus_port.drop_listener();
let running_node = ctx.ns.spawn_node(spawn_ops).await?;
let ws_uri = format!("ws://{}:{}", LOCALHOST, node.rpc_port.0);
let prometheus_uri = format!("http://{}:{}/metrics", LOCALHOST, node.prometheus_port.0);
println!("π {}, should be running now", node.name);
println!(
"π {} : direct link https://polkadot.js.org/apps/?rpc={ws_uri}#/explorer",
node.name
);
println!("π {} : metrics link {prometheus_uri}", node.name);
println!("\n");
Ok(NetworkNode::new(
node.name.clone(),
ws_uri,
prometheus_uri,
node.clone(),
running_node,
))
}