use cumulus_client_consensus_common::{
ParachainBlockImportMarker, ParachainCandidate, ParachainConsensus,
};
use cumulus_primitives_core::{relay_chain::Hash as PHash, ParaId, PersistedValidationData};
use cumulus_relay_chain_interface::RelayChainInterface;
use sc_consensus::{BlockImport, BlockImportParams};
use sp_consensus::{
BlockOrigin, EnableProofRecording, Environment, ProofRecording, Proposal, Proposer,
};
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use parking_lot::Mutex;
use std::{marker::PhantomData, sync::Arc, time::Duration};
mod import_queue;
pub use import_queue::{import_queue, Verifier};
const LOG_TARGET: &str = "cumulus-consensus-relay-chain";
pub struct RelayChainConsensus<B, PF, BI, RCInterface, CIDP> {
para_id: ParaId,
proposer_factory: Arc<Mutex<PF>>,
create_inherent_data_providers: Arc<CIDP>,
block_import: Arc<futures::lock::Mutex<BI>>,
relay_chain_interface: RCInterface,
_phantom: PhantomData<B>,
}
impl<B, PF, BI, RCInterface, CIDP> Clone for RelayChainConsensus<B, PF, BI, RCInterface, CIDP>
where
RCInterface: Clone,
{
fn clone(&self) -> Self {
Self {
para_id: self.para_id,
proposer_factory: self.proposer_factory.clone(),
create_inherent_data_providers: self.create_inherent_data_providers.clone(),
block_import: self.block_import.clone(),
relay_chain_interface: self.relay_chain_interface.clone(),
_phantom: PhantomData,
}
}
}
impl<B, PF, BI, RCInterface, CIDP> RelayChainConsensus<B, PF, BI, RCInterface, CIDP>
where
B: BlockT,
BI: ParachainBlockImportMarker,
RCInterface: RelayChainInterface,
CIDP: CreateInherentDataProviders<B, (PHash, PersistedValidationData)>,
{
pub fn new(
para_id: ParaId,
proposer_factory: PF,
create_inherent_data_providers: CIDP,
block_import: BI,
relay_chain_interface: RCInterface,
) -> Self {
Self {
para_id,
proposer_factory: Arc::new(Mutex::new(proposer_factory)),
create_inherent_data_providers: Arc::new(create_inherent_data_providers),
block_import: Arc::new(futures::lock::Mutex::new(block_import)),
relay_chain_interface,
_phantom: PhantomData,
}
}
async fn inherent_data(
&self,
parent: B::Hash,
validation_data: &PersistedValidationData,
relay_parent: PHash,
) -> Option<InherentData> {
let inherent_data_providers = self
.create_inherent_data_providers
.create_inherent_data_providers(parent, (relay_parent, validation_data.clone()))
.await
.map_err(|e| {
tracing::error!(
target: LOG_TARGET,
error = ?e,
"Failed to create inherent data providers.",
)
})
.ok()?;
inherent_data_providers
.create_inherent_data()
.await
.map_err(|e| {
tracing::error!(
target: LOG_TARGET,
error = ?e,
"Failed to create inherent data.",
)
})
.ok()
}
}
#[async_trait::async_trait]
impl<B, PF, BI, RCInterface, CIDP> ParachainConsensus<B>
for RelayChainConsensus<B, PF, BI, RCInterface, CIDP>
where
B: BlockT,
RCInterface: RelayChainInterface + Clone,
BI: BlockImport<B> + ParachainBlockImportMarker + Send + Sync,
PF: Environment<B> + Send + Sync,
PF::Proposer: Proposer<
B,
ProofRecording = EnableProofRecording,
Proof = <EnableProofRecording as ProofRecording>::Proof,
>,
CIDP: CreateInherentDataProviders<B, (PHash, PersistedValidationData)>,
{
async fn produce_candidate(
&mut self,
parent: &B::Header,
relay_parent: PHash,
validation_data: &PersistedValidationData,
) -> Option<ParachainCandidate<B>> {
let proposer_future = self.proposer_factory.lock().init(parent);
let proposer = proposer_future
.await
.map_err(
|e| tracing::error!(target: LOG_TARGET, error = ?e, "Could not create proposer."),
)
.ok()?;
let inherent_data =
self.inherent_data(parent.hash(), validation_data, relay_parent).await?;
let Proposal { block, storage_changes, proof } = proposer
.propose(
inherent_data,
Default::default(),
Duration::from_millis(500),
Some((validation_data.max_pov_size / 2) as usize),
)
.await
.map_err(|e| tracing::error!(target: LOG_TARGET, error = ?e, "Proposing failed."))
.ok()?;
let (header, extrinsics) = block.clone().deconstruct();
let mut block_import_params = BlockImportParams::new(BlockOrigin::Own, header);
block_import_params.body = Some(extrinsics);
block_import_params.state_action = sc_consensus::StateAction::ApplyChanges(
sc_consensus::StorageChanges::Changes(storage_changes),
);
if let Err(err) = self.block_import.lock().await.import_block(block_import_params).await {
tracing::error!(
target: LOG_TARGET,
at = ?parent.hash(),
error = ?err,
"Error importing build block.",
);
return None
}
Some(ParachainCandidate { block, proof })
}
}
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_interface: RCInterface,
}
pub fn build_relay_chain_consensus<Block, PF, BI, CIDP, RCInterface>(
BuildRelayChainConsensusParams {
para_id,
proposer_factory,
create_inherent_data_providers,
block_import,
relay_chain_interface,
}: BuildRelayChainConsensusParams<PF, BI, CIDP, RCInterface>,
) -> Box<dyn ParachainConsensus<Block>>
where
Block: BlockT,
PF: Environment<Block> + Send + Sync + 'static,
PF::Proposer: Proposer<
Block,
ProofRecording = EnableProofRecording,
Proof = <EnableProofRecording as ProofRecording>::Proof,
>,
BI: BlockImport<Block> + ParachainBlockImportMarker + Send + Sync + 'static,
CIDP: CreateInherentDataProviders<Block, (PHash, PersistedValidationData)> + 'static,
RCInterface: RelayChainInterface + Clone + 'static,
{
Box::new(RelayChainConsensus::new(
para_id,
proposer_factory,
create_inherent_data_providers,
block_import,
relay_chain_interface,
))
}