1use crate::{
20 error::{Error, Result},
21 params::{
22 ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, RpcEndpoint,
23 SharedParams, TransactionPoolParams,
24 },
25 CliConfiguration, PrometheusParams, RpcParams, RuntimeParams, TelemetryParams,
26};
27use clap::Parser;
28use regex::Regex;
29use sc_service::{
30 config::{
31 BasePath, IpNetwork, PrometheusConfig, RpcBatchRequestConfig, TransactionPoolOptions,
32 },
33 ChainSpec, Role,
34};
35use sc_telemetry::TelemetryEndpoints;
36use std::num::NonZeroU32;
37
38#[derive(Debug, Clone, Parser)]
40pub struct RunCmd {
41 #[arg(long)]
47 pub validator: bool,
48
49 #[arg(long)]
54 pub no_grandpa: bool,
55
56 #[arg(long, value_name = "NAME")]
60 pub name: Option<String>,
61
62 #[allow(missing_docs)]
63 #[clap(flatten)]
64 pub rpc_params: RpcParams,
65
66 #[allow(missing_docs)]
67 #[clap(flatten)]
68 pub telemetry_params: TelemetryParams,
69
70 #[allow(missing_docs)]
71 #[clap(flatten)]
72 pub prometheus_params: PrometheusParams,
73
74 #[allow(missing_docs)]
75 #[clap(flatten)]
76 pub runtime_params: RuntimeParams,
77
78 #[allow(missing_docs)]
79 #[clap(flatten)]
80 pub offchain_worker_params: OffchainWorkerParams,
81
82 #[allow(missing_docs)]
83 #[clap(flatten)]
84 pub shared_params: SharedParams,
85
86 #[allow(missing_docs)]
87 #[clap(flatten)]
88 pub import_params: ImportParams,
89
90 #[allow(missing_docs)]
91 #[clap(flatten)]
92 pub network_params: NetworkParams,
93
94 #[allow(missing_docs)]
95 #[clap(flatten)]
96 pub pool_config: TransactionPoolParams,
97
98 #[allow(missing_docs)]
99 #[clap(flatten)]
100 pub keystore_params: KeystoreParams,
101
102 #[arg(long, conflicts_with_all = &["bob", "charlie", "dave", "eve", "ferdie", "one", "two"])]
106 pub alice: bool,
107
108 #[arg(long, conflicts_with_all = &["alice", "charlie", "dave", "eve", "ferdie", "one", "two"])]
112 pub bob: bool,
113
114 #[arg(long, conflicts_with_all = &["alice", "bob", "dave", "eve", "ferdie", "one", "two"])]
118 pub charlie: bool,
119
120 #[arg(long, conflicts_with_all = &["alice", "bob", "charlie", "eve", "ferdie", "one", "two"])]
124 pub dave: bool,
125
126 #[arg(long, conflicts_with_all = &["alice", "bob", "charlie", "dave", "ferdie", "one", "two"])]
130 pub eve: bool,
131
132 #[arg(long, conflicts_with_all = &["alice", "bob", "charlie", "dave", "eve", "one", "two"])]
136 pub ferdie: bool,
137
138 #[arg(long, conflicts_with_all = &["alice", "bob", "charlie", "dave", "eve", "ferdie", "two"])]
142 pub one: bool,
143
144 #[arg(long, conflicts_with_all = &["alice", "bob", "charlie", "dave", "eve", "ferdie", "one"])]
148 pub two: bool,
149
150 #[arg(long)]
152 pub force_authoring: bool,
153
154 #[arg(long, conflicts_with = "base_path")]
164 pub tmp: bool,
165}
166
167impl RunCmd {
168 pub fn get_keyring(&self) -> Option<sp_keyring::Sr25519Keyring> {
170 use sp_keyring::Sr25519Keyring::*;
171
172 if self.alice {
173 Some(Alice)
174 } else if self.bob {
175 Some(Bob)
176 } else if self.charlie {
177 Some(Charlie)
178 } else if self.dave {
179 Some(Dave)
180 } else if self.eve {
181 Some(Eve)
182 } else if self.ferdie {
183 Some(Ferdie)
184 } else if self.one {
185 Some(One)
186 } else if self.two {
187 Some(Two)
188 } else {
189 None
190 }
191 }
192}
193
194impl CliConfiguration for RunCmd {
195 fn shared_params(&self) -> &SharedParams {
196 &self.shared_params
197 }
198
199 fn import_params(&self) -> Option<&ImportParams> {
200 Some(&self.import_params)
201 }
202
203 fn network_params(&self) -> Option<&NetworkParams> {
204 Some(&self.network_params)
205 }
206
207 fn keystore_params(&self) -> Option<&KeystoreParams> {
208 Some(&self.keystore_params)
209 }
210
211 fn offchain_worker_params(&self) -> Option<&OffchainWorkerParams> {
212 Some(&self.offchain_worker_params)
213 }
214
215 fn node_name(&self) -> Result<String> {
216 let name: String = match (self.name.as_ref(), self.get_keyring()) {
217 (Some(name), _) => name.to_string(),
218 (_, Some(keyring)) => keyring.to_string(),
219 (None, None) => crate::generate_node_name(),
220 };
221
222 is_node_name_valid(&name).map_err(|msg| {
223 Error::Input(format!(
224 "Invalid node name '{}'. Reason: {}. If unsure, use none.",
225 name, msg
226 ))
227 })?;
228
229 Ok(name)
230 }
231
232 fn dev_key_seed(&self, is_dev: bool) -> Result<Option<String>> {
233 Ok(self.get_keyring().map(|a| format!("//{}", a)).or_else(|| {
234 if is_dev {
235 Some("//Alice".into())
236 } else {
237 None
238 }
239 }))
240 }
241
242 fn telemetry_endpoints(
243 &self,
244 chain_spec: &Box<dyn ChainSpec>,
245 ) -> Result<Option<TelemetryEndpoints>> {
246 let params = &self.telemetry_params;
247 Ok(if params.no_telemetry {
248 None
249 } else if !params.telemetry_endpoints.is_empty() {
250 Some(
251 TelemetryEndpoints::new(params.telemetry_endpoints.clone())
252 .map_err(|e| e.to_string())?,
253 )
254 } else {
255 chain_spec.telemetry_endpoints().clone()
256 })
257 }
258
259 fn role(&self, is_dev: bool) -> Result<Role> {
260 let keyring = self.get_keyring();
261 let is_authority = self.validator || is_dev || keyring.is_some();
262
263 Ok(if is_authority { Role::Authority } else { Role::Full })
264 }
265
266 fn force_authoring(&self) -> Result<bool> {
267 Ok(self.shared_params.dev || self.force_authoring)
269 }
270
271 fn prometheus_config(
272 &self,
273 default_listen_port: u16,
274 chain_spec: &Box<dyn ChainSpec>,
275 ) -> Result<Option<PrometheusConfig>> {
276 Ok(self
277 .prometheus_params
278 .prometheus_config(default_listen_port, chain_spec.id().to_string()))
279 }
280
281 fn disable_grandpa(&self) -> Result<bool> {
282 Ok(self.no_grandpa)
283 }
284
285 fn rpc_max_connections(&self) -> Result<u32> {
286 Ok(self.rpc_params.rpc_max_connections)
287 }
288
289 fn rpc_cors(&self, is_dev: bool) -> Result<Option<Vec<String>>> {
290 self.rpc_params.rpc_cors(is_dev)
291 }
292
293 fn rpc_addr(&self, default_listen_port: u16) -> Result<Option<Vec<RpcEndpoint>>> {
294 self.rpc_params.rpc_addr(self.is_dev()?, self.validator, default_listen_port)
295 }
296
297 fn rpc_methods(&self) -> Result<sc_service::config::RpcMethods> {
298 Ok(self.rpc_params.rpc_methods.into())
299 }
300
301 fn rpc_max_request_size(&self) -> Result<u32> {
302 Ok(self.rpc_params.rpc_max_request_size)
303 }
304
305 fn rpc_max_response_size(&self) -> Result<u32> {
306 Ok(self.rpc_params.rpc_max_response_size)
307 }
308
309 fn rpc_max_subscriptions_per_connection(&self) -> Result<u32> {
310 Ok(self.rpc_params.rpc_max_subscriptions_per_connection)
311 }
312
313 fn rpc_buffer_capacity_per_connection(&self) -> Result<u32> {
314 Ok(self.rpc_params.rpc_message_buffer_capacity_per_connection)
315 }
316
317 fn rpc_batch_config(&self) -> Result<RpcBatchRequestConfig> {
318 self.rpc_params.rpc_batch_config()
319 }
320
321 fn rpc_rate_limit(&self) -> Result<Option<NonZeroU32>> {
322 Ok(self.rpc_params.rpc_rate_limit)
323 }
324
325 fn rpc_rate_limit_whitelisted_ips(&self) -> Result<Vec<IpNetwork>> {
326 Ok(self.rpc_params.rpc_rate_limit_whitelisted_ips.clone())
327 }
328
329 fn rpc_rate_limit_trust_proxy_headers(&self) -> Result<bool> {
330 Ok(self.rpc_params.rpc_rate_limit_trust_proxy_headers)
331 }
332
333 fn transaction_pool(&self, is_dev: bool) -> Result<TransactionPoolOptions> {
334 Ok(self.pool_config.transaction_pool(is_dev))
335 }
336
337 fn max_runtime_instances(&self) -> Result<Option<usize>> {
338 Ok(Some(self.runtime_params.max_runtime_instances))
339 }
340
341 fn runtime_cache_size(&self) -> Result<u8> {
342 Ok(self.runtime_params.runtime_cache_size)
343 }
344
345 fn base_path(&self) -> Result<Option<BasePath>> {
346 Ok(if self.tmp {
347 Some(BasePath::new_temp_dir()?)
348 } else {
349 match self.shared_params().base_path()? {
350 Some(r) => Some(r),
351 None if self.shared_params().is_dev() => Some(BasePath::new_temp_dir()?),
353 None => None,
354 }
355 })
356 }
357}
358
359pub fn is_node_name_valid(_name: &str) -> std::result::Result<(), &str> {
361 let name = _name.to_string();
362
363 if name.is_empty() {
364 return Err("Node name cannot be empty");
365 }
366
367 if name.chars().count() >= crate::NODE_NAME_MAX_LENGTH {
368 return Err("Node name too long");
369 }
370
371 let invalid_chars = r"[\\.@]";
372 let re = Regex::new(invalid_chars).unwrap();
373 if re.is_match(&name) {
374 return Err("Node name should not contain invalid chars such as '.' and '@'");
375 }
376
377 let invalid_patterns = r"^https?:";
378 let re = Regex::new(invalid_patterns).unwrap();
379 if re.is_match(&name) {
380 return Err("Node name should not contain urls");
381 }
382
383 Ok(())
384}
385
386#[cfg(test)]
387mod tests {
388 use super::*;
389
390 #[test]
391 fn tests_node_name_good() {
392 assert!(is_node_name_valid("short name").is_ok());
393 assert!(is_node_name_valid("www").is_ok());
394 assert!(is_node_name_valid("aawww").is_ok());
395 assert!(is_node_name_valid("wwwaa").is_ok());
396 assert!(is_node_name_valid("www aa").is_ok());
397 }
398
399 #[test]
400 fn tests_node_name_bad() {
401 assert!(is_node_name_valid("").is_err());
402 assert!(is_node_name_valid(
403 "very very long names are really not very cool for the ui at all, really they're not"
404 )
405 .is_err());
406 assert!(is_node_name_valid("Dots.not.Ok").is_err());
407 assert!(is_node_name_valid("http://visitme").is_err());
410 assert!(is_node_name_valid("http:/visitme").is_err());
411 assert!(is_node_name_valid("http:visitme").is_err());
412 assert!(is_node_name_valid("https://visitme").is_err());
413 assert!(is_node_name_valid("https:/visitme").is_err());
414 assert!(is_node_name_valid("https:visitme").is_err());
415 assert!(is_node_name_valid("www.visit.me").is_err());
416 assert!(is_node_name_valid("www.visit").is_err());
417 assert!(is_node_name_valid("hello\\world").is_err());
418 assert!(is_node_name_valid("visit.www").is_err());
419 assert!(is_node_name_valid("email@domain").is_err());
420 }
421}