git.delta.rocks / unique-network / refs/commits / 8f1c92ce5d7f

difftreelog

Add RuntimeId, use match on runtime identification

Daniel Shiposha2022-03-14parent: #060d940.patch.diff
in: master

2 files changed

modifiednode/cli/src/chain_spec.rsdiffbeforeafterboth
38#[cfg(feature = "opal-runtime")]38#[cfg(feature = "opal-runtime")]
39pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;39pub type OpalChainSpec = sc_service::GenericChainSpec<opal_runtime::GenesisConfig, Extensions>;
40
41pub enum RuntimeId {
42 Unique,
43 Quartz,
44 Opal,
45 Unknown(String),
46}
4047
41pub trait RuntimeIdentification {48pub trait RuntimeIdentification {
42 fn is_unique(&self) -> bool;49 fn runtime_id(&self) -> RuntimeId;
43
44 fn is_quartz(&self) -> bool;
45
46 fn is_opal(&self) -> bool;
47}50}
4851
49impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {52impl RuntimeIdentification for Box<dyn sc_service::ChainSpec> {
50 fn is_unique(&self) -> bool {
51 self.id().starts_with("unique")
52 }
53
54 fn is_quartz(&self) -> bool {
55 self.id().starts_with("quartz")
56 }
57
58 fn is_opal(&self) -> bool {53 fn runtime_id(&self) -> RuntimeId {
54 #[cfg(feature = "unique-runtime")]
55 if self.id().starts_with("unique") {
56 return RuntimeId::Unique;
57 }
58
59 #[cfg(feature = "quartz-runtime")]
60 if self.id().starts_with("quartz") {
61 return RuntimeId::Quartz;
62 }
63
64 #[cfg(feature = "opal-runtime")]
59 self.id().starts_with("opal") || self.id() == "dev" || self.id() == "local_testnet"65 if self.id().starts_with("opal") {
66 return RuntimeId::Opal;
67 }
68
69 RuntimeId::Unknown(self.id().into())
60 }70 }
61}71}
6272
modifiednode/cli/src/command.rsdiffbeforeafterboth
33// limitations under the License.33// limitations under the License.
3434
35use 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;
6767
68macro_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_name
73 )73 )
74 };74 };
75}75}
7676
77fn 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>;
8788
89 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 }
9392
94 #[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 }
9995
100 #[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 }
10598
106 Err(no_runtime_err!(chain_spec))99 RuntimeId::Unknown(chain) => return Err(no_runtime_err!(chain)),
100 }
107 }101 }
108 }102 })
109}103}
110104
111impl SubstrateCli for Cli {105impl SubstrateCli for Cli {
146 }140 }
147141
148 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 }
153146
154 #[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 }
158149
159 #[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 }
163152
164 panic!("{}", no_runtime_err!(chain_spec));153 RuntimeId::Unknown(chain) => panic!("{}", no_runtime_err!(chain)),
154 }
165 }155 }
166}156}
167157
214 .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}
206
207macro_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;
221
222 { $( $code )* }.map(|v| (v, task_manager))
223 })
224 };
225}
216226
217macro_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)?;
220230
231 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 }
234237
235 #[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 }
248243
249 #[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 )?;249
258 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 }
262
263 Err(no_runtime_err!(runner.config().chain_spec).into())
264 }}252 }}
265}253}
266254
364 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 }
372358
373 #[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 }
377361
378 #[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 }
382364
383 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 );
437419
420 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 .await
446 .map(|r| r.0)428 .map(|r| r.0)
447 .map_err(Into::into);429 .map_err(Into::into),
448 }
449430
450 #[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 .await
458 .map(|r| r.0)438 .map(|r| r.0)
459 .map_err(Into::into);439 .map_err(Into::into),
460 }
461440
462 #[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 .await
470 .map(|r| r.0)448 .map(|r| r.0)
471 .map_err(Into::into);449 .map_err(Into::into),
472 }
473450
474 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 }