Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

3 changes: 1 addition & 2 deletions substrate/bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::{
};
use frame_benchmarking_cli::*;
use kitchensink_runtime::{ExistentialDeposit, RuntimeApi};
use node_executor::ExecutorDispatch;
use node_primitives::Block;
use sc_cli::{Result, SubstrateCli};
use sc_service::PartialComponents;
Expand Down Expand Up @@ -89,7 +88,7 @@ pub fn run() -> Result<()> {
Some(Subcommand::Inspect(cmd)) => {
let runner = cli.create_runner(cmd)?;

runner.sync_run(|config| cmd.run::<Block, RuntimeApi, ExecutorDispatch>(config))
runner.sync_run(|config| cmd.run::<Block, RuntimeApi>(config))
},
Some(Subcommand::Benchmark(cmd)) => {
let runner = cli.create_runner(cmd)?;
Expand Down
8 changes: 3 additions & 5 deletions substrate/bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ use frame_benchmarking_cli::SUBSTRATE_REFERENCE_HARDWARE;
use frame_system_rpc_runtime_api::AccountNonceApi;
use futures::prelude::*;
use kitchensink_runtime::RuntimeApi;
use node_executor::ExecutorDispatch;
use node_executor::ClientWasmExecutor;
use node_primitives::Block;
use sc_client_api::{Backend, BlockBackend};
use sc_consensus_babe::{self, SlotProportion};
use sc_executor::NativeElseWasmExecutor;
use sc_network::{event::Event, NetworkEventStream, NetworkService};
use sc_network_sync::{warp::WarpSyncParams, SyncingService};
use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
Expand All @@ -43,8 +42,7 @@ use sp_runtime::{generic, traits::Block as BlockT, SaturatedConversion};
use std::sync::Arc;

/// The full client type definition.
pub type FullClient =
sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
pub type FullClient = sc_service::TFullClient<Block, RuntimeApi, ClientWasmExecutor>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
type FullGrandpaBlockImport =
Expand Down Expand Up @@ -174,7 +172,7 @@ pub fn new_partial(
})
.transpose()?;

let executor = sc_service::new_native_or_wasm_executor(&config);
let executor = sc_service::new_wasm_executor(&config);

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ node-primitives = { path = "../primitives" }
kitchensink-runtime = { path = "../runtime" }
sc-executor = { path = "../../../client/executor" }
sp-core = { path = "../../../primitives/core", features=["serde"] }
sp-io = { path = "../../../primitives/io" }
sp-keystore = { path = "../../../primitives/keystore" }
sp-state-machine = { path = "../../../primitives/state-machine" }
sp-tracing = { path = "../../../primitives/tracing" }
Expand Down
106 changes: 43 additions & 63 deletions substrate/bin/node/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ use kitchensink_runtime::{
constants::currency::*, Block, BuildStorage, CheckedExtrinsic, Header, RuntimeCall,
RuntimeGenesisConfig, UncheckedExtrinsic,
};
use node_executor::ExecutorDispatch;
use node_executor::ClientWasmExecutor;
use node_primitives::{BlockNumber, Hash};
use node_testing::keyring::*;
use sc_executor::{
Externalities, NativeElseWasmExecutor, RuntimeVersionOf, WasmExecutionMethod, WasmExecutor,
WasmtimeInstantiationStrategy,
Externalities, RuntimeVersionOf, WasmExecutionMethod, WasmtimeInstantiationStrategy,
};
use sp_core::{
storage::well_known_keys,
Expand Down Expand Up @@ -58,12 +57,6 @@ const HEAP_PAGES: u64 = 20;

type TestExternalities<H> = CoreTestExternalities<H>;

#[derive(Debug)]
enum ExecutionMethod {
Native,
Wasm(WasmExecutionMethod),
}

fn sign(xt: CheckedExtrinsic) -> UncheckedExtrinsic {
node_testing::keyring::sign(xt, SPEC_VERSION, TRANSACTION_VERSION, GENESIS_HASH)
}
Expand All @@ -80,7 +73,7 @@ fn new_test_ext(genesis_config: &RuntimeGenesisConfig) -> TestExternalities<Blak
}

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

fn test_blocks(
genesis_config: &RuntimeGenesisConfig,
executor: &NativeElseWasmExecutor<ExecutorDispatch>,
executor: &ClientWasmExecutor,
) -> Vec<(Vec<u8>, Hash)> {
let mut test_ext = new_test_ext(genesis_config);
let mut block1_extrinsics = vec![CheckedExtrinsic {
Expand All @@ -181,56 +174,43 @@ fn test_blocks(

fn bench_execute_block(c: &mut Criterion) {
let mut group = c.benchmark_group("execute blocks");
let execution_methods = vec![
ExecutionMethod::Native,
ExecutionMethod::Wasm(WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
}),
];

for strategy in execution_methods {
group.bench_function(format!("{:?}", strategy), |b| {
let genesis_config = node_testing::genesis::config();
let use_native = match strategy {
ExecutionMethod::Native => true,
ExecutionMethod::Wasm(..) => false,
};

let executor =
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build());
let runtime_code = RuntimeCode {
code_fetcher: &sp_core::traits::WrappedRuntimeCode(compact_code_unwrap().into()),
hash: vec![1, 2, 3],
heap_pages: None,
};

// Get the runtime version to initialize the runtimes cache.
{
let mut test_ext = new_test_ext(&genesis_config);
executor.runtime_version(&mut test_ext.ext(), &runtime_code).unwrap();
}

let blocks = test_blocks(&genesis_config, &executor);

b.iter_batched_ref(
|| new_test_ext(&genesis_config),
|test_ext| {
for block in blocks.iter() {
executor
.call(
&mut test_ext.ext(),
&runtime_code,
"Core_execute_block",
&block.0,
use_native,
CallContext::Offchain,
)
.0
.unwrap();
}
},
BatchSize::LargeInput,
);
});
}

group.bench_function("wasm", |b| {
let genesis_config = node_testing::genesis::config();

let executor = ClientWasmExecutor::builder().build();
let runtime_code = RuntimeCode {
code_fetcher: &sp_core::traits::WrappedRuntimeCode(compact_code_unwrap().into()),
hash: vec![1, 2, 3],
heap_pages: None,
};

// Get the runtime version to initialize the runtimes cache.
{
let mut test_ext = new_test_ext(&genesis_config);
executor.runtime_version(&mut test_ext.ext(), &runtime_code).unwrap();
}

let blocks = test_blocks(&genesis_config, &executor);

b.iter_batched_ref(
|| new_test_ext(&genesis_config),
|test_ext| {
for block in blocks.iter() {
executor
.call(
&mut test_ext.ext(),
&runtime_code,
"Core_execute_block",
&block.0,
false,
CallContext::Offchain,
)
.0
.unwrap();
}
},
BatchSize::LargeInput,
);
});
}
28 changes: 7 additions & 21 deletions substrate/bin/node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! A `CodeExecutor` specialization which uses natively compiled runtime when the wasm to be
//! executed is equivalent to the natively compiled code.
//! Provides executor related types intended to be used across Substrate node.

pub use sc_executor::NativeElseWasmExecutor;
/// Host functions required for kitchensink runtime and Substrate node.
pub type HostFunctions =
(sp_io::SubstrateHostFunctions, sp_statement_store::runtime_api::HostFunctions);

// 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 ExecutorDispatch {
type ExtendHostFunctions = (
frame_benchmarking::benchmarking::HostFunctions,
sp_statement_store::runtime_api::HostFunctions,
);

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
kitchensink_runtime::api::dispatch(method, data)
}

fn native_version() -> sc_executor::NativeVersion {
kitchensink_runtime::native_version()
}
}
/// A specialized `WasmExecutor` intended to use accross substrate node. It provides all required
/// HostFunctions.
pub type ClientWasmExecutor = sc_executor::WasmExecutor<HostFunctions>;
9 changes: 4 additions & 5 deletions substrate/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, NativeElseWasmExecutor, WasmExecutor};
use sc_executor::error::Result;
use sp_consensus_babe::{
digests::{PreDigest, SecondaryPlainPreDigest},
Slot, BABE_ENGINE_ID,
Expand All @@ -38,11 +38,10 @@ use kitchensink_runtime::{
constants::currency::*, Block, BuildStorage, CheckedExtrinsic, Header, Runtime,
UncheckedExtrinsic,
};
use node_executor::ExecutorDispatch;
use node_primitives::{BlockNumber, Hash};
use node_testing::keyring::*;
use sp_externalities::Externalities;
use staging_node_executor as node_executor;
use staging_node_executor::ClientWasmExecutor;

pub const TEST_KEY_TYPE_ID: KeyTypeId = KeyTypeId(*b"test");

Expand Down Expand Up @@ -98,8 +97,8 @@ pub fn from_block_number(n: u32) -> Header {
Header::new(n, Default::default(), Default::default(), [69; 32].into(), Default::default())
}

pub fn executor() -> NativeElseWasmExecutor<ExecutorDispatch> {
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
pub fn executor() -> ClientWasmExecutor {
ClientWasmExecutor::builder().build()
}

pub fn executor_call(
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/inspect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
clap = { version = "4.4.6", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.6.1" }
thiserror = "1.0"
node-executor = { package = "staging-node-executor", path = "../executor" }
sc-cli = { path = "../../../client/cli" }
sc-client-api = { path = "../../../client/api" }
sc-service = { path = "../../../client/service", default-features = false}
Expand Down
7 changes: 3 additions & 4 deletions substrate/bin/node/inspect/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ use crate::{
Inspector,
};
use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams};
use sc_service::{Configuration, NativeExecutionDispatch};
use sc_service::Configuration;
use sp_runtime::traits::Block;

impl InspectCmd {
/// Run the inspect command, passing the inspector.
pub fn run<B, RA, D>(&self, config: Configuration) -> Result<()>
pub fn run<B, RA>(&self, config: Configuration) -> Result<()>
where
B: Block,
RA: Send + Sync + 'static,
D: NativeExecutionDispatch + 'static,
{
let executor = sc_service::new_native_or_wasm_executor::<D>(&config);
let executor = sc_service::new_wasm_executor::<node_executor::HostFunctions>(&config);
let client = sc_service::new_full_client::<B, RA, _>(&config, None, executor)?;
let inspect = Inspector::<B>::new(client);

Expand Down
14 changes: 6 additions & 8 deletions substrate/bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use sc_block_builder::BlockBuilderBuilder;
use sc_client_api::{execution_extensions::ExecutionExtensions, UsageProvider};
use sc_client_db::PruningMode;
use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy, ImportResult, ImportedAux};
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod, WasmtimeInstantiationStrategy};
use sc_executor::{WasmExecutionMethod, WasmtimeInstantiationStrategy};
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_consensus::BlockOrigin;
Expand Down Expand Up @@ -388,13 +388,11 @@ impl BenchDb {
let task_executor = TaskExecutor::new();

let backend = sc_service::new_db_backend(db_config).expect("Should not fail");
let executor = NativeElseWasmExecutor::new_with_wasm_executor(
sc_executor::WasmExecutor::builder()
.with_execution_method(WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
})
.build(),
);
let executor = sc_executor::WasmExecutor::builder()
.with_execution_method(WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
})
.build();

let client_config = sc_service::ClientConfig::default();
let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
Expand Down
19 changes: 14 additions & 5 deletions substrate/bin/node/testing/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ use sp_runtime::BuildStorage;
pub use substrate_test_client::*;

/// Call executor for `kitchensink-runtime` `TestClient`.
pub type ExecutorDispatch = sc_executor::NativeElseWasmExecutor<node_executor::ExecutorDispatch>;
use node_executor::ClientWasmExecutor;

/// Default backend type.
pub type Backend = sc_client_db::Backend<node_primitives::Block>;

/// Test client type.
pub type Client = client::Client<
Backend,
client::LocalCallExecutor<node_primitives::Block, Backend, ExecutorDispatch>,
client::LocalCallExecutor<node_primitives::Block, Backend, ClientWasmExecutor>,
node_primitives::Block,
kitchensink_runtime::RuntimeApi,
>;
Expand Down Expand Up @@ -63,16 +63,25 @@ pub trait TestClientBuilderExt: Sized {
impl TestClientBuilderExt
for substrate_test_client::TestClientBuilder<
node_primitives::Block,
client::LocalCallExecutor<node_primitives::Block, Backend, ExecutorDispatch>,
client::LocalCallExecutor<node_primitives::Block, Backend, ClientWasmExecutor>,
Backend,
GenesisParameters,
>
{
fn new() -> Self {
Self::default()
}

fn build(self) -> Client {
self.build_with_native_executor(None).0
let executor = ClientWasmExecutor::builder().build();
use sc_service::client::LocalCallExecutor;
use std::sync::Arc;
let executor = LocalCallExecutor::new(
self.backend().clone(),
executor.clone(),
Default::default(),
ExecutionExtensions::new(None, Arc::new(executor)),
)
.expect("Creates LocalCallExecutor");
self.build_with_executor(executor).0
}
}