diff --git a/node/src/command.rs b/node/src/command.rs index 2b4eaeaed0eeee7633c0b669cdaa716653effd73..bfa0271c69b5689e468dee3c35bdfc946228b25a 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -22,7 +22,7 @@ use crate::service::G1Executor; use crate::service::GDevExecutor; #[cfg(feature = "gtest")] use crate::service::GTestExecutor; -use crate::service::IdentifyVariant; +use crate::service::{IdentifyRuntimeType, RuntimeType}; use crate::{chain_spec, service}; use sc_cli::{ChainSpec, RuntimeVersion, SubstrateCli}; @@ -74,17 +74,13 @@ impl SubstrateCli for Cli { .unwrap_or(false) }; - enum RuntimeType { - G1, - GDev, - GTest, - } - let runtime_type = if starts_with("g1") { RuntimeType::G1 - } else if starts_with("gdem") || starts_with("gtest") { + } else if starts_with("gdem") { + RuntimeType::GTest + } else if starts_with("dev") || starts_with("gdev") { RuntimeType::GDev - } else if starts_with("gdev") { + } else if starts_with("gt") { RuntimeType::GTest } else { panic!("unknown runtime") @@ -108,13 +104,13 @@ impl SubstrateCli for Cli { } fn native_runtime_version(spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion { - match spec { + match spec.runtime_type() { #[cfg(feature = "g1")] - spec if spec.is_main() => &g1_runtime::VERSION, + RuntimeType::G1 => &g1_runtime::VERSION, #[cfg(feature = "gtest")] - spec if spec.is_test() => >est_runtime::VERSION, + RuntimeType::GTest => >est_runtime::VERSION, #[cfg(feature = "gdev")] - spec if spec.is_dev() => &gdev_runtime::VERSION, + RuntimeType::GDev => &gdev_runtime::VERSION, _ => panic!("unknown runtime"), } } @@ -130,19 +126,19 @@ pub fn run() -> sc_cli::Result<()> { let runner = cli.create_runner(cmd)?; runner.sync_run(|config| { if cmd.shared_params.dev { - match &config.chain_spec { + match config.chain_spec.runtime_type() { #[cfg(feature = "g1")] - chain_spec if chain_spec.is_main() => cmd.run( + RuntimeType::G1 => cmd.run( Box::new(chain_spec::g1::development_chain_spec()?), config.network, ), #[cfg(feature = "gtest")] - chain_spec if chain_spec.is_test() => cmd.run( + RuntimeType::GTest => cmd.run( Box::new(chain_spec::gtest::development_chain_spec()?), config.network, ), #[cfg(feature = "gdev")] - chain_spec if chain_spec.is_dev() => cmd.run( + RuntimeType::GDev => cmd.run( Box::new(chain_spec::gdev::development_chain_spec()?), config.network, ), @@ -197,16 +193,16 @@ pub fn run() -> sc_cli::Result<()> { let runner = cli.create_runner(cmd)?; let chain_spec = &runner.config().chain_spec; - match chain_spec { + match chain_spec.runtime_type() { #[cfg(feature = "g1")] - chain_spec if chain_spec.is_main() => { + RuntimeType::G1 => { runner.sync_run(|config| cmd.run::<g1_runtime::Block, G1Executor>(config)) } #[cfg(feature = "gtest")] - chain_spec if chain_spec.is_test() => runner + RuntimeType::GTest => runner .sync_run(|config| cmd.run::<gtest_runtime::Block, GTestExecutor>(config)), #[cfg(feature = "gdev")] - chain_spec if chain_spec.is_dev() => runner + RuntimeType::GDev => runner .sync_run(|config| cmd.run::<gdev_runtime::Block, GDevExecutor>(config)), _ => Err(sc_cli::Error::Application("unknown runtime".into())), } @@ -219,19 +215,19 @@ pub fn run() -> sc_cli::Result<()> { None => { let runner = cli.create_runner(&cli.run)?; runner.run_node_until_exit(|config| async move { - match &config.chain_spec { + match config.chain_spec.runtime_type() { #[cfg(feature = "g1")] - chain_spec if chain_spec.is_main() => { + RuntimeType::G1 => { service::new_full::<g1_runtime::RuntimeApi, G1Executor>(config, None) .map_err(sc_cli::Error::Service) } #[cfg(feature = "gtest")] - chain_spec if chain_spec.is_test() => { + RuntimeType::GTest => { service::new_full::<gtest_runtime::RuntimeApi, GTestExecutor>(config, None) .map_err(sc_cli::Error::Service) } #[cfg(feature = "gdev")] - chain_spec if chain_spec.is_dev() => { + RuntimeType::GDev => { service::new_full::<gdev_runtime::RuntimeApi, GDevExecutor>( config, Some(cli.sealing), diff --git a/node/src/service.rs b/node/src/service.rs index c40e7e0d1165e5161a2e050dec977ab298b668b9..a0b30a00116fd112278cb44d6839b1b44f9eff5d 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -98,30 +98,32 @@ impl sc_executor::NativeExecutionDispatch for G1Executor { } } -/// Can be called for a `Configuration` to check if it is a configuration for -/// a particular network. -pub trait IdentifyVariant { - /// Returns `true` if this is a configuration for the `main` network. - fn is_main(&self) -> bool; - - /// Returns `true` if this is a configuration for the `test` network. - fn is_test(&self) -> bool; - - /// Returns `true` if this is a configuration for a dev network. - fn is_dev(&self) -> bool; +pub enum RuntimeType { + G1, + GDev, + GTest, } -impl IdentifyVariant for Box<dyn sc_chain_spec::ChainSpec> { - fn is_main(&self) -> bool { - self.id().starts_with("g1") - } - - fn is_test(&self) -> bool { - self.id().starts_with("gdem") || self.id().starts_with("gtest") - } +/// Can be called for a `Configuration` to check if it is a configuration for +/// a particular runtime type. +pub trait IdentifyRuntimeType { + /// Returns the runtime type + fn runtime_type(&self) -> RuntimeType; +} - fn is_dev(&self) -> bool { - self.id().starts_with("dev") || self.id().starts_with("gdev") +impl IdentifyRuntimeType for Box<dyn sc_chain_spec::ChainSpec> { + fn runtime_type(&self) -> RuntimeType { + if self.id().starts_with("g1") { + RuntimeType::G1 + } else if self.id().starts_with("gdem") { + RuntimeType::GTest + } else if self.id().starts_with("dev") || self.id().starts_with("gdev") { + RuntimeType::GDev + } else if self.id().starts_with("gt") { + RuntimeType::GTest + } else { + panic!("unknown runtime") + } } } @@ -138,9 +140,9 @@ pub fn new_chain_ops( ), ServiceError, > { - match &config.chain_spec { + match config.chain_spec.runtime_type() { #[cfg(feature = "g1")] - chain_spec if chain_spec.is_main() => { + RuntimeType::G1::G1 => { let PartialComponents { client, backend, @@ -156,7 +158,7 @@ pub fn new_chain_ops( )) } #[cfg(feature = "gtest")] - chain_spec if chain_spec.is_test() => { + RuntimeType::GTest => { let PartialComponents { client, backend, @@ -172,7 +174,7 @@ pub fn new_chain_ops( )) } #[cfg(feature = "gdev")] - chain_spec if chain_spec.is_dev() => { + RuntimeType::GDev => { let PartialComponents { client, backend,