mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 05:11:02 +00:00
Introduce interface for relay chain interaction (#835)
This commit is contained in:
@@ -19,11 +19,11 @@ sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "mast
|
||||
substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
|
||||
# Polkadot dependencies
|
||||
polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" }
|
||||
|
||||
# Cumulus dependencies
|
||||
cumulus-client-consensus-common = { path = "../common" }
|
||||
cumulus-primitives-core = { path = "../../../primitives/core" }
|
||||
cumulus-relay-chain-interface = { path = "../../relay-chain-interface" }
|
||||
|
||||
# Other deps
|
||||
futures = { version = "0.3.8", features = ["compat"] }
|
||||
|
||||
@@ -36,20 +36,16 @@
|
||||
use cumulus_client_consensus_common::{
|
||||
ParachainBlockImport, ParachainCandidate, ParachainConsensus,
|
||||
};
|
||||
use cumulus_primitives_core::{
|
||||
relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost},
|
||||
ParaId, PersistedValidationData,
|
||||
};
|
||||
use cumulus_primitives_core::{relay_chain::v1::Hash as PHash, ParaId, PersistedValidationData};
|
||||
use cumulus_relay_chain_interface::RelayChainInterface;
|
||||
use parking_lot::Mutex;
|
||||
use polkadot_client::ClientHandle;
|
||||
use sc_client_api::Backend;
|
||||
|
||||
use sc_consensus::{BlockImport, BlockImportParams};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_consensus::{
|
||||
BlockOrigin, EnableProofRecording, Environment, ProofRecording, Proposal, Proposer,
|
||||
};
|
||||
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
|
||||
use sp_runtime::traits::{Block as BlockT, HashFor, Header as HeaderT};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
use std::{marker::PhantomData, sync::Arc, time::Duration};
|
||||
|
||||
mod import_queue;
|
||||
@@ -58,18 +54,18 @@ pub use import_queue::{import_queue, Verifier};
|
||||
const LOG_TARGET: &str = "cumulus-consensus-relay-chain";
|
||||
|
||||
/// The implementation of the relay-chain provided consensus for parachains.
|
||||
pub struct RelayChainConsensus<B, PF, BI, RClient, RBackend, CIDP> {
|
||||
pub struct RelayChainConsensus<B, PF, BI, RCInterface, CIDP> {
|
||||
para_id: ParaId,
|
||||
_phantom: PhantomData<B>,
|
||||
proposer_factory: Arc<Mutex<PF>>,
|
||||
create_inherent_data_providers: Arc<CIDP>,
|
||||
block_import: Arc<futures::lock::Mutex<ParachainBlockImport<BI>>>,
|
||||
relay_chain_client: Arc<RClient>,
|
||||
relay_chain_backend: Arc<RBackend>,
|
||||
relay_chain_interface: RCInterface,
|
||||
}
|
||||
|
||||
impl<B, PF, BI, RClient, RBackend, CIDP> Clone
|
||||
for RelayChainConsensus<B, PF, BI, RClient, RBackend, CIDP>
|
||||
impl<B, PF, BI, RCInterface, CIDP> Clone for RelayChainConsensus<B, PF, BI, RCInterface, CIDP>
|
||||
where
|
||||
RCInterface: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
@@ -78,18 +74,15 @@ impl<B, PF, BI, RClient, RBackend, CIDP> Clone
|
||||
proposer_factory: self.proposer_factory.clone(),
|
||||
create_inherent_data_providers: self.create_inherent_data_providers.clone(),
|
||||
block_import: self.block_import.clone(),
|
||||
relay_chain_backend: self.relay_chain_backend.clone(),
|
||||
relay_chain_client: self.relay_chain_client.clone(),
|
||||
relay_chain_interface: self.relay_chain_interface.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, PF, BI, RClient, RBackend, CIDP> RelayChainConsensus<B, PF, BI, RClient, RBackend, CIDP>
|
||||
impl<B, PF, BI, RCInterface, CIDP> RelayChainConsensus<B, PF, BI, RCInterface, CIDP>
|
||||
where
|
||||
B: BlockT,
|
||||
RClient: ProvideRuntimeApi<PBlock>,
|
||||
RClient::Api: ParachainHost<PBlock>,
|
||||
RBackend: Backend<PBlock>,
|
||||
RCInterface: RelayChainInterface,
|
||||
CIDP: CreateInherentDataProviders<B, (PHash, PersistedValidationData)>,
|
||||
{
|
||||
/// Create a new instance of relay-chain provided consensus.
|
||||
@@ -98,8 +91,7 @@ where
|
||||
proposer_factory: PF,
|
||||
create_inherent_data_providers: CIDP,
|
||||
block_import: BI,
|
||||
polkadot_client: Arc<RClient>,
|
||||
polkadot_backend: Arc<RBackend>,
|
||||
relay_chain_interface: RCInterface,
|
||||
) -> Self {
|
||||
Self {
|
||||
para_id,
|
||||
@@ -108,8 +100,7 @@ where
|
||||
block_import: Arc::new(futures::lock::Mutex::new(ParachainBlockImport::new(
|
||||
block_import,
|
||||
))),
|
||||
relay_chain_backend: polkadot_backend,
|
||||
relay_chain_client: polkadot_client,
|
||||
relay_chain_interface,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -148,13 +139,11 @@ where
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<B, PF, BI, RClient, RBackend, CIDP> ParachainConsensus<B>
|
||||
for RelayChainConsensus<B, PF, BI, RClient, RBackend, CIDP>
|
||||
impl<B, PF, BI, RCInterface, CIDP> ParachainConsensus<B>
|
||||
for RelayChainConsensus<B, PF, BI, RCInterface, CIDP>
|
||||
where
|
||||
B: BlockT,
|
||||
RClient: ProvideRuntimeApi<PBlock> + Send + Sync,
|
||||
RClient::Api: ParachainHost<PBlock>,
|
||||
RBackend: Backend<PBlock>,
|
||||
RCInterface: RelayChainInterface + Clone,
|
||||
BI: BlockImport<B> + Send + Sync,
|
||||
PF: Environment<B> + Send + Sync,
|
||||
PF::Proposer: Proposer<
|
||||
@@ -229,27 +218,25 @@ where
|
||||
}
|
||||
|
||||
/// Paramaters of [`build_relay_chain_consensus`].
|
||||
pub struct BuildRelayChainConsensusParams<PF, BI, RBackend, CIDP> {
|
||||
pub struct BuildRelayChainConsensusParams<PF, BI, CIDP, RCInterface> {
|
||||
pub para_id: ParaId,
|
||||
pub proposer_factory: PF,
|
||||
pub create_inherent_data_providers: CIDP,
|
||||
pub block_import: BI,
|
||||
pub relay_chain_client: polkadot_client::Client,
|
||||
pub relay_chain_backend: Arc<RBackend>,
|
||||
pub relay_chain_interface: RCInterface,
|
||||
}
|
||||
|
||||
/// Build the [`RelayChainConsensus`].
|
||||
///
|
||||
/// Returns a boxed [`ParachainConsensus`].
|
||||
pub fn build_relay_chain_consensus<Block, PF, BI, RBackend, CIDP>(
|
||||
pub fn build_relay_chain_consensus<Block, PF, BI, CIDP, RCInterface>(
|
||||
BuildRelayChainConsensusParams {
|
||||
para_id,
|
||||
proposer_factory,
|
||||
create_inherent_data_providers,
|
||||
block_import,
|
||||
relay_chain_client,
|
||||
relay_chain_backend,
|
||||
}: BuildRelayChainConsensusParams<PF, BI, RBackend, CIDP>,
|
||||
relay_chain_interface,
|
||||
}: BuildRelayChainConsensusParams<PF, BI, CIDP, RCInterface>,
|
||||
) -> Box<dyn ParachainConsensus<Block>>
|
||||
where
|
||||
Block: BlockT,
|
||||
@@ -261,108 +248,14 @@ where
|
||||
Proof = <EnableProofRecording as ProofRecording>::Proof,
|
||||
>,
|
||||
BI: BlockImport<Block> + Send + Sync + 'static,
|
||||
RBackend: Backend<PBlock> + 'static,
|
||||
CIDP: CreateInherentDataProviders<Block, (PHash, PersistedValidationData)> + 'static,
|
||||
RCInterface: RelayChainInterface + Clone + 'static,
|
||||
{
|
||||
RelayChainConsensusBuilder::new(
|
||||
Box::new(RelayChainConsensus::new(
|
||||
para_id,
|
||||
proposer_factory,
|
||||
block_import,
|
||||
create_inherent_data_providers,
|
||||
relay_chain_client,
|
||||
relay_chain_backend,
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Relay chain consensus builder.
|
||||
///
|
||||
/// Builds a [`RelayChainConsensus`] for a parachain. As this requires
|
||||
/// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`]
|
||||
/// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`]
|
||||
/// the builder gets access to this concrete instance.
|
||||
struct RelayChainConsensusBuilder<Block, PF, BI, RBackend, CIDP> {
|
||||
para_id: ParaId,
|
||||
_phantom: PhantomData<Block>,
|
||||
proposer_factory: PF,
|
||||
create_inherent_data_providers: CIDP,
|
||||
block_import: BI,
|
||||
relay_chain_backend: Arc<RBackend>,
|
||||
relay_chain_client: polkadot_client::Client,
|
||||
}
|
||||
|
||||
impl<Block, PF, BI, RBackend, CIDP> RelayChainConsensusBuilder<Block, PF, BI, RBackend, CIDP>
|
||||
where
|
||||
Block: BlockT,
|
||||
PF: Environment<Block> + Send + Sync + 'static,
|
||||
PF::Proposer: Proposer<
|
||||
Block,
|
||||
Transaction = BI::Transaction,
|
||||
ProofRecording = EnableProofRecording,
|
||||
Proof = <EnableProofRecording as ProofRecording>::Proof,
|
||||
>,
|
||||
BI: BlockImport<Block> + Send + Sync + 'static,
|
||||
RBackend: Backend<PBlock> + 'static,
|
||||
CIDP: CreateInherentDataProviders<Block, (PHash, PersistedValidationData)> + 'static,
|
||||
{
|
||||
/// Create a new instance of the builder.
|
||||
fn new(
|
||||
para_id: ParaId,
|
||||
proposer_factory: PF,
|
||||
block_import: BI,
|
||||
create_inherent_data_providers: CIDP,
|
||||
relay_chain_client: polkadot_client::Client,
|
||||
relay_chain_backend: Arc<RBackend>,
|
||||
) -> Self {
|
||||
Self {
|
||||
para_id,
|
||||
_phantom: PhantomData,
|
||||
proposer_factory,
|
||||
block_import,
|
||||
create_inherent_data_providers,
|
||||
relay_chain_backend,
|
||||
relay_chain_client,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the relay chain consensus.
|
||||
fn build(self) -> Box<dyn ParachainConsensus<Block>> {
|
||||
self.relay_chain_client.clone().execute_with(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block, PF, BI, RBackend, CIDP> polkadot_client::ExecuteWithClient
|
||||
for RelayChainConsensusBuilder<Block, PF, BI, RBackend, CIDP>
|
||||
where
|
||||
Block: BlockT,
|
||||
PF: Environment<Block> + Send + Sync + 'static,
|
||||
PF::Proposer: Proposer<
|
||||
Block,
|
||||
Transaction = BI::Transaction,
|
||||
ProofRecording = EnableProofRecording,
|
||||
Proof = <EnableProofRecording as ProofRecording>::Proof,
|
||||
>,
|
||||
BI: BlockImport<Block> + Send + Sync + 'static,
|
||||
RBackend: Backend<PBlock> + 'static,
|
||||
CIDP: CreateInherentDataProviders<Block, (PHash, PersistedValidationData)> + 'static,
|
||||
{
|
||||
type Output = Box<dyn ParachainConsensus<Block>>;
|
||||
|
||||
fn execute_with_client<PClient, Api, PBackend>(self, client: Arc<PClient>) -> Self::Output
|
||||
where
|
||||
<Api as sp_api::ApiExt<PBlock>>::StateBackend: sp_api::StateBackend<HashFor<PBlock>>,
|
||||
PBackend: Backend<PBlock>,
|
||||
PBackend::State: sp_api::StateBackend<sp_runtime::traits::BlakeTwo256>,
|
||||
Api: polkadot_client::RuntimeApiCollection<StateBackend = PBackend::State>,
|
||||
PClient: polkadot_client::AbstractClient<PBlock, PBackend, Api = Api> + 'static,
|
||||
{
|
||||
Box::new(RelayChainConsensus::new(
|
||||
self.para_id,
|
||||
self.proposer_factory,
|
||||
self.create_inherent_data_providers,
|
||||
self.block_import,
|
||||
client.clone(),
|
||||
self.relay_chain_backend,
|
||||
))
|
||||
}
|
||||
block_import,
|
||||
relay_chain_interface,
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user