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