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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

//! This module constructs and executes the appropriate service components for the given subcommand

use crate::cli::{Cli, RelayChainCli, RunCmd, Subcommand};
use cumulus_client_cli::extract_genesis_wasm;
use cumulus_primitives_core::ParaId;
use frame_benchmarking_cli::BenchmarkCmd;
use log::{info, warn};
use moonbeam_cli_opt::EthApi;

#[cfg(feature = "moonbase-native")]
use moonbeam_service::moonbase_runtime;
#[cfg(feature = "moonbeam-native")]
use moonbeam_service::moonbeam_runtime;
#[cfg(feature = "moonriver-native")]
use moonbeam_service::moonriver_runtime;

use moonbeam_service::{chain_spec, frontier_database_dir, HostFunctions, IdentifyVariant};
use parity_scale_codec::Encode;
#[cfg(feature = "westend-native")]
use polkadot_service::WestendChainSpec;
use sc_cli::{
	ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams,
	NetworkParams, Result, RuntimeVersion, SharedParams, SubstrateCli,
};
use sc_service::{
	config::{BasePath, PrometheusConfig},
	DatabaseSource, PartialComponents,
};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::{
	traits::{
		AccountIdConversion, Block as BlockT, Hash as HashT, HashingFor, Header as HeaderT, Zero,
	},
	StateVersion,
};
use std::{io::Write, net::SocketAddr};

fn load_spec(
	id: &str,
	para_id: ParaId,
	run_cmd: &RunCmd,
) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
	Ok(match id {
		// Moonbase networks
		"moonbase-alpha" | "alphanet" => Box::new(chain_spec::RawChainSpec::from_json_bytes(
			&include_bytes!("../../../specs/alphanet/parachain-embedded-specs-v8.json")[..],
		)?),
		#[cfg(feature = "moonbase-native")]
		"moonbase-local" => Box::new(chain_spec::moonbase::get_chain_spec(para_id)),
		#[cfg(feature = "moonbase-native")]
		"moonbase-dev" | "dev" | "development" => {
			Box::new(chain_spec::moonbase::development_chain_spec(None, None))
		}
		#[cfg(all(feature = "test-spec", feature = "moonbeam-native"))]
		"staking" => Box::new(chain_spec::test_spec::staking_spec(para_id)),
		// Moonriver networks
		"moonriver" => Box::new(chain_spec::RawChainSpec::from_json_bytes(
			&include_bytes!("../../../specs/moonriver/parachain-embedded-specs.json")[..],
		)?),
		#[cfg(feature = "moonriver-native")]
		"moonriver-dev" => Box::new(chain_spec::moonriver::development_chain_spec(None, None)),
		#[cfg(feature = "moonriver-native")]
		"moonriver-local" => Box::new(chain_spec::moonriver::get_chain_spec(para_id)),

		// Moonbeam networks
		"moonbeam" | "" => Box::new(chain_spec::RawChainSpec::from_json_bytes(
			&include_bytes!("../../../specs/moonbeam/parachain-embedded-specs.json")[..],
		)?),
		#[cfg(feature = "moonbeam-native")]
		"moonbeam-dev" => Box::new(chain_spec::moonbeam::development_chain_spec(None, None)),
		#[cfg(feature = "moonbeam-native")]
		"moonbeam-local" => Box::new(chain_spec::moonbeam::get_chain_spec(para_id)),

		// Specs provided as json specify which runtime to use in their file name. For example,
		// `moonbeam-custom.json` uses the moonbeam runtime.
		// `moonbase-dev-workshop.json` uses the moonbase runtime.
		// If no magic strings match, then the moonbase runtime is used by default.
		// TODO explore CLI options to make this nicer. eg `--force-moonriver-runtime`
		path => {
			let path = std::path::PathBuf::from(path);

			let starts_with = |prefix: &str| {
				path.file_name()
					.and_then(|f| f.to_str().map(|s| s.starts_with(&prefix)))
					.unwrap_or(false)
			};

			if run_cmd.force_moonbase || starts_with("moonbase") {
				Box::new(chain_spec::moonbase::ChainSpec::from_json_file(path)?)
			} else if run_cmd.force_moonriver || starts_with("moonriver") {
				Box::new(chain_spec::moonriver::ChainSpec::from_json_file(path)?)
			} else {
				Box::new(chain_spec::moonbeam::ChainSpec::from_json_file(path)?)
			}
		}
	})
}

