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

Commit

Permalink
refactor(sc-executor): use wasm executor builder instead of old apis (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
yjhmelody authored Apr 9, 2023
1 parent 82e824b commit 2e94e25
Show file tree
Hide file tree
Showing 29 changed files with 213 additions and 210 deletions.
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_or_wasm_executor(&config);

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
Expand Down
4 changes: 2 additions & 2 deletions bin/node/cli/benches/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
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_or_wasm_executor(&config);

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
Expand Down
11 changes: 6 additions & 5 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 @@ -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],
Expand Down
4 changes: 2 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, WasmExecutor};
use sp_consensus_babe::{
digests::{PreDigest, SecondaryPlainPreDigest},
Slot, BABE_ENGINE_ID,
Expand Down Expand Up @@ -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(
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_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(())
},
}
Expand Down
14 changes: 7 additions & 7 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
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 @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -539,6 +545,7 @@ pub struct NativeElseWasmExecutor<D: NativeExecutionDispatch> {
}

impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
///
/// Create new instance.
///
/// # Parameters
Expand All @@ -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 }
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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>(
Expand Down
4 changes: 2 additions & 2 deletions client/executor/src/integration_tests/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 2e94e25

Please sign in to comment.