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

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

Merged
Show file tree
Hide file tree
Changes from all 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_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