Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 3b62357

Browse files
authored
Allow using any polkadot client instead of enum Client (#1575)
* WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * Apply suggestions from code review * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * WIP Forked at: e916423 Parent branch: origin/rococo-branch * CLEANUP Forked at: e916423 Parent branch: origin/rococo-branch * link in doc * doc
1 parent f73f2a8 commit 3b62357

File tree

7 files changed

+89
-31
lines changed

7 files changed

+89
-31
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collator/src/lib.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub use sc_network::PeerId;
6767
pub use service::{RuntimeApiCollection, Client};
6868
pub use sc_cli::SubstrateCli;
6969
#[cfg(not(feature = "service-rewr"))]
70-
use polkadot_service::{FullNodeHandles, AbstractClient};
70+
use polkadot_service::{FullNodeHandles, AbstractClient, ClientHandle};
7171
#[cfg(feature = "service-rewr")]
7272
use polkadot_service_new::{
7373
self as polkadot_service,
@@ -77,6 +77,7 @@ use sc_service::SpawnTaskHandle;
7777
use sp_core::traits::SpawnNamed;
7878
use sp_runtime::traits::BlakeTwo256;
7979
use consensus_common::SyncOracle;
80+
use sc_client_api::Backend as BackendT;
8081

8182
const COLLATION_TIMEOUT: Duration = Duration::from_secs(30);
8283

@@ -117,14 +118,19 @@ pub trait BuildParachainContext {
117118
type ParachainContext: self::ParachainContext;
118119

119120
/// Build the `ParachainContext`.
120-
fn build<SP>(
121+
fn build<SP, Client, Backend>(
121122
self,
122-
client: polkadot_service::Client,
123+
client: Arc<Client>,
123124
spawner: SP,
124125
network: impl Network + SyncOracle + Clone + 'static,
125126
) -> Result<Self::ParachainContext, ()>
126127
where
127-
SP: SpawnNamed + Clone + Send + Sync + 'static;
128+
SP: SpawnNamed + Clone + Send + Sync + 'static,
129+
Backend: BackendT<Block>,
130+
Backend::State: sp_api::StateBackend<BlakeTwo256>,
131+
Client: polkadot_service::AbstractClient<Block, Backend> + 'static,
132+
Client::Api: RuntimeApiCollection<StateBackend = Backend::State>,
133+
;
128134
}
129135

130136
/// Parachain context needed for collation.
@@ -194,11 +200,12 @@ pub async fn collate<P>(
194200
Some(collation)
195201
}
196202

203+
/// Build a collator service based on the `ClientHandle`.
197204
#[cfg(feature = "service-rewr")]
198-
fn build_collator_service<P>(
205+
pub fn build_collator_service<P>(
199206
spawner: SpawnTaskHandle,
200207
handles: FullNodeHandles,
201-
client: polkadot_service::Client,
208+
client: impl ClientHandle,
202209
para_id: ParaId,
203210
key: Arc<CollatorPair>,
204211
build_parachain_context: P,
@@ -217,7 +224,6 @@ struct BuildCollationWork<P> {
217224
key: Arc<CollatorPair>,
218225
build_parachain_context: P,
219226
spawner: SpawnTaskHandle,
220-
client: polkadot_service::Client,
221227
}
222228

223229
impl<P> polkadot_service::ExecuteWithClient for BuildCollationWork<P>
@@ -233,7 +239,7 @@ impl<P> polkadot_service::ExecuteWithClient for BuildCollationWork<P>
233239
Backend: sc_client_api::Backend<Block>,
234240
Backend::State: sp_api::StateBackend<BlakeTwo256>,
235241
Api: RuntimeApiCollection<StateBackend = Backend::State>,
236-
Client: AbstractClient<Block, Backend, Api = Api> + 'static
242+
Client: AbstractClient<Block, Backend, Api = Api> + 'static,
237243
{
238244
let polkadot_network = self.handles
239245
.polkadot_network
@@ -246,7 +252,7 @@ impl<P> polkadot_service::ExecuteWithClient for BuildCollationWork<P>
246252
.ok_or_else(|| "Collator cannot run when validation networking has not been started")?;
247253

248254
let parachain_context = match self.build_parachain_context.build(
249-
self.client,
255+
client.clone(),
250256
self.spawner.clone(),
251257
polkadot_network.clone(),
252258
) {
@@ -346,11 +352,12 @@ impl<P> polkadot_service::ExecuteWithClient for BuildCollationWork<P>
346352
}
347353
}
348354

355+
/// Build a collator service based on the `ClientHandle`.
349356
#[cfg(not(feature = "service-rewr"))]
350-
fn build_collator_service<P>(
357+
pub fn build_collator_service<P>(
351358
spawner: SpawnTaskHandle,
352359
handles: FullNodeHandles,
353-
client: polkadot_service::Client,
360+
client: impl ClientHandle,
354361
para_id: ParaId,
355362
key: Arc<CollatorPair>,
356363
build_parachain_context: P,
@@ -366,7 +373,6 @@ fn build_collator_service<P>(
366373
key,
367374
build_parachain_context,
368375
spawner,
369-
client: client.clone(),
370376
})
371377
}
372378

@@ -454,12 +460,19 @@ mod tests {
454460
impl BuildParachainContext for BuildDummyParachainContext {
455461
type ParachainContext = DummyParachainContext;
456462

457-
fn build<SP>(
463+
fn build<SP, Client, Backend>(
458464
self,
459-
_: polkadot_service::Client,
465+
_: Arc<Client>,
460466
_: SP,
461467
_: impl Network + Clone + 'static,
462-
) -> Result<Self::ParachainContext, ()> {
468+
) -> Result<Self::ParachainContext, ()>
469+
where
470+
SP: SpawnNamed + Clone + Send + Sync + 'static,
471+
Backend: BackendT<Block>,
472+
Backend::State: sp_api::StateBackend<BlakeTwo256>,
473+
Client: polkadot_service::AbstractClient<Block, Backend> + 'static,
474+
Client::Api: RuntimeApiCollection<StateBackend = Backend::State>,
475+
{
463476
Ok(DummyParachainContext)
464477
}
465478
}

node/test-service/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use polkadot_primitives::v0::{
2626
Block, Hash, CollatorId, Id as ParaId,
2727
};
2828
use polkadot_runtime_common::{parachains, registrar, BlockHashCount};
29-
use polkadot_service::{new_full, FullNodeHandles, AbstractClient};
29+
use polkadot_service::{new_full, FullNodeHandles, AbstractClient, ClientHandle, ExecuteWithClient};
3030
use polkadot_test_runtime::{RestrictFunctionality, Runtime, SignedExtra, SignedPayload, VERSION};
3131
use sc_chain_spec::ChainSpec;
3232
use sc_client_api::{execution_extensions::ExecutionStrategies, BlockchainEvents};
@@ -67,7 +67,7 @@ pub fn polkadot_test_new_full(
6767
) -> Result<
6868
(
6969
TaskManager,
70-
Arc<impl AbstractClient<Block, TFullBackend<Block>>>,
70+
Arc<polkadot_service::FullClient<polkadot_test_runtime::RuntimeApi, PolkadotTestExecutor>>,
7171
FullNodeHandles,
7272
Arc<NetworkService<Block, Hash>>,
7373
Arc<RpcHandlers>,
@@ -88,6 +88,15 @@ pub fn polkadot_test_new_full(
8888
Ok((task_manager, client, handles, network, rpc_handlers))
8989
}
9090

91+
/// A wrapper for the test client that implements `ClientHandle`.
92+
pub struct TestClient(pub Arc<polkadot_service::FullClient<polkadot_test_runtime::RuntimeApi, PolkadotTestExecutor>>);
93+
94+
impl ClientHandle for TestClient {
95+
fn execute_with<T: ExecuteWithClient>(&self, t: T) -> T::Output {
96+
T::execute_with_client::<_, _, polkadot_service::FullBackend>(t, self.0.clone())
97+
}
98+
}
99+
91100
/// Create a Polkadot `Configuration`. By default an in-memory socket will be used, therefore you need to provide boot
92101
/// nodes if you want the future node to be connected to other nodes. The `storage_update_func` can be used to make
93102
/// adjustements to the runtime before the node starts.

parachain/test-parachains/adder/collator/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ adder = { package = "test-parachain-adder", path = ".." }
99
parachain = { package = "polkadot-parachain", path = "../../.." }
1010
collator = { package = "polkadot-collator", path = "../../../../collator" }
1111
primitives = { package = "polkadot-primitives", path = "../../../../primitives" }
12+
service = { package = "polkadot-service", path = "../../../../service" }
1213
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
1314
client-api = { package = "sc-client-api", git = "https://github.com/paritytech/substrate", branch = "master" }
15+
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
16+
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
1417
parking_lot = "0.10.0"
1518
codec = { package = "parity-scale-codec", version = "1.3.4" }
1619
futures = "0.3.4"

parachain/test-parachains/adder/collator/src/main.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ use std::collections::HashMap;
2020
use std::sync::Arc;
2121

2222
use adder::{HeadData as AdderHead, BlockData as AdderBody};
23-
use sp_core::Pair;
23+
use sp_core::{traits::SpawnNamed, Pair};
2424
use codec::{Encode, Decode};
2525
use primitives::v0::{
26-
Hash, DownwardMessage,
26+
Block, Hash, DownwardMessage,
2727
HeadData, BlockData, Id as ParaId, LocalValidationData, GlobalValidationData,
2828
};
2929
use collator::{ParachainContext, Network, BuildParachainContext, Cli, SubstrateCli};
3030
use parking_lot::Mutex;
3131
use futures::future::{Ready, ready, FutureExt};
32+
use sp_runtime::traits::BlakeTwo256;
33+
use client_api::Backend as BackendT;
3234

3335
const GENESIS: AdderHead = AdderHead {
3436
number: 0,
@@ -103,12 +105,19 @@ impl ParachainContext for AdderContext {
103105
impl BuildParachainContext for AdderContext {
104106
type ParachainContext = Self;
105107

106-
fn build<SP>(
108+
fn build<SP, Client, Backend>(
107109
self,
108-
_: collator::Client,
110+
_: Arc<Client>,
109111
_: SP,
110112
network: impl Network + Clone + 'static,
111-
) -> Result<Self::ParachainContext, ()> {
113+
) -> Result<Self::ParachainContext, ()>
114+
where
115+
SP: SpawnNamed + Clone + Send + Sync + 'static,
116+
Backend: BackendT<Block>,
117+
Backend::State: sp_api::StateBackend<BlakeTwo256>,
118+
Client: service::AbstractClient<Block, Backend> + 'static,
119+
Client::Api: service::RuntimeApiCollection<StateBackend = Backend::State>,
120+
{
112121
Ok(Self { _network: Some(Arc::new(network)), ..self })
113122
}
114123
}

service/src/client.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ pub trait ExecuteWithClient {
118118
Client: AbstractClient<Block, Backend, Api = Api> + 'static;
119119
}
120120

121+
/// A handle to a Polkadot client instance.
122+
///
123+
/// The Polkadot service supports multiple different runtimes (Westend, Polkadot itself, etc). As each runtime has a
124+
/// specialized client, we need to hide them behind a trait. This is this trait.
125+
///
126+
/// When wanting to work with the inner client, you need to use `execute_with`.
127+
///
128+
/// See [`ExecuteWithClient`](trait.ExecuteWithClient.html) for more information.
129+
pub trait ClientHandle {
130+
/// Execute the given something with the client.
131+
fn execute_with<T: ExecuteWithClient>(&self, t: T) -> T::Output;
132+
}
133+
121134
/// A client instance of Polkadot.
122135
///
123136
/// See [`ExecuteWithClient`] for more information.
@@ -129,9 +142,8 @@ pub enum Client {
129142
Rococo(Arc<crate::FullClient<rococo_runtime::RuntimeApi, crate::RococoExecutor>>),
130143
}
131144

132-
impl Client {
133-
/// Execute the given something with the client.
134-
pub fn execute_with<T: ExecuteWithClient>(&self, t: T) -> T::Output {
145+
impl ClientHandle for Client {
146+
fn execute_with<T: ExecuteWithClient>(&self, t: T) -> T::Output {
135147
match self {
136148
Self::Polkadot(client) => {
137149
T::execute_with_client::<_, _, crate::FullBackend>(t, client.clone())

service/src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,25 @@ impl IdentifyVariant for Box<dyn ChainSpec> {
109109
}
110110
}
111111

112-
type FullBackend = service::TFullBackend<Block>;
113-
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
114-
type FullClient<RuntimeApi, Executor> = service::TFullClient<Block, RuntimeApi, Executor>;
115-
type FullGrandpaBlockImport<RuntimeApi, Executor> = grandpa::GrandpaBlockImport<
112+
/// Polkadot's full backend.
113+
pub type FullBackend = service::TFullBackend<Block>;
114+
115+
/// Polkadot's select chain.
116+
pub type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
117+
118+
/// Polkadot's full client.
119+
pub type FullClient<RuntimeApi, Executor> = service::TFullClient<Block, RuntimeApi, Executor>;
120+
121+
/// Polkadot's full Grandpa block import.
122+
pub type FullGrandpaBlockImport<RuntimeApi, Executor> = grandpa::GrandpaBlockImport<
116123
FullBackend, Block, FullClient<RuntimeApi, Executor>, FullSelectChain
117124
>;
118125

119-
type LightBackend = service::TLightBackendWithHash<Block, sp_runtime::traits::BlakeTwo256>;
126+
/// Polkadot's light backend.
127+
pub type LightBackend = service::TLightBackendWithHash<Block, sp_runtime::traits::BlakeTwo256>;
120128

121-
type LightClient<RuntimeApi, Executor> =
129+
/// Polkadot's light client.
130+
pub type LightClient<RuntimeApi, Executor> =
122131
service::TLightClientWithBackend<Block, RuntimeApi, Executor, LightBackend>;
123132

124133
#[cfg(feature = "full-node")]

0 commit comments

Comments
 (0)