Refactor service to allow building full (and light) node matching chain spec (#1467)

This commit is contained in:
Cecile Tonglet
2020-07-27 07:54:24 +02:00
committed by GitHub
parent fa598f176b
commit 1cb92aa83e
3 changed files with 101 additions and 74 deletions
+2 -2
View File
@@ -46,8 +46,8 @@ async fn start_inner(chain_spec: String, log_level: String) -> Result<Client, Bo
info!("👤 Role: {}", config.display_role()); info!("👤 Role: {}", config.display_role());
// Create the service. This is the most heavy initialization step. // Create the service. This is the most heavy initialization step.
let (task_manager, rpc_handlers) = service::kusama_new_light(config) let builder = service::NodeBuilder::new(config);
.map_err(|e| format!("{:?}", e))?; let (task_manager, rpc_handlers) = builder.build_light().map_err(|e| format!("{:?}", e))?;
Ok(browser_utils::start_client(task_manager, rpc_handlers)) Ok(browser_utils::start_client(task_manager, rpc_handlers))
} }
+28 -51
View File
@@ -106,8 +106,8 @@ pub fn run() -> Result<()> {
match &cli.subcommand { match &cli.subcommand {
None => { None => {
let runtime = cli.create_runner(&cli.run.base)?; let runner = cli.create_runner(&cli.run.base)?;
let chain_spec = &runtime.config().chain_spec; let chain_spec = &runner.config().chain_spec;
set_default_ss58_version(chain_spec); set_default_ss58_version(chain_spec);
@@ -124,55 +124,32 @@ pub fn run() -> Result<()> {
info!(" endorsed by the "); info!(" endorsed by the ");
info!(" KUSAMA FOUNDATION "); info!(" KUSAMA FOUNDATION ");
info!("----------------------------"); info!("----------------------------");
runtime.run_node_until_exit(|config| match config.role {
Role::Light => service::kusama_new_light(config)
.map(|(components, _)| components),
_ => service::kusama_new_full(
config,
None,
None,
authority_discovery_disabled,
6000,
grandpa_pause,
).map(|(components, _, _)| components)
})
} else if chain_spec.is_westend() {
runtime.run_node_until_exit(|config| match config.role {
Role::Light => service::westend_new_light(config)
.map(|(components, _)| components),
_ => service::westend_new_full(
config,
None,
None,
authority_discovery_disabled,
6000,
grandpa_pause,
).map(|(components, _, _)| components)
})
} else {
runtime.run_node_until_exit(|config| match config.role {
Role::Light => service::polkadot_new_light(config)
.map(|(components, _)| components),
_ => service::polkadot_new_full(
config,
None,
None,
authority_discovery_disabled,
6000,
grandpa_pause,
).map(|(components, _, _)| components)
})
} }
runner.run_node_until_exit(|config| {
let role = config.role.clone();
let builder = service::NodeBuilder::new(config);
match role {
Role::Light => builder.build_light().map(|(task_manager, _)| task_manager),
_ => builder.build_full(
None,
None,
authority_discovery_disabled,
6000,
grandpa_pause,
),
}
})
}, },
Some(Subcommand::Base(subcommand)) => { Some(Subcommand::Base(subcommand)) => {
let runtime = cli.create_runner(subcommand)?; let runner = cli.create_runner(subcommand)?;
let chain_spec = &runtime.config().chain_spec; let chain_spec = &runner.config().chain_spec;
set_default_ss58_version(chain_spec); set_default_ss58_version(chain_spec);
if chain_spec.is_kusama() { if chain_spec.is_kusama() {
runtime.run_subcommand(subcommand, |config| runner.run_subcommand(subcommand, |config|
service::new_chain_ops::< service::new_chain_ops::<
service::kusama_runtime::RuntimeApi, service::kusama_runtime::RuntimeApi,
service::KusamaExecutor, service::KusamaExecutor,
@@ -180,7 +157,7 @@ pub fn run() -> Result<()> {
>(config) >(config)
) )
} else if chain_spec.is_westend() { } else if chain_spec.is_westend() {
runtime.run_subcommand(subcommand, |config| runner.run_subcommand(subcommand, |config|
service::new_chain_ops::< service::new_chain_ops::<
service::westend_runtime::RuntimeApi, service::westend_runtime::RuntimeApi,
service::WestendExecutor, service::WestendExecutor,
@@ -188,7 +165,7 @@ pub fn run() -> Result<()> {
>(config) >(config)
) )
} else { } else {
runtime.run_subcommand(subcommand, |config| runner.run_subcommand(subcommand, |config|
service::new_chain_ops::< service::new_chain_ops::<
service::polkadot_runtime::RuntimeApi, service::polkadot_runtime::RuntimeApi,
service::PolkadotExecutor, service::PolkadotExecutor,
@@ -209,21 +186,21 @@ pub fn run() -> Result<()> {
} }
}, },
Some(Subcommand::Benchmark(cmd)) => { Some(Subcommand::Benchmark(cmd)) => {
let runtime = cli.create_runner(cmd)?; let runner = cli.create_runner(cmd)?;
let chain_spec = &runtime.config().chain_spec; let chain_spec = &runner.config().chain_spec;
set_default_ss58_version(chain_spec); set_default_ss58_version(chain_spec);
if chain_spec.is_kusama() { if chain_spec.is_kusama() {
runtime.sync_run(|config| { runner.sync_run(|config| {
cmd.run::<service::kusama_runtime::Block, service::KusamaExecutor>(config) cmd.run::<service::kusama_runtime::Block, service::KusamaExecutor>(config)
}) })
} else if chain_spec.is_westend() { } else if chain_spec.is_westend() {
runtime.sync_run(|config| { runner.sync_run(|config| {
cmd.run::<service::westend_runtime::Block, service::WestendExecutor>(config) cmd.run::<service::westend_runtime::Block, service::WestendExecutor>(config)
}) })
} else { } else {
runtime.sync_run(|config| { runner.sync_run(|config| {
cmd.run::<service::polkadot_runtime::Block, service::PolkadotExecutor>(config) cmd.run::<service::polkadot_runtime::Block, service::PolkadotExecutor>(config)
}) })
} }
+69 -19
View File
@@ -793,26 +793,76 @@ pub struct FullNodeHandles {
pub validation_service_handle: Option<consensus::ServiceHandle>, pub validation_service_handle: Option<consensus::ServiceHandle>,
} }
/// Create a new Polkadot service for a light client. /// A builder for a node.
pub fn polkadot_new_light(config: Configuration) -> Result< pub struct NodeBuilder {
(TaskManager, Arc<RpcHandlers>), ServiceError config: Configuration,
>
{
new_light::<polkadot_runtime::RuntimeApi, PolkadotExecutor, _>(config)
} }
/// Create a new Kusama service for a light client. impl NodeBuilder {
pub fn kusama_new_light(config: Configuration) -> Result< /// Create a new node builder.
(TaskManager, Arc<RpcHandlers>), ServiceError pub fn new(config: Configuration) -> Self {
> Self {
{ config,
new_light::<kusama_runtime::RuntimeApi, KusamaExecutor, _>(config) }
} }
/// Create a new Westend service for a light client. /// Build a new light node.
pub fn westend_new_light(config: Configuration, ) -> Result< pub fn build_light(self) -> Result<(TaskManager, Arc<RpcHandlers>), ServiceError> {
(TaskManager, Arc<RpcHandlers>), ServiceError if self.config.chain_spec.is_kusama() {
> new_light::<kusama_runtime::RuntimeApi, KusamaExecutor, _>(
{ self.config,
new_light::<westend_runtime::RuntimeApi, KusamaExecutor, _>(config) )
} else if self.config.chain_spec.is_westend() {
new_light::<westend_runtime::RuntimeApi, WestendExecutor, _>(
self.config,
)
} else {
new_light::<polkadot_runtime::RuntimeApi, PolkadotExecutor, _>(
self.config,
)
}
}
/// Build a new full node.
#[cfg(feature = "full-node")]
pub fn build_full(
self,
collating_for: Option<(CollatorId, parachain::Id)>,
max_block_data_size: Option<u64>,
authority_discovery_disabled: bool,
slot_duration: u64,
grandpa_pause: Option<(u32, u32)>,
) -> Result<TaskManager, ServiceError> {
if self.config.chain_spec.is_kusama() {
new_full::<kusama_runtime::RuntimeApi, KusamaExecutor, _>(
self.config,
collating_for,
max_block_data_size,
authority_discovery_disabled,
slot_duration,
grandpa_pause,
false,
).map(|(task_manager, _, _, _, _)| task_manager)
} else if self.config.chain_spec.is_westend() {
new_full::<westend_runtime::RuntimeApi, WestendExecutor, _>(
self.config,
collating_for,
max_block_data_size,
authority_discovery_disabled,
slot_duration,
grandpa_pause,
false,
).map(|(task_manager, _, _, _, _)| task_manager)
} else {
new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor, _>(
self.config,
collating_for,
max_block_data_size,
authority_discovery_disabled,
slot_duration,
grandpa_pause,
false,
).map(|(task_manager, _, _, _, _)| task_manager)
}
}
} }