Skip to content
Snippets Groups Projects
Commit c884c10b authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

Some changes for Statemint (#448)

* Make Aura and relay chain verifier buildable

* AHHH

* Ahhh2

* Ahhh3

* Move `ParachainBlockImport`

* Updates because of Substrate

* Revert "AHHH"

This reverts commit 3f7c84327e1036ed71a8e2bd30f6416d32bae5a2.

* Fix warning
parent 093b574d
No related merge requests found
This diff is collapsed.
......@@ -17,8 +17,10 @@
//! Parachain specific wrapper for the AuRa import queue.
use codec::Codec;
use sc_client_api::{backend::AuxStore, BlockOf};
use sc_client_api::{backend::AuxStore, BlockOf, UsageProvider};
use sc_consensus_aura::AuraVerifier;
use sc_consensus_slots::InherentDataProviderExt;
use sc_telemetry::TelemetryHandle;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::{HeaderBackend, ProvideCache};
......@@ -31,16 +33,15 @@ use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::{Block as BlockT, DigestItemFor};
use std::{fmt::Debug, hash::Hash, sync::Arc};
use substrate_prometheus_endpoint::Registry;
use sc_telemetry::TelemetryHandle;
/// Parameters of [`import_queue`].
pub struct ImportQueueParams<'a, I, C, IDP, S, CAW> {
pub struct ImportQueueParams<'a, I, C, CIDP, S, CAW> {
/// The block import to use.
pub block_import: I,
/// The client to interact with the chain.
pub client: Arc<C>,
/// The inherent data providers, to create the inherent data.
pub create_inherent_data_providers: IDP,
pub create_inherent_data_providers: CIDP,
/// The spawner to spawn background tasks.
pub spawner: &'a S,
/// The prometheus registry.
......@@ -52,7 +53,7 @@ pub struct ImportQueueParams<'a, I, C, IDP, S, CAW> {
}
/// Start an import queue for the Aura consensus algorithm.
pub fn import_queue<'a, P, Block, I, C, S, CAW, IDP>(
pub fn import_queue<'a, P, Block, I, C, S, CAW, CIDP>(
ImportQueueParams {
block_import,
client,
......@@ -61,7 +62,7 @@ pub fn import_queue<'a, P, Block, I, C, S, CAW, IDP>(
registry,
can_author_with,
telemetry,
}: ImportQueueParams<'a, I, C, IDP, S, CAW>,
}: ImportQueueParams<'a, I, C, CIDP, S, CAW>,
) -> Result<DefaultImportQueue<Block, C>, sp_consensus::Error>
where
Block: BlockT,
......@@ -73,6 +74,7 @@ where
+ Send
+ Sync
+ AuxStore
+ UsageProvider<Block>
+ HeaderBackend<Block>,
I: BlockImport<Block, Error = ConsensusError, Transaction = sp_api::TransactionFor<C, Block>>
+ Send
......@@ -84,11 +86,11 @@ where
P::Signature: Codec,
S: sp_core::traits::SpawnEssentialNamed,
CAW: CanAuthorWith<Block> + Send + Sync + 'static,
IDP: CreateInherentDataProviders<Block, ()> + Sync + Send + 'static,
IDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
CIDP: CreateInherentDataProviders<Block, ()> + Sync + Send + 'static,
CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
{
sc_consensus_aura::import_queue::<P, _, _, _, _, _, _>(sc_consensus_aura::ImportQueueParams {
block_import: crate::ParachainBlockImport(block_import),
block_import: cumulus_client_consensus_common::ParachainBlockImport::new(block_import),
justification_import: None,
client,
create_inherent_data_providers,
......@@ -99,3 +101,33 @@ where
telemetry,
})
}
/// Parameters of [`build_verifier`].
pub struct BuildVerifierParams<C, CIDP, CAW> {
/// The client to interact with the chain.
pub client: Arc<C>,
/// The inherent data providers, to create the inherent data.
pub create_inherent_data_providers: CIDP,
/// Can we author with the current node?
pub can_author_with: CAW,
/// The telemetry handle.
pub telemetry: Option<TelemetryHandle>,
}
/// Build the [`AuraVerifier`].
pub fn build_verifier<P, C, CIDP, CAW>(
BuildVerifierParams {
client,
create_inherent_data_providers,
can_author_with,
telemetry,
}: BuildVerifierParams<C, CIDP, CAW>,
) -> AuraVerifier<C, P, CAW, CIDP> {
sc_consensus_aura::build_verifier(sc_consensus_aura::BuildVerifierParams {
client,
create_inherent_data_providers,
can_author_with,
telemetry,
check_for_equivocation: sc_consensus_aura::CheckForEquivocation::No,
})
}
......@@ -23,7 +23,9 @@
//! For more information about AuRa, the Substrate crate should be checked.
use codec::{Decode, Encode};
use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus};
use cumulus_client_consensus_common::{
ParachainBlockImport, ParachainCandidate, ParachainConsensus,
};
use cumulus_primitives_core::{
relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost},
PersistedValidationData,
......@@ -48,9 +50,10 @@ use std::{convert::TryFrom, hash::Hash, marker::PhantomData, sync::Arc};
mod import_queue;
pub use import_queue::{import_queue, ImportQueueParams};
pub use import_queue::{build_verifier, import_queue, BuildVerifierParams, ImportQueueParams};
pub use sc_consensus_aura::{
slot_duration, AuraBlockImport, BuildAuraWorkerParams, SlotDuration, SlotProportion,
slot_duration, AuraVerifier, BuildAuraWorkerParams, SlotDuration,
SlotProportion,
};
pub use sc_consensus_slots::InherentDataProviderExt;
......@@ -137,7 +140,7 @@ where
let worker =
sc_consensus_aura::build_aura_worker::<P, _, _, _, _, _, _, _>(BuildAuraWorkerParams {
client: para_client,
block_import: ParachainBlockImport(block_import),
block_import: ParachainBlockImport::new(block_import),
proposer_factory,
sync_oracle,
force_authoring,
......@@ -234,43 +237,6 @@ where
}
}
/// Parachain specific block import.
///
/// This is used to set `block_import_params.fork_choice` to `false` as long as the block origin is
/// not `NetworkInitialSync`. The best block for parachains is determined by the relay chain. Meaning
/// we will update the best block, as it is included by the relay-chain.
struct ParachainBlockImport<I>(I);
#[async_trait::async_trait]
impl<Block, I> BlockImport<Block> for ParachainBlockImport<I>
where
Block: BlockT,
I: BlockImport<Block> + Send,
{
type Error = I::Error;
type Transaction = I::Transaction;
async fn check_block(
&mut self,
block: sp_consensus::BlockCheckParams<Block>,
) -> Result<sp_consensus::ImportResult, Self::Error> {
self.0.check_block(block).await
}
async fn import_block(
&mut self,
mut block_import_params: sp_consensus::BlockImportParams<Block, Self::Transaction>,
cache: std::collections::HashMap<sp_consensus::import_queue::CacheKeyId, Vec<u8>>,
) -> Result<sp_consensus::ImportResult, Self::Error> {
// Best block is determined by the relay chain, or if we are doing the intial sync
// we import all blocks as new best.
block_import_params.fork_choice = Some(sp_consensus::ForkChoiceStrategy::Custom(
block_import_params.origin == sp_consensus::BlockOrigin::NetworkInitialSync,
));
self.0.import_block(block_import_params, cache).await
}
}
/// Paramaters of [`build_aura_consensus`].
pub struct BuildAuraConsensusParams<PF, BI, RBackend, CIDP, Client, BS, SO> {
pub proposer_factory: PF,
......
......@@ -561,6 +561,50 @@ impl<B: BlockT> ParachainConsensus<B> for Box<dyn ParachainConsensus<B> + Send +
}
}
/// Parachain specific block import.
///
/// This is used to set `block_import_params.fork_choice` to `false` as long as the block origin is
/// not `NetworkInitialSync`. The best block for parachains is determined by the relay chain. Meaning
/// we will update the best block, as it is included by the relay-chain.
pub struct ParachainBlockImport<I>(I);
impl<I> ParachainBlockImport<I> {
/// Create a new instance.
pub fn new(inner: I) -> Self {
Self(inner)
}
}
#[async_trait::async_trait]
impl<Block, I> BlockImport<Block> for ParachainBlockImport<I>
where
Block: BlockT,
I: BlockImport<Block> + Send,
{
type Error = I::Error;
type Transaction = I::Transaction;
async fn check_block(
&mut self,
block: sp_consensus::BlockCheckParams<Block>,
) -> Result<sp_consensus::ImportResult, Self::Error> {
self.0.check_block(block).await
}
async fn import_block(
&mut self,
mut block_import_params: sp_consensus::BlockImportParams<Block, Self::Transaction>,
cache: std::collections::HashMap<sp_consensus::import_queue::CacheKeyId, Vec<u8>>,
) -> Result<sp_consensus::ImportResult, Self::Error> {
// Best block is determined by the relay chain, or if we are doing the intial sync
// we import all blocks as new best.
block_import_params.fork_choice = Some(sp_consensus::ForkChoiceStrategy::Custom(
block_import_params.origin == sp_consensus::BlockOrigin::NetworkInitialSync,
));
self.0.import_block(block_import_params, cache).await
}
}
#[cfg(test)]
mod tests {
use super::*;
......
......@@ -22,7 +22,7 @@ use sp_blockchain::Result as ClientResult;
use sp_consensus::{
error::Error as ConsensusError,
import_queue::{BasicQueue, CacheKeyId, Verifier as VerifierT},
BlockImport, BlockImportParams, BlockOrigin, ForkChoiceStrategy,
BlockImport, BlockImportParams, BlockOrigin,
};
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use sp_runtime::{
......@@ -32,12 +32,23 @@ use sp_runtime::{
};
/// A verifier that just checks the inherents.
struct Verifier<Client, Block, CIDP> {
pub struct Verifier<Client, Block, CIDP> {
client: Arc<Client>,
create_inherent_data_providers: CIDP,
_marker: PhantomData<Block>,
}
impl<Client, Block, CIDP> Verifier<Client, Block, CIDP> {
/// Create a new instance.
pub fn new(client: Arc<Client>, create_inherent_data_providers: CIDP) -> Self {
Self {
client,
create_inherent_data_providers,
_marker: PhantomData,
}
}
}
#[async_trait::async_trait]
impl<Client, Block, CIDP> VerifierT<Block> for Verifier<Client, Block, CIDP>
where
......@@ -103,11 +114,6 @@ where
block_import_params.body = body;
block_import_params.justifications = justifications;
// Best block is determined by the relay chain, or if we are doing the intial sync
// we import all blocks as new best.
block_import_params.fork_choice = Some(ForkChoiceStrategy::Custom(
origin == BlockOrigin::NetworkInitialSync,
));
block_import_params.post_hash = post_hash;
Ok((block_import_params, None))
......@@ -129,15 +135,13 @@ where
<Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>,
CIDP: CreateInherentDataProviders<Block, ()> + 'static,
{
let verifier = Verifier {
client,
create_inherent_data_providers,
_marker: PhantomData,
};
let verifier = Verifier::new(client, create_inherent_data_providers);
Ok(BasicQueue::new(
verifier,
Box::new(block_import),
Box::new(cumulus_client_consensus_common::ParachainBlockImport::new(
block_import,
)),
None,
spawner,
registry,
......
......@@ -33,7 +33,9 @@
//!
//! 5. After the parachain candidate got backed and included, all collators start at 1.
use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus};
use cumulus_client_consensus_common::{
ParachainBlockImport, ParachainCandidate, ParachainConsensus,
};
use cumulus_primitives_core::{
relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost},
ParaId, PersistedValidationData,
......@@ -43,15 +45,15 @@ use polkadot_service::ClientHandle;
use sc_client_api::Backend;
use sp_api::ProvideRuntimeApi;
use sp_consensus::{
BlockImport, BlockImportParams, BlockOrigin, EnableProofRecording, Environment,
ForkChoiceStrategy, ProofRecording, Proposal, Proposer,
BlockImport, BlockImportParams, BlockOrigin, EnableProofRecording, Environment, ProofRecording,
Proposal, Proposer,
};
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_runtime::traits::{Block as BlockT, HashFor, Header as HeaderT};
use std::{marker::PhantomData, sync::Arc, time::Duration};
mod import_queue;
pub use import_queue::import_queue;
pub use import_queue::{import_queue, Verifier};
const LOG_TARGET: &str = "cumulus-consensus-relay-chain";
......@@ -61,7 +63,7 @@ pub struct RelayChainConsensus<B, PF, BI, RClient, RBackend, CIDP> {
_phantom: PhantomData<B>,
proposer_factory: Arc<Mutex<PF>>,
create_inherent_data_providers: Arc<CIDP>,
block_import: Arc<futures::lock::Mutex<BI>>,
block_import: Arc<futures::lock::Mutex<ParachainBlockImport<BI>>>,
relay_chain_client: Arc<RClient>,
relay_chain_backend: Arc<RBackend>,
}
......@@ -103,7 +105,9 @@ where
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)),
block_import: Arc::new(futures::lock::Mutex::new(ParachainBlockImport::new(
block_import,
))),
relay_chain_backend: polkadot_backend,
relay_chain_client: polkadot_client,
_phantom: PhantomData,
......@@ -204,8 +208,6 @@ where
let mut block_import_params = BlockImportParams::new(BlockOrigin::Own, header);
block_import_params.body = Some(extrinsics);
// Best block is determined by the relay chain.
block_import_params.fork_choice = Some(ForkChoiceStrategy::Custom(false));
block_import_params.storage_changes = Some(storage_changes);
if let Err(err) = self
......
......@@ -339,13 +339,6 @@ pub fn rococo_parachain_build_import_queue(
> {
let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
let block_import = cumulus_client_consensus_aura::AuraBlockImport::<
_,
_,
_,
sp_consensus_aura::sr25519::AuthorityPair,
>::new(client.clone(), client.clone());
cumulus_client_consensus_aura::import_queue::<
sp_consensus_aura::sr25519::AuthorityPair,
_,
......@@ -355,7 +348,7 @@ pub fn rococo_parachain_build_import_queue(
_,
_,
>(cumulus_client_consensus_aura::ImportQueueParams {
block_import,
block_import: client.clone(),
client: client.clone(),
create_inherent_data_providers: move |_, _| async move {
let time = sp_timestamp::InherentDataProvider::from_system_time();
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment