Skip to content
Snippets Groups Projects
Commit a4538ef0 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

fix(#214): add Cargo feature to use native Runtime

parent 3669c6a6
No related branches found
No related tags found
1 merge request!255Resolve "Allow native Runtime execution"
Pipeline #36457 failed
...@@ -30,6 +30,7 @@ constant-fees = [ ...@@ -30,6 +30,7 @@ constant-fees = [
] ]
gtest = ["gtest-runtime", "std"] gtest = ["gtest-runtime", "std"]
embed = [] embed = []
native = []
runtime-benchmarks = [ runtime-benchmarks = [
"common-runtime/runtime-benchmarks", "common-runtime/runtime-benchmarks",
"dc-distance/runtime-benchmarks", "dc-distance/runtime-benchmarks",
......
...@@ -444,17 +444,21 @@ pub fn run() -> sc_cli::Result<()> { ...@@ -444,17 +444,21 @@ pub fn run() -> sc_cli::Result<()> {
match config.chain_spec.runtime_type() { match config.chain_spec.runtime_type() {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
RuntimeType::G1 => { RuntimeType::G1 => {
service::new_full::<g1_runtime::RuntimeApi>(config, cli.sealing) service::new_full::<g1_runtime::RuntimeApi, G1Executor>(config, cli.sealing)
.map_err(sc_cli::Error::Service) .map_err(sc_cli::Error::Service)
} }
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
RuntimeType::GTest => { RuntimeType::GTest => service::new_full::<
service::new_full::<gtest_runtime::RuntimeApi>(config, cli.sealing) gtest_runtime::RuntimeApi,
.map_err(sc_cli::Error::Service) GTestExecutor,
} >(config, cli.sealing)
.map_err(sc_cli::Error::Service),
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
RuntimeType::GDev => { RuntimeType::GDev => {
service::new_full::<gdev_runtime::RuntimeApi>(config, cli.sealing) service::new_full::<gdev_runtime::RuntimeApi, GDevExecutor>(
config,
cli.sealing,
)
.map_err(sc_cli::Error::Service) .map_err(sc_cli::Error::Service)
} }
_ => Err(sc_cli::Error::Application("unknown runtime".into())), _ => Err(sc_cli::Error::Application("unknown runtime".into())),
......
...@@ -26,7 +26,6 @@ use sc_client_api::client::BlockBackend; ...@@ -26,7 +26,6 @@ use sc_client_api::client::BlockBackend;
use sc_client_api::Backend; use sc_client_api::Backend;
use sc_consensus_grandpa::SharedVoterState; use sc_consensus_grandpa::SharedVoterState;
use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams}; use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};
pub use sc_executor::WasmExecutor;
use sc_service::WarpSyncParams; use sc_service::WarpSyncParams;
use sc_service::{error::Error as ServiceError, Configuration, PartialComponents, TaskManager}; use sc_service::{error::Error as ServiceError, Configuration, PartialComponents, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker}; use sc_telemetry::{Telemetry, TelemetryWorker};
...@@ -43,15 +42,29 @@ type HostFunctions = ( ...@@ -43,15 +42,29 @@ type HostFunctions = (
frame_benchmarking::benchmarking::HostFunctions, frame_benchmarking::benchmarking::HostFunctions,
); );
type FullClient<RuntimeApi> = // Allow to use native Runtime for debugging/development purposes
sc_service::TFullClient<Block, RuntimeApi, WasmExecutor<HostFunctions>>; #[cfg(feature = "native")]
type FullClient<RuntimeApi, Executor> =
sc_service::TFullClient<Block, RuntimeApi, sc_executor::NativeElseWasmExecutor<Executor>>;
// By default: WASM only Runtime
#[cfg(not(feature = "native"))]
type FullClient<RuntimeApi, Executor> =
sc_service::TFullClient<Block, RuntimeApi, sc_executor::WasmExecutor<Executor>>;
type FullBackend = sc_service::TFullBackend<Block>; type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>; type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(feature = "native")]
pub trait ExecutorTrait: sc_executor::NativeExecutionDispatch {}
#[cfg(not(feature = "native"))]
pub trait ExecutorTrait {}
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
pub mod gdev_executor { pub mod gdev_executor {
use crate::service::{ExecutorTrait, HostFunctions};
pub use gdev_runtime; pub use gdev_runtime;
use sc_executor::sp_wasm_interface::{Function, HostFunctionRegistry};
pub struct GDevExecutor; pub struct GDevExecutor;
impl sc_executor::NativeExecutionDispatch for GDevExecutor { impl sc_executor::NativeExecutionDispatch for GDevExecutor {
...@@ -65,6 +78,19 @@ pub mod gdev_executor { ...@@ -65,6 +78,19 @@ pub mod gdev_executor {
gdev_runtime::native_version() gdev_runtime::native_version()
} }
} }
impl ExecutorTrait for GDevExecutor {}
impl sc_executor::sp_wasm_interface::HostFunctions for GDevExecutor {
fn host_functions() -> Vec<&'static dyn Function> {
HostFunctions::host_functions()
}
fn register_static<T>(registry: &mut T) -> Result<(), T::Error>
where
T: HostFunctionRegistry,
{
HostFunctions::register_static(registry)
}
}
} }
#[allow(dead_code)] #[allow(dead_code)]
...@@ -160,7 +186,10 @@ pub fn new_chain_ops( ...@@ -160,7 +186,10 @@ pub fn new_chain_ops(
import_queue, import_queue,
task_manager, task_manager,
.. ..
} = new_partial::<g1_runtime::RuntimeApi>(config, manual_consensus)?; } = new_partial::<g1_runtime::RuntimeApi, g1_executor::G1Executor>(
config,
manual_consensus,
)?;
Ok(( Ok((
Arc::new(Client::G1(client)), Arc::new(Client::G1(client)),
backend, backend,
...@@ -176,7 +205,10 @@ pub fn new_chain_ops( ...@@ -176,7 +205,10 @@ pub fn new_chain_ops(
import_queue, import_queue,
task_manager, task_manager,
.. ..
} = new_partial::<gtest_runtime::RuntimeApi>(config, manual_consensus)?; } = new_partial::<gtest_runtime::RuntimeApi, gtest_executor::GTestExecutor>(
config,
manual_consensus,
)?;
Ok(( Ok((
Arc::new(Client::GTest(client)), Arc::new(Client::GTest(client)),
backend, backend,
...@@ -192,7 +224,10 @@ pub fn new_chain_ops( ...@@ -192,7 +224,10 @@ pub fn new_chain_ops(
import_queue, import_queue,
task_manager, task_manager,
.. ..
} = new_partial::<gdev_runtime::RuntimeApi>(config, manual_consensus)?; } = new_partial::<gdev_runtime::RuntimeApi, gdev_executor::GDevExecutor>(
config,
manual_consensus,
)?;
Ok(( Ok((
Arc::new(Client::GDev(client)), Arc::new(Client::GDev(client)),
backend, backend,
...@@ -204,41 +239,50 @@ pub fn new_chain_ops( ...@@ -204,41 +239,50 @@ pub fn new_chain_ops(
} }
} }
type FullGrandpaBlockImport<RuntimeApi> = sc_consensus_grandpa::GrandpaBlockImport< type FullGrandpaBlockImport<RuntimeApi, Executor> = sc_consensus_grandpa::GrandpaBlockImport<
FullBackend, FullBackend,
Block, Block,
FullClient<RuntimeApi>, FullClient<RuntimeApi, Executor>,
FullSelectChain, FullSelectChain,
>; >;
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub fn new_partial<RuntimeApi>( pub fn new_partial<RuntimeApi, Executor>(
config: &Configuration, config: &Configuration,
consensus_manual: bool, consensus_manual: bool,
) -> Result< ) -> Result<
sc_service::PartialComponents< sc_service::PartialComponents<
FullClient<RuntimeApi>, FullClient<RuntimeApi, Executor>,
FullBackend, FullBackend,
FullSelectChain, FullSelectChain,
sc_consensus::DefaultImportQueue<Block>, sc_consensus::DefaultImportQueue<Block>,
sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi>>, sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, Executor>>,
( (
sc_consensus_babe::BabeBlockImport< sc_consensus_babe::BabeBlockImport<
Block, Block,
FullClient<RuntimeApi>, FullClient<RuntimeApi, Executor>,
FullGrandpaBlockImport<RuntimeApi>, FullGrandpaBlockImport<RuntimeApi, Executor>,
>, >,
sc_consensus_babe::BabeLink<Block>, sc_consensus_babe::BabeLink<Block>,
Option<sc_consensus_babe::BabeWorkerHandle<Block>>, Option<sc_consensus_babe::BabeWorkerHandle<Block>>,
sc_consensus_grandpa::LinkHalf<Block, FullClient<RuntimeApi>, FullSelectChain>, sc_consensus_grandpa::LinkHalf<
Block,
FullClient<RuntimeApi, Executor>,
FullSelectChain,
>,
Option<Telemetry>, Option<Telemetry>,
), ),
>, >,
ServiceError, ServiceError,
> >
where where
RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi>> + Send + Sync + 'static, RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>
+ Send
+ Sync
+ 'static,
RuntimeApi::RuntimeApi: RuntimeApiCollection, RuntimeApi::RuntimeApi: RuntimeApiCollection,
Executor: ExecutorTrait + 'static,
Executor: sc_executor::sp_wasm_interface::HostFunctions + 'static,
{ {
let telemetry = config let telemetry = config
.telemetry_endpoints .telemetry_endpoints
...@@ -251,6 +295,9 @@ where ...@@ -251,6 +295,9 @@ where
}) })
.transpose()?; .transpose()?;
#[cfg(feature = "native")]
let executor = sc_service::new_native_or_wasm_executor(config);
#[cfg(not(feature = "native"))]
let executor = sc_service::new_wasm_executor(config); let executor = sc_service::new_wasm_executor(config);
let (client, backend, keystore_container, task_manager) = let (client, backend, keystore_container, task_manager) =
...@@ -353,13 +400,18 @@ where ...@@ -353,13 +400,18 @@ where
} }
/// Builds a new service for a full client. /// Builds a new service for a full client.
pub fn new_full<RuntimeApi>( pub fn new_full<RuntimeApi, Executor>(
config: Configuration, config: Configuration,
sealing: crate::cli::Sealing, sealing: crate::cli::Sealing,
) -> Result<TaskManager, ServiceError> ) -> Result<TaskManager, ServiceError>
where where
RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi>> + Send + Sync + 'static, RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>
+ Send
+ Sync
+ 'static,
RuntimeApi::RuntimeApi: RuntimeApiCollection, RuntimeApi::RuntimeApi: RuntimeApiCollection,
Executor: ExecutorTrait + 'static,
Executor: sc_executor::sp_wasm_interface::HostFunctions + 'static,
{ {
let sc_service::PartialComponents { let sc_service::PartialComponents {
client, client,
...@@ -370,7 +422,7 @@ where ...@@ -370,7 +422,7 @@ where
select_chain, select_chain,
transaction_pool, transaction_pool,
other: (block_import, babe_link, babe_worker_handle, grandpa_link, mut telemetry), other: (block_import, babe_link, babe_worker_handle, grandpa_link, mut telemetry),
} = new_partial::<RuntimeApi>(&config, sealing.is_manual_consensus())?; } = new_partial::<RuntimeApi, Executor>(&config, sealing.is_manual_consensus())?;
let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name( let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name(
&client &client
...@@ -536,7 +588,7 @@ where ...@@ -536,7 +588,7 @@ where
let distance = let distance =
dc_distance::create_distance_inherent_data_provider::< dc_distance::create_distance_inherent_data_provider::<
Block, Block,
FullClient<RuntimeApi>, FullClient<RuntimeApi, Executor>,
FullBackend, FullBackend,
>( >(
&*client, parent, distance_dir, &babe_owner_keys.clone() &*client, parent, distance_dir, &babe_owner_keys.clone()
...@@ -583,7 +635,7 @@ where ...@@ -583,7 +635,7 @@ where
let distance = dc_distance::create_distance_inherent_data_provider::< let distance = dc_distance::create_distance_inherent_data_provider::<
Block, Block,
FullClient<RuntimeApi>, FullClient<RuntimeApi, Executor>,
FullBackend, FullBackend,
>( >(
&*client, parent, distance_dir, &babe_owner_keys.clone() &*client, parent, distance_dir, &babe_owner_keys.clone()
......
...@@ -146,11 +146,11 @@ impl<Api> RuntimeApiCollection for Api where ...@@ -146,11 +146,11 @@ impl<Api> RuntimeApiCollection for Api where
#[derive(Clone)] #[derive(Clone)]
pub enum Client { pub enum Client {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
G1(Arc<super::FullClient<g1_runtime::RuntimeApi>>), G1(Arc<super::FullClient<g1_runtime::RuntimeApi, super::g1_executor::G1Executor>>),
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
GTest(Arc<super::FullClient<gtest_runtime::RuntimeApi>>), GTest(Arc<super::FullClient<gtest_runtime::RuntimeApi, super::gtest_executor::GTestExecutor>>),
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
GDev(Arc<super::FullClient<gdev_runtime::RuntimeApi>>), GDev(Arc<super::FullClient<gdev_runtime::RuntimeApi, super::gdev_executor::GDevExecutor>>),
} }
macro_rules! with_client { macro_rules! with_client {
...@@ -197,22 +197,38 @@ impl ClientHandle for Client { ...@@ -197,22 +197,38 @@ impl ClientHandle for Client {
} }
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
impl From<Arc<super::FullClient<g1_runtime::RuntimeApi>>> for Client { impl From<Arc<super::FullClient<g1_runtime::RuntimeApi, super::g1_executor::G1Executor>>>
fn from(client: Arc<super::FullClient<g1_runtime::RuntimeApi>>) -> Self { for Client
{
fn from(
client: Arc<super::FullClient<g1_runtime::RuntimeApi, super::g1_executor::G1Executor>>,
) -> Self {
Self::G1(client) Self::G1(client)
} }
} }
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
impl From<Arc<super::FullClient<gtest_runtime::RuntimeApi>>> for Client { impl From<Arc<super::FullClient<gtest_runtime::RuntimeApi, super::gtest_executor::GTestExecutor>>>
fn from(client: Arc<super::FullClient<gtest_runtime::RuntimeApi>>) -> Self { for Client
{
fn from(
client: Arc<
super::FullClient<gtest_runtime::RuntimeApi, super::gtest_executor::GTestExecutor>,
>,
) -> Self {
Self::GTest(client) Self::GTest(client)
} }
} }
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
impl From<Arc<super::FullClient<gdev_runtime::RuntimeApi>>> for Client { impl From<Arc<super::FullClient<gdev_runtime::RuntimeApi, super::gdev_executor::GDevExecutor>>>
fn from(client: Arc<super::FullClient<gdev_runtime::RuntimeApi>>) -> Self { for Client
{
fn from(
client: Arc<
super::FullClient<gdev_runtime::RuntimeApi, super::gdev_executor::GDevExecutor>,
>,
) -> Self {
Self::GDev(client) Self::GDev(client)
} }
} }
...@@ -311,14 +327,16 @@ trait BenchmarkCallSigner<RuntimeCall: Encode + Clone, Signer: Pair> { ...@@ -311,14 +327,16 @@ trait BenchmarkCallSigner<RuntimeCall: Encode + Clone, Signer: Pair> {
#[cfg(feature = "g1")] #[cfg(feature = "g1")]
use g1_runtime as runtime; use g1_runtime as runtime;
#[cfg(feature = "g1")]
type FullClient = super::FullClient<runtime::RuntimeApi, super::g1_executor::G1Executor>;
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
use gdev_runtime as runtime; use gdev_runtime as runtime;
#[cfg(feature = "gdev")] #[cfg(feature = "gdev")]
type FullClient = super::FullClient<runtime::RuntimeApi>; type FullClient = super::FullClient<runtime::RuntimeApi, super::gdev_executor::GDevExecutor>;
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
use gtest_runtime as runtime; use gtest_runtime as runtime;
#[cfg(feature = "gtest")] #[cfg(feature = "gtest")]
type FullClient = super::FullClient<runtime::RuntimeApi>; type FullClient = super::FullClient<runtime::RuntimeApi, super::gtest_executor::GTestExecutor>;
#[cfg(any(feature = "gdev", feature = "gtest"))] #[cfg(any(feature = "gdev", feature = "gtest"))]
impl BenchmarkCallSigner<runtime::RuntimeCall, sp_core::sr25519::Pair> for FullClient { impl BenchmarkCallSigner<runtime::RuntimeCall, sp_core::sr25519::Pair> for FullClient {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment