Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0edd825
refactor: use builder api for all executors
yjhmelody Mar 28, 2023
9750b54
improve a lot
yjhmelody Mar 28, 2023
de50bf8
remove unused args
yjhmelody Mar 28, 2023
fb0539d
cleanup deps
yjhmelody Mar 28, 2023
b60bbd8
fix inconsistency about heap alloc
yjhmelody Mar 28, 2023
4eb0dc2
add `heap_pages` back to try-runtime
yjhmelody Mar 28, 2023
f449134
fix
yjhmelody Mar 28, 2023
c4175b2
chore: reduce duplicated code for sc-service-test
yjhmelody Mar 28, 2023
86172e6
cleanup code
yjhmelody Mar 28, 2023
f2c2e54
fmt
yjhmelody Mar 28, 2023
ce6dfb6
improve test executor
yjhmelody Mar 28, 2023
95dca94
improve
yjhmelody Mar 29, 2023
cd3bacb
Merge branch 'master' into refactor-use-executor-builder
yjhmelody Mar 29, 2023
c22e1bf
Merge branch 'master' into refactor-use-executor-builder
yjhmelody Mar 29, 2023
7147033
use #[deprecated]
yjhmelody Mar 29, 2023
157646e
set runtime_cache_size: 4
yjhmelody Mar 29, 2023
2dfbe0e
fix and improve
yjhmelody Mar 30, 2023
31ac181
refactor builder
yjhmelody Mar 30, 2023
d4cacc0
fix
yjhmelody Mar 30, 2023
7054f42
fix bench
yjhmelody Mar 30, 2023
fd173f6
fix tests
yjhmelody Mar 30, 2023
c523c6e
fix warnings
yjhmelody Mar 30, 2023
68661c0
fix warnings
yjhmelody Mar 30, 2023
00bc047
fix
yjhmelody Mar 30, 2023
35701eb
fix
yjhmelody Mar 30, 2023
3ab11e4
Merge branch 'master' into refactor-use-executor-builder
yjhmelody Mar 31, 2023
994b190
update by suggestions
yjhmelody Apr 6, 2023
fa1d260
Merge branch 'master' into refactor-use-executor-builder
yjhmelody Apr 6, 2023
efd17ed
update name
yjhmelody Apr 6, 2023
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
7 changes: 1 addition & 6 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ pub fn new_partial(
})
.transpose()?;

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

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
Expand Down
7 changes: 1 addition & 6 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,7 @@ pub fn new_partial(
})
.transpose()?;

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

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
Expand Down
6 changes: 4 additions & 2 deletions bin/node/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use node_executor::ExecutorDispatch;
use node_primitives::{BlockNumber, Hash};
use node_testing::keyring::*;
use sc_executor::{
Externalities, NativeElseWasmExecutor, RuntimeVersionOf, WasmExecutionMethod,
Externalities, NativeElseWasmExecutor, RuntimeVersionOf, WasmExecutionMethod, WasmExecutor,
WasmtimeInstantiationStrategy,
};
use sp_core::{
Expand Down Expand Up @@ -196,7 +196,9 @@ fn bench_execute_block(c: &mut Criterion) {
ExecutionMethod::Wasm(wasm_method) => (false, wasm_method),
};

let executor = NativeElseWasmExecutor::new(wasm_method, None, 8, 2);
let executor = NativeElseWasmExecutor::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
);
let runtime_code = RuntimeCode {
code_fetcher: &sp_core::traits::WrappedRuntimeCode(compact_code_unwrap().into()),
hash: vec![1, 2, 3],
Expand Down
6 changes: 4 additions & 2 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, NativeElseWasmExecutor, WasmExecutionMethod};
use sc_executor::{error::Result, NativeElseWasmExecutor, WasmExecutionMethod, WasmExecutor};
use sp_consensus_babe::{
digests::{PreDigest, SecondaryPlainPreDigest},
Slot, BABE_ENGINE_ID,
Expand Down Expand Up @@ -98,7 +98,9 @@ pub fn from_block_number(n: u32) -> Header {
}

pub fn executor() -> NativeElseWasmExecutor<ExecutorDispatch> {
NativeElseWasmExecutor::new(WasmExecutionMethod::Interpreted, None, 8, 2)
NativeElseWasmExecutor::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
)
}

pub fn executor_call(
Expand Down
25 changes: 9 additions & 16 deletions bin/node/inspect/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,34 @@ use crate::{
Inspector,
};
use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams};
use sc_executor::NativeElseWasmExecutor;
use sc_service::{new_full_client, Configuration, NativeExecutionDispatch};
use sc_service::{Configuration, NativeExecutionDispatch};
use sp_runtime::traits::Block;
use std::str::FromStr;

impl InspectCmd {
/// Run the inspect command, passing the inspector.
pub fn run<B, RA, EX>(&self, config: Configuration) -> Result<()>
pub fn run<B, RA, D>(&self, config: Configuration) -> Result<()>
where
B: Block,
B::Hash: FromStr,
RA: Send + Sync + 'static,
EX: NativeExecutionDispatch + 'static,
D: NativeExecutionDispatch + 'static,
{
let executor = NativeElseWasmExecutor::<EX>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
config.runtime_cache_size,
);

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

match &self.command {
InspectSubCmd::Block { input } => {
let input = input.parse()?;
let res = inspect.block(input).map_err(|e| format!("{}", e))?;
println!("{}", res);
let res = inspect.block(input).map_err(|e| e.to_string())?;
println!("{res}");
Ok(())
},
InspectSubCmd::Extrinsic { input } => {
let input = input.parse()?;
let res = inspect.extrinsic(input).map_err(|e| format!("{}", e))?;
println!("{}", res);
let res = inspect.extrinsic(input).map_err(|e| e.to_string())?;
println!("{res}");
Ok(())
},
}
Expand Down
11 changes: 5 additions & 6 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,13 @@ impl BenchDb {
let task_executor = TaskExecutor::new();

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

let client_config = sc_service::ClientConfig::default();
let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
&keyring.generate_genesis(),
Expand Down
7 changes: 3 additions & 4 deletions client/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use codec::Encode;

use sc_executor_common::{
runtime_blob::RuntimeBlob,
wasm_runtime::{HeapAllocStrategy, WasmInstance, WasmModule},
wasm_runtime::{HeapAllocStrategy, WasmInstance, WasmModule, DEFAULT_HEAP_ALLOC_STRATEGY},
};
use sc_executor_wasmtime::InstantiationStrategy;
use sc_runtime_test::wasm_binary_unwrap as test_runtime;
Expand Down Expand Up @@ -51,13 +51,12 @@ fn initialize(
) -> Arc<dyn WasmModule> {
let blob = RuntimeBlob::uncompress_if_needed(runtime).unwrap();
let host_functions = sp_io::SubstrateHostFunctions::host_functions();
let extra_pages = 2048;
let allow_missing_func_imports = true;

match method {
Method::Interpreted => sc_executor_wasmi::create_runtime(
blob,
HeapAllocStrategy::Static { extra_pages },
DEFAULT_HEAP_ALLOC_STRATEGY,
host_functions,
allow_missing_func_imports,
)
Expand All @@ -67,7 +66,7 @@ fn initialize(
allow_missing_func_imports,
cache_path: None,
semantics: sc_executor_wasmtime::Semantics {
heap_alloc_strategy: HeapAllocStrategy::Static { extra_pages },
heap_alloc_strategy: DEFAULT_HEAP_ALLOC_STRATEGY,
instantiation_strategy,
deterministic_stack_limit: None,
canonicalize_nans: false,
Expand Down
7 changes: 7 additions & 0 deletions client/executor/common/src/wasm_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ use sp_wasm_interface::Value;

pub use sc_allocator::AllocationStats;

/// Default heap allocation strategy.
pub const DEFAULT_HEAP_ALLOC_STRATEGY: HeapAllocStrategy =
HeapAllocStrategy::Static { extra_pages: DEFAULT_HEAP_ALLOC_PAGES };

/// Default heap allocation pages.
pub const DEFAULT_HEAP_ALLOC_PAGES: u32 = 2048;

/// A method to be used to find the entrypoint when calling into the runtime
///
/// Contains variants on how to resolve wasm function that will be invoked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ use std::{
use codec::Encode;
use sc_executor_common::{
runtime_blob::RuntimeBlob,
wasm_runtime::{AllocationStats, HeapAllocStrategy, WasmInstance, WasmModule},
wasm_runtime::{
AllocationStats, HeapAllocStrategy, WasmInstance, WasmModule, DEFAULT_HEAP_ALLOC_STRATEGY,
},
};
use sp_core::traits::{CallContext, CodeExecutor, Externalities, RuntimeCode};
use sp_version::{GetNativeVersion, NativeVersion, RuntimeVersion};
use sp_wasm_interface::{ExtendedHostFunctions, HostFunctions};

/// Default heap allocation strategy.
const DEFAULT_HEAP_ALLOC_STRATEGY: HeapAllocStrategy =
HeapAllocStrategy::Static { extra_pages: 2048 };

/// Set up the externalities and safe calling environment to execute runtime calls.
///
/// If the inner closure panics, it will be caught and return an error.
Expand Down Expand Up @@ -107,7 +105,7 @@ impl<H> WasmExecutorBuilder<H> {
onchain_heap_alloc_strategy: None,
offchain_heap_alloc_strategy: None,
max_runtime_instances: 2,
runtime_cache_size: 4,
runtime_cache_size: 8,
allow_missing_host_functions: false,
cache_path: None,
}
Expand Down Expand Up @@ -173,7 +171,7 @@ impl<H> WasmExecutorBuilder<H> {
/// Defines the number of different runtimes/instantiated wasm blobs the cache stores.
/// Runtimes/wasm blobs are differentiated based on the hash and the number of heap pages.
///
/// By default this value is set to `4`.
/// By default this value is set to `8`.
pub fn with_runtime_cache_size(mut self, runtime_cache_size: u8) -> Self {
self.runtime_cache_size = runtime_cache_size;
self
Expand Down Expand Up @@ -238,6 +236,8 @@ impl<H> WasmExecutor<H>
where
H: HostFunctions,
{
/// *To be deprecated*: use [`Self::builder`] method instead of it.
///
/// Create new instance.
///
/// # Parameters
Expand Down Expand Up @@ -539,6 +539,8 @@ pub struct NativeElseWasmExecutor<D: NativeExecutionDispatch> {
}

impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
/// *To be deprecated*: use [`Self::new_with_wasm_executor`] method instead of it.
///
/// Create new instance.
///
/// # Parameters
Expand Down Expand Up @@ -579,6 +581,8 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
Self { native_version: D::native_version(), wasm: executor }
}

/// *To be deprecated*: use [`Self::new_with_wasm_executor`] method to configure it.
///
/// Ignore missing function imports if set true.
pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) {
self.wasm.allow_missing_host_functions = allow_missing_host_functions
Expand Down Expand Up @@ -714,11 +718,8 @@ mod tests {

#[test]
fn native_executor_registers_custom_interface() {
let executor = NativeElseWasmExecutor::<MyExecutorDispatch>::new(
WasmExecutionMethod::Interpreted,
None,
8,
2,
let executor = NativeElseWasmExecutor::<MyExecutorDispatch>::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
);

fn extract_host_functions<H>(
Expand Down
23 changes: 7 additions & 16 deletions client/executor/src/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ fn call_in_wasm<E: Externalities>(
execution_method: WasmExecutionMethod,
ext: &mut E,
) -> Result<Vec<u8>, Error> {
let executor =
crate::WasmExecutor::<HostFunctions>::new(execution_method, Some(1024), 8, None, 2);
let executor = crate::WasmExecutor::<HostFunctions>::builder(execution_method).build();

executor.uncached_call(
RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
ext,
Expand Down Expand Up @@ -446,13 +446,10 @@ test_wasm_execution!(should_trap_when_heap_exhausted);
fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
let mut ext = TestExternalities::default();

let executor = crate::WasmExecutor::<HostFunctions>::new(
wasm_method,
Some(17), // `17` is the initial number of pages compiled into the binary.
8,
None,
2,
);
let executor = crate::WasmExecutor::<HostFunctions>::builder(wasm_method)
// `17` is the initial number of pages compiled into the binary.
.with_onchain_heap_alloc_strategy(HeapAllocStrategy::Static { extra_pages: 17 })
.build();

let err = executor
.uncached_call(
Expand Down Expand Up @@ -579,13 +576,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {

test_wasm_execution!(parallel_execution);
fn parallel_execution(wasm_method: WasmExecutionMethod) {
let executor = std::sync::Arc::new(crate::WasmExecutor::<HostFunctions>::new(
wasm_method,
Some(1024),
8,
None,
2,
));
let executor = Arc::new(crate::WasmExecutor::<HostFunctions>::builder(wasm_method).build());
let threads: Vec<_> = (0..8)
.map(|_| {
let executor = executor.clone();
Expand Down
26 changes: 14 additions & 12 deletions client/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@
#![recursion_limit = "128"]

#[macro_use]
mod native_executor;
mod executor;
#[cfg(test)]
mod integration_tests;
mod wasm_runtime;

pub use codec::Codec;
pub use native_executor::{
with_externalities_safe, NativeElseWasmExecutor, NativeExecutionDispatch, WasmExecutor,
pub use self::{
executor::{
with_externalities_safe, NativeElseWasmExecutor, NativeExecutionDispatch, WasmExecutor,
},
wasm_runtime::{read_embedded_version, WasmExecutionMethod},
};
pub use codec::Codec;
#[doc(hidden)]
pub use sp_core::traits::Externalities;
pub use sp_version::{NativeVersion, RuntimeVersion};
#[doc(hidden)]
pub use sp_wasm_interface;
pub use wasm_runtime::{read_embedded_version, WasmExecutionMethod};
pub use wasmi;

pub use sc_executor_common::error;
pub use sc_executor_common::{
error,
wasm_runtime::{HeapAllocStrategy, DEFAULT_HEAP_ALLOC_PAGES, DEFAULT_HEAP_ALLOC_STRATEGY},
};
pub use sc_executor_wasmtime::InstantiationStrategy as WasmtimeInstantiationStrategy;

/// Extracts the runtime version of a given runtime code.
Expand All @@ -74,13 +79,10 @@ mod tests {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();

let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::new(
let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::builder(
WasmExecutionMethod::Interpreted,
Some(8),
8,
None,
2,
);
)
.build();
let res = executor
.uncached_call(
RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion client/executor/src/wasm_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ where
// The following unwind safety assertion is OK because if the method call panics, the
// runtime will be dropped.
let runtime = AssertUnwindSafe(runtime.as_ref());
crate::native_executor::with_externalities_safe(&mut **ext, move || {
crate::executor::with_externalities_safe(&mut **ext, move || {
runtime.new_instance()?.call("Core_version".into(), &[])
})
.map_err(|_| WasmError::Instantiation("panic in call to get runtime version".into()))?
Expand Down
4 changes: 2 additions & 2 deletions client/executor/wasmtime/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use codec::{Decode as _, Encode as _};
use sc_executor_common::{
error::Error,
runtime_blob::RuntimeBlob,
wasm_runtime::{HeapAllocStrategy, WasmModule},
wasm_runtime::{HeapAllocStrategy, WasmModule, DEFAULT_HEAP_ALLOC_STRATEGY},
};
use sc_runtime_test::wasm_binary_unwrap;

Expand Down Expand Up @@ -473,7 +473,7 @@ fn test_instances_without_reuse_are_not_leaked() {
deterministic_stack_limit: None,
canonicalize_nans: false,
parallel_compilation: true,
heap_alloc_strategy: HeapAllocStrategy::Static { extra_pages: 2048 },
heap_alloc_strategy: DEFAULT_HEAP_ALLOC_STRATEGY,
},
},
)
Expand Down
Loading