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 1 commit
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
Prev Previous commit
Next Next commit
refactor builder
  • Loading branch information
yjhmelody committed Mar 30, 2023
commit 31ac1812d337ee36552340f0c9c930947d0f3fe4
7 changes: 3 additions & 4 deletions bin/node/executor/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,12 @@ fn bench_execute_block(c: &mut Criterion) {
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::Native => (true),
ExecutionMethod::Wasm(wasm_method) => (false, wasm_method),
};

let executor = NativeElseWasmExecutor::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
);
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: 1 addition & 3 deletions bin/node/executor/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ pub fn from_block_number(n: u32) -> Header {
}

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

pub fn executor_call(
Expand Down
9 changes: 5 additions & 4 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,11 @@ impl BenchDb {

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

let client_config = sc_service::ClientConfig::default();
Expand Down
33 changes: 21 additions & 12 deletions client/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,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 @@ -111,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 @@ -282,8 +288,8 @@ 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.
Expand Down Expand Up @@ -561,13 +567,16 @@ impl<D: NativeExecutionDispatch> NativeElseWasmExecutor<D> {
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 Down Expand Up @@ -718,7 +727,7 @@ mod tests {
#[test]
fn native_executor_registers_custom_interface() {
let executor = NativeElseWasmExecutor::<MyExecutorDispatch>::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
WasmExecutor::builder().build(),
);

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

executor.uncached_call(
RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
Expand Down Expand Up @@ -446,7 +448,8 @@ 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>::builder(wasm_method)
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();
Expand Down Expand Up @@ -576,7 +579,11 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {

test_wasm_execution!(parallel_execution);
fn parallel_execution(wasm_method: WasmExecutionMethod) {
let executor = Arc::new(crate::WasmExecutor::<HostFunctions>::builder(wasm_method).build());
let executor = Arc::new(
crate::WasmExecutor::<HostFunctions>::builder()
.with_execution_method(wasm_method)
.build(),
);
let threads: Vec<_> = (0..8)
.map(|_| {
let executor = executor.clone();
Expand Down
5 changes: 1 addition & 4 deletions client/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ mod tests {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();

let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::builder(
WasmExecutionMethod::Interpreted,
)
.build();
let executor = WasmExecutor::<sp_io::SubstrateHostFunctions>::builder().build();
let res = executor
.uncached_call(
RuntimeBlob::uncompress_if_needed(wasm_binary_unwrap()).unwrap(),
Expand Down
4 changes: 1 addition & 3 deletions client/executor/wasmtime/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ use codec::{Decode as _, Encode as _};
use sc_executor_common::{
error::Error,
runtime_blob::RuntimeBlob,
wasm_runtime::{
HeapAllocStrategy, WasmModule, DEFAULT_HEAP_ALLOC_PAGES, DEFAULT_HEAP_ALLOC_STRATEGY,
},
wasm_runtime::{HeapAllocStrategy, WasmModule, DEFAULT_HEAP_ALLOC_STRATEGY},
};
use sc_runtime_test::wasm_binary_unwrap;

Expand Down
11 changes: 6 additions & 5 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use sc_client_db::{Backend, DatabaseSettings};
use sc_consensus::import_queue::ImportQueue;
use sc_executor::{
sp_wasm_interface::HostFunctions, HeapAllocStrategy, NativeElseWasmExecutor,
NativeExecutionDispatch, RuntimeVersionOf, WasmExecutor, DEFAULT_HEAP_ALLOC_PAGES,
NativeExecutionDispatch, RuntimeVersionOf, WasmExecutor, DEFAULT_HEAP_ALLOC_STRATEGY,
};
use sc_keystore::LocalKeystore;
use sc_network::{
Expand Down Expand Up @@ -241,10 +241,11 @@ pub fn new_native_executor<D: NativeExecutionDispatch>(

/// Creates a [`WasmExecutor`] according to [`Configuration`].
pub fn new_wasm_executor<H: HostFunctions>(config: &Configuration) -> WasmExecutor<H> {
let extra_pages =
config.default_heap_pages.map(|p| p as u32).unwrap_or(DEFAULT_HEAP_ALLOC_PAGES);
let strategy = HeapAllocStrategy::Static { extra_pages };
WasmExecutor::<H>::builder(config.wasm_method)
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_onchain_heap_alloc_strategy(strategy)
.with_max_runtime_instances(config.max_runtime_instances)
Expand Down
4 changes: 2 additions & 2 deletions client/service/src/client/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ mod tests {
use super::*;
use backend::Backend;
use sc_client_api::in_mem;
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod, WasmExecutor};
use sc_executor::{NativeElseWasmExecutor, WasmExecutor};
use sp_core::{
testing::TaskExecutor,
traits::{FetchRuntimeCode, WrappedRuntimeCode},
Expand All @@ -378,7 +378,7 @@ mod tests {

fn executor() -> NativeElseWasmExecutor<LocalExecutorDispatch> {
NativeElseWasmExecutor::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted)
WasmExecutor::builder()
.with_max_runtime_instances(1)
.with_runtime_cache_size(2)
.build(),
Expand Down
6 changes: 2 additions & 4 deletions client/service/src/client/wasm_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,13 @@ pub fn dummy_overrides() -> WasmOverride {
#[cfg(test)]
mod tests {
use super::*;
use sc_executor::{
HeapAllocStrategy, NativeElseWasmExecutor, WasmExecutionMethod, WasmExecutor,
};
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(WasmExecutionMethod::Interpreted)
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)
Expand Down
2 changes: 1 addition & 1 deletion primitives/api/test/tests/runtime_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn record_proof_works() {
// Use the proof backend to execute `execute_block`.
let mut overlay = Default::default();
let executor = NativeElseWasmExecutor::<LocalExecutorDispatch>::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
WasmExecutor::builder().build(),
);
execution_proof_check_on_trie_backend(
&backend,
Expand Down
4 changes: 1 addition & 3 deletions test-utils/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +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_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
)
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
});
let executor = LocalCallExecutor::new(
self.backend.clone(),
Expand Down
4 changes: 1 addition & 3 deletions test-utils/runtime/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,5 @@ pub fn new() -> Client<Backend> {

/// Create a new native executor.
pub fn new_native_executor() -> NativeElseWasmExecutor<LocalExecutorDispatch> {
NativeElseWasmExecutor::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
)
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
}
6 changes: 2 additions & 4 deletions test-utils/runtime/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ mod tests {
use super::*;

use crate::{wasm_binary_unwrap, Header, Transfer};
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod, WasmExecutor};
use sc_executor::{NativeElseWasmExecutor, WasmExecutor};
use sp_core::{
map,
traits::{CallContext, CodeExecutor, RuntimeCode},
Expand All @@ -374,9 +374,7 @@ mod tests {
}

fn executor() -> NativeElseWasmExecutor<NativeDispatch> {
NativeElseWasmExecutor::new_with_wasm_executor(
WasmExecutor::builder(WasmExecutionMethod::Interpreted).build(),
)
NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
}

fn new_test_ext() -> TestExternalities {
Expand Down
3 changes: 2 additions & 1 deletion utils/frame/benchmarking-cli/src/pallet/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ impl PalletCmd {
execution_method_from_cli(self.wasm_method, self.wasmtime_instantiation_strategy);

let executor = NativeElseWasmExecutor::<ExecDispatch>::new_with_wasm_executor(
WasmExecutor::builder(method)
WasmExecutor::builder()
.with_execution_method(method)
.with_max_runtime_instances(2)
.with_runtime_cache_size(2)
.build(),
Expand Down
28 changes: 14 additions & 14 deletions utils/frame/try-runtime/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,8 @@ use sc_cli::{
WasmtimeInstantiationStrategy, DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
DEFAULT_WASM_EXECUTION_METHOD,
};
use sc_executor::{
sp_wasm_interface::HostFunctions, HeapAllocStrategy, WasmExecutor, DEFAULT_HEAP_ALLOC_PAGES,
};
use sc_executor::{sp_wasm_interface::HostFunctions, HeapAllocStrategy, WasmExecutor};
use sc_executor_common::wasm_runtime::DEFAULT_HEAP_ALLOC_STRATEGY;
use sp_api::HashT;
use sp_core::{
hexdisplay::HexDisplay,
Expand Down Expand Up @@ -829,17 +828,18 @@ pub(crate) fn full_extensions() -> Extensions {

/// Build wasm executor by default config.
pub(crate) fn build_executor<H: HostFunctions>(shared: &SharedParams) -> WasmExecutor<H> {
let heap_pages = HeapAllocStrategy::Static {
extra_pages: shared.heap_pages.map(|p| p as u32).unwrap_or(DEFAULT_HEAP_ALLOC_PAGES),
};

WasmExecutor::builder(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()
let heap_pages = shared
.heap_pages
.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |p| HeapAllocStrategy::Static { extra_pages: p as _ });

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`
Expand Down