impl SubstrateCli for Cli {
	fn impl_name() -> String {
		"Moonbeam Parachain Collator".into()
	}

	fn impl_version() -> String {
		env!("SUBSTRATE_CLI_IMPL_VERSION").into()
	}

	fn description() -> String {
		format!(
			"Moonbase Parachain Collator\n\nThe command-line arguments provided first will be \
		passed to the parachain node, while the arguments provided after -- will be passed \
		to the relaychain node.\n\n\
		{} [parachain-args] -- [relaychain-args]",
			Self::executable_name()
		)
	}

	fn author() -> String {
		env!("CARGO_PKG_AUTHORS").into()
	}

	fn support_url() -> String {
		"https://github.com/moonbeam-foundation/moonbeam/issues/new".into()
	}

	fn copyright_start_year() -> i32 {
		2019
	}

	fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
		load_spec(id, self.run.parachain_id.unwrap_or(1000).into(), &self.run)
	}
}

impl Cli {
	fn runtime_version(spec: &Box<dyn sc_service::ChainSpec>) -> &'static RuntimeVersion {
		match spec {
			#[cfg(feature = "moonriver-native")]
			spec if spec.is_moonriver() => return &moonbeam_service::moonriver_runtime::VERSION,
			#[cfg(feature = "moonbeam-native")]
			spec if spec.is_moonbeam() => return &moonbeam_service::moonbeam_runtime::VERSION,
			#[cfg(feature = "moonbase-native")]
			_ => return &moonbeam_service::moonbase_runtime::VERSION,
			#[cfg(not(feature = "moonbase-native"))]
			_ => panic!("invalid chain spec"),
		}
	}
}

impl SubstrateCli for RelayChainCli {
	fn impl_name() -> String {
		"Moonbeam Parachain Collator".into()
	}

	fn impl_version() -> String {
		env!("SUBSTRATE_CLI_IMPL_VERSION").into()
	}

	fn description() -> String {
		"Moonbeam Parachain Collator\n\nThe command-line arguments provided first will be \
		passed to the parachain node, while the arguments provided after -- will be passed \
		to the relaychain node.\n\n\
		parachain-collator [parachain-args] -- [relaychain-args]"
			.into()
	}

	fn author() -> String {
		env!("CARGO_PKG_AUTHORS").into()
	}

	fn support_url() -> String {
		"https://github.com/moonbeam-foundation/moonbeam/issues/new".into()
	}

	fn copyright_start_year() -> i32 {
		2019
	}

	fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
		match id {
			#[cfg(feature = "westend-native")]
			"westend_moonbase_relay_testnet" => Ok(Box::new(WestendChainSpec::from_json_bytes(
				&include_bytes!("../../../specs/alphanet/westend-embedded-specs-v8.json")[..],
			)?)),
			// If we are not using a moonbeam-centric pre-baked relay spec, then fall back to the
			// Polkadot service to interpret the id.
			_ => polkadot_cli::Cli::from_iter([RelayChainCli::executable_name()].iter())
				.load_spec(id),
		}
	}
}

fn validate_trace_environment(cli: &Cli) -> Result<()> {
	if (cli.run.ethapi.contains(&EthApi::Debug) || cli.run.ethapi.contains(&EthApi::Trace))
		&& cli
			.run
			.base
			.base
			.import_params
			.wasm_runtime_overrides
			.is_none()
	{
		return Err(
			"`debug` or `trace` namespaces requires `--wasm-runtime-overrides /path/to/overrides`."
				.into(),
		);
	}
	Ok(())
}

/// Parse command line arguments into service configuration.
pub fn run() -> Result<()> {
	let cli = Cli::from_args();
	let _ = validate_trace_environment(&cli)?;

	match &cli.subcommand {
		Some(Subcommand::BuildSpec(params)) => {
			let runner = cli.create_runner(&params.base)?;
			runner.sync_run(|config| {
				if params.mnemonic.is_some() || params.accounts.is_some() {
					if config.chain_spec.is_moonbeam() {
						params.base.run(
							Box::new(chain_spec::moonbeam::development_chain_spec(
								params.mnemonic.clone(),
								params.accounts,
							)),
							config.network,
						)
					} else if config.chain_spec.is_moonriver() {
						params.base.run(
							Box::new(chain_spec::moonriver::development_chain_spec(
								params.mnemonic.clone(),
								params.accounts,
							)),
							config.network,
						)
					} else {
						params.base.run(
							Box::new(chain_spec::moonbase::development_chain_spec(
								params.mnemonic.clone(),
								params.accounts,
							)),
							config.network,
						)
					}
				} else {
					params.base.run(config.chain_spec, config.network)
				}
			})
		}
		Some(Subcommand::CheckBlock(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			let rpc_config = cli.run.new_rpc_config();
			runner.async_run(|mut config| {
				let (client, _, import_queue, task_manager) =
					moonbeam_service::new_chain_ops(&mut config, &rpc_config)?;
				Ok((cmd.run(client, import_queue), task_manager))
			})
		}
		Some(Subcommand::ExportBlocks(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			let rpc_config = cli.run.new_rpc_config();
			runner.async_run(|mut config| {
				let (client, _, _, task_manager) =
					moonbeam_service::new_chain_ops(&mut config, &rpc_config)?;
				Ok((cmd.run(client, config.database), task_manager))
			})
		}
		Some(Subcommand::ExportState(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			let rpc_config = cli.run.new_rpc_config();
			runner.async_run(|mut config| {
				let (client, _, _, task_manager) =
					moonbeam_service::new_chain_ops(&mut config, &rpc_config)?;
				Ok((cmd.run(client, config.chain_spec), task_manager))
			})
		}
		Some(Subcommand::ImportBlocks(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			let rpc_config = cli.run.new_rpc_config();
			runner.async_run(|mut config| {
				let (client, _, import_queue, task_manager) =
					moonbeam_service::new_chain_ops(&mut config, &rpc_config)?;
				Ok((cmd.run(client, import_queue), task_manager))
			})
		}
		Some(Subcommand::PurgeChain(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			runner.sync_run(|config| {
				// Although the cumulus_client_cli::PurgeCommand will extract the relay chain id,
				// we need to extract it here to determine whether we are running the dev service.
				let extension = chain_spec::Extensions::try_get(&*config.chain_spec);
				let relay_chain_id = extension.map(|e| e.relay_chain.as_str());
				let dev_service = cli.run.dev_service || relay_chain_id == Some("dev-service");

				// Remove Frontier offchain db
				let frontier_database_config = match config.database {
					DatabaseSource::RocksDb { .. } => DatabaseSource::RocksDb {
						path: frontier_database_dir(&config, "db"),
						cache_size: 0,
					},
					DatabaseSource::ParityDb { .. } => DatabaseSource::ParityDb {
						path: frontier_database_dir(&config, "paritydb"),
					},
					_ => {
						return Err(format!("Cannot purge `{:?}` database", config.database).into())
					}
				};
				cmd.base.run(frontier_database_config)?;

				if dev_service {
					// base refers to the encapsulated "regular" sc_cli::PurgeChain command
					return cmd.base.run(config.database);
				}

				let polkadot_cli = RelayChainCli::new(
					&config,
					[RelayChainCli::executable_name().to_string()]
						.iter()
						.chain(cli.relaychain_args.iter()),
				);

				let polkadot_config = SubstrateCli::create_configuration(
					&polkadot_cli,
					&polkadot_cli,
					config.tokio_handle.clone(),
				)
				.map_err(|err| format!("Relay chain argument error: {}", err))?;

				cmd.run(config, polkadot_config)
			})
		}
		Some(Subcommand::Revert(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			let chain_spec = &runner.config().chain_spec;
			let rpc_config = cli.run.new_rpc_config();
			match chain_spec {
				#[cfg(feature = "moonriver-native")]
				spec if spec.is_moonriver() => runner.async_run(|mut config| {
					let params = moonbeam_service::new_partial::<
						moonbeam_service::moonriver_runtime::RuntimeApi,
						moonbeam_service::MoonriverCustomizations,
					>(&mut config, &rpc_config, false)?;

					Ok((
						cmd.run(params.client, params.backend, None),
						params.task_manager,
					))
				}),
				#[cfg(feature = "moonbeam-native")]
				spec if spec.is_moonbeam() => runner.async_run(|mut config| {
					let params = moonbeam_service::new_partial::<
						moonbeam_service::moonbeam_runtime::RuntimeApi,
						moonbeam_service::MoonbeamCustomizations,
					>(&mut config, &rpc_config, false)?;

					Ok((
						cmd.run(params.client, params.backend, None),
						params.task_manager,
					))
				}),
				#[cfg(feature = "moonbase-native")]
				_ => runner.async_run(|mut config| {
					let params = moonbeam_service::new_partial::<
						moonbeam_service::moonbase_runtime::RuntimeApi,
						moonbeam_service::MoonbaseCustomizations,
					>(&mut config, &rpc_config, false)?;

					Ok((
						cmd.run(params.client, params.backend, None),
						params.task_manager,
					))
				}),
				#[cfg(not(feature = "moonbase-native"))]
				_ => panic!("invalid chain spec"),
			}
		}
		Some(Subcommand::ExportGenesisHead(params)) => {
			let mut builder = sc_cli::LoggerBuilder::new("");
			builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
			let _ = builder.init();

			// Cumulus approach here, we directly call the generic load_spec func
			let chain_spec = load_spec(
				params.chain.as_deref().unwrap_or_default(),
				params.parachain_id.unwrap_or(1000).into(),
				&cli.run,
			)?;
			let state_version = Cli::runtime_version(&chain_spec).state_version();

			let output_buf = match chain_spec {
				#[cfg(feature = "moonriver-native")]
				chain_spec if chain_spec.is_moonriver() => {
					let block: moonbeam_service::moonriver_runtime::Block =
						generate_genesis_block(&*chain_spec, state_version)?;
					let raw_header = block.header().encode();
					let output_buf = if params.raw {
						raw_header
					} else {
						format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
					};
					output_buf
				}
				#[cfg(feature = "moonbeam-native")]
				chain_spec if chain_spec.is_moonbeam() => {
					let block: moonbeam_service::moonbeam_runtime::Block =
						generate_genesis_block(&*chain_spec, state_version)?;
					let raw_header = block.header().encode();
					let output_buf = if params.raw {
						raw_header
					} else {
						format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
					};
					output_buf
				}
				#[cfg(feature = "moonbase-native")]
				_ => {
					let block: moonbeam_service::moonbase_runtime::Block =
						generate_genesis_block(&*chain_spec, state_version)?;
					let raw_header = block.header().encode();
					let output_buf = if params.raw {
						raw_header
					} else {
						format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
					};
					output_buf
				}
				#[cfg(not(feature = "moonbase-native"))]
				_ => panic!("invalid chain spec"),
			};

			if let Some(output) = &params.output {
				std::fs::write(output, output_buf)?;
			} else {
				std::io::stdout().write_all(&output_buf)?;
			}

			Ok(())
		}
		Some(Subcommand::ExportGenesisWasm(params)) => {
			let mut builder = sc_cli::LoggerBuilder::new("");
			builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
			let _ = builder.init();

			let raw_wasm_blob = extract_genesis_wasm(
				&*cli.load_spec(params.chain.as_deref().unwrap_or_default())?,
			)?;
			let output_buf = if params.raw {
				raw_wasm_blob
			} else {
				format!("0x{:?}", HexDisplay::from(&raw_wasm_blob)).into_bytes()
			};

			if let Some(output) = &params.output {
				std::fs::write(output, output_buf)?;
			} else {
				std::io::stdout().write_all(&output_buf)?;
			}

			Ok(())
		}
		Some(Subcommand::Benchmark(cmd)) => {
			let runner = cli.create_runner(cmd)?;

			// Switch on the concrete benchmark sub-command
			match cmd {
				BenchmarkCmd::Pallet(cmd) => {
					if cfg!(feature = "runtime-benchmarks") {
						let chain_spec = &runner.config().chain_spec;
						match chain_spec {
							#[cfg(feature = "moonriver-native")]
							spec if spec.is_moonriver() => {
								return runner.sync_run(|config| {
									cmd.run_with_spec::<HashingFor<moonriver_runtime::Block>, HostFunctions>(
										Some(config.chain_spec),
									)
								})
							}
							#[cfg(feature = "moonbeam-native")]
							spec if spec.is_moonbeam() => {
								return runner.sync_run(|config| {
									cmd.run_with_spec::<HashingFor<moonbeam_runtime::Block>, HostFunctions>(
										Some(config.chain_spec),
									)
								})
							}
							#[cfg(feature = "moonbase-native")]
							_ => {
								return runner.sync_run(|config| {
									cmd.run_with_spec::<HashingFor<moonbase_runtime::Block>, HostFunctions>(
										Some(config.chain_spec),
									)
								})
							}
							#[cfg(not(feature = "moonbase-native"))]
							_ => panic!("invalid chain spec"),
						}
					} else if cfg!(feature = "moonbase-runtime-benchmarks") {
						#[cfg(feature = "moonbase-native")]
						return runner.sync_run(|config| {
							cmd.run_with_spec::<HashingFor<moonbeam_service::moonbase_runtime::Block>, HostFunctions>(
								Some(config.chain_spec),
							)
						});
						#[cfg(not(feature = "moonbase-native"))]
						panic!("Benchmarking wasn't enabled when building the node.");
					} else {
						Err("Benchmarking wasn't enabled when building the node. \
					You can enable it with `--features runtime-benchmarks`."
							.into())
					}
				}
				BenchmarkCmd::Block(cmd) => {
					let chain_spec = &runner.config().chain_spec;
					let rpc_config = cli.run.new_rpc_config();
					match chain_spec {
						#[cfg(feature = "moonriver-native")]
						spec if spec.is_moonriver() => {
							return runner.sync_run(|mut config| {
								let params = moonbeam_service::new_partial::<
									moonbeam_service::moonriver_runtime::RuntimeApi,
									moonbeam_service::MoonriverCustomizations,
								>(&mut config, &rpc_config, false)?;

								cmd.run(params.client)
							})
						}
						#[cfg(feature = "moonbeam-native")]
						spec if spec.is_moonbeam() => {
							return runner.sync_run(|mut config| {
								let params = moonbeam_service::new_partial::<
									moonbeam_service::moonbeam_runtime::RuntimeApi,
									moonbeam_service::MoonbeamCustomizations,
								>(&mut config, &rpc_config, false)?;

								cmd.run(params.client)
							})
						}
						#[cfg(feature = "moonbase-native")]
						_ => {
							return runner.sync_run(|mut config| {
								let params = moonbeam_service::new_partial::<
									moonbeam_service::moonbase_runtime::RuntimeApi,
									moonbeam_service::MoonbaseCustomizations,
								>(&mut config, &rpc_config, false)?;

								cmd.run(params.client)
							})
						}
						#[cfg(not(feature = "moonbase-native"))]
						_ => panic!("invalid chain spec"),
					}
				}
				#[cfg(not(feature = "runtime-benchmarks"))]
				BenchmarkCmd::Storage(_) => Err(
					"Storage benchmarking can be enabled with `--features runtime-benchmarks`."
						.into(),
				),
				#[cfg(feature = "runtime-benchmarks")]
				BenchmarkCmd::Storage(cmd) => {
					let chain_spec = &runner.config().chain_spec;
					let rpc_config = cli.run.new_rpc_config();
					match chain_spec {
						#[cfg(feature = "moonriver-native")]
						spec if spec.is_moonriver() => {
							return runner.sync_run(|mut config| {
								let params = moonbeam_service::new_partial::<
									moonbeam_service::moonriver_runtime::RuntimeApi,
									moonbeam_service::MoonriverCustomizations,
								>(&mut config, &rpc_config, false)?;

								let db = params.backend.expose_db();
								let storage = params.backend.expose_storage();

								cmd.run(config, params.client, db, storage)
							})
						}
						#[cfg(feature = "moonbeam-native")]
						spec if spec.is_moonbeam() => {
							return runner.sync_run(|mut config| {
								let params = moonbeam_service::new_partial::<
									moonbeam_service::moonbeam_runtime::RuntimeApi,
									moonbeam_service::MoonbeamCustomizations,
								>(&mut config, &rpc_config, false)?;

								let db = params.backend.expose_db();
								let storage = params.backend.expose_storage();

								cmd.run(config, params.client, db, storage)
							})
						}
						#[cfg(feature = "moonbase-native")]
						_ => {
							return runner.sync_run(|mut config| {
								let params = moonbeam_service::new_partial::<
									moonbeam_service::moonbase_runtime::RuntimeApi,
									moonbeam_service::MoonbaseCustomizations,
								>(&mut config, &rpc_config, false)?;

								let db = params.backend.expose_db();
								let storage = params.backend.expose_storage();

								cmd.run(config, params.client, db, storage)
							})
						}
						#[cfg(not(feature = "moonbase-native"))]
						_ => panic!("invalid chain spec"),
					}
				}
				BenchmarkCmd::Overhead(_) => Err("Unsupported benchmarking command".into()),
				BenchmarkCmd::Extrinsic(_) => Err("Unsupported benchmarking command".into()),
				BenchmarkCmd::Machine(cmd) => {
					return runner.sync_run(|config| {
						cmd.run(
							&config,
							frame_benchmarking_cli::SUBSTRATE_REFERENCE_HARDWARE.clone(),
						)
					});
				}
			}
		}
		Some(Subcommand::TryRuntime) => Err("The `try-runtime` subcommand has been migrated to a \
			standalone CLI (https://github.com/paritytech/try-runtime-cli). It is no longer \
			being maintained here and will be removed entirely some time after January 2024. \
			Please remove this subcommand from your runtime and use the standalone CLI."
			.into()),
		Some(Subcommand::Key(cmd)) => Ok(cmd.run(&cli)?),
		Some(Subcommand::PrecompileWasm(cmd)) => {
			let runner = cli.create_runner(cmd)?;
			let rpc_config = cli.run.new_rpc_config();
			runner.async_run(|mut config| match &config.chain_spec {
				#[cfg(feature = "moonriver-native")]
				spec if spec.is_moonriver() => {
					let PartialComponents {
						task_manager,
						backend,
						..
					} = moonbeam_service::new_partial::<
						moonbeam_service::moonriver_runtime::RuntimeApi,
						moonbeam_service::MoonriverCustomizations,
					>(&mut config, &rpc_config, false)?;

					Ok((cmd.run(backend, config.chain_spec), task_manager))
				}
				#[cfg(feature = "moonbeam-native")]
				spec if spec.is_moonbeam() => {
					let PartialComponents {
						task_manager,
						backend,
						..
					} = moonbeam_service::new_partial::<
						moonbeam_service::moonbeam_runtime::RuntimeApi,
						moonbeam_service::MoonbeamCustomizations,
					>(&mut config, &rpc_config, false)?;

					Ok((cmd.run(backend, config.chain_spec), task_manager))
				}
				#[cfg(feature = "moonbase-native")]
				_ => {
					let PartialComponents {
						task_manager,
						backend,
						..
					} = moonbeam_service::new_partial::<
						moonbeam_service::moonbase_runtime::RuntimeApi,
						moonbeam_service::MoonbaseCustomizations,
					>(&mut config, &rpc_config, false)?;

					Ok((cmd.run(backend, config.chain_spec), task_manager))
				}
				#[cfg(not(feature = "moonbase-native"))]
				_ => panic!("invalid chain spec"),
			})
		}
		None => {
			let runner = cli.create_runner(&(*cli.run).normalize())?;
			let collator_options = cli.run.collator_options();

			runner.run_node_until_exit(|mut config| async move {
				let hwbench = if !cli.run.no_hardware_benchmarks {
					config.database.path().map(|database_path| {
						let _ = std::fs::create_dir_all(&database_path);
						sc_sysinfo::gather_hwbench(Some(database_path))
					})
				} else {
					None
				};

				let extension = chain_spec::Extensions::try_get(&*config.chain_spec);

				let rpc_config = cli.run.new_rpc_config();

				// If dev service was requested, start up manual or instant seal.
				// Otherwise continue with the normal parachain node.
				// Dev service can be requested in two ways.
				// 1. by providing the --dev-service flag to the CLI
				// 2. by specifying "dev-service" in the chain spec's "relay-chain" field.
				// NOTE: the --dev flag triggers the dev service by way of number 2
				let relay_chain_id = extension.map(|e| e.relay_chain.as_str());
				let para_id = extension.map(|e| e.para_id);

				let dev_service = cli.run.dev_service
					|| config.chain_spec.is_dev()
					|| relay_chain_id == Some("dev-service");
				if dev_service {
					// When running the dev service, just use Alice's author inherent
					//TODO maybe make the --alice etc flags work here, and consider bringing back
					// the author-id flag. For now, this will work.
					let author_id = Some(chain_spec::get_from_seed::<nimbus_primitives::NimbusId>(
						"Alice",
					));

					return match &config.chain_spec {
						#[cfg(feature = "moonriver-native")]
						spec if spec.is_moonriver() => moonbeam_service::new_dev::<
							moonbeam_service::moonriver_runtime::RuntimeApi,
							moonbeam_service::MoonriverCustomizations,
							sc_network::NetworkWorker<_, _>,
						>(
							config,
							para_id,
							author_id,
							cli.run.sealing,
							rpc_config,
							hwbench,
						)
						.await
						.map_err(Into::into),
						#[cfg(feature = "moonbeam-native")]
						spec if spec.is_moonbeam() => moonbeam_service::new_dev::<
							moonbeam_service::moonbeam_runtime::RuntimeApi,
							moonbeam_service::MoonbeamCustomizations,
							sc_network::NetworkWorker<_, _>,
						>(
							config,
							para_id,
							author_id,
							cli.run.sealing,
							rpc_config,
							hwbench,
						)
						.await
						.map_err(Into::into),
						#[cfg(feature = "moonbase-native")]
						_ => moonbeam_service::new_dev::<
							moonbeam_service::moonbase_runtime::RuntimeApi,
							moonbeam_service::MoonbaseCustomizations,
							sc_network::NetworkWorker<_, _>,
						>(
							config,
							para_id,
							author_id,
							cli.run.sealing,
							rpc_config,
							hwbench,
						)
						.await
						.map_err(Into::into),
						#[cfg(not(feature = "moonbase-native"))]
						_ => panic!("invalid chain spec"),
					};
				}
				#[cfg(feature = "lazy-loading")]
				if let Some(fork_chain_from_rpc) = cli.run.fork_chain_from_rpc {
					let author_id = Some(chain_spec::get_from_seed::<nimbus_primitives::NimbusId>(
						"Alice",
					));

					let lazy_loading_config = moonbeam_cli_opt::LazyLoadingConfig {
						state_rpc: fork_chain_from_rpc,
						from_block: cli.run.block,
						state_overrides_path: cli.run.fork_state_overrides,
						runtime_override: cli.run.runtime_override,
					};

					let spec_builder =
						chain_spec::test_spec::lazy_loading_spec_builder(Default::default());
					config.chain_spec = Box::new(spec_builder.build());

					// TODO: create a tokio runtime inside offchain_worker thread (otherwise it will panic)
					// We just disable it for now, since it is not needed
					config.offchain_worker.enabled = false;

					return moonbeam_service::lazy_loading::new_lazy_loading_service::<
						moonbeam_runtime::RuntimeApi,
						moonbeam_service::MoonbeamCustomizations,
						sc_network::NetworkWorker<_, _>,
					>(
						config,
						author_id,
						cli.run.sealing,
						rpc_config,
						lazy_loading_config,
						hwbench,
					)
					.await
					.map_err(Into::into);
				}

				let polkadot_cli = RelayChainCli::new(
					&config,
					[RelayChainCli::executable_name().to_string()]
						.iter()
						.chain(cli.relaychain_args.iter()),
				);

				let para_id = extension.map(|e| e.para_id);
				let id = ParaId::from(cli.run.parachain_id.clone().or(para_id).unwrap_or(1000));

				let parachain_account =
					AccountIdConversion::<polkadot_primitives::v7::AccountId>::into_account_truncating(&id);

				let tokio_handle = config.tokio_handle.clone();
				let polkadot_config =
					SubstrateCli::create_configuration(&polkadot_cli, &polkadot_cli, tokio_handle)
						.map_err(|err| format!("Relay chain argument error: {}", err))?;

				info!("Parachain Account: {}", parachain_account);
				info!(
					"Is collating: {}",
					if config.role.is_authority() {
						"yes"
					} else {
						"no"
					}
				);

				if !rpc_config.relay_chain_rpc_urls.is_empty() && cli.relaychain_args.len() > 0 {
					warn!(
						"Detected relay chain node arguments together with \
					--relay-chain-rpc-url. This command starts a minimal Polkadot node that only \
					uses a network-related subset of all relay chain CLI options."
					);
				}

				match &config.chain_spec {
					#[cfg(feature = "moonriver-native")]
					spec if spec.is_moonriver() => moonbeam_service::start_node::<
						moonbeam_service::moonriver_runtime::RuntimeApi,
						moonbeam_service::MoonriverCustomizations,
					>(
						config,
						polkadot_config,
						collator_options,
						id,
						rpc_config,
						true,
						cli.run.block_authoring_duration,
						hwbench,
					)
					.await
					.map(|r| r.0)
					.map_err(Into::into),
					#[cfg(feature = "moonbeam-native")]
					spec if spec.is_moonbeam() => moonbeam_service::start_node::<
						moonbeam_service::moonbeam_runtime::RuntimeApi,
						moonbeam_service::MoonbeamCustomizations,
					>(
						config,
						polkadot_config,
						collator_options,
						id,
						rpc_config,
						true,
						cli.run.block_authoring_duration,
						hwbench,
					)
					.await
					.map(|r| r.0)
					.map_err(Into::into),
					#[cfg(feature = "moonbase-native")]
					_ => moonbeam_service::start_node::<
						moonbeam_service::moonbase_runtime::RuntimeApi,
						moonbeam_service::MoonbaseCustomizations,
					>(
						config,
						polkadot_config,
						collator_options,
						id,
						rpc_config,
						true,
						cli.run.block_authoring_duration,
						hwbench,
					)
					.await
					.map(|r| r.0)
					.map_err(Into::into),
					#[cfg(not(feature = "moonbase-native"))]
					_ => panic!("invalid chain spec"),
				}
			})
		}
	}
}

impl DefaultConfigurationValues for RelayChainCli {
	fn p2p_listen_port() -> u16 {
		30334
	}

	fn rpc_listen_port() -> u16 {
		9945
	}

	fn prometheus_listen_port() -> u16 {
		9616
	}
}

impl CliConfiguration<Self> for RelayChainCli {
	fn shared_params(&self) -> &SharedParams {
		self.base.base.shared_params()
	}

	fn import_params(&self) -> Option<&ImportParams> {
		self.base.base.import_params()
	}

	fn network_params(&self) -> Option<&NetworkParams> {
		self.base.base.network_params()
	}

	fn keystore_params(&self) -> Option<&KeystoreParams> {
		self.base.base.keystore_params()
	}

	fn base_path(&self) -> Result<Option<BasePath>> {
		Ok(self
			.shared_params()
			.base_path()?
			.or_else(|| Some(self.base_path.clone().into())))
	}

	fn rpc_addr(&self, default_listen_port: u16) -> Result<Option<SocketAddr>> {
		self.base.base.rpc_addr(default_listen_port)
	}

	fn prometheus_config(
		&self,
		default_listen_port: u16,
		chain_spec: &Box<dyn ChainSpec>,
	) -> Result<Option<PrometheusConfig>> {
		self.base
			.base
			.prometheus_config(default_listen_port, chain_spec)
	}

	fn init<F>(
		&self,
		_support_url: &String,
		_impl_version: &String,
		_logger_hook: F,
		_config: &sc_service::Configuration,
	) -> Result<()>
	where
		F: FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration),
	{
		unreachable!("PolkadotCli is never initialized; qed");
	}

	fn chain_id(&self, is_dev: bool) -> Result<String> {
		let chain_id = self.base.base.chain_id(is_dev)?;

		Ok(if chain_id.is_empty() {
			self.chain_id.clone().unwrap_or_default()
		} else {
			chain_id
		})
	}

	fn role(&self, is_dev: bool) -> Result<sc_service::Role> {
		self.base.base.role(is_dev)
	}

	fn transaction_pool(&self, is_dev: bool) -> Result<sc_service::config::TransactionPoolOptions> {
		self.base.base.transaction_pool(is_dev)
	}

	fn rpc_methods(&self) -> Result<sc_service::config::RpcMethods> {
		self.base.base.rpc_methods()
	}

	fn rpc_max_connections(&self) -> Result<u32> {
		self.base.base.rpc_max_connections()
	}

	fn rpc_cors(&self, is_dev: bool) -> Result<Option<Vec<String>>> {
		self.base.base.rpc_cors(is_dev)
	}

	// fn telemetry_external_transport(&self) -> Result<Option<sc_service::config::ExtTransport>> {
	// 	self.base.base.telemetry_external_transport()
	// }

	fn default_heap_pages(&self) -> Result<Option<u64>> {
		self.base.base.default_heap_pages()
	}

	fn force_authoring(&self) -> Result<bool> {
		self.base.base.force_authoring()
	}

	fn disable_grandpa(&self) -> Result<bool> {
		self.base.base.disable_grandpa()
	}

	fn max_runtime_instances(&self) -> Result<Option<usize>> {
		self.base.base.max_runtime_instances()
	}

	fn announce_block(&self) -> Result<bool> {
		self.base.base.announce_block()
	}
}

/// Generate the genesis block from a given ChainSpec.
pub fn generate_genesis_block<Block: BlockT>(
	chain_spec: &dyn ChainSpec,
	genesis_state_version: StateVersion,
) -> std::result::Result<Block, String> {
	let storage = chain_spec.build_storage()?;

	let child_roots = storage.children_default.iter().map(|(sk, child_content)| {
		let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
			child_content.data.clone().into_iter().collect(),
			genesis_state_version,
		);
		(sk.clone(), state_root.encode())
	});
	let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
		storage.top.clone().into_iter().chain(child_roots).collect(),
		genesis_state_version,
	);

	let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
		Vec::new(),
		genesis_state_version,
	);

	Ok(Block::new(
		<<Block as BlockT>::Header as HeaderT>::new(
			Zero::zero(),
			extrinsics_root,
			state_root,
			Default::default(),
			Default::default(),
		),
		Default::default(),
	))
}