refactor(sc-executor): use wasm executor builder instead of old apis (#13740)

* refactor: use builder api for all executors

* improve a lot

* remove unused args

* cleanup deps

* fix inconsistency about heap alloc

* add `heap_pages` back to try-runtime

* fix

* chore: reduce duplicated code for sc-service-test

* cleanup code

* fmt

* improve test executor

* improve

* use #[deprecated]

* set runtime_cache_size: 4

* fix and improve

* refactor builder

* fix

* fix bench

* fix tests

* fix warnings

* fix warnings

* fix

* fix

* update by suggestions

* update name
This commit is contained in:
yjh
2023-04-10 07:48:40 +08:00
committed by GitHub
parent f4d079a723
commit d5e460b3bf
29 changed files with 212 additions and 209 deletions
@@ -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.
@@ -100,10 +98,10 @@ impl<H> WasmExecutorBuilder<H> {
/// Create a new instance of `Self`
///
/// - `method`: The wasm execution method that should be used by the executor.
pub fn new(method: WasmExecutionMethod) -> Self {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
method,
method: WasmExecutionMethod::default(),
onchain_heap_alloc_strategy: None,
offchain_heap_alloc_strategy: None,
max_runtime_instances: 2,
@@ -113,6 +111,12 @@ impl<H> WasmExecutorBuilder<H> {
}
}
/// Create the wasm executor with execution method that should be used by the executor.
pub fn with_execution_method(mut self, method: WasmExecutionMethod) -> Self {
self.method = method;
self
}
/// Create the wasm executor with the given number of `heap_alloc_strategy` for onchain runtime
/// calls.
pub fn with_onchain_heap_alloc_strategy(
@@ -256,6 +260,7 @@ where
/// compiled execution method is used.
///
/// `runtime_cache_size` - The capacity of runtime cache.
#[deprecated(note = "use `Self::builder` method instead of it")]
pub fn new(
method: WasmExecutionMethod,
default_heap_pages: Option<u64>,
@@ -283,11 +288,12 @@ where
}
/// Instantiate a builder for creating an instance of `Self`.
pub fn builder(method: WasmExecutionMethod) -> WasmExecutorBuilder<H> {
WasmExecutorBuilder::new(method)
pub fn builder() -> WasmExecutorBuilder<H> {
WasmExecutorBuilder::new()
}
/// Ignore missing function imports if set true.
#[deprecated(note = "use `Self::builder` method instead of it")]
pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) {
self.allow_missing_host_functions = allow_missing_host_functions
}
@@ -539,6 +545,7 @@ pub struct NativeElseWasmExecutor<D: NativeExecutionDispatch> {
}
impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
///
/// Create new instance.
///
/// # Parameters
@@ -553,19 +560,23 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
/// `max_runtime_instances` - The number of runtime instances to keep in memory ready for reuse.
///
/// `runtime_cache_size` - The capacity of runtime cache.
#[deprecated(note = "use `Self::new_with_wasm_executor` method instead of it")]
pub fn new(
fallback_method: WasmExecutionMethod,
default_heap_pages: Option<u64>,
max_runtime_instances: usize,
runtime_cache_size: u8,
) -> Self {
let wasm = WasmExecutor::new(
fallback_method,
default_heap_pages,
max_runtime_instances,
None,
runtime_cache_size,
);
let heap_pages = default_heap_pages.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| {
HeapAllocStrategy::Static { extra_pages: h as _ }
});
let wasm = WasmExecutor::builder()
.with_execution_method(fallback_method)
.with_onchain_heap_alloc_strategy(heap_pages)
.with_offchain_heap_alloc_strategy(heap_pages)
.with_max_runtime_instances(max_runtime_instances)
.with_runtime_cache_size(runtime_cache_size)
.build();
NativeElseWasmExecutor { native_version: D::native_version(), wasm }
}
@@ -580,6 +591,7 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
}
/// Ignore missing function imports if set true.
#[deprecated(note = "use `Self::new_with_wasm_executor` method instead of it")]
pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) {
self.wasm.allow_missing_host_functions = allow_missing_host_functions
}
@@ -714,11 +726,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().build(),
);
fn extract_host_functions<H>(
@@ -21,7 +21,7 @@
use super::mk_test_runtime;
use crate::WasmExecutionMethod;
use codec::Encode as _;
use sc_executor_common::wasm_runtime::HeapAllocStrategy;
use sc_executor_common::wasm_runtime::DEFAULT_HEAP_ALLOC_STRATEGY;
mod smaps;
@@ -74,7 +74,7 @@ fn memory_consumption(wasm_method: WasmExecutionMethod) {
// For that we make a series of runtime calls, probing the RSS for the VMA matching the linear
// memory. After the call we expect RSS to be equal to 0.
let runtime = mk_test_runtime(wasm_method, HeapAllocStrategy::Static { extra_pages: 1024 });
let runtime = mk_test_runtime(wasm_method, DEFAULT_HEAP_ALLOC_STRATEGY);
let mut instance = runtime.new_instance().unwrap();
let heap_base = instance
@@ -24,7 +24,7 @@ use codec::{Decode, Encode};
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;
use sp_core::{
@@ -114,8 +114,10 @@ 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()
.with_execution_method(execution_method)
.build();
executor.uncached_call(
RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
ext,
@@ -446,13 +448,11 @@ 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()
.with_execution_method(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(
@@ -560,7 +560,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
test_wasm_execution!(interpreted_only heap_is_reset_between_calls);
fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
let runtime = mk_test_runtime(wasm_method, HeapAllocStrategy::Static { extra_pages: 1024 });
let runtime = mk_test_runtime(wasm_method, DEFAULT_HEAP_ALLOC_STRATEGY);
let mut instance = runtime.new_instance().unwrap();
let heap_base = instance
@@ -579,13 +579,11 @@ 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()
.with_execution_method(wasm_method)
.build(),
);
let threads: Vec<_> = (0..8)
.map(|_| {
let executor = executor.clone();
+12 -13
View File
@@ -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.
@@ -74,13 +79,7 @@ mod tests {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::new(
WasmExecutionMethod::Interpreted,
Some(8),
8,
None,
2,
);
let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::builder().build();
let res = executor
.uncached_call(
RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
@@ -434,7 +434,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()))?