From 49a41ab3bb3f630c20e5b24cec8d92382404631c Mon Sep 17 00:00:00 2001
From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com>
Date: Tue, 28 Nov 2023 12:18:05 +0100
Subject: [PATCH] substrate-node: NativeElseWasmExecutor is no longer used

---
 substrate/bin/node/cli/src/command.rs       |  3 +--
 substrate/bin/node/cli/src/service.rs       |  8 +++---
 substrate/bin/node/executor/Cargo.toml      |  1 +
 substrate/bin/node/executor/src/lib.rs      | 28 ++++++---------------
 substrate/bin/node/executor/tests/common.rs |  9 +++----
 substrate/bin/node/inspect/Cargo.toml       |  1 +
 substrate/bin/node/inspect/src/command.rs   |  7 +++---
 substrate/bin/node/testing/src/bench.rs     | 14 +++++------
 substrate/bin/node/testing/src/client.rs    | 19 ++++++++++----
 9 files changed, 40 insertions(+), 50 deletions(-)

diff --git a/substrate/bin/node/cli/src/command.rs b/substrate/bin/node/cli/src/command.rs
index 16d0415ff26..dc28705c2ae 100644
--- a/substrate/bin/node/cli/src/command.rs
+++ b/substrate/bin/node/cli/src/command.rs
@@ -24,7 +24,6 @@ use crate::{
 };
 use frame_benchmarking_cli::*;
 use kitchensink_runtime::{ExistentialDeposit, RuntimeApi};
-use node_executor::ExecutorDispatch;
 use node_primitives::Block;
 use sc_cli::{Result, SubstrateCli};
 use sc_service::PartialComponents;
@@ -89,7 +88,7 @@ pub fn run() -> Result<()> {
 		Some(Subcommand::Inspect(cmd)) => {
 			let runner = cli.create_runner(cmd)?;
 
-			runner.sync_run(|config| cmd.run::<Block, RuntimeApi, ExecutorDispatch>(config))
+			runner.sync_run(|config| cmd.run::<Block, RuntimeApi>(config))
 		},
 		Some(Subcommand::Benchmark(cmd)) => {
 			let runner = cli.create_runner(cmd)?;
diff --git a/substrate/bin/node/cli/src/service.rs b/substrate/bin/node/cli/src/service.rs
index 1c71b5a3956..a7237b6de43 100644
--- a/substrate/bin/node/cli/src/service.rs
+++ b/substrate/bin/node/cli/src/service.rs
@@ -26,11 +26,10 @@ use frame_benchmarking_cli::SUBSTRATE_REFERENCE_HARDWARE;
 use frame_system_rpc_runtime_api::AccountNonceApi;
 use futures::prelude::*;
 use kitchensink_runtime::RuntimeApi;
-use node_executor::ExecutorDispatch;
+use node_executor::ClientWasmExecutor;
 use node_primitives::Block;
 use sc_client_api::{Backend, BlockBackend};
 use sc_consensus_babe::{self, SlotProportion};
-use sc_executor::NativeElseWasmExecutor;
 use sc_network::{event::Event, NetworkEventStream, NetworkService};
 use sc_network_sync::{warp::WarpSyncParams, SyncingService};
 use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
@@ -43,8 +42,7 @@ use sp_runtime::{generic, traits::Block as BlockT, SaturatedConversion};
 use std::sync::Arc;
 
 /// The full client type definition.
-pub type FullClient =
-	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
+pub type FullClient = sc_service::TFullClient<Block, RuntimeApi, ClientWasmExecutor>;
 type FullBackend = sc_service::TFullBackend<Block>;
 type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
 type FullGrandpaBlockImport =
@@ -174,7 +172,7 @@ pub fn new_partial(
 		})
 		.transpose()?;
 
-	let executor = sc_service::new_native_or_wasm_executor(&config);
+	let executor = sc_service::new_wasm_executor(&config);
 
 	let (client, backend, keystore_container, task_manager) =
 		sc_service::new_full_parts::<Block, RuntimeApi, _>(
diff --git a/substrate/bin/node/executor/Cargo.toml b/substrate/bin/node/executor/Cargo.toml
index 595a313d2cb..a0138f841ff 100644
--- a/substrate/bin/node/executor/Cargo.toml
+++ b/substrate/bin/node/executor/Cargo.toml
@@ -20,6 +20,7 @@ node-primitives = { path = "../primitives" }
 kitchensink-runtime = { path = "../runtime" }
 sc-executor = { path = "../../../client/executor" }
 sp-core = { path = "../../../primitives/core", features=["serde"] }
+sp-io = { path = "../../../primitives/io" }
 sp-keystore = { path = "../../../primitives/keystore" }
 sp-state-machine = { path = "../../../primitives/state-machine" }
 sp-tracing = { path = "../../../primitives/tracing" }
diff --git a/substrate/bin/node/executor/src/lib.rs b/substrate/bin/node/executor/src/lib.rs
index 3557a16740b..09f52c31b5b 100644
--- a/substrate/bin/node/executor/src/lib.rs
+++ b/substrate/bin/node/executor/src/lib.rs
@@ -15,26 +15,12 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-//! A `CodeExecutor` specialization which uses natively compiled runtime when the wasm to be
-//! executed is equivalent to the natively compiled code.
+//! Provides executor related types intended to use accross substrate node.
 
-pub use sc_executor::NativeElseWasmExecutor;
+/// Host functions required for kitchensink runtime and substrate node.
+pub type HostFunctions =
+	(sp_io::SubstrateHostFunctions, sp_statement_store::runtime_api::HostFunctions);
 
-// Declare an instance of the native executor named `ExecutorDispatch`. Include the wasm binary as
-// the equivalent wasm code.
-pub struct ExecutorDispatch;
-
-impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
-	type ExtendHostFunctions = (
-		frame_benchmarking::benchmarking::HostFunctions,
-		sp_statement_store::runtime_api::HostFunctions,
-	);
-
-	fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
-		kitchensink_runtime::api::dispatch(method, data)
-	}
-
-	fn native_version() -> sc_executor::NativeVersion {
-		kitchensink_runtime::native_version()
-	}
-}
+/// A specialized `WasmExecutor` intended to use accross substrate node. It provides all required
+/// HostFunctions.
+pub type ClientWasmExecutor = sc_executor::WasmExecutor<HostFunctions>;
diff --git a/substrate/bin/node/executor/tests/common.rs b/substrate/bin/node/executor/tests/common.rs
index 2d68c88db92..83595b3de45 100644
--- a/substrate/bin/node/executor/tests/common.rs
+++ b/substrate/bin/node/executor/tests/common.rs
@@ -18,7 +18,7 @@
 use codec::{Decode, Encode};
 use frame_support::Hashable;
 use frame_system::offchain::AppCrypto;
-use sc_executor::{error::Result, NativeElseWasmExecutor, WasmExecutor};
+use sc_executor::error::Result;
 use sp_consensus_babe::{
 	digests::{PreDigest, SecondaryPlainPreDigest},
 	Slot, BABE_ENGINE_ID,
@@ -38,11 +38,10 @@ use kitchensink_runtime::{
 	constants::currency::*, Block, BuildStorage, CheckedExtrinsic, Header, Runtime,
 	UncheckedExtrinsic,
 };
-use node_executor::ExecutorDispatch;
 use node_primitives::{BlockNumber, Hash};
 use node_testing::keyring::*;
 use sp_externalities::Externalities;
-use staging_node_executor as node_executor;
+use staging_node_executor::ClientWasmExecutor;
 
 pub const TEST_KEY_TYPE_ID: KeyTypeId = KeyTypeId(*b"test");
 
@@ -98,8 +97,8 @@ pub fn from_block_number(n: u32) -> Header {
 	Header::new(n, Default::default(), Default::default(), [69; 32].into(), Default::default())
 }
 
-pub fn executor() -> NativeElseWasmExecutor<ExecutorDispatch> {
-	NativeElseWasmExecutor::new_with_wasm_executor(WasmExecutor::builder().build())
+pub fn executor() -> ClientWasmExecutor {
+	ClientWasmExecutor::builder().build()
 }
 
 pub fn executor_call(
diff --git a/substrate/bin/node/inspect/Cargo.toml b/substrate/bin/node/inspect/Cargo.toml
index 30cc22b0e8c..c1b3e0dec94 100644
--- a/substrate/bin/node/inspect/Cargo.toml
+++ b/substrate/bin/node/inspect/Cargo.toml
@@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
 clap = { version = "4.4.6", features = ["derive"] }
 codec = { package = "parity-scale-codec", version = "3.6.1" }
 thiserror = "1.0"
+node-executor = { package = "staging-node-executor", path = "../executor" }
 sc-cli = { path = "../../../client/cli" }
 sc-client-api = { path = "../../../client/api" }
 sc-service = { path = "../../../client/service", default-features = false}
diff --git a/substrate/bin/node/inspect/src/command.rs b/substrate/bin/node/inspect/src/command.rs
index dcecfd78826..db5c990b4bf 100644
--- a/substrate/bin/node/inspect/src/command.rs
+++ b/substrate/bin/node/inspect/src/command.rs
@@ -23,18 +23,17 @@ use crate::{
 	Inspector,
 };
 use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams};
-use sc_service::{Configuration, NativeExecutionDispatch};
+use sc_service::Configuration;
 use sp_runtime::traits::Block;
 
 impl InspectCmd {
 	/// Run the inspect command, passing the inspector.
-	pub fn run<B, RA, D>(&self, config: Configuration) -> Result<()>
+	pub fn run<B, RA>(&self, config: Configuration) -> Result<()>
 	where
 		B: Block,
 		RA: Send + Sync + 'static,
-		D: NativeExecutionDispatch + 'static,
 	{
-		let executor = sc_service::new_native_or_wasm_executor::<D>(&config);
+		let executor = sc_service::new_wasm_executor::<node_executor::HostFunctions>(&config);
 		let client = sc_service::new_full_client::<B, RA, _>(&config, None, executor)?;
 		let inspect = Inspector::<B>::new(client);
 
diff --git a/substrate/bin/node/testing/src/bench.rs b/substrate/bin/node/testing/src/bench.rs
index 89b96c0191c..98d3b968a35 100644
--- a/substrate/bin/node/testing/src/bench.rs
+++ b/substrate/bin/node/testing/src/bench.rs
@@ -43,7 +43,7 @@ use sc_block_builder::BlockBuilderBuilder;
 use sc_client_api::{execution_extensions::ExecutionExtensions, UsageProvider};
 use sc_client_db::PruningMode;
 use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy, ImportResult, ImportedAux};
-use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod, WasmtimeInstantiationStrategy};
+use sc_executor::{WasmExecutionMethod, WasmtimeInstantiationStrategy};
 use sp_api::ProvideRuntimeApi;
 use sp_block_builder::BlockBuilder;
 use sp_consensus::BlockOrigin;
@@ -388,13 +388,11 @@ impl BenchDb {
 		let task_executor = TaskExecutor::new();
 
 		let backend = sc_service::new_db_backend(db_config).expect("Should not fail");
-		let executor = NativeElseWasmExecutor::new_with_wasm_executor(
-			sc_executor::WasmExecutor::builder()
-				.with_execution_method(WasmExecutionMethod::Compiled {
-					instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
-				})
-				.build(),
-		);
+		let 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(
diff --git a/substrate/bin/node/testing/src/client.rs b/substrate/bin/node/testing/src/client.rs
index 22276833fb6..d572afde5da 100644
--- a/substrate/bin/node/testing/src/client.rs
+++ b/substrate/bin/node/testing/src/client.rs
@@ -23,7 +23,7 @@ use sp_runtime::BuildStorage;
 pub use substrate_test_client::*;
 
 /// Call executor for `kitchensink-runtime` `TestClient`.
-pub type ExecutorDispatch = sc_executor::NativeElseWasmExecutor<node_executor::ExecutorDispatch>;
+use node_executor::ClientWasmExecutor;
 
 /// Default backend type.
 pub type Backend = sc_client_db::Backend<node_primitives::Block>;
@@ -31,7 +31,7 @@ pub type Backend = sc_client_db::Backend<node_primitives::Block>;
 /// Test client type.
 pub type Client = client::Client<
 	Backend,
-	client::LocalCallExecutor<node_primitives::Block, Backend, ExecutorDispatch>,
+	client::LocalCallExecutor<node_primitives::Block, Backend, ClientWasmExecutor>,
 	node_primitives::Block,
 	kitchensink_runtime::RuntimeApi,
 >;
@@ -63,7 +63,7 @@ pub trait TestClientBuilderExt: Sized {
 impl TestClientBuilderExt
 	for substrate_test_client::TestClientBuilder<
 		node_primitives::Block,
-		client::LocalCallExecutor<node_primitives::Block, Backend, ExecutorDispatch>,
+		client::LocalCallExecutor<node_primitives::Block, Backend, ClientWasmExecutor>,
 		Backend,
 		GenesisParameters,
 	>
@@ -71,8 +71,17 @@ impl TestClientBuilderExt
 	fn new() -> Self {
 		Self::default()
 	}
-
 	fn build(self) -> Client {
-		self.build_with_native_executor(None).0
+		let executor = ClientWasmExecutor::builder().build();
+		use sc_service::client::LocalCallExecutor;
+		use std::sync::Arc;
+		let executor = LocalCallExecutor::new(
+			self.backend().clone(),
+			executor.clone(),
+			Default::default(),
+			ExecutionExtensions::new(None, Arc::new(executor)),
+		)
+		.expect("Creates LocalCallExecutor");
+		self.build_with_executor(executor).0
 	}
 }
-- 
GitLab