From 72fb8bd3cd4a5051bb855415b360657d7ce247fb Mon Sep 17 00:00:00 2001
From: Rodrigo Quelhas <22591718+RomarQ@users.noreply.github.com>
Date: Fri, 29 Nov 2024 10:33:46 +0000
Subject: [PATCH] Expose types from `sc-service` (#5855)

# Description

At moonbeam we have worked on a `lazy-loading` feature which is a client
mode that forks a live parachain and fetches its state on-demand, we
have been able to do this by duplicating some code from
`sc_service::client`. The objective of this PR is to simplify the
implementation by making public some types in polkadot-sdk.

- Modules:
- `sc_service::client` **I do not see a point to only expose this type
when `test-helpers` feature is enabled**

## Integration

Not applicable, the PR just makes some types public.

## Review Notes

The changes included in this PR give more flexibility for client
developers by exposing important types.
---
 prdoc/pr_5855.prdoc                           | 15 +++++++
 substrate/bin/node/testing/Cargo.toml         |  2 +-
 substrate/client/network/test/Cargo.toml      |  2 +-
 substrate/client/rpc-spec-v2/Cargo.toml       |  2 +-
 .../src/chain_head/subscription/inner.rs      |  6 +--
 .../rpc-spec-v2/src/chain_head/tests.rs       |  6 +--
 substrate/client/service/Cargo.toml           |  2 -
 substrate/client/service/src/client/client.rs | 40 +------------------
 substrate/client/service/src/client/mod.rs    |  3 +-
 substrate/client/service/src/lib.rs           |  5 +--
 substrate/client/service/test/Cargo.toml      |  2 +-
 .../client/service/test/src/client/mod.rs     |  6 +--
 substrate/test-utils/client/Cargo.toml        |  4 +-
 substrate/test-utils/runtime/Cargo.toml       |  2 +-
 14 files changed, 34 insertions(+), 63 deletions(-)
 create mode 100644 prdoc/pr_5855.prdoc

diff --git a/prdoc/pr_5855.prdoc b/prdoc/pr_5855.prdoc
new file mode 100644
index 00000000000..7735cfee9f3
--- /dev/null
+++ b/prdoc/pr_5855.prdoc
@@ -0,0 +1,15 @@
+# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
+# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
+
+title: Remove feature `test-helpers` from sc-service
+
+doc:
+  - audience: Node Dev
+    description: |
+      Removes feature `test-helpers` from sc-service.
+
+crates:
+  - name: sc-service
+    bump: major
+  - name: sc-rpc-spec-v2
+    bump: major
diff --git a/substrate/bin/node/testing/Cargo.toml b/substrate/bin/node/testing/Cargo.toml
index 16112386ad7..1972c03a368 100644
--- a/substrate/bin/node/testing/Cargo.toml
+++ b/substrate/bin/node/testing/Cargo.toml
@@ -37,7 +37,7 @@ sc-client-api = { workspace = true, default-features = true }
 sc-client-db = { features = ["rocksdb"], workspace = true, default-features = true }
 sc-consensus = { workspace = true, default-features = true }
 sc-executor = { workspace = true, default-features = true }
-sc-service = { features = ["rocksdb", "test-helpers"], workspace = true, default-features = true }
+sc-service = { features = ["rocksdb"], workspace = true, default-features = true }
 sp-api = { workspace = true, default-features = true }
 sp-block-builder = { workspace = true, default-features = true }
 sp-blockchain = { workspace = true, default-features = true }
diff --git a/substrate/client/network/test/Cargo.toml b/substrate/client/network/test/Cargo.toml
index ebece1762f2..6340d1dfb2f 100644
--- a/substrate/client/network/test/Cargo.toml
+++ b/substrate/client/network/test/Cargo.toml
@@ -33,7 +33,7 @@ sc-network-types = { workspace = true, default-features = true }
 sc-utils = { workspace = true, default-features = true }
 sc-network-light = { workspace = true, default-features = true }
 sc-network-sync = { workspace = true, default-features = true }
-sc-service = { features = ["test-helpers"], workspace = true }
+sc-service = { workspace = true }
 sp-blockchain = { workspace = true, default-features = true }
 sp-consensus = { workspace = true, default-features = true }
 sp-core = { workspace = true, default-features = true }
diff --git a/substrate/client/rpc-spec-v2/Cargo.toml b/substrate/client/rpc-spec-v2/Cargo.toml
index b304bc90592..70f68436767 100644
--- a/substrate/client/rpc-spec-v2/Cargo.toml
+++ b/substrate/client/rpc-spec-v2/Cargo.toml
@@ -56,7 +56,7 @@ sp-consensus = { workspace = true, default-features = true }
 sp-externalities = { workspace = true, default-features = true }
 sp-maybe-compressed-blob = { workspace = true, default-features = true }
 sc-block-builder = { workspace = true, default-features = true }
-sc-service = { features = ["test-helpers"], workspace = true, default-features = true }
+sc-service = { workspace = true, default-features = true }
 sc-rpc = { workspace = true, default-features = true, features = ["test-helpers"] }
 assert_matches = { workspace = true }
 pretty_assertions = { workspace = true }
diff --git a/substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs b/substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs
index 95a7c7fe183..3e1bd23776d 100644
--- a/substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs
+++ b/substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs
@@ -784,7 +784,7 @@ mod tests {
 	use super::*;
 	use jsonrpsee::ConnectionId;
 	use sc_block_builder::BlockBuilderBuilder;
-	use sc_service::client::new_in_mem;
+	use sc_service::client::new_with_backend;
 	use sp_consensus::BlockOrigin;
 	use sp_core::{testing::TaskExecutor, H256};
 	use substrate_test_runtime_client::{
@@ -811,13 +811,13 @@ mod tests {
 		)
 		.unwrap();
 		let client = Arc::new(
-			new_in_mem::<_, Block, _, RuntimeApi>(
+			new_with_backend::<_, _, Block, _, RuntimeApi>(
 				backend.clone(),
 				executor,
 				genesis_block_builder,
+				Box::new(TaskExecutor::new()),
 				None,
 				None,
-				Box::new(TaskExecutor::new()),
 				client_config,
 			)
 			.unwrap(),
diff --git a/substrate/client/rpc-spec-v2/src/chain_head/tests.rs b/substrate/client/rpc-spec-v2/src/chain_head/tests.rs
index c505566d887..21e8365622a 100644
--- a/substrate/client/rpc-spec-v2/src/chain_head/tests.rs
+++ b/substrate/client/rpc-spec-v2/src/chain_head/tests.rs
@@ -34,7 +34,7 @@ use jsonrpsee::{
 use sc_block_builder::BlockBuilderBuilder;
 use sc_client_api::ChildInfo;
 use sc_rpc::testing::TokioTestExecutor;
-use sc_service::client::new_in_mem;
+use sc_service::client::new_with_backend;
 use sp_blockchain::HeaderBackend;
 use sp_consensus::BlockOrigin;
 use sp_core::{
@@ -2547,13 +2547,13 @@ async fn pin_block_references() {
 	.unwrap();
 
 	let client = Arc::new(
-		new_in_mem::<_, Block, _, RuntimeApi>(
+		new_with_backend::<_, _, Block, _, RuntimeApi>(
 			backend.clone(),
 			executor,
 			genesis_block_builder,
+			Box::new(TokioTestExecutor::default()),
 			None,
 			None,
-			Box::new(TokioTestExecutor::default()),
 			client_config,
 		)
 		.unwrap(),
diff --git a/substrate/client/service/Cargo.toml b/substrate/client/service/Cargo.toml
index f2fc65ef243..3981395d976 100644
--- a/substrate/client/service/Cargo.toml
+++ b/substrate/client/service/Cargo.toml
@@ -20,8 +20,6 @@ default = ["rocksdb"]
 # The RocksDB feature activates the RocksDB database backend. If it is not activated, and you pass
 # a path to a database, an error will be produced at runtime.
 rocksdb = ["sc-client-db/rocksdb"]
-# exposes the client type
-test-helpers = []
 runtime-benchmarks = [
 	"sc-client-db/runtime-benchmarks",
 	"sp-runtime/runtime-benchmarks",
diff --git a/substrate/client/service/src/client/client.rs b/substrate/client/service/src/client/client.rs
index ce5b92551bf..eddbb9260c0 100644
--- a/substrate/client/service/src/client/client.rs
+++ b/substrate/client/service/src/client/client.rs
@@ -85,10 +85,8 @@ use std::{
 	sync::Arc,
 };
 
-#[cfg(feature = "test-helpers")]
-use {
-	super::call_executor::LocalCallExecutor, sc_client_api::in_mem, sp_core::traits::CodeExecutor,
-};
+use super::call_executor::LocalCallExecutor;
+use sp_core::traits::CodeExecutor;
 
 type NotificationSinks<T> = Mutex<Vec<TracingUnboundedSender<T>>>;
 
@@ -152,39 +150,6 @@ enum PrepareStorageChangesResult<Block: BlockT> {
 	Discard(ImportResult),
 	Import(Option<sc_consensus::StorageChanges<Block>>),
 }
-
-/// Create an instance of in-memory client.
-#[cfg(feature = "test-helpers")]
-pub fn new_in_mem<E, Block, G, RA>(
-	backend: Arc<in_mem::Backend<Block>>,
-	executor: E,
-	genesis_block_builder: G,
-	prometheus_registry: Option<Registry>,
-	telemetry: Option<TelemetryHandle>,
-	spawn_handle: Box<dyn SpawnNamed>,
-	config: ClientConfig<Block>,
-) -> sp_blockchain::Result<
-	Client<in_mem::Backend<Block>, LocalCallExecutor<Block, in_mem::Backend<Block>, E>, Block, RA>,
->
-where
-	E: CodeExecutor + sc_executor::RuntimeVersionOf,
-	Block: BlockT,
-	G: BuildGenesisBlock<
-			Block,
-			BlockImportOperation = <in_mem::Backend<Block> as backend::Backend<Block>>::BlockImportOperation,
-		>,
-{
-	new_with_backend(
-		backend,
-		executor,
-		genesis_block_builder,
-		spawn_handle,
-		prometheus_registry,
-		telemetry,
-		config,
-	)
-}
-
 /// Client configuration items.
 #[derive(Debug, Clone)]
 pub struct ClientConfig<Block: BlockT> {
@@ -218,7 +183,6 @@ impl<Block: BlockT> Default for ClientConfig<Block> {
 
 /// Create a client with the explicitly provided backend.
 /// This is useful for testing backend implementations.
-#[cfg(feature = "test-helpers")]
 pub fn new_with_backend<B, E, Block, G, RA>(
 	backend: Arc<B>,
 	executor: E,
diff --git a/substrate/client/service/src/client/mod.rs b/substrate/client/service/src/client/mod.rs
index ec77a92f162..3020b3d296f 100644
--- a/substrate/client/service/src/client/mod.rs
+++ b/substrate/client/service/src/client/mod.rs
@@ -56,5 +56,4 @@ pub use call_executor::LocalCallExecutor;
 pub use client::{Client, ClientConfig};
 pub(crate) use code_provider::CodeProvider;
 
-#[cfg(feature = "test-helpers")]
-pub use self::client::{new_in_mem, new_with_backend};
+pub use self::client::new_with_backend;
diff --git a/substrate/client/service/src/lib.rs b/substrate/client/service/src/lib.rs
index 9c01d7288a8..b5a38d875e3 100644
--- a/substrate/client/service/src/lib.rs
+++ b/substrate/client/service/src/lib.rs
@@ -23,14 +23,11 @@
 #![recursion_limit = "1024"]
 
 pub mod chain_ops;
+pub mod client;
 pub mod config;
 pub mod error;
 
 mod builder;
-#[cfg(feature = "test-helpers")]
-pub mod client;
-#[cfg(not(feature = "test-helpers"))]
-mod client;
 mod metrics;
 mod task_manager;
 
diff --git a/substrate/client/service/test/Cargo.toml b/substrate/client/service/test/Cargo.toml
index 0edfc5b1931..632b98104f6 100644
--- a/substrate/client/service/test/Cargo.toml
+++ b/substrate/client/service/test/Cargo.toml
@@ -31,7 +31,7 @@ sc-consensus = { workspace = true, default-features = true }
 sc-executor = { workspace = true, default-features = true }
 sc-network = { workspace = true, default-features = true }
 sc-network-sync = { workspace = true, default-features = true }
-sc-service = { features = ["test-helpers"], workspace = true, default-features = true }
+sc-service = { workspace = true, default-features = true }
 sc-transaction-pool-api = { workspace = true, default-features = true }
 sp-api = { workspace = true, default-features = true }
 sp-blockchain = { workspace = true, default-features = true }
diff --git a/substrate/client/service/test/src/client/mod.rs b/substrate/client/service/test/src/client/mod.rs
index 55bbfcdd859..ead90c4c65d 100644
--- a/substrate/client/service/test/src/client/mod.rs
+++ b/substrate/client/service/test/src/client/mod.rs
@@ -29,7 +29,7 @@ use sc_consensus::{
 	BlockCheckParams, BlockImport, BlockImportParams, ForkChoiceStrategy, ImportResult,
 };
 use sc_executor::WasmExecutor;
-use sc_service::client::{new_in_mem, Client, LocalCallExecutor};
+use sc_service::client::{new_with_backend, Client, LocalCallExecutor};
 use sp_api::ProvideRuntimeApi;
 use sp_consensus::{BlockOrigin, Error as ConsensusError, SelectChain};
 use sp_core::{testing::TaskExecutor, traits::CallContext, H256};
@@ -2087,13 +2087,13 @@ fn cleans_up_closed_notification_sinks_on_block_import() {
 	// NOTE: we need to build the client here instead of using the client
 	// provided by test_runtime_client otherwise we can't access the private
 	// `import_notification_sinks` and `finality_notification_sinks` fields.
-	let mut client = new_in_mem::<_, Block, _, RuntimeApi>(
+	let mut client = new_with_backend::<_, _, Block, _, RuntimeApi>(
 		backend,
 		executor,
 		genesis_block_builder,
+		Box::new(TaskExecutor::new()),
 		None,
 		None,
-		Box::new(TaskExecutor::new()),
 		client_config,
 	)
 	.unwrap();
diff --git a/substrate/test-utils/client/Cargo.toml b/substrate/test-utils/client/Cargo.toml
index ebd1eab5980..a67c91fc5f7 100644
--- a/substrate/test-utils/client/Cargo.toml
+++ b/substrate/test-utils/client/Cargo.toml
@@ -29,9 +29,7 @@ sc-client-db = { features = [
 sc-consensus = { workspace = true, default-features = true }
 sc-executor = { workspace = true, default-features = true }
 sc-offchain = { workspace = true, default-features = true }
-sc-service = { features = [
-	"test-helpers",
-], workspace = true }
+sc-service = { workspace = true }
 sp-blockchain = { workspace = true, default-features = true }
 sp-consensus = { workspace = true, default-features = true }
 sp-core = { workspace = true, default-features = true }
diff --git a/substrate/test-utils/runtime/Cargo.toml b/substrate/test-utils/runtime/Cargo.toml
index 1c82c73072b..96a88805287 100644
--- a/substrate/test-utils/runtime/Cargo.toml
+++ b/substrate/test-utils/runtime/Cargo.toml
@@ -45,7 +45,7 @@ sp-consensus-grandpa = { features = ["serde"], workspace = true }
 sp-trie = { workspace = true }
 sp-transaction-pool = { workspace = true }
 trie-db = { workspace = true }
-sc-service = { features = ["test-helpers"], optional = true, workspace = true }
+sc-service = { optional = true, workspace = true }
 sp-state-machine = { workspace = true }
 sp-externalities = { workspace = true }
 
-- 
GitLab