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
This commit is contained in:
Bastian Köcher
2021-05-23 13:29:43 +02:00
committed by GitHub
parent 70afaae2cf
commit 6565a95343
7 changed files with 490 additions and 316 deletions
Generated
+370 -237
View File
File diff suppressed because it is too large Load Diff
+41 -9
View File
@@ -17,8 +17,10 @@
//! Parachain specific wrapper for the AuRa import queue. //! Parachain specific wrapper for the AuRa import queue.
use codec::Codec; 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_consensus_slots::InherentDataProviderExt;
use sc_telemetry::TelemetryHandle;
use sp_api::{ApiExt, ProvideRuntimeApi}; use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder as BlockBuilderApi; use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::{HeaderBackend, ProvideCache}; use sp_blockchain::{HeaderBackend, ProvideCache};
@@ -31,16 +33,15 @@ use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::{Block as BlockT, DigestItemFor}; use sp_runtime::traits::{Block as BlockT, DigestItemFor};
use std::{fmt::Debug, hash::Hash, sync::Arc}; use std::{fmt::Debug, hash::Hash, sync::Arc};
use substrate_prometheus_endpoint::Registry; use substrate_prometheus_endpoint::Registry;
use sc_telemetry::TelemetryHandle;
/// Parameters of [`import_queue`]. /// 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. /// The block import to use.
pub block_import: I, pub block_import: I,
/// The client to interact with the chain. /// The client to interact with the chain.
pub client: Arc<C>, pub client: Arc<C>,
/// The inherent data providers, to create the inherent data. /// 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. /// The spawner to spawn background tasks.
pub spawner: &'a S, pub spawner: &'a S,
/// The prometheus registry. /// 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. /// 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 { ImportQueueParams {
block_import, block_import,
client, client,
@@ -61,7 +62,7 @@ pub fn import_queue<'a, P, Block, I, C, S, CAW, IDP>(
registry, registry,
can_author_with, can_author_with,
telemetry, telemetry,
}: ImportQueueParams<'a, I, C, IDP, S, CAW>, }: ImportQueueParams<'a, I, C, CIDP, S, CAW>,
) -> Result<DefaultImportQueue<Block, C>, sp_consensus::Error> ) -> Result<DefaultImportQueue<Block, C>, sp_consensus::Error>
where where
Block: BlockT, Block: BlockT,
@@ -73,6 +74,7 @@ where
+ Send + Send
+ Sync + Sync
+ AuxStore + AuxStore
+ UsageProvider<Block>
+ HeaderBackend<Block>, + HeaderBackend<Block>,
I: BlockImport<Block, Error = ConsensusError, Transaction = sp_api::TransactionFor<C, Block>> I: BlockImport<Block, Error = ConsensusError, Transaction = sp_api::TransactionFor<C, Block>>
+ Send + Send
@@ -84,11 +86,11 @@ where
P::Signature: Codec, P::Signature: Codec,
S: sp_core::traits::SpawnEssentialNamed, S: sp_core::traits::SpawnEssentialNamed,
CAW: CanAuthorWith<Block> + Send + Sync + 'static, CAW: CanAuthorWith<Block> + Send + Sync + 'static,
IDP: CreateInherentDataProviders<Block, ()> + Sync + Send + 'static, CIDP: CreateInherentDataProviders<Block, ()> + Sync + Send + 'static,
IDP::InherentDataProviders: InherentDataProviderExt + Send + Sync, CIDP::InherentDataProviders: InherentDataProviderExt + Send + Sync,
{ {
sc_consensus_aura::import_queue::<P, _, _, _, _, _, _>(sc_consensus_aura::ImportQueueParams { 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, justification_import: None,
client, client,
create_inherent_data_providers, create_inherent_data_providers,
@@ -99,3 +101,33 @@ where
telemetry, 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,
})
}
+7 -41
View File
@@ -23,7 +23,9 @@
//! For more information about AuRa, the Substrate crate should be checked. //! For more information about AuRa, the Substrate crate should be checked.
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_client_consensus_common::{
ParachainBlockImport, ParachainCandidate, ParachainConsensus,
};
use cumulus_primitives_core::{ use cumulus_primitives_core::{
relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost}, relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost},
PersistedValidationData, PersistedValidationData,
@@ -48,9 +50,10 @@ use std::{convert::TryFrom, hash::Hash, marker::PhantomData, sync::Arc};
mod import_queue; 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::{ pub use sc_consensus_aura::{
slot_duration, AuraBlockImport, BuildAuraWorkerParams, SlotDuration, SlotProportion, slot_duration, AuraVerifier, BuildAuraWorkerParams, SlotDuration,
SlotProportion,
}; };
pub use sc_consensus_slots::InherentDataProviderExt; pub use sc_consensus_slots::InherentDataProviderExt;
@@ -137,7 +140,7 @@ where
let worker = let worker =
sc_consensus_aura::build_aura_worker::<P, _, _, _, _, _, _, _>(BuildAuraWorkerParams { sc_consensus_aura::build_aura_worker::<P, _, _, _, _, _, _, _>(BuildAuraWorkerParams {
client: para_client, client: para_client,
block_import: ParachainBlockImport(block_import), block_import: ParachainBlockImport::new(block_import),
proposer_factory, proposer_factory,
sync_oracle, sync_oracle,
force_authoring, 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`]. /// Paramaters of [`build_aura_consensus`].
pub struct BuildAuraConsensusParams<PF, BI, RBackend, CIDP, Client, BS, SO> { pub struct BuildAuraConsensusParams<PF, BI, RBackend, CIDP, Client, BS, SO> {
pub proposer_factory: PF, pub proposer_factory: PF,
+44
View File
@@ -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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -22,7 +22,7 @@ use sp_blockchain::Result as ClientResult;
use sp_consensus::{ use sp_consensus::{
error::Error as ConsensusError, error::Error as ConsensusError,
import_queue::{BasicQueue, CacheKeyId, Verifier as VerifierT}, import_queue::{BasicQueue, CacheKeyId, Verifier as VerifierT},
BlockImport, BlockImportParams, BlockOrigin, ForkChoiceStrategy, BlockImport, BlockImportParams, BlockOrigin,
}; };
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider}; use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use sp_runtime::{ use sp_runtime::{
@@ -32,12 +32,23 @@ use sp_runtime::{
}; };
/// A verifier that just checks the inherents. /// A verifier that just checks the inherents.
struct Verifier<Client, Block, CIDP> { pub struct Verifier<Client, Block, CIDP> {
client: Arc<Client>, client: Arc<Client>,
create_inherent_data_providers: CIDP, create_inherent_data_providers: CIDP,
_marker: PhantomData<Block>, _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] #[async_trait::async_trait]
impl<Client, Block, CIDP> VerifierT<Block> for Verifier<Client, Block, CIDP> impl<Client, Block, CIDP> VerifierT<Block> for Verifier<Client, Block, CIDP>
where where
@@ -103,11 +114,6 @@ where
block_import_params.body = body; block_import_params.body = body;
block_import_params.justifications = justifications; 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; block_import_params.post_hash = post_hash;
Ok((block_import_params, None)) Ok((block_import_params, None))
@@ -129,15 +135,13 @@ where
<Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>, <Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>,
CIDP: CreateInherentDataProviders<Block, ()> + 'static, CIDP: CreateInherentDataProviders<Block, ()> + 'static,
{ {
let verifier = Verifier { let verifier = Verifier::new(client, create_inherent_data_providers);
client,
create_inherent_data_providers,
_marker: PhantomData,
};
Ok(BasicQueue::new( Ok(BasicQueue::new(
verifier, verifier,
Box::new(block_import), Box::new(cumulus_client_consensus_common::ParachainBlockImport::new(
block_import,
)),
None, None,
spawner, spawner,
registry, registry,
+10 -8
View File
@@ -33,7 +33,9 @@
//! //!
//! 5. After the parachain candidate got backed and included, all collators start at 1. //! 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::{ use cumulus_primitives_core::{
relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost}, relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost},
ParaId, PersistedValidationData, ParaId, PersistedValidationData,
@@ -43,15 +45,15 @@ use polkadot_service::ClientHandle;
use sc_client_api::Backend; use sc_client_api::Backend;
use sp_api::ProvideRuntimeApi; use sp_api::ProvideRuntimeApi;
use sp_consensus::{ use sp_consensus::{
BlockImport, BlockImportParams, BlockOrigin, EnableProofRecording, Environment, BlockImport, BlockImportParams, BlockOrigin, EnableProofRecording, Environment, ProofRecording,
ForkChoiceStrategy, ProofRecording, Proposal, Proposer, Proposal, Proposer,
}; };
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider}; use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_runtime::traits::{Block as BlockT, HashFor, Header as HeaderT}; use sp_runtime::traits::{Block as BlockT, HashFor, Header as HeaderT};
use std::{marker::PhantomData, sync::Arc, time::Duration}; use std::{marker::PhantomData, sync::Arc, time::Duration};
mod import_queue; mod import_queue;
pub use import_queue::import_queue; pub use import_queue::{import_queue, Verifier};
const LOG_TARGET: &str = "cumulus-consensus-relay-chain"; const LOG_TARGET: &str = "cumulus-consensus-relay-chain";
@@ -61,7 +63,7 @@ pub struct RelayChainConsensus<B, PF, BI, RClient, RBackend, CIDP> {
_phantom: PhantomData<B>, _phantom: PhantomData<B>,
proposer_factory: Arc<Mutex<PF>>, proposer_factory: Arc<Mutex<PF>>,
create_inherent_data_providers: Arc<CIDP>, 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_client: Arc<RClient>,
relay_chain_backend: Arc<RBackend>, relay_chain_backend: Arc<RBackend>,
} }
@@ -103,7 +105,9 @@ where
para_id, para_id,
proposer_factory: Arc::new(Mutex::new(proposer_factory)), proposer_factory: Arc::new(Mutex::new(proposer_factory)),
create_inherent_data_providers: Arc::new(create_inherent_data_providers), 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_backend: polkadot_backend,
relay_chain_client: polkadot_client, relay_chain_client: polkadot_client,
_phantom: PhantomData, _phantom: PhantomData,
@@ -204,8 +208,6 @@ where
let mut block_import_params = BlockImportParams::new(BlockOrigin::Own, header); let mut block_import_params = BlockImportParams::new(BlockOrigin::Own, header);
block_import_params.body = Some(extrinsics); 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); block_import_params.storage_changes = Some(storage_changes);
if let Err(err) = self if let Err(err) = self
+1 -8
View File
@@ -339,13 +339,6 @@ pub fn rococo_parachain_build_import_queue(
> { > {
let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?; 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::< cumulus_client_consensus_aura::import_queue::<
sp_consensus_aura::sr25519::AuthorityPair, sp_consensus_aura::sr25519::AuthorityPair,
_, _,
@@ -355,7 +348,7 @@ pub fn rococo_parachain_build_import_queue(
_, _,
_, _,
>(cumulus_client_consensus_aura::ImportQueueParams { >(cumulus_client_consensus_aura::ImportQueueParams {
block_import, block_import: client.clone(),
client: client.clone(), client: client.clone(),
create_inherent_data_providers: move |_, _| async move { create_inherent_data_providers: move |_, _| async move {
let time = sp_timestamp::InherentDataProvider::from_system_time(); let time = sp_timestamp::InherentDataProvider::from_system_time();