Skip to content
Snippets Groups Projects
Commit 163cb9d3 authored by Éloïs's avatar Éloïs
Browse files

ref(node): normalize chain spec runtime type identification

parent fcfe9f0e
Branches
Tags
1 merge request!18Several things
...@@ -22,7 +22,7 @@ use crate::service::G1Executor; ...@@ -22,7 +22,7 @@ use crate::service::G1Executor;
use crate::service::GDevExecutor; use crate::service::GDevExecutor;
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
use crate::service::GTestExecutor; use crate::service::GTestExecutor;
use crate::service::IdentifyVariant; use crate::service::{IdentifyRuntimeType, RuntimeType};
use crate::{chain_spec, service}; use crate::{chain_spec, service};
use sc_cli::{ChainSpec, RuntimeVersion, SubstrateCli}; use sc_cli::{ChainSpec, RuntimeVersion, SubstrateCli};
...@@ -74,17 +74,13 @@ impl SubstrateCli for Cli { ...@@ -74,17 +74,13 @@ impl SubstrateCli for Cli {
.unwrap_or(false) .unwrap_or(false)
}; };
enum RuntimeType {
G1,
GDev,
GTest,
}
let runtime_type = if starts_with("g1") { let runtime_type = if starts_with("g1") {
RuntimeType::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 RuntimeType::GDev
} else if starts_with("gdev") { } else if starts_with("gt") {
RuntimeType::GTest RuntimeType::GTest
} else { } else {
panic!("unknown runtime") panic!("unknown runtime")
...@@ -108,13 +104,13 @@ impl SubstrateCli for Cli { ...@@ -108,13 +104,13 @@ impl SubstrateCli for Cli {
} }
fn native_runtime_version(spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion { fn native_runtime_version(spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
match spec { match spec.runtime_type() {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
spec if spec.is_main() => &g1_runtime::VERSION, RuntimeType::G1 => &g1_runtime::VERSION,
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
spec if spec.is_test() => &gtest_runtime::VERSION, RuntimeType::GTest => &gtest_runtime::VERSION,
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
spec if spec.is_dev() => &gdev_runtime::VERSION, RuntimeType::GDev => &gdev_runtime::VERSION,
_ => panic!("unknown runtime"), _ => panic!("unknown runtime"),
} }
} }
...@@ -130,19 +126,19 @@ pub fn run() -> sc_cli::Result<()> { ...@@ -130,19 +126,19 @@ pub fn run() -> sc_cli::Result<()> {
let runner = cli.create_runner(cmd)?; let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| { runner.sync_run(|config| {
if cmd.shared_params.dev { if cmd.shared_params.dev {
match &config.chain_spec { match config.chain_spec.runtime_type() {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
chain_spec if chain_spec.is_main() => cmd.run( RuntimeType::G1 => cmd.run(
Box::new(chain_spec::g1::development_chain_spec()?), Box::new(chain_spec::g1::development_chain_spec()?),
config.network, config.network,
), ),
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
chain_spec if chain_spec.is_test() => cmd.run( RuntimeType::GTest => cmd.run(
Box::new(chain_spec::gtest::development_chain_spec()?), Box::new(chain_spec::gtest::development_chain_spec()?),
config.network, config.network,
), ),
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
chain_spec if chain_spec.is_dev() => cmd.run( RuntimeType::GDev => cmd.run(
Box::new(chain_spec::gdev::development_chain_spec()?), Box::new(chain_spec::gdev::development_chain_spec()?),
config.network, config.network,
), ),
...@@ -197,16 +193,16 @@ pub fn run() -> sc_cli::Result<()> { ...@@ -197,16 +193,16 @@ pub fn run() -> sc_cli::Result<()> {
let runner = cli.create_runner(cmd)?; let runner = cli.create_runner(cmd)?;
let chain_spec = &runner.config().chain_spec; let chain_spec = &runner.config().chain_spec;
match chain_spec { match chain_spec.runtime_type() {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
chain_spec if chain_spec.is_main() => { RuntimeType::G1 => {
runner.sync_run(|config| cmd.run::<g1_runtime::Block, G1Executor>(config)) runner.sync_run(|config| cmd.run::<g1_runtime::Block, G1Executor>(config))
} }
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
chain_spec if chain_spec.is_test() => runner RuntimeType::GTest => runner
.sync_run(|config| cmd.run::<gtest_runtime::Block, GTestExecutor>(config)), .sync_run(|config| cmd.run::<gtest_runtime::Block, GTestExecutor>(config)),
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
chain_spec if chain_spec.is_dev() => runner RuntimeType::GDev => runner
.sync_run(|config| cmd.run::<gdev_runtime::Block, GDevExecutor>(config)), .sync_run(|config| cmd.run::<gdev_runtime::Block, GDevExecutor>(config)),
_ => Err(sc_cli::Error::Application("unknown runtime".into())), _ => Err(sc_cli::Error::Application("unknown runtime".into())),
} }
...@@ -219,19 +215,19 @@ pub fn run() -> sc_cli::Result<()> { ...@@ -219,19 +215,19 @@ pub fn run() -> sc_cli::Result<()> {
None => { None => {
let runner = cli.create_runner(&cli.run)?; let runner = cli.create_runner(&cli.run)?;
runner.run_node_until_exit(|config| async move { runner.run_node_until_exit(|config| async move {
match &config.chain_spec { match config.chain_spec.runtime_type() {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
chain_spec if chain_spec.is_main() => { RuntimeType::G1 => {
service::new_full::<g1_runtime::RuntimeApi, G1Executor>(config, None) service::new_full::<g1_runtime::RuntimeApi, G1Executor>(config, None)
.map_err(sc_cli::Error::Service) .map_err(sc_cli::Error::Service)
} }
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
chain_spec if chain_spec.is_test() => { RuntimeType::GTest => {
service::new_full::<gtest_runtime::RuntimeApi, GTestExecutor>(config, None) service::new_full::<gtest_runtime::RuntimeApi, GTestExecutor>(config, None)
.map_err(sc_cli::Error::Service) .map_err(sc_cli::Error::Service)
} }
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
chain_spec if chain_spec.is_dev() => { RuntimeType::GDev => {
service::new_full::<gdev_runtime::RuntimeApi, GDevExecutor>( service::new_full::<gdev_runtime::RuntimeApi, GDevExecutor>(
config, config,
Some(cli.sealing), Some(cli.sealing),
......
...@@ -98,30 +98,32 @@ impl sc_executor::NativeExecutionDispatch for G1Executor { ...@@ -98,30 +98,32 @@ impl sc_executor::NativeExecutionDispatch for G1Executor {
} }
} }
/// Can be called for a `Configuration` to check if it is a configuration for pub enum RuntimeType {
/// a particular network. G1,
pub trait IdentifyVariant { GDev,
/// Returns `true` if this is a configuration for the `main` network. GTest,
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;
}
impl IdentifyVariant for Box<dyn sc_chain_spec::ChainSpec> {
fn is_main(&self) -> bool {
self.id().starts_with("g1")
} }
fn is_test(&self) -> bool { /// Can be called for a `Configuration` to check if it is a configuration for
self.id().starts_with("gdem") || self.id().starts_with("gtest") /// a particular runtime type.
pub trait IdentifyRuntimeType {
/// Returns the runtime type
fn runtime_type(&self) -> RuntimeType;
}
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")
} }
fn is_dev(&self) -> bool {
self.id().starts_with("dev") || self.id().starts_with("gdev")
} }
} }
...@@ -138,9 +140,9 @@ pub fn new_chain_ops( ...@@ -138,9 +140,9 @@ pub fn new_chain_ops(
), ),
ServiceError, ServiceError,
> { > {
match &config.chain_spec { match config.chain_spec.runtime_type() {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
chain_spec if chain_spec.is_main() => { RuntimeType::G1::G1 => {
let PartialComponents { let PartialComponents {
client, client,
backend, backend,
...@@ -156,7 +158,7 @@ pub fn new_chain_ops( ...@@ -156,7 +158,7 @@ pub fn new_chain_ops(
)) ))
} }
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
chain_spec if chain_spec.is_test() => { RuntimeType::GTest => {
let PartialComponents { let PartialComponents {
client, client,
backend, backend,
...@@ -172,7 +174,7 @@ pub fn new_chain_ops( ...@@ -172,7 +174,7 @@ pub fn new_chain_ops(
)) ))
} }
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
chain_spec if chain_spec.is_dev() => { RuntimeType::GDev => {
let PartialComponents { let PartialComponents {
client, client,
backend, backend,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment