difftreelog
Add RuntimeId, use match on runtime identification
in: master
2 files changed
node/cli/src/chain_spec.rsdiffbeforeafterboth--- a/node/cli/src/chain_spec.rs
+++ b/node/cli/src/chain_spec.rs
@@ -38,25 +38,35 @@
#[cfg(feature = "opal-runtime")]
pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;
+pub enum RuntimeId {
+ Unique,
+ Quartz,
+ Opal,
+ Unknown(String),
+}
+
pub trait RuntimeIdentification {
- fn is_unique(&self) -> bool;
-
- fn is_quartz(&self) -> bool;
-
- fn is_opal(&self) -> bool;
+ fn runtime_id(&self) -> RuntimeId;
}
impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {
- fn is_unique(&self) -> bool {
- self.id().starts_with("unique")
- }
+ fn runtime_id(&self) -> RuntimeId {
+ #[cfg(feature = "unique-runtime")]
+ if self.id().starts_with("unique") {
+ return RuntimeId::Unique;
+ }
- fn is_quartz(&self) -> bool {
- self.id().starts_with("quartz")
- }
+ #[cfg(feature = "quartz-runtime")]
+ if self.id().starts_with("quartz") {
+ return RuntimeId::Quartz;
+ }
+
+ #[cfg(feature = "opal-runtime")]
+ if self.id().starts_with("opal") {
+ return RuntimeId::Opal;
+ }
- fn is_opal(&self) -> bool {
- self.id().starts_with("opal") || self.id() == "dev" || self.id() == "local_testnet"
+ RuntimeId::Unknown(self.id().into())
}
}
node/cli/src/command.rsdiffbeforeafterboth33// limitations under the License.33// limitations under the License.343435use crate::{35use crate::{36 chain_spec::{self, RuntimeIdentification},36 chain_spec::{self, RuntimeId, RuntimeIdentification},37 cli::{Cli, RelayChainCli, Subcommand},37 cli::{Cli, RelayChainCli, Subcommand},38 service::new_partial,38 service::new_partial,39};39};66use unique_runtime_common::types::Block;66use unique_runtime_common::types::Block;676768macro_rules! no_runtime_err {68macro_rules! no_runtime_err {69 ($chain_spec:expr) => {69 ($chain_name:expr) => {70 format!(70 format!(71 "No runtime valid runtime was found, chain id: {}",71 "No runtime valid runtime was found for chain {}",72 $chain_spec.id()72 $chain_name73 )73 )74 };74 };75}75}767677fn load_spec(id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {77fn load_spec(id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {78 match id {78 Ok(match id {79 "westend-local" => Ok(Box::new(chain_spec::local_testnet_westend_config())),79 "westend-local" => Box::new(chain_spec::local_testnet_westend_config()),80 "rococo-local" => Ok(Box::new(chain_spec::local_testnet_rococo_config())),80 "rococo-local" => Box::new(chain_spec::local_testnet_rococo_config()),81 "dev" => Ok(Box::new(chain_spec::development_config())),81 "dev" => Box::new(chain_spec::development_config()),82 "" | "local" => Ok(Box::new(chain_spec::local_testnet_rococo_config())),82 "" | "local" => Box::new(chain_spec::local_testnet_rococo_config()),83 path => {83 path => {84 let path = std::path::PathBuf::from(path);84 let path = std::path::PathBuf::from(path);85 let chain_spec = Box::new(sc_service::GenericChainSpec::<()>::from_json_file(path.clone())?)85 let chain_spec = Box::new(sc_service::GenericChainSpec::<()>::from_json_file(86 path.clone(),86 as Box<dyn sc_service::ChainSpec>;87 )?) as Box<dyn sc_service::ChainSpec>;878889 match chain_spec.runtime_id() {88 #[cfg(feature = "unique-runtime")]90 #[cfg(feature = "unique-runtime")]89 if chain_spec.is_unique() {90 let chain_spec = chain_spec::UniqueChainSpec::from_json_file(path)?;91 RuntimeId::Unique => Box::new(chain_spec::UniqueChainSpec::from_json_file(path)?),91 return Ok(Box::new(chain_spec));92 }939294 #[cfg(feature = "quartz-runtime")]93 #[cfg(feature = "quartz-runtime")]95 if chain_spec.is_quartz() {96 let chain_spec = chain_spec::QuartzChainSpec::from_json_file(path)?;94 RuntimeId::Quartz => Box::new(chain_spec::QuartzChainSpec::from_json_file(path)?),97 return Ok(Box::new(chain_spec));98 }9995100 #[cfg(feature = "opal-runtime")]96 #[cfg(feature = "opal-runtime")]101 if chain_spec.is_opal() {102 let chain_spec = chain_spec::OpalChainSpec::from_json_file(path)?;97 RuntimeId::Opal => Box::new(chain_spec::OpalChainSpec::from_json_file(path)?),103 return Ok(Box::new(chain_spec));104 }10598106 Err(no_runtime_err!(chain_spec))99 RuntimeId::Unknown(chain) => return Err(no_runtime_err!(chain)),100 }107 }101 }108 }102 })109}103}110104111impl SubstrateCli for Cli {105impl SubstrateCli for Cli {146 }140 }147141148 fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {142 fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {143 match chain_spec.runtime_id() {149 #[cfg(feature = "unique-runtime")]144 #[cfg(feature = "unique-runtime")]150 if chain_spec.is_unique() {151 return &unique_runtime::VERSION;145 RuntimeId::Unique => &unique_runtime::VERSION,152 }153146154 #[cfg(feature = "quartz-runtime")]147 #[cfg(feature = "quartz-runtime")]155 if chain_spec.is_quartz() {156 return &quartz_runtime::VERSION;148 RuntimeId::Quartz => &quartz_runtime::VERSION,157 }158149159 #[cfg(feature = "opal-runtime")]150 #[cfg(feature = "opal-runtime")]160 if chain_spec.is_opal() {161 return &opal_runtime::VERSION;151 RuntimeId::Opal => &opal_runtime::VERSION,162 }163152164 panic!("{}", no_runtime_err!(chain_spec));153 RuntimeId::Unknown(chain) => panic!("{}", no_runtime_err!(chain)),154 }165 }155 }166}156}167157214 .ok_or_else(|| "Could not find wasm file in genesis state!".into())204 .ok_or_else(|| "Could not find wasm file in genesis state!".into())215}205}206207macro_rules! async_run_with_runtime {208 (209 $runtime_api:path, $executor:path,210 $runner:ident, $components:ident, $cli:ident, $cmd:ident, $config:ident,211 $( $code:tt )*212 ) => {213 $runner.async_run(|$config| {214 let $components = new_partial::<215 $runtime_api, $executor, _216 >(217 &$config,218 crate::service::parachain_build_import_queue,219 )?;220 let task_manager = $components.task_manager;221222 { $( $code )* }.map(|v| (v, task_manager))223 })224 };225}216226217macro_rules! construct_async_run {227macro_rules! construct_async_run {218 (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{228 (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{219 let runner = $cli.create_runner($cmd)?;229 let runner = $cli.create_runner($cmd)?;220230231 match runner.config().chain_spec.runtime_id() {221 #[cfg(feature = "unique-runtime")]232 #[cfg(feature = "unique-runtime")]222 if runner.config().chain_spec.is_unique() {223 return runner.async_run(|$config| {224 let $components = new_partial::<233 RuntimeId::Unique => async_run_with_runtime!(225 unique_runtime::RuntimeApi, UniqueRuntimeExecutor, _226 >(227 &$config,228 crate::service::parachain_build_import_queue,234 unique_runtime::RuntimeApi, UniqueRuntimeExecutor,235 runner, $components, $cli, $cmd, $config, $( $code )*229 )?;236 ),230 let task_manager = $components.task_manager;231 { $( $code )* }.map(|v| (v, task_manager))232 });233 }234237235 #[cfg(feature = "quartz-runtime")]238 #[cfg(feature = "quartz-runtime")]236 if runner.config().chain_spec.is_quartz() {237 return runner.async_run(|$config| {238 let $components = new_partial::<239 RuntimeId::Quartz => async_run_with_runtime!(239 quartz_runtime::RuntimeApi, QuartzRuntimeExecutor, _240 quartz_runtime::RuntimeApi, QuartzRuntimeExecutor,240 >(241 &$config,241 runner, $components, $cli, $cmd, $config, $( $code )*242 crate::service::parachain_build_import_queue,243 )?;244 let task_manager = $components.task_manager;245 { $( $code )* }.map(|v| (v, task_manager))242 ),246 });247 }248243249 #[cfg(feature = "opal-runtime")]244 #[cfg(feature = "opal-runtime")]250 if runner.config().chain_spec.is_opal() {251 return runner.async_run(|$config| {252 let $components = new_partial::<245 RuntimeId::Opal => async_run_with_runtime!(253 opal_runtime::RuntimeApi, OpalRuntimeExecutor, _246 opal_runtime::RuntimeApi, OpalRuntimeExecutor,254 >(255 &$config,247 runner, $components, $cli, $cmd, $config, $( $code )*256 crate::service::parachain_build_import_queue,248 ),257 )?;249258 let task_manager = $components.task_manager;259 { $( $code )* }.map(|v| (v, task_manager))250 RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into())260 });251 }261 }262263 Err(no_runtime_err!(runner.config().chain_spec).into())264 }}252 }}265}253}266254364 Some(Subcommand::Benchmark(cmd)) => {352 Some(Subcommand::Benchmark(cmd)) => {365 if cfg!(feature = "runtime-benchmarks") {353 if cfg!(feature = "runtime-benchmarks") {366 let runner = cli.create_runner(cmd)?;354 let runner = cli.create_runner(cmd)?;367 runner.sync_run(|config| {355 runner.sync_run(|config| match config.chain_spec.runtime_id() {368 #[cfg(feature = "unique-runtime")]356 #[cfg(feature = "unique-runtime")]369 if config.chain_spec.is_unique() {370 return cmd.run::<Block, UniqueRuntimeExecutor>(config);357 RuntimeId::Unique => cmd.run::<Block, UniqueRuntimeExecutor>(config),371 }372358373 #[cfg(feature = "quartz-runtime")]359 #[cfg(feature = "quartz-runtime")]374 if config.chain_spec.is_quartz() {375 return cmd.run::<Block, QuartzRuntimeExecutor>(config);360 RuntimeId::Quartz => cmd.run::<Block, QuartzRuntimeExecutor>(config),376 }377361378 #[cfg(feature = "opal-runtime")]362 #[cfg(feature = "opal-runtime")]379 if config.chain_spec.is_opal() {380 return cmd.run::<Block, OpalRuntimeExecutor>(config);363 RuntimeId::Opal => cmd.run::<Block, OpalRuntimeExecutor>(config),381 }382364383 Err(no_runtime_err!(config.chain_spec).into())365 RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),384 })366 })385 } else {367 } else {386 Err("Benchmarking wasn't enabled when building the node. \368 Err("Benchmarking wasn't enabled when building the node. \435 }417 }436 );418 );437419420 match config.chain_spec.runtime_id() {438 #[cfg(feature = "unique-runtime")]421 #[cfg(feature = "unique-runtime")]439 if config.chain_spec.is_unique() {440 return crate::service::start_node::<422 RuntimeId::Unique => crate::service::start_node::<441 unique_runtime::Runtime,423 unique_runtime::Runtime,442 unique_runtime::RuntimeApi,424 unique_runtime::RuntimeApi,443 UniqueRuntimeExecutor,425 UniqueRuntimeExecutor,444 >(config, polkadot_config, id)426 >(config, polkadot_config, id)445 .await427 .await446 .map(|r| r.0)428 .map(|r| r.0)447 .map_err(Into::into);429 .map_err(Into::into),448 }449430450 #[cfg(feature = "quartz-runtime")]431 #[cfg(feature = "quartz-runtime")]451 if config.chain_spec.is_quartz() {452 return crate::service::start_node::<432 RuntimeId::Quartz => crate::service::start_node::<453 quartz_runtime::Runtime,433 quartz_runtime::Runtime,454 quartz_runtime::RuntimeApi,434 quartz_runtime::RuntimeApi,455 QuartzRuntimeExecutor,435 QuartzRuntimeExecutor,456 >(config, polkadot_config, id)436 >(config, polkadot_config, id)457 .await437 .await458 .map(|r| r.0)438 .map(|r| r.0)459 .map_err(Into::into);439 .map_err(Into::into),460 }461440462 #[cfg(feature = "opal-runtime")]441 #[cfg(feature = "opal-runtime")]463 if config.chain_spec.is_opal() {464 return crate::service::start_node::<442 RuntimeId::Opal => crate::service::start_node::<465 opal_runtime::Runtime,443 opal_runtime::Runtime,466 opal_runtime::RuntimeApi,444 opal_runtime::RuntimeApi,467 OpalRuntimeExecutor,445 OpalRuntimeExecutor,468 >(config, polkadot_config, id)446 >(config, polkadot_config, id)469 .await447 .await470 .map(|r| r.0)448 .map(|r| r.0)471 .map_err(Into::into);449 .map_err(Into::into),472 }473450474 Err(no_runtime_err!(config.chain_spec).into())451 RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),452 }475 })453 })476 }454 }477 }455 }