1use crate::{
4 benchmarking::{inherent_benchmark_data, RemarkBuilder},
5 cli::{Cli, RelayChainCli, Subcommand},
6};
7use common_primitives::node::Block;
8use cumulus_client_service::storage_proof_size::HostFunctions as ReclaimHostFunctions;
9use frame_benchmarking_cli::BenchmarkCmd;
10use frequency_service::{
11 chain_spec,
12 service::{frequency_runtime::VERSION, new_partial},
13};
14use sc_cli::{
15 ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams,
16 NetworkParams, Result, RuntimeVersion, SharedParams, SubstrateCli,
17};
18use sc_service::config::{BasePath, PrometheusConfig};
19use sp_runtime::traits::HashingFor;
20
21#[derive(Debug)]
22enum ChainIdentity {
23 Frequency,
24 FrequencyPaseo,
25 FrequencyLocal,
26 FrequencyDev,
27 FrequencyWestendLocal,
28 FrequencyWestend,
29}
30
31trait IdentifyChain {
32 fn identify(&self) -> ChainIdentity;
33}
34
35impl IdentifyChain for dyn sc_service::ChainSpec {
36 fn identify(&self) -> ChainIdentity {
37 if self.id() == "frequency" {
38 ChainIdentity::Frequency
39 } else if self.id() == "frequency-paseo" {
40 ChainIdentity::FrequencyPaseo
41 } else if self.id() == "frequency-local" {
42 ChainIdentity::FrequencyLocal
43 } else if self.id() == "dev" {
44 ChainIdentity::FrequencyDev
45 } else if self.id() == "frequency-westend-local" {
46 ChainIdentity::FrequencyWestendLocal
47 } else if self.id() == "frequency-westend" {
48 ChainIdentity::FrequencyWestend
49 } else {
50 panic!("Unknown chain identity")
51 }
52 }
53}
54
55impl PartialEq for ChainIdentity {
56 fn eq(&self, other: &Self) -> bool {
57 #[allow(clippy::match_like_matches_macro)]
58 match (self, other) {
59 (ChainIdentity::Frequency, ChainIdentity::Frequency) => true,
60 (ChainIdentity::FrequencyPaseo, ChainIdentity::FrequencyPaseo) => true,
61 (ChainIdentity::FrequencyLocal, ChainIdentity::FrequencyLocal) => true,
62 (ChainIdentity::FrequencyDev, ChainIdentity::FrequencyDev) => true,
63 (ChainIdentity::FrequencyWestendLocal, ChainIdentity::FrequencyWestendLocal) => true,
64 (ChainIdentity::FrequencyWestend, ChainIdentity::FrequencyWestend) => true,
65 _ => false,
66 }
67 }
68}
69
70impl<T: sc_service::ChainSpec + 'static> IdentifyChain for T {
71 fn identify(&self) -> ChainIdentity {
72 <dyn sc_service::ChainSpec>::identify(self)
73 }
74}
75
76fn load_spec(id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
77 match id {
78 #[cfg(feature = "runtime-benchmarks")]
79 "frequency-bench" => Ok(Box::new(chain_spec::frequency::benchmark_mainnet_config())),
80 #[cfg(feature = "frequency")]
81 "frequency" => Ok(Box::new(chain_spec::frequency::load_frequency_spec())),
82 #[cfg(feature = "frequency-no-relay")]
83 "dev" | "frequency-no-relay" => Ok(Box::new(chain_spec::frequency_dev::development_config())),
84 #[cfg(feature = "frequency-local")]
85 "frequency-paseo-local" =>
86 Ok(Box::new(chain_spec::frequency_paseo_local::local_paseo_testnet_config())),
87 #[cfg(feature = "frequency-bridging")]
88 "frequency-westend-local" =>
89 Ok(Box::new(chain_spec::frequency_westend_local::westend_local_config())),
90 #[cfg(feature = "frequency-testnet")]
91 "frequency-testnet" | "frequency-paseo" | "paseo" | "testnet" =>
92 Ok(Box::new(chain_spec::frequency_paseo::load_frequency_paseo_spec())),
93 #[cfg(feature = "frequency-westend")]
94 "frequency-westend" | "westend" =>
95 Ok(Box::new(chain_spec::frequency_westend::load_frequency_westend_spec())),
96 path => {
97 if path.is_empty() {
98 if cfg!(feature = "frequency") {
99 #[cfg(feature = "frequency")]
100 {
101 return Ok(Box::new(chain_spec::frequency::load_frequency_spec()));
102 }
103 #[cfg(not(feature = "frequency"))]
104 return Err("Frequency runtime is not available.".into());
105 } else if cfg!(feature = "frequency-no-relay") {
106 #[cfg(feature = "frequency-no-relay")]
107 {
108 return Ok(Box::new(chain_spec::frequency_dev::development_config()));
109 }
110 #[cfg(not(feature = "frequency-no-relay"))]
111 return Err("Frequency Development (no relay) runtime is not available.".into());
112 } else if cfg!(feature = "frequency-local") {
113 #[cfg(feature = "frequency-local")]
114 {
115 return Ok(Box::new(
116 chain_spec::frequency_paseo_local::local_paseo_testnet_config(),
117 ));
118 }
119 #[cfg(not(feature = "frequency-local"))]
120 return Err("Frequency Local runtime is not available.".into());
121 } else if cfg!(feature = "frequency-bridging") {
122 #[cfg(feature = "frequency-bridging")]
123 {
124 return Ok(Box::new(
125 chain_spec::frequency_westend_local::westend_local_config(),
126 ));
127 }
128 #[cfg(not(feature = "frequency-bridging"))]
129 return Err("Frequency Westend Local runtime is not available.".into());
130 } else if cfg!(feature = "frequency-testnet") {
131 #[cfg(feature = "frequency-testnet")]
132 {
133 return Ok(Box::new(
134 chain_spec::frequency_paseo::load_frequency_paseo_spec(),
135 ));
136 }
137 #[cfg(not(feature = "frequency-testnet"))]
138 return Err("Frequency Paseo runtime is not available.".into());
139 } else if cfg!(feature = "frequency-westend") {
140 #[cfg(feature = "frequency-westend")]
141 {
142 return Ok(Box::new(
143 chain_spec::frequency_westend::load_frequency_westend_spec(),
144 ));
145 }
146 #[cfg(not(feature = "frequency-westend"))]
147 return Err("Frequency Westend runtime is not available.".into());
148 } else {
149 return Err("No chain spec is available.".into());
150 }
151 }
152 let path_buf = std::path::PathBuf::from(path);
153 let spec = Box::new(chain_spec::DummyChainSpec::from_json_file(path_buf.clone())?)
154 as Box<dyn ChainSpec>;
155 if ChainIdentity::Frequency == spec.identify() {
156 #[cfg(feature = "frequency")]
157 {
158 Ok(Box::new(chain_spec::frequency::ChainSpec::from_json_file(path_buf)?))
159 }
160 #[cfg(not(feature = "frequency"))]
161 return Err("Frequency runtime is not available.".into());
162 } else if ChainIdentity::FrequencyPaseo == spec.identify() {
163 #[cfg(feature = "frequency-testnet")]
164 {
165 Ok(Box::new(chain_spec::frequency_paseo::ChainSpec::from_json_file(path_buf)?))
166 }
167 #[cfg(not(feature = "frequency-testnet"))]
168 return Err("Frequency Paseo runtime is not available.".into());
169 } else if ChainIdentity::FrequencyLocal == spec.identify() {
170 #[cfg(feature = "frequency-local")]
171 {
172 Ok(Box::new(chain_spec::frequency_paseo_local::ChainSpec::from_json_file(
173 path_buf,
174 )?))
175 }
176 #[cfg(not(feature = "frequency-local"))]
177 return Err("Frequency Local runtime is not available.".into());
178 } else if ChainIdentity::FrequencyWestendLocal == spec.identify() {
179 #[cfg(feature = "frequency-bridging")]
180 {
181 return Ok(Box::new(
182 chain_spec::frequency_westend_local::ChainSpec::from_json_file(path_buf)?,
183 ));
184 }
185 #[cfg(not(feature = "frequency-bridging"))]
186 return Err("Frequency Westend Local runtime is not available.".into());
187 } else if ChainIdentity::FrequencyDev == spec.identify() {
188 #[cfg(feature = "frequency-no-relay")]
189 {
190 Ok(Box::new(chain_spec::frequency_paseo_local::ChainSpec::from_json_file(
191 path_buf,
192 )?))
193 }
194 #[cfg(not(feature = "frequency-no-relay"))]
195 return Err("Frequency Dev (no relay) runtime is not available.".into());
196 } else if ChainIdentity::FrequencyWestend == spec.identify() {
197 #[cfg(feature = "frequency-westend")]
198 {
199 Ok(Box::new(chain_spec::frequency_westend::ChainSpec::from_json_file(
200 path_buf,
201 )?))
202 }
203 #[cfg(not(feature = "frequency-westend"))]
204 return Err("Frequency Westend runtime is not available.".into());
205 } else {
206 Err("Unknown chain spec.".into())
207 }
208 },
209 }
210}
211
212fn chain_name() -> String {
213 "Frequency".into()
214}
215
216impl SubstrateCli for Cli {
217 fn impl_name() -> String {
218 format!("{} Node", chain_name())
219 }
220
221 fn impl_version() -> String {
222 env!("SUBSTRATE_CLI_IMPL_VERSION").into()
223 }
224
225 fn description() -> String {
226 "Frequency\n\nThe command-line arguments provided first will be \
227 passed to the parachain node, while the arguments provided after -- will be passed \
228 to the relay chain node.\n\n\
229 frequency <parachain-args> -- <relay-chain-args>"
230 .into()
231 }
232
233 fn author() -> String {
234 env!("CARGO_PKG_AUTHORS").into()
235 }
236
237 fn support_url() -> String {
238 "https://github.com/frequency-chain/frequency/issues/new".into()
239 }
240
241 fn copyright_start_year() -> i32 {
242 2020
243 }
244
245 fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
246 load_spec(id)
247 }
248}
249
250impl Cli {
251 fn runtime_version() -> &'static RuntimeVersion {
253 &VERSION
254 }
255}
256
257impl SubstrateCli for RelayChainCli {
258 fn impl_name() -> String {
259 "Frequency".into()
260 }
261
262 fn impl_version() -> String {
263 env!("SUBSTRATE_CLI_IMPL_VERSION").into()
264 }
265
266 fn description() -> String {
267 "Frequency\n\nThe command-line arguments provided first will be \
268 passed to the parachain node, while the arguments provided after -- will be passed \
269 to the relay chain node.\n\n\
270 frequency <parachain-args> -- <relay-chain-args>"
271 .into()
272 }
273
274 fn author() -> String {
275 env!("CARGO_PKG_AUTHORS").into()
276 }
277
278 fn support_url() -> String {
279 "https://github.com/paritytech/cumulus/issues/new".into()
280 }
281
282 fn copyright_start_year() -> i32 {
283 2020
284 }
285
286 fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
287 match id {
288 #[cfg(feature = "frequency-local")]
290 "paseo-local" => Ok(Box::new(chain_spec::frequency_paseo_local::load_paseo_local_spec())),
291 #[cfg(feature = "frequency-testnet")]
293 "paseo" => Ok(Box::new(chain_spec::frequency_paseo::load_paseo_spec())),
294 _ => polkadot_cli::Cli::from_iter([RelayChainCli::executable_name()].iter())
295 .load_spec(id),
296 }
297 }
298}
299
300macro_rules! construct_async_run {
301 (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
302 let runner = $cli.create_runner($cmd)?;
303 runner.async_run(|$config| {
304 let $components = new_partial(&$config, false, None)?;
305 let task_manager = $components.task_manager;
306 { $( $code )* }.map(|v| (v, task_manager))
307 })
308 }}
309}
310
311#[allow(clippy::result_large_err)]
313pub fn run() -> Result<()> {
314 let cli = Cli::from_args();
315
316 match &cli.subcommand {
317 Some(Subcommand::Key(cmd)) => cmd.run(&cli),
318 Some(Subcommand::BuildSpec(cmd)) => {
319 let runner = cli.create_runner(cmd)?;
320 runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
321 },
322 Some(Subcommand::CheckBlock(cmd)) => {
323 construct_async_run!(|components, cli, cmd, config| {
324 Ok(cmd.run(components.client, components.import_queue))
325 })
326 },
327 Some(Subcommand::ExportBlocks(cmd)) => {
328 construct_async_run!(|components, cli, cmd, config| {
329 Ok(cmd.run(components.client, config.database))
330 })
331 },
332 Some(Subcommand::ExportState(cmd)) => {
333 construct_async_run!(|components, cli, cmd, config| {
334 Ok(cmd.run(components.client, config.chain_spec))
335 })
336 },
337 Some(Subcommand::ImportBlocks(cmd)) => {
338 construct_async_run!(|components, cli, cmd, config| {
339 Ok(cmd.run(components.client, components.import_queue))
340 })
341 },
342 Some(Subcommand::ExportMetadata(cmd)) => {
343 construct_async_run!(|components, cli, cmd, config| Ok(cmd.run(components.client)))
344 },
345 Some(Subcommand::PurgeChain(cmd)) => {
346 let runner = cli.create_runner(cmd)?;
347
348 runner.sync_run(|config| {
349 let polkadot_cli = RelayChainCli::new(
350 &config,
351 [RelayChainCli::executable_name()].iter().chain(cli.relay_chain_args.iter()),
352 );
353
354 let polkadot_config = SubstrateCli::create_configuration(
355 &polkadot_cli,
356 &polkadot_cli,
357 config.tokio_handle.clone(),
358 )
359 .map_err(|err| format!("Relay chain argument error: {err:?}"))?;
360
361 cmd.run(config, polkadot_config)
362 })
363 },
364 Some(Subcommand::Revert(cmd)) => {
365 construct_async_run!(|components, cli, cmd, config| {
366 Ok(cmd.run(components.client, components.backend, None))
367 })
368 },
369 Some(Subcommand::ExportGenesisHead(cmd)) => {
370 let runner = cli.create_runner(cmd)?;
371 runner.sync_run(|config| {
372 let partials = new_partial(&config, false, None)?;
373
374 cmd.run(partials.client)
375 })
376 },
377 Some(Subcommand::ExportGenesisWasm(cmd)) => {
378 let runner = cli.create_runner(cmd)?;
379 runner.sync_run(|_config| {
380 let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
381 cmd.run(&*spec)
382 })
383 },
384 Some(Subcommand::Benchmark(cmd)) => {
385 let runner = cli.create_runner(cmd)?;
386
387 match cmd {
388 BenchmarkCmd::Pallet(cmd) =>
389 if cfg!(feature = "runtime-benchmarks") {
390 runner.sync_run(|config| {
391 cmd.run_with_spec::<HashingFor<Block>, ReclaimHostFunctions>(Some(
392 config.chain_spec,
393 ))
394 })
395 } else {
396 Err("Benchmarking wasn't enabled when building the node. \
397 You can enable it with `--features runtime-benchmarks`."
398 .into())
399 },
400 BenchmarkCmd::Block(cmd) => runner.sync_run(|config| {
401 let partials = new_partial(&config, false, None)?;
402 cmd.run(partials.client)
403 }),
404 #[cfg(not(feature = "runtime-benchmarks"))]
405 BenchmarkCmd::Storage(_) =>
406 return Err(sc_cli::Error::Input(
407 "Compile with --features=runtime-benchmarks \
408 to enable storage benchmarks."
409 .into(),
410 )
411 .into()),
412 #[cfg(feature = "runtime-benchmarks")]
413 BenchmarkCmd::Storage(cmd) => runner.sync_run(|config| {
414 let partials = new_partial(&config, false, None)?;
415 let db = partials.backend.expose_db();
416 let storage = partials.backend.expose_storage();
417 let shared_cache = partials.backend.expose_shared_trie_cache();
418 cmd.run(config, partials.client.clone(), db, storage, shared_cache)
419 }),
420 BenchmarkCmd::Overhead(cmd) => runner.sync_run(|config| {
421 let partials = new_partial(&config, false, None)?;
422 let ext_builder = RemarkBuilder::new(partials.client.clone());
423 let should_record_proof = false;
424
425 cmd.run(
426 chain_name(),
427 partials.client,
428 inherent_benchmark_data()?,
429 Vec::new(),
430 &ext_builder,
431 should_record_proof,
432 )
433 }),
434 BenchmarkCmd::Machine(cmd) => runner.sync_run(|config| {
435 cmd.run(&config, frame_benchmarking_cli::SUBSTRATE_REFERENCE_HARDWARE.clone())
436 }),
437 BenchmarkCmd::Extrinsic(_cmd) =>
438 Err("Benchmarking command not implemented.".into()),
439 }
440 },
441
442 Some(Subcommand::ExportRuntimeVersion(cmd)) => {
443 let runner = cli.create_runner(cmd)?;
444
445 runner.async_run(|config| {
446 let version = Cli::runtime_version();
447 let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry);
449 let task_manager =
450 sc_service::TaskManager::new(config.tokio_handle.clone(), registry)
451 .map_err(|e| format!("Error: {e:?}"))?;
452 Ok((cmd.run(version), task_manager))
453 })
454 },
455 None => run_chain(cli),
456 }
457}
458
459#[allow(clippy::result_large_err)]
461pub fn run_chain(cli: Cli) -> sc_service::Result<(), sc_cli::Error> {
462 #[allow(unused)]
463 let mut result: sc_service::Result<(), polkadot_cli::Error> = Ok(());
464 #[cfg(feature = "frequency-no-relay")]
465 {
466 result = crate::run_as_localchain::run_as_localchain(cli);
467 }
468 #[cfg(not(feature = "frequency-no-relay"))]
469 {
470 result = crate::run_as_parachain::run_as_parachain(cli);
471 }
472
473 result
474}
475
476impl DefaultConfigurationValues for RelayChainCli {
477 fn p2p_listen_port() -> u16 {
478 30334
479 }
480
481 fn rpc_listen_port() -> u16 {
482 9945
483 }
484
485 fn prometheus_listen_port() -> u16 {
486 9616
487 }
488}
489
490impl CliConfiguration<Self> for RelayChainCli {
491 fn shared_params(&self) -> &SharedParams {
492 self.base.base.shared_params()
493 }
494
495 fn import_params(&self) -> Option<&ImportParams> {
496 self.base.base.import_params()
497 }
498
499 fn network_params(&self) -> Option<&NetworkParams> {
500 self.base.base.network_params()
501 }
502
503 fn keystore_params(&self) -> Option<&KeystoreParams> {
504 self.base.base.keystore_params()
505 }
506
507 fn base_path(&self) -> Result<Option<BasePath>> {
508 Ok(self
509 .shared_params()
510 .base_path()?
511 .or_else(|| self.base_path.clone().map(Into::into)))
512 }
513
514 fn prometheus_config(
515 &self,
516 default_listen_port: u16,
517 chain_spec: &Box<dyn ChainSpec>,
518 ) -> Result<Option<PrometheusConfig>> {
519 self.base.base.prometheus_config(default_listen_port, chain_spec)
520 }
521
522 fn init<F>(&self, _support_url: &String, _impl_version: &String, _logger_hook: F) -> Result<()>
523 where
524 F: FnOnce(&mut sc_cli::LoggerBuilder),
525 {
526 unreachable!("PolkadotCli is never initialized; qed");
527 }
528
529 fn chain_id(&self, is_dev: bool) -> Result<String> {
530 let chain_id = self.base.base.chain_id(is_dev)?;
531
532 Ok(if chain_id.is_empty() { self.chain_id.clone().unwrap_or_default() } else { chain_id })
533 }
534
535 fn role(&self, is_dev: bool) -> Result<sc_service::Role> {
536 self.base.base.role(is_dev)
537 }
538
539 fn transaction_pool(&self, is_dev: bool) -> Result<sc_service::config::TransactionPoolOptions> {
540 self.base.base.transaction_pool(is_dev)
541 }
542
543 fn trie_cache_maximum_size(&self) -> Result<Option<usize>> {
544 self.base.base.trie_cache_maximum_size()
545 }
546
547 fn rpc_methods(&self) -> Result<sc_service::config::RpcMethods> {
548 self.base.base.rpc_methods()
549 }
550
551 fn rpc_cors(&self, is_dev: bool) -> Result<Option<Vec<String>>> {
552 self.base.base.rpc_cors(is_dev)
553 }
554
555 fn default_heap_pages(&self) -> Result<Option<u64>> {
556 self.base.base.default_heap_pages()
557 }
558
559 fn disable_grandpa(&self) -> Result<bool> {
560 self.base.base.disable_grandpa()
561 }
562
563 fn max_runtime_instances(&self) -> Result<Option<usize>> {
564 self.base.base.max_runtime_instances()
565 }
566
567 fn announce_block(&self) -> Result<bool> {
568 self.base.base.announce_block()
569 }
570
571 fn telemetry_endpoints(
572 &self,
573 chain_spec: &Box<dyn ChainSpec>,
574 ) -> Result<Option<sc_telemetry::TelemetryEndpoints>> {
575 self.base.base.telemetry_endpoints(chain_spec)
576 }
577
578 fn node_name(&self) -> Result<String> {
579 self.base.base.node_name()
580 }
581}