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
@@ -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_or_wasm_executor(&config);
let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
@@ -27,7 +27,7 @@ use sc_client_api::execution_extensions::ExecutionStrategies;
use sc_service::{
config::{
BlocksPruning, DatabaseSource, KeystoreConfig, NetworkConfiguration, OffchainWorkerConfig,
PruningMode, TransactionPoolOptions, WasmExecutionMethod,
PruningMode, TransactionPoolOptions,
},
BasePath, Configuration, Role,
};
@@ -69,7 +69,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
state_pruning: Some(PruningMode::ArchiveAll),
blocks_pruning: BlocksPruning::KeepAll,
chain_spec: spec,
wasm_method: WasmExecutionMethod::Interpreted,
wasm_method: Default::default(),
// NOTE: we enforce the use of the native runtime to make the errors more debuggable
execution_strategies: ExecutionStrategies {
syncing: sc_client_api::ExecutionStrategy::NativeWhenPossible,
+1 -6
View File
@@ -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_or_wasm_executor(&config);
let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
+6 -5
View File
@@ -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::{
@@ -191,12 +191,13 @@ fn bench_execute_block(c: &mut Criterion) {
for strategy in execution_methods {
group.bench_function(format!("{:?}", strategy), |b| {
let genesis_config = node_testing::genesis::config(Some(compact_code_unwrap()));
let (use_native, wasm_method) = match strategy {
ExecutionMethod::Native => (true, WasmExecutionMethod::Interpreted),
ExecutionMethod::Wasm(wasm_method) => (false, wasm_method),
let use_native = match strategy {
ExecutionMethod::Native => true,
ExecutionMethod::Wasm(..) => false,
};
let executor = NativeElseWasmExecutor::new(wasm_method, None, 8, 2);
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],
+2 -2
View File
@@ -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, WasmExecutor};
use sp_consensus_babe::{
digests::{PreDigest, SecondaryPlainPreDigest},
Slot, BABE_ENGINE_ID,
@@ -98,7 +98,7 @@ 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().build())
}
pub fn executor_call(
+9 -16
View File
@@ -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_or_wasm_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(())
},
}
+7 -7
View File
@@ -392,14 +392,14 @@ 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 {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
},
None,
8,
2,
let executor = NativeElseWasmExecutor::new_with_wasm_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(
&keyring.generate_genesis(),
+3 -4
View File
@@ -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;
@@ -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,
)
@@ -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,
@@ -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.
@@ -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()))?
@@ -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;
@@ -93,7 +93,7 @@ impl RuntimeBuilder {
instantiation_strategy,
canonicalize_nans: false,
deterministic_stack: false,
heap_pages: HeapAllocStrategy::Static { extra_pages: 1024 },
heap_pages: DEFAULT_HEAP_ALLOC_STRATEGY,
precompile_runtime: false,
tmpdir: None,
}
@@ -477,7 +477,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,
wasm_multi_value: false,
wasm_bulk_memory: false,
wasm_reference_types: false,
+29 -6
View File
@@ -36,7 +36,10 @@ use sc_client_api::{
};
use sc_client_db::{Backend, DatabaseSettings};
use sc_consensus::import_queue::ImportQueue;
use sc_executor::RuntimeVersionOf;
use sc_executor::{
sp_wasm_interface::HostFunctions, HeapAllocStrategy, NativeElseWasmExecutor,
NativeExecutionDispatch, RuntimeVersionOf, WasmExecutor, DEFAULT_HEAP_ALLOC_STRATEGY,
};
use sc_keystore::LocalKeystore;
use sc_network::{config::SyncMode, NetworkService, NetworkStateInfo, NetworkStatusProvider};
use sc_network_bitswap::BitswapRequestHandler;
@@ -74,11 +77,10 @@ pub type TFullClient<TBl, TRtApi, TExec> =
Client<TFullBackend<TBl>, TFullCallExecutor<TBl, TExec>, TBl, TRtApi>;
/// Full client backend type.
pub type TFullBackend<TBl> = sc_client_db::Backend<TBl>;
pub type TFullBackend<TBl> = Backend<TBl>;
/// Full client call executor type.
pub type TFullCallExecutor<TBl, TExec> =
crate::client::LocalCallExecutor<TBl, sc_client_db::Backend<TBl>, TExec>;
pub type TFullCallExecutor<TBl, TExec> = crate::client::LocalCallExecutor<TBl, Backend<TBl>, TExec>;
type TFullParts<TBl, TRtApi, TExec> =
(TFullClient<TBl, TRtApi, TExec>, Arc<TFullBackend<TBl>>, KeystoreContainer, TaskManager);
@@ -229,6 +231,27 @@ where
Ok((client, backend, keystore_container, task_manager))
}
/// Creates a [`NativeElseWasmExecutor`] according to [`Configuration`].
pub fn new_native_or_wasm_executor<D: NativeExecutionDispatch>(
config: &Configuration,
) -> NativeElseWasmExecutor<D> {
NativeElseWasmExecutor::new_with_wasm_executor(new_wasm_executor(config))
}
/// Creates a [`WasmExecutor`] according to [`Configuration`].
pub fn new_wasm_executor<H: HostFunctions>(config: &Configuration) -> WasmExecutor<H> {
let strategy = config
.default_heap_pages
.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |p| HeapAllocStrategy::Static { extra_pages: p as _ });
WasmExecutor::<H>::builder()
.with_execution_method(config.wasm_method)
.with_onchain_heap_alloc_strategy(strategy)
.with_offchain_heap_alloc_strategy(strategy)
.with_max_runtime_instances(config.max_runtime_instances)
.with_runtime_cache_size(config.runtime_cache_size)
.build()
}
/// Create an instance of default DB-backend backend.
pub fn new_db_backend<Block>(
settings: DatabaseSettings,
@@ -254,7 +277,7 @@ pub fn new_client<E, Block, RA, G>(
telemetry: Option<TelemetryHandle>,
config: ClientConfig<Block>,
) -> Result<
crate::client::Client<
Client<
Backend<Block>,
crate::client::LocalCallExecutor<Block, Backend<Block>, E>,
Block,
@@ -277,7 +300,7 @@ where
execution_extensions,
)?;
crate::client::Client::new(
Client::new(
backend,
executor,
spawn_handle,
@@ -360,7 +360,7 @@ mod tests {
use super::*;
use backend::Backend;
use sc_client_api::in_mem;
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod};
use sc_executor::{NativeElseWasmExecutor, WasmExecutor};
use sp_core::{
testing::TaskExecutor,
traits::{FetchRuntimeCode, WrappedRuntimeCode},
@@ -368,14 +368,18 @@ mod tests {
use std::collections::HashMap;
use substrate_test_runtime_client::{runtime, GenesisInit, LocalExecutorDispatch};
fn executor() -> NativeElseWasmExecutor<LocalExecutorDispatch> {
NativeElseWasmExecutor::new_with_wasm_executor(
WasmExecutor::builder()
.with_max_runtime_instances(1)
.with_runtime_cache_size(2)
.build(),
)
}
#[test]
fn should_get_override_if_exists() {
let executor = NativeElseWasmExecutor::<LocalExecutorDispatch>::new(
WasmExecutionMethod::Interpreted,
Some(128),
1,
2,
);
let executor = executor();
let overrides = crate::client::wasm_override::dummy_overrides();
let onchain_code = WrappedRuntimeCode(substrate_test_runtime::wasm_binary_unwrap().into());
@@ -443,12 +447,7 @@ mod tests {
fn returns_runtime_version_from_substitute() {
const SUBSTITUTE_SPEC_NAME: &str = "substitute-spec-name-cool";
let executor = NativeElseWasmExecutor::<LocalExecutorDispatch>::new(
WasmExecutionMethod::Interpreted,
Some(128),
1,
2,
);
let executor = executor();
let backend = Arc::new(in_mem::Backend::<runtime::Block>::new());
@@ -264,21 +264,26 @@ pub fn dummy_overrides() -> WasmOverride {
#[cfg(test)]
mod tests {
use super::*;
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod};
use sc_executor::{HeapAllocStrategy, NativeElseWasmExecutor, WasmExecutor};
use std::fs::{self, File};
use substrate_test_runtime_client::LocalExecutorDispatch;
fn executor() -> NativeElseWasmExecutor<substrate_test_runtime_client::LocalExecutorDispatch> {
NativeElseWasmExecutor::<substrate_test_runtime_client::LocalExecutorDispatch>::new_with_wasm_executor(
WasmExecutor::builder()
.with_onchain_heap_alloc_strategy(HeapAllocStrategy::Static {extra_pages: 128})
.with_offchain_heap_alloc_strategy(HeapAllocStrategy::Static {extra_pages: 128})
.with_max_runtime_instances(1)
.with_runtime_cache_size(2)
.build()
)
}
fn wasm_test<F>(fun: F)
where
F: Fn(&Path, &[u8], &NativeElseWasmExecutor<LocalExecutorDispatch>),
{
let exec =
NativeElseWasmExecutor::<substrate_test_runtime_client::LocalExecutorDispatch>::new(
WasmExecutionMethod::Interpreted,
Some(128),
1,
2,
);
let exec = executor();
let bytes = substrate_test_runtime::wasm_binary_unwrap();
let dir = tempfile::tempdir().expect("Create a temporary directory");
fun(dir.path(), bytes, &exec);
@@ -287,12 +292,7 @@ mod tests {
#[test]
fn should_get_runtime_version() {
let executor = NativeElseWasmExecutor::<LocalExecutorDispatch>::new(
WasmExecutionMethod::Interpreted,
Some(128),
1,
2,
);
let executor = executor();
let version = WasmOverride::runtime_version(
&executor,
+3 -3
View File
@@ -56,9 +56,9 @@ use sp_runtime::{
pub use self::{
builder::{
build_network, build_offchain_workers, new_client, new_db_backend, new_full_client,
new_full_parts, new_full_parts_with_genesis_builder, spawn_tasks, BuildNetworkParams,
KeystoreContainer, NetworkStarter, SpawnTasksParams, TFullBackend, TFullCallExecutor,
TFullClient,
new_full_parts, new_full_parts_with_genesis_builder, new_native_or_wasm_executor,
spawn_tasks, BuildNetworkParams, KeystoreContainer, NetworkStarter, SpawnTasksParams,
TFullBackend, TFullCallExecutor, TFullClient,
},
client::{ClientConfig, LocalCallExecutor},
error::Error,
@@ -45,6 +45,7 @@ use sp_trie::{LayoutV0, TrieConfiguration};
use std::{collections::HashSet, sync::Arc};
use substrate_test_runtime::TestAPI;
use substrate_test_runtime_client::{
new_native_or_wasm_executor,
prelude::*,
runtime::{
genesismap::{insert_genesis_block, GenesisConfig},
@@ -58,29 +59,6 @@ mod db;
const TEST_ENGINE_ID: ConsensusEngineId = *b"TEST";
pub struct ExecutorDispatch;
impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
type ExtendHostFunctions = ();
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
substrate_test_runtime_client::runtime::api::dispatch(method, data)
}
fn native_version() -> sc_executor::NativeVersion {
substrate_test_runtime_client::runtime::native_version()
}
}
fn executor() -> sc_executor::NativeElseWasmExecutor<ExecutorDispatch> {
sc_executor::NativeElseWasmExecutor::new(
sc_executor::WasmExecutionMethod::Interpreted,
None,
8,
2,
)
}
fn construct_block(
backend: &InMemoryBackend<BlakeTwo256>,
number: BlockNumber,
@@ -108,7 +86,7 @@ fn construct_block(
StateMachine::new(
backend,
&mut overlay,
&executor(),
&new_native_or_wasm_executor(),
"Core_initialize_block",
&header.encode(),
Default::default(),
@@ -122,7 +100,7 @@ fn construct_block(
StateMachine::new(
backend,
&mut overlay,
&executor(),
&new_native_or_wasm_executor(),
"BlockBuilder_apply_extrinsic",
&tx.encode(),
Default::default(),
@@ -136,7 +114,7 @@ fn construct_block(
let ret_data = StateMachine::new(
backend,
&mut overlay,
&executor(),
&new_native_or_wasm_executor(),
"BlockBuilder_finalize_block",
&[],
Default::default(),
@@ -208,7 +186,7 @@ fn construct_genesis_should_work_with_native() {
let _ = StateMachine::new(
&backend,
&mut overlay,
&executor(),
&new_native_or_wasm_executor(),
"Core_execute_block",
&b1data,
Default::default(),
@@ -241,7 +219,7 @@ fn construct_genesis_should_work_with_wasm() {
let _ = StateMachine::new(
&backend,
&mut overlay,
&executor(),
&new_native_or_wasm_executor(),
"Core_execute_block",
&b1data,
Default::default(),
@@ -274,7 +252,7 @@ fn construct_genesis_with_bad_transaction_should_panic() {
let r = StateMachine::new(
&backend,
&mut overlay,
&executor(),
&new_native_or_wasm_executor(),
"Core_execute_block",
&b1data,
Default::default(),
@@ -1910,7 +1888,7 @@ fn cleans_up_closed_notification_sinks_on_block_import() {
use substrate_test_runtime_client::GenesisInit;
let backend = Arc::new(sc_client_api::in_mem::Backend::new());
let executor = substrate_test_runtime_client::new_native_executor();
let executor = new_native_or_wasm_executor();
let client_config = sc_service::ClientConfig::default();
let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
+3 -5
View File
@@ -65,9 +65,7 @@ impl<G, E, F, U> Drop for TestNet<G, E, F, U> {
}
}
pub trait TestNetNode:
Clone + Future<Output = Result<(), sc_service::Error>> + Send + 'static
{
pub trait TestNetNode: Clone + Future<Output = Result<(), Error>> + Send + 'static {
type Block: BlockT;
type Backend: Backend<Self::Block>;
type Executor: CallExecutor<Self::Block> + Send + Sync;
@@ -128,7 +126,7 @@ impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Clone
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Future
for TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
{
type Output = Result<(), sc_service::Error>;
type Output = Result<(), Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
Pin::new(&mut self.task_manager.lock().future()).poll(cx)
@@ -248,7 +246,7 @@ fn node_config<
state_pruning: Default::default(),
blocks_pruning: BlocksPruning::KeepFinalized,
chain_spec: Box::new((*spec).clone()),
wasm_method: sc_service::config::WasmExecutionMethod::Interpreted,
wasm_method: Default::default(),
wasm_runtime_overrides: Default::default(),
execution_strategies: Default::default(),
rpc_http: None,
+2 -2
View File
@@ -589,7 +589,7 @@ fn assert_runtime_updated_digest(num: usize) {
#[test]
fn set_code_with_real_wasm_blob() {
let executor = substrate_test_runtime_client::new_native_executor();
let executor = substrate_test_runtime_client::new_native_or_wasm_executor();
let mut ext = new_test_ext();
ext.register_extension(sp_core::traits::ReadRuntimeVersionExt::new(executor));
ext.execute_with(|| {
@@ -613,7 +613,7 @@ fn set_code_with_real_wasm_blob() {
#[test]
fn runtime_upgraded_with_set_storage() {
let executor = substrate_test_runtime_client::new_native_executor();
let executor = substrate_test_runtime_client::new_native_or_wasm_executor();
let mut ext = new_test_ext();
ext.register_extension(sp_core::traits::ReadRuntimeVersionExt::new(executor));
ext.execute_with(|| {
@@ -29,6 +29,7 @@ use substrate_test_runtime_client::{
use codec::Encode;
use sc_block_builder::BlockBuilderProvider;
use sp_consensus::SelectChain;
use substrate_test_runtime_client::sc_executor::WasmExecutor;
fn calling_function_with_strat(strat: ExecutionStrategy) {
let client = TestClientBuilder::new().set_execution_strategy(strat).build();
@@ -178,11 +179,8 @@ fn record_proof_works() {
// Use the proof backend to execute `execute_block`.
let mut overlay = Default::default();
let executor = NativeElseWasmExecutor::<LocalExecutorDispatch>::new(
WasmExecutionMethod::Interpreted,
None,
8,
2,
let executor = NativeElseWasmExecutor::<LocalExecutorDispatch>::new_with_wasm_executor(
WasmExecutor::builder().build(),
);
execution_proof_check_on_trie_backend(
&backend,
@@ -42,7 +42,8 @@ fn call_wasm_method_with_result<HF: HostFunctionsT>(
let executor = sc_executor::WasmExecutor::<
ExtendedHostFunctions<sp_io::SubstrateHostFunctions, HF>,
>::new(sc_executor::WasmExecutionMethod::Interpreted, Some(8), 8, None, 2);
>::builder()
.build();
let (result, allocation_stats) = executor.uncached_call_with_allocation_stats(
RuntimeBlob::uncompress_if_needed(binary).expect("Failed to parse binary"),
+2 -2
View File
@@ -27,7 +27,7 @@ pub use sc_client_api::{
BadBlocks, ForkBlocks,
};
pub use sc_client_db::{self, Backend, BlocksPruning};
pub use sc_executor::{self, NativeElseWasmExecutor, WasmExecutionMethod};
pub use sc_executor::{self, NativeElseWasmExecutor, WasmExecutionMethod, WasmExecutor};
pub use sc_service::{client, RpcHandlers};
pub use sp_consensus;
pub use sp_keyring::{
@@ -286,7 +286,7 @@ impl<Block: BlockT, D, Backend, G: GenesisInit>
Backend: sc_client_api::backend::Backend<Block> + 'static,
{
let executor = executor.into().unwrap_or_else(|| {
NativeElseWasmExecutor::new(WasmExecutionMethod::Interpreted, None, 8, 2)
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
});
let executor = LocalCallExecutor::new(
self.backend.clone(),
@@ -38,6 +38,7 @@ use sp_core::{
Pair,
};
use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT};
use substrate_test_client::sc_executor::WasmExecutor;
use substrate_test_runtime::genesismap::{additional_storage_with_genesis, GenesisConfig};
/// A prelude to import in tests.
@@ -171,7 +172,7 @@ pub type Client<B> = client::Client<
client::LocalCallExecutor<
substrate_test_runtime::Block,
B,
sc_executor::NativeElseWasmExecutor<LocalExecutorDispatch>,
NativeElseWasmExecutor<LocalExecutorDispatch>,
>,
substrate_test_runtime::Block,
substrate_test_runtime::RuntimeApi,
@@ -290,6 +291,6 @@ pub fn new() -> Client<Backend> {
}
/// Create a new native executor.
pub fn new_native_executor() -> NativeElseWasmExecutor<LocalExecutorDispatch> {
NativeElseWasmExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, None, 8, 2)
pub fn new_native_or_wasm_executor() -> NativeElseWasmExecutor<LocalExecutorDispatch> {
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
}
+2 -2
View File
@@ -376,7 +376,7 @@ mod tests {
use super::*;
use crate::{wasm_binary_unwrap, Header, Transfer};
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod};
use sc_executor::{NativeElseWasmExecutor, WasmExecutor};
use sp_core::{
map,
traits::{CallContext, CodeExecutor, RuntimeCode},
@@ -400,7 +400,7 @@ mod tests {
}
fn executor() -> NativeElseWasmExecutor<NativeDispatch> {
NativeElseWasmExecutor::new(WasmExecutionMethod::Interpreted, None, 8, 2)
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
}
fn new_test_ext() -> TestExternalities {
@@ -27,7 +27,7 @@ use sc_cli::{
execution_method_from_cli, CliConfiguration, ExecutionStrategy, Result, SharedParams,
};
use sc_client_db::BenchmarkingState;
use sc_executor::NativeElseWasmExecutor;
use sc_executor::{NativeElseWasmExecutor, WasmExecutor};
use sc_service::{Configuration, NativeExecutionDispatch};
use serde::Serialize;
use sp_core::{
@@ -209,11 +209,16 @@ impl PalletCmd {
// Do not enable storage tracking
false,
)?;
let executor = NativeElseWasmExecutor::<ExecDispatch>::new(
execution_method_from_cli(self.wasm_method, self.wasmtime_instantiation_strategy),
self.heap_pages,
2, // The runtime instances cache size.
2, // The runtime cache size
let method =
execution_method_from_cli(self.wasm_method, self.wasmtime_instantiation_strategy);
let executor = NativeElseWasmExecutor::<ExecDispatch>::new_with_wasm_executor(
WasmExecutor::builder()
.with_execution_method(method)
.with_max_runtime_instances(2)
.with_runtime_cache_size(2)
.build(),
);
let extensions = || -> Extensions {
@@ -368,7 +368,9 @@ use sc_cli::{
WasmtimeInstantiationStrategy, DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
DEFAULT_WASM_EXECUTION_METHOD,
};
use sc_executor::{sp_wasm_interface::HostFunctions, WasmExecutor};
use sc_executor::{
sp_wasm_interface::HostFunctions, HeapAllocStrategy, WasmExecutor, DEFAULT_HEAP_ALLOC_STRATEGY,
};
use sp_api::HashT;
use sp_core::{
hexdisplay::HexDisplay,
@@ -824,18 +826,20 @@ pub(crate) fn full_extensions<H: HostFunctions>(wasm_executor: WasmExecutor<H>)
extensions
}
/// Build wasm executor by default config.
pub(crate) fn build_executor<H: HostFunctions>(shared: &SharedParams) -> WasmExecutor<H> {
let heap_pages = shared.heap_pages.or(Some(2048));
let max_runtime_instances = 8;
let runtime_cache_size = 2;
let heap_pages = shared
.heap_pages
.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |p| HeapAllocStrategy::Static { extra_pages: p as _ });
WasmExecutor::new(
execution_method_from_cli(shared.wasm_method, shared.wasmtime_instantiation_strategy),
heap_pages,
max_runtime_instances,
None,
runtime_cache_size,
)
WasmExecutor::builder()
.with_execution_method(execution_method_from_cli(
shared.wasm_method,
shared.wasmtime_instantiation_strategy,
))
.with_onchain_heap_alloc_strategy(heap_pages)
.with_offchain_heap_alloc_strategy(heap_pages)
.build()
}
/// Ensure that the given `ext` is compiled with `try-runtime`