Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Make choosing an executor (native/wasm) an explicit part of service c…
Browse files Browse the repository at this point in the history
…onstruction (#9525)

* Split native executor stuff from wasm executor stuff

* Remove `native_runtime_version` in places

* Fix warning

* Fix test warning

* Remove redundant NativeRuntimeInfo trait

* Add a warning for use_native

* Run cargo fmt

* Revert "Add a warning for use_native"

This reverts commit 9494f76.

* Make choosing an executor (native/wasm) an explicit part of service construction

* Add Cargo.lock

* Rename Executor to ExecutorDispatch

* Update bin/node/executor/src/lib.rs

Co-authored-by: Squirrel <gilescope@gmail.com>

* Fix tests

* Fix minor node-executor error

* Fix node cli command thing

Co-authored-by: Squirrel <gilescope@gmail.com>
  • Loading branch information
expenses and gilescope authored Aug 18, 2021
1 parent d2a43d4 commit 3c2b267
Show file tree
Hide file tree
Showing 33 changed files with 271 additions and 186 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn run() -> sc_cli::Result<()> {
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;

runner.sync_run(|config| cmd.run::<Block, service::Executor>(config))
runner.sync_run(|config| cmd.run::<Block, service::ExecutorDispatch>(config))
} else {
Err("Benchmarking wasn't enabled when building the node. You can enable it with \
`--features runtime-benchmarks`."
Expand Down
27 changes: 21 additions & 6 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use node_template_runtime::{self, opaque::Block, RuntimeApi};
use sc_client_api::{ExecutorProvider, RemoteBackend};
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
pub use sc_executor::NativeExecutor;
pub use sc_executor::NativeElseWasmExecutor;
use sc_finality_grandpa::SharedVoterState;
use sc_keystore::LocalKeystore;
use sc_service::{error::Error as ServiceError, Configuration, TaskManager};
Expand All @@ -13,9 +13,9 @@ use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use std::{sync::Arc, time::Duration};

// Our native executor instance.
pub struct Executor;
pub struct ExecutorDispatch;

impl sc_executor::NativeExecutionDispatch for Executor {
impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
Expand All @@ -27,7 +27,8 @@ impl sc_executor::NativeExecutionDispatch for Executor {
}
}

type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
type FullClient =
sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;

Expand Down Expand Up @@ -68,10 +69,17 @@ pub fn new_partial(
})
.transpose()?;

let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
);

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, Executor>(
sc_service::new_full_parts::<Block, RuntimeApi, _>(
&config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor,
)?;
let client = Arc::new(client);

Expand Down Expand Up @@ -336,10 +344,17 @@ pub fn new_light(mut config: Configuration) -> Result<TaskManager, ServiceError>
})
.transpose()?;

let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
);

let (client, backend, keystore_container, mut task_manager, on_demand) =
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(
sc_service::new_light_parts::<Block, RuntimeApi, _>(
&config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor,
)?;

let mut telemetry = telemetry.map(|(worker, telemetry)| {
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ sc-rpc = { version = "4.0.0-dev", path = "../../../client/rpc" }
sc-basic-authorship = { version = "0.10.0-dev", path = "../../../client/basic-authorship" }
sc-service = { version = "0.10.0-dev", default-features = false, path = "../../../client/service" }
sc-telemetry = { version = "4.0.0-dev", path = "../../../client/telemetry" }
sc-executor = { version = "0.10.0-dev", path = "../../../client/executor" }
sc-authority-discovery = { version = "0.10.0-dev", path = "../../../client/authority-discovery" }
sc-sync-state-rpc = { version = "0.10.0-dev", path = "../../../client/sync-state-rpc" }

Expand Down
8 changes: 4 additions & 4 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{chain_spec, service, service::new_partial, Cli, Subcommand};
use node_executor::Executor;
use node_executor::ExecutorDispatch;
use node_runtime::{Block, RuntimeApi};
use sc_cli::{ChainSpec, Result, Role, RuntimeVersion, SubstrateCli};
use sc_service::PartialComponents;
Expand Down Expand Up @@ -87,13 +87,13 @@ pub fn run() -> Result<()> {
Some(Subcommand::Inspect(cmd)) => {
let runner = cli.create_runner(cmd)?;

runner.sync_run(|config| cmd.run::<Block, RuntimeApi, Executor>(config))
runner.sync_run(|config| cmd.run::<Block, RuntimeApi, ExecutorDispatch>(config))
},
Some(Subcommand::Benchmark(cmd)) =>
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;

runner.sync_run(|config| cmd.run::<Block, Executor>(config))
runner.sync_run(|config| cmd.run::<Block, ExecutorDispatch>(config))
} else {
Err("Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
Expand Down Expand Up @@ -159,7 +159,7 @@ pub fn run() -> Result<()> {
sc_service::TaskManager::new(config.task_executor.clone(), registry)
.map_err(|e| sc_cli::Error::Service(sc_service::Error::Prometheus(e)))?;

Ok((cmd.run::<Block, Executor>(config), task_manager))
Ok((cmd.run::<Block, ExecutorDispatch>(config), task_manager))
})
},
#[cfg(not(feature = "try-runtime"))]
Expand Down
27 changes: 22 additions & 5 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@
//! Service implementation. Specialized wrapper over substrate service.

use futures::prelude::*;
use node_executor::Executor;
use node_executor::ExecutorDispatch;
use node_primitives::Block;
use node_runtime::RuntimeApi;
use sc_client_api::{ExecutorProvider, RemoteBackend};
use sc_consensus_babe::{self, SlotProportion};
use sc_executor::NativeElseWasmExecutor;
use sc_network::{Event, NetworkService};
use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;

type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
type FullClient =
sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
type FullGrandpaBlockImport =
grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>;
type LightClient = sc_service::TLightClient<Block, RuntimeApi, Executor>;
type LightClient =
sc_service::TLightClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;

pub fn new_partial(
config: &Configuration,
Expand Down Expand Up @@ -75,10 +78,17 @@ pub fn new_partial(
})
.transpose()?;

let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
);

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, Executor>(
sc_service::new_full_parts::<Block, RuntimeApi, _>(
&config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor,
)?;
let client = Arc::new(client);

Expand Down Expand Up @@ -447,10 +457,17 @@ pub fn new_light_base(
})
.transpose()?;

let executor = NativeElseWasmExecutor::<ExecutorDispatch>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
);

let (client, backend, keystore_container, mut task_manager, on_demand) =
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(
sc_service::new_light_parts::<Block, RuntimeApi, _>(
&config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor,
)?;

let mut telemetry = telemetry.map(|(worker, telemetry)| {
Expand Down
10 changes: 5 additions & 5 deletions bin/node/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
use codec::{Decode, Encode};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use frame_support::Hashable;
use node_executor::Executor;
use node_executor::ExecutorDispatch;
use node_primitives::{BlockNumber, Hash};
use node_runtime::{
constants::currency::*, Block, BuildStorage, Call, CheckedExtrinsic, GenesisConfig, Header,
UncheckedExtrinsic,
};
use node_testing::keyring::*;
use sc_executor::{Externalities, NativeExecutor, RuntimeVersionOf, WasmExecutionMethod};
use sc_executor::{Externalities, NativeElseWasmExecutor, RuntimeVersionOf, WasmExecutionMethod};
use sp_core::{
storage::well_known_keys,
traits::{CodeExecutor, RuntimeCode},
Expand Down Expand Up @@ -77,7 +77,7 @@ fn new_test_ext(genesis_config: &GenesisConfig) -> TestExternalities<BlakeTwo256
}

fn construct_block<E: Externalities>(
executor: &NativeExecutor<Executor>,
executor: &NativeElseWasmExecutor<ExecutorDispatch>,
ext: &mut E,
number: BlockNumber,
parent_hash: Hash,
Expand Down Expand Up @@ -157,7 +157,7 @@ fn construct_block<E: Externalities>(

fn test_blocks(
genesis_config: &GenesisConfig,
executor: &NativeExecutor<Executor>,
executor: &NativeElseWasmExecutor<ExecutorDispatch>,
) -> Vec<(Vec<u8>, Hash)> {
let mut test_ext = new_test_ext(genesis_config);
let mut block1_extrinsics = vec![CheckedExtrinsic {
Expand Down Expand Up @@ -191,7 +191,7 @@ fn bench_execute_block(c: &mut Criterion) {
ExecutionMethod::Wasm(wasm_method) => (false, wasm_method),
};

let executor = NativeExecutor::new(wasm_method, None, 8);
let executor = NativeElseWasmExecutor::new(wasm_method, None, 8);
let runtime_code = RuntimeCode {
code_fetcher: &sp_core::traits::WrappedRuntimeCode(compact_code_unwrap().into()),
hash: vec![1, 2, 3],
Expand Down
10 changes: 5 additions & 5 deletions bin/node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
//! A `CodeExecutor` specialization which uses natively compiled runtime when the wasm to be
//! executed is equivalent to the natively compiled code.

pub use sc_executor::NativeExecutor;
pub use sc_executor::NativeElseWasmExecutor;

// Declare an instance of the native executor named `Executor`. Include the wasm binary as the
// equivalent wasm code.
pub struct Executor;
// Declare an instance of the native executor named `ExecutorDispatch`. Include the wasm binary as
// the equivalent wasm code.
pub struct ExecutorDispatch;

impl sc_executor::NativeExecutionDispatch for Executor {
impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
Expand Down
8 changes: 4 additions & 4 deletions bin/node/executor/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use codec::{Decode, Encode};
use frame_support::Hashable;
use frame_system::offchain::AppCrypto;
use sc_executor::{error::Result, NativeExecutor, WasmExecutionMethod};
use sc_executor::{error::Result, NativeElseWasmExecutor, WasmExecutionMethod};
use sp_consensus_babe::{
digests::{PreDigest, SecondaryPlainPreDigest},
Slot, BABE_ENGINE_ID,
Expand All @@ -35,7 +35,7 @@ use sp_runtime::{
};
use sp_state_machine::TestExternalities as CoreTestExternalities;

use node_executor::Executor;
use node_executor::ExecutorDispatch;
use node_primitives::{BlockNumber, Hash};
use node_runtime::{
constants::currency::*, Block, BuildStorage, CheckedExtrinsic, Header, Runtime,
Expand Down Expand Up @@ -95,8 +95,8 @@ pub fn from_block_number(n: u32) -> Header {
Header::new(n, Default::default(), Default::default(), [69; 32].into(), Default::default())
}

pub fn executor() -> NativeExecutor<Executor> {
NativeExecutor::new(WasmExecutionMethod::Interpreted, None, 8)
pub fn executor() -> NativeElseWasmExecutor<ExecutorDispatch> {
NativeElseWasmExecutor::new(WasmExecutionMethod::Interpreted, None, 8)
}

pub fn executor_call<
Expand Down
1 change: 1 addition & 0 deletions bin/node/inspect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0" }
derive_more = "0.99"
sc-cli = { version = "0.10.0-dev", path = "../../../client/cli" }
sc-client-api = { version = "4.0.0-dev", path = "../../../client/api" }
sc-executor = { version = "0.10.0-dev", path = "../../../client/executor" }
sc-service = { version = "0.10.0-dev", default-features = false, path = "../../../client/service" }
sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" }
sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" }
Expand Down
9 changes: 8 additions & 1 deletion bin/node/inspect/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
Inspector,
};
use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams};
use sc_executor::NativeElseWasmExecutor;
use sc_service::{new_full_client, Configuration, NativeExecutionDispatch};
use sp_runtime::traits::Block;
use std::str::FromStr;
Expand All @@ -36,7 +37,13 @@ impl InspectCmd {
RA: Send + Sync + 'static,
EX: NativeExecutionDispatch + 'static,
{
let client = new_full_client::<B, RA, EX>(&config, None)?;
let executor = NativeElseWasmExecutor::<EX>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
);

let client = new_full_client::<B, RA, _>(&config, None, executor)?;
let inspect = Inspector::<B>::new(client);

match &self.command {
Expand Down
9 changes: 5 additions & 4 deletions bin/node/test-runner-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use grandpa::GrandpaBlockImport;
use sc_consensus_babe::BabeBlockImport;
use sc_consensus_manual_seal::consensus::babe::SlotTimestampProvider;
use sc_executor::NativeElseWasmExecutor;
use sc_service::{TFullBackend, TFullClient};
use sp_runtime::generic::Era;
use test_runner::{ChainInfo, SignatureVerificationOverride};
Expand All @@ -30,9 +31,9 @@ type BlockImport<B, BE, C, SC> = BabeBlockImport<B, C, GrandpaBlockImport<BE, B,

/// A unit struct which implements `NativeExecutionDispatch` feeding in the
/// hard-coded runtime.
pub struct Executor;
pub struct ExecutorDispatch;

impl sc_executor::NativeExecutionDispatch for Executor {
impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
type ExtendHostFunctions =
(frame_benchmarking::benchmarking::HostFunctions, SignatureVerificationOverride);

Expand All @@ -50,14 +51,14 @@ struct NodeTemplateChainInfo;

impl ChainInfo for NodeTemplateChainInfo {
type Block = node_primitives::Block;
type Executor = Executor;
type ExecutorDispatch = ExecutorDispatch;
type Runtime = node_runtime::Runtime;
type RuntimeApi = node_runtime::RuntimeApi;
type SelectChain = sc_consensus::LongestChain<TFullBackend<Self::Block>, Self::Block>;
type BlockImport = BlockImport<
Self::Block,
TFullBackend<Self::Block>,
TFullClient<Self::Block, Self::RuntimeApi, Self::Executor>,
TFullClient<Self::Block, Self::RuntimeApi, NativeElseWasmExecutor<Self::ExecutorDispatch>>,
Self::SelectChain,
>;
type SignedExtras = node_runtime::SignedExtra;
Expand Down
4 changes: 2 additions & 2 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use sc_client_api::{
};
use sc_client_db::PruningMode;
use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy, ImportResult, ImportedAux};
use sc_executor::{NativeExecutor, WasmExecutionMethod};
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod};
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_consensus::BlockOrigin;
Expand Down Expand Up @@ -390,7 +390,7 @@ impl BenchDb {
let backend = sc_service::new_db_backend(db_config).expect("Should not fail");
let client = sc_service::new_client(
backend.clone(),
NativeExecutor::new(WasmExecutionMethod::Compiled, None, 8),
NativeElseWasmExecutor::new(WasmExecutionMethod::Compiled, None, 8),
&keyring.generate_genesis(),
None,
None,
Expand Down
Loading

0 comments on commit 3c2b267

Please sign in to comment.