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
+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,