polkadot-service: Make native runtime configurable (#3189)

* polkadot-service: Make native runtime configurable

This pull requests adds support for configuring the native runtimes used
by polkadot-service. While this whole pr doesn't change that much for
polkadot, besides not having the light-node enabled for the default
polkadot binary. However, downstream projects (parachains) will have a
much better compile time. In cumulus for example the `cargo test --all
--release` is about 4m faster to compile.

* Fixes

* Fix

* Enable rococo-native

* Fix light client

* 🤦

* Fixes
This commit is contained in:
Bastian Köcher
2021-06-08 22:05:20 +02:00
committed by GitHub
parent 2abaca3a8c
commit f6cbe8e8d8
11 changed files with 711 additions and 421 deletions
+75 -65
View File
@@ -20,7 +20,6 @@
pub mod chain_spec;
mod grandpa_support;
mod client;
mod parachains_db;
#[cfg(feature = "full-node")]
@@ -53,11 +52,22 @@ use std::sync::Arc;
use std::time::Duration;
use prometheus_endpoint::Registry;
use sc_executor::native_executor_instance;
use service::RpcHandlers;
use telemetry::{Telemetry, TelemetryWorker, TelemetryWorkerHandle};
pub use self::client::{AbstractClient, Client, ClientHandle, ExecuteWithClient, RuntimeApiCollection};
#[cfg(feature = "rococo-native")]
pub use polkadot_client::RococoExecutor;
#[cfg(feature = "westend-native")]
pub use polkadot_client::WestendExecutor;
#[cfg(feature = "kusama-native")]
pub use polkadot_client::KusamaExecutor;
pub use polkadot_client::{
PolkadotExecutor, FullBackend, FullClient, AbstractClient, Client, ClientHandle, ExecuteWithClient,
RuntimeApiCollection,
};
pub use chain_spec::{PolkadotChainSpec, KusamaChainSpec, WestendChainSpec, RococoChainSpec};
pub use consensus_common::{Proposal, SelectChain, BlockImport, block_validation::Chain};
pub use polkadot_primitives::v1::{Block, BlockId, CollatorPair, Hash, Id as ParaId};
@@ -73,42 +83,17 @@ pub use service::config::{DatabaseConfig, PrometheusConfig};
pub use sp_api::{ApiRef, Core as CoreApi, ConstructRuntimeApi, ProvideRuntimeApi, StateBackend};
pub use sp_runtime::traits::{DigestFor, HashFor, NumberFor, Block as BlockT, self as runtime_traits, BlakeTwo256};
#[cfg(feature = "kusama-native")]
pub use kusama_runtime;
pub use polkadot_runtime;
#[cfg(feature = "rococo-native")]
pub use rococo_runtime;
#[cfg(feature = "westend-native")]
pub use westend_runtime;
/// The maximum number of active leaves we forward to the [`Overseer`] on startup.
const MAX_ACTIVE_LEAVES: usize = 4;
native_executor_instance!(
pub PolkadotExecutor,
polkadot_runtime::api::dispatch,
polkadot_runtime::native_version,
frame_benchmarking::benchmarking::HostFunctions,
);
native_executor_instance!(
pub KusamaExecutor,
kusama_runtime::api::dispatch,
kusama_runtime::native_version,
frame_benchmarking::benchmarking::HostFunctions,
);
native_executor_instance!(
pub WestendExecutor,
westend_runtime::api::dispatch,
westend_runtime::native_version,
frame_benchmarking::benchmarking::HostFunctions,
);
native_executor_instance!(
pub RococoExecutor,
rococo_runtime::api::dispatch,
rococo_runtime::native_version,
frame_benchmarking::benchmarking::HostFunctions,
);
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
@@ -209,17 +194,17 @@ fn jaeger_launch_collector_with_agent(spawner: impl SpawnNamed, config: &Configu
Ok(())
}
pub type FullBackend = service::TFullBackend<Block>;
#[cfg(feature = "full-node")]
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
pub type FullClient<RuntimeApi, Executor> = service::TFullClient<Block, RuntimeApi, Executor>;
#[cfg(feature = "full-node")]
type FullGrandpaBlockImport<RuntimeApi, Executor> = grandpa::GrandpaBlockImport<
FullBackend, Block, FullClient<RuntimeApi, Executor>, FullSelectChain
>;
#[cfg(feature = "light-node")]
type LightBackend = service::TLightBackendWithHash<Block, sp_runtime::traits::BlakeTwo256>;
#[cfg(feature = "light-node")]
type LightClient<RuntimeApi, Executor> =
service::TLightClientWithBackend<Block, RuntimeApi, Executor, LightBackend>;
@@ -975,7 +960,7 @@ pub fn new_full<RuntimeApi, Executor>(
min_block_delta: if chain_spec.is_wococo() { 4 } else { 8 },
prometheus_registry: prometheus_registry.clone(),
};
let gadget = beefy_gadget::start_beefy_gadget::<_, beefy_primitives::ecdsa::AuthorityPair, _, _, _>(
beefy_params
);
@@ -1069,6 +1054,7 @@ pub fn new_full<RuntimeApi, Executor>(
}
/// Builds a new service for a light client.
#[cfg(feature = "light-node")]
fn new_light<Runtime, Dispatch>(mut config: Configuration) -> Result<(
TaskManager,
RpcHandlers,
@@ -1239,39 +1225,56 @@ pub fn new_chain_ops(
>
{
config.keystore = service::config::KeystoreConfig::InMemory;
#[cfg(feature = "rococo-native")]
if config.chain_spec.is_rococo() || config.chain_spec.is_wococo() {
let service::PartialComponents { client, backend, import_queue, task_manager, .. }
= new_partial::<rococo_runtime::RuntimeApi, RococoExecutor>(config, jaeger_agent, None)?;
Ok((Arc::new(Client::Rococo(client)), backend, import_queue, task_manager))
} else if config.chain_spec.is_kusama() {
return Ok((Arc::new(Client::Rococo(client)), backend, import_queue, task_manager))
}
#[cfg(feature = "kusama-native")]
if config.chain_spec.is_kusama() {
let service::PartialComponents { client, backend, import_queue, task_manager, .. }
= new_partial::<kusama_runtime::RuntimeApi, KusamaExecutor>(config, jaeger_agent, None)?;
Ok((Arc::new(Client::Kusama(client)), backend, import_queue, task_manager))
} else if config.chain_spec.is_westend() {
return Ok((Arc::new(Client::Kusama(client)), backend, import_queue, task_manager))
}
#[cfg(feature = "westend-native")]
if config.chain_spec.is_westend() {
let service::PartialComponents { client, backend, import_queue, task_manager, .. }
= new_partial::<westend_runtime::RuntimeApi, WestendExecutor>(config, jaeger_agent, None)?;
Ok((Arc::new(Client::Westend(client)), backend, import_queue, task_manager))
} else {
let service::PartialComponents { client, backend, import_queue, task_manager, .. }
= new_partial::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(config, jaeger_agent, None)?;
Ok((Arc::new(Client::Polkadot(client)), backend, import_queue, task_manager))
return Ok((Arc::new(Client::Westend(client)), backend, import_queue, task_manager))
}
let service::PartialComponents { client, backend, import_queue, task_manager, .. }
= new_partial::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(config, jaeger_agent, None)?;
Ok((Arc::new(Client::Polkadot(client)), backend, import_queue, task_manager))
}
/// Build a new light node.
#[cfg(feature = "light-node")]
pub fn build_light(config: Configuration) -> Result<(
TaskManager,
RpcHandlers,
), Error> {
#[cfg(feature = "rococo-native")]
if config.chain_spec.is_rococo() || config.chain_spec.is_wococo() {
new_light::<rococo_runtime::RuntimeApi, RococoExecutor>(config)
} else if config.chain_spec.is_kusama() {
new_light::<kusama_runtime::RuntimeApi, KusamaExecutor>(config)
} else if config.chain_spec.is_westend() {
new_light::<westend_runtime::RuntimeApi, WestendExecutor>(config)
} else {
new_light::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(config)
return new_light::<rococo_runtime::RuntimeApi, RococoExecutor>(config)
}
#[cfg(feature = "kusama-native")]
if config.chain_spec.is_kusama() {
return new_light::<kusama_runtime::RuntimeApi, KusamaExecutor>(config)
}
#[cfg(feature = "westend-native")]
if config.chain_spec.is_westend() {
return new_light::<westend_runtime::RuntimeApi, WestendExecutor>(config)
}
new_light::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(config)
}
#[cfg(feature = "full-node")]
@@ -1283,8 +1286,9 @@ pub fn build_full(
jaeger_agent: Option<std::net::SocketAddr>,
telemetry_worker_handle: Option<TelemetryWorkerHandle>,
) -> Result<NewFull<Client>, Error> {
#[cfg(feature = "rococo-native")]
if config.chain_spec.is_rococo() || config.chain_spec.is_wococo() {
new_full::<rococo_runtime::RuntimeApi, RococoExecutor>(
return new_full::<rococo_runtime::RuntimeApi, RococoExecutor>(
config,
is_collator,
grandpa_pause,
@@ -1293,8 +1297,11 @@ pub fn build_full(
telemetry_worker_handle,
None,
).map(|full| full.with_client(Client::Rococo))
} else if config.chain_spec.is_kusama() {
new_full::<kusama_runtime::RuntimeApi, KusamaExecutor>(
}
#[cfg(feature = "kusama-native")]
if config.chain_spec.is_kusama() {
return new_full::<kusama_runtime::RuntimeApi, KusamaExecutor>(
config,
is_collator,
grandpa_pause,
@@ -1303,8 +1310,11 @@ pub fn build_full(
telemetry_worker_handle,
None,
).map(|full| full.with_client(Client::Kusama))
} else if config.chain_spec.is_westend() {
new_full::<westend_runtime::RuntimeApi, WestendExecutor>(
}
#[cfg(feature = "westend-native")]
if config.chain_spec.is_westend() {
return new_full::<westend_runtime::RuntimeApi, WestendExecutor>(
config,
is_collator,
grandpa_pause,
@@ -1313,15 +1323,15 @@ pub fn build_full(
telemetry_worker_handle,
None,
).map(|full| full.with_client(Client::Westend))
} else {
new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(
config,
is_collator,
grandpa_pause,
disable_beefy,
jaeger_agent,
telemetry_worker_handle,
None,
).map(|full| full.with_client(Client::Polkadot))
}
new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(
config,
is_collator,
grandpa_pause,
disable_beefy,
jaeger_agent,
telemetry_worker_handle,
None,
).map(|full| full.with_client(Client::Polkadot))
}