feat: generalize ConsensusDataProvider for manual-seal (#11827)

* feat: generalize ConsensusDataProvider for manual-seal

* rename all generic type param `proof`/`PROOF` to `P`

* rename a missing thing

* Update client/consensus/manual-seal/src/consensus.rs

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Update client/consensus/manual-seal/src/consensus/babe.rs

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Update client/consensus/manual-seal/src/consensus/aura.rs

Co-authored-by: Davide Galassi <davxy@datawok.net>

Co-authored-by: Davide Galassi <davxy@datawok.net>
This commit is contained in:
yjh
2022-07-30 06:07:34 +08:00
committed by GitHub
parent 56f16f5500
commit e82da9d499
5 changed files with 49 additions and 25 deletions
@@ -33,6 +33,9 @@ pub trait ConsensusDataProvider<B: BlockT>: Send + Sync {
/// Block import transaction type /// Block import transaction type
type Transaction; type Transaction;
/// The proof type.
type Proof;
/// Attempt to create a consensus digest. /// Attempt to create a consensus digest.
fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error>; fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error>;
@@ -42,5 +45,6 @@ pub trait ConsensusDataProvider<B: BlockT>: Send + Sync {
parent: &B::Header, parent: &B::Header,
params: &mut BlockImportParams<B, Self::Transaction>, params: &mut BlockImportParams<B, Self::Transaction>,
inherents: &InherentData, inherents: &InherentData,
proof: Self::Proof,
) -> Result<(), Error>; ) -> Result<(), Error>;
} }
@@ -35,14 +35,14 @@ use sp_timestamp::TimestampInherentData;
use std::{marker::PhantomData, sync::Arc}; use std::{marker::PhantomData, sync::Arc};
/// Consensus data provider for Aura. /// Consensus data provider for Aura.
pub struct AuraConsensusDataProvider<B, C> { pub struct AuraConsensusDataProvider<B, C, P> {
// slot duration // slot duration
slot_duration: SlotDuration, slot_duration: SlotDuration,
// phantom data for required generics // phantom data for required generics
_phantom: PhantomData<(B, C)>, _phantom: PhantomData<(B, C, P)>,
} }
impl<B, C> AuraConsensusDataProvider<B, C> impl<B, C, P> AuraConsensusDataProvider<B, C, P>
where where
B: BlockT, B: BlockT,
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>, C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
@@ -58,7 +58,7 @@ where
} }
} }
impl<B, C> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, C> impl<B, C, P> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, C, P>
where where
B: BlockT, B: BlockT,
C: AuxStore C: AuxStore
@@ -67,8 +67,10 @@ where
+ UsageProvider<B> + UsageProvider<B>
+ ProvideRuntimeApi<B>, + ProvideRuntimeApi<B>,
C::Api: AuraApi<B, AuthorityId>, C::Api: AuraApi<B, AuthorityId>,
P: Send + Sync,
{ {
type Transaction = TransactionFor<C, B>; type Transaction = TransactionFor<C, B>;
type Proof = P;
fn create_digest( fn create_digest(
&self, &self,
@@ -92,6 +94,7 @@ where
_parent: &B::Header, _parent: &B::Header,
_params: &mut BlockImportParams<B, Self::Transaction>, _params: &mut BlockImportParams<B, Self::Transaction>,
_inherents: &InherentData, _inherents: &InherentData,
_proof: Self::Proof,
) -> Result<(), Error> { ) -> Result<(), Error> {
Ok(()) Ok(())
} }
@@ -31,7 +31,7 @@ use sc_consensus_epochs::{
descendent_query, EpochHeader, SharedEpochChanges, ViableEpochDescriptor, descendent_query, EpochHeader, SharedEpochChanges, ViableEpochDescriptor,
}; };
use sp_keystore::SyncCryptoStorePtr; use sp_keystore::SyncCryptoStorePtr;
use std::{borrow::Cow, sync::Arc}; use std::{borrow::Cow, marker::PhantomData, sync::Arc};
use sc_consensus::{BlockImportParams, ForkChoiceStrategy, Verifier}; use sc_consensus::{BlockImportParams, ForkChoiceStrategy, Verifier};
use sp_api::{ProvideRuntimeApi, TransactionFor}; use sp_api::{ProvideRuntimeApi, TransactionFor};
@@ -53,7 +53,7 @@ use sp_timestamp::TimestampInherentData;
/// Provides BABE-compatible predigests and BlockImportParams. /// Provides BABE-compatible predigests and BlockImportParams.
/// Intended for use with BABE runtimes. /// Intended for use with BABE runtimes.
pub struct BabeConsensusDataProvider<B: BlockT, C> { pub struct BabeConsensusDataProvider<B: BlockT, C, P> {
/// shared reference to keystore /// shared reference to keystore
keystore: SyncCryptoStorePtr, keystore: SyncCryptoStorePtr,
@@ -68,6 +68,7 @@ pub struct BabeConsensusDataProvider<B: BlockT, C> {
/// Authorities to be used for this babe chain. /// Authorities to be used for this babe chain.
authorities: Vec<(AuthorityId, BabeAuthorityWeight)>, authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
_phantom: PhantomData<P>,
} }
/// Verifier to be used for babe chains /// Verifier to be used for babe chains
@@ -131,7 +132,7 @@ where
} }
} }
impl<B, C> BabeConsensusDataProvider<B, C> impl<B, C, P> BabeConsensusDataProvider<B, C, P>
where where
B: BlockT, B: BlockT,
C: AuxStore C: AuxStore
@@ -153,7 +154,14 @@ where
let config = Config::get(&*client)?; let config = Config::get(&*client)?;
Ok(Self { config, client, keystore, epoch_changes, authorities }) Ok(Self {
config,
client,
keystore,
epoch_changes,
authorities,
_phantom: Default::default(),
})
} }
fn epoch(&self, parent: &B::Header, slot: Slot) -> Result<Epoch, Error> { fn epoch(&self, parent: &B::Header, slot: Slot) -> Result<Epoch, Error> {
@@ -181,7 +189,7 @@ where
} }
} }
impl<B, C> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C> impl<B, C, P> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C, P>
where where
B: BlockT, B: BlockT,
C: AuxStore C: AuxStore
@@ -190,8 +198,10 @@ where
+ UsageProvider<B> + UsageProvider<B>
+ ProvideRuntimeApi<B>, + ProvideRuntimeApi<B>,
C::Api: BabeApi<B>, C::Api: BabeApi<B>,
P: Send + Sync,
{ {
type Transaction = TransactionFor<C, B>; type Transaction = TransactionFor<C, B>;
type Proof = P;
fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error> { fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error> {
let slot = inherents let slot = inherents
@@ -259,6 +269,7 @@ where
parent: &B::Header, parent: &B::Header,
params: &mut BlockImportParams<B, Self::Transaction>, params: &mut BlockImportParams<B, Self::Transaction>,
inherents: &InherentData, inherents: &InherentData,
_proof: Self::Proof,
) -> Result<(), Error> { ) -> Result<(), Error> {
let slot = inherents let slot = inherents
.babe_inherent_data()? .babe_inherent_data()?
@@ -81,7 +81,7 @@ where
} }
/// Params required to start the instant sealing authorship task. /// Params required to start the instant sealing authorship task.
pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CS, CIDP> { pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CS, CIDP, P> {
/// Block import instance for well. importing blocks. /// Block import instance for well. importing blocks.
pub block_import: BI, pub block_import: BI,
@@ -103,14 +103,14 @@ pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, C
/// Digest provider for inclusion in blocks. /// Digest provider for inclusion in blocks.
pub consensus_data_provider: pub consensus_data_provider:
Option<Box<dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>>, Option<Box<dyn ConsensusDataProvider<B, Proof = P, Transaction = TransactionFor<C, B>>>>,
/// Something that can create the inherent data providers. /// Something that can create the inherent data providers.
pub create_inherent_data_providers: CIDP, pub create_inherent_data_providers: CIDP,
} }
/// Params required to start the manual sealing authorship task. /// Params required to start the manual sealing authorship task.
pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CIDP> { pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CIDP, P> {
/// Block import instance for well. importing blocks. /// Block import instance for well. importing blocks.
pub block_import: BI, pub block_import: BI,
@@ -128,14 +128,14 @@ pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC,
/// Digest provider for inclusion in blocks. /// Digest provider for inclusion in blocks.
pub consensus_data_provider: pub consensus_data_provider:
Option<Box<dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>>, Option<Box<dyn ConsensusDataProvider<B, Proof = P, Transaction = TransactionFor<C, B>>>>,
/// Something that can create the inherent data providers. /// Something that can create the inherent data providers.
pub create_inherent_data_providers: CIDP, pub create_inherent_data_providers: CIDP,
} }
/// Creates the background authorship task for the manual seal engine. /// Creates the background authorship task for the manual seal engine.
pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>( pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP, P>(
ManualSealParams { ManualSealParams {
mut block_import, mut block_import,
mut env, mut env,
@@ -145,7 +145,7 @@ pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>(
select_chain, select_chain,
consensus_data_provider, consensus_data_provider,
create_inherent_data_providers, create_inherent_data_providers,
}: ManualSealParams<B, BI, E, C, TP, SC, CS, CIDP>, }: ManualSealParams<B, BI, E, C, TP, SC, CS, CIDP, P>,
) where ) where
B: BlockT + 'static, B: BlockT + 'static,
BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>> BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>>
@@ -155,12 +155,13 @@ pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>(
C: HeaderBackend<B> + Finalizer<B, CB> + ProvideRuntimeApi<B> + 'static, C: HeaderBackend<B> + Finalizer<B, CB> + ProvideRuntimeApi<B> + 'static,
CB: ClientBackend<B> + 'static, CB: ClientBackend<B> + 'static,
E: Environment<B> + 'static, E: Environment<B> + 'static,
E::Proposer: Proposer<B, Transaction = TransactionFor<C, B>>, E::Proposer: Proposer<B, Proof = P, Transaction = TransactionFor<C, B>>,
CS: Stream<Item = EngineCommand<<B as BlockT>::Hash>> + Unpin + 'static, CS: Stream<Item = EngineCommand<<B as BlockT>::Hash>> + Unpin + 'static,
SC: SelectChain<B> + 'static, SC: SelectChain<B> + 'static,
TransactionFor<C, B>: 'static, TransactionFor<C, B>: 'static,
TP: TransactionPool<Block = B>, TP: TransactionPool<Block = B>,
CIDP: CreateInherentDataProviders<B, ()>, CIDP: CreateInherentDataProviders<B, ()>,
P: Send + Sync + 'static,
{ {
while let Some(command) = commands_stream.next().await { while let Some(command) = commands_stream.next().await {
match command { match command {
@@ -198,7 +199,7 @@ pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>(
/// runs the background authorship task for the instant seal engine. /// runs the background authorship task for the instant seal engine.
/// instant-seal creates a new block for every transaction imported into /// instant-seal creates a new block for every transaction imported into
/// the transaction pool. /// the transaction pool.
pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP>( pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP, P>(
InstantSealParams { InstantSealParams {
block_import, block_import,
env, env,
@@ -207,7 +208,7 @@ pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP>(
select_chain, select_chain,
consensus_data_provider, consensus_data_provider,
create_inherent_data_providers, create_inherent_data_providers,
}: InstantSealParams<B, BI, E, C, TP, SC, CIDP>, }: InstantSealParams<B, BI, E, C, TP, SC, CIDP, P>,
) where ) where
B: BlockT + 'static, B: BlockT + 'static,
BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>> BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>>
@@ -217,11 +218,12 @@ pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP>(
C: HeaderBackend<B> + Finalizer<B, CB> + ProvideRuntimeApi<B> + 'static, C: HeaderBackend<B> + Finalizer<B, CB> + ProvideRuntimeApi<B> + 'static,
CB: ClientBackend<B> + 'static, CB: ClientBackend<B> + 'static,
E: Environment<B> + 'static, E: Environment<B> + 'static,
E::Proposer: Proposer<B, Transaction = TransactionFor<C, B>>, E::Proposer: Proposer<B, Proof = P, Transaction = TransactionFor<C, B>>,
SC: SelectChain<B> + 'static, SC: SelectChain<B> + 'static,
TransactionFor<C, B>: 'static, TransactionFor<C, B>: 'static,
TP: TransactionPool<Block = B>, TP: TransactionPool<Block = B>,
CIDP: CreateInherentDataProviders<B, ()>, CIDP: CreateInherentDataProviders<B, ()>,
P: Send + Sync + 'static,
{ {
// instant-seal creates blocks as soon as transactions are imported // instant-seal creates blocks as soon as transactions are imported
// into the transaction pool. // into the transaction pool.
@@ -275,6 +277,7 @@ mod tests {
C: ProvideRuntimeApi<B> + Send + Sync, C: ProvideRuntimeApi<B> + Send + Sync,
{ {
type Transaction = TransactionFor<C, B>; type Transaction = TransactionFor<C, B>;
type Proof = ();
fn create_digest( fn create_digest(
&self, &self,
@@ -289,6 +292,7 @@ mod tests {
_parent: &B::Header, _parent: &B::Header,
params: &mut BlockImportParams<B, Self::Transaction>, params: &mut BlockImportParams<B, Self::Transaction>,
_inherents: &InherentData, _inherents: &InherentData,
_proof: Self::Proof,
) -> Result<(), Error> { ) -> Result<(), Error> {
params.post_digests.push(DigestItem::Other(vec![1])); params.post_digests.push(DigestItem::Other(vec![1]));
Ok(()) Ok(())
@@ -36,7 +36,7 @@ use std::{collections::HashMap, sync::Arc, time::Duration};
pub const MAX_PROPOSAL_DURATION: u64 = 10; pub const MAX_PROPOSAL_DURATION: u64 = 10;
/// params for sealing a new block /// params for sealing a new block
pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP, CIDP> { pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP, CIDP, P> {
/// if true, empty blocks(without extrinsics) will be created. /// if true, empty blocks(without extrinsics) will be created.
/// otherwise, will return Error::EmptyTransactionPool. /// otherwise, will return Error::EmptyTransactionPool.
pub create_empty: bool, pub create_empty: bool,
@@ -56,7 +56,7 @@ pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP
pub select_chain: &'a SC, pub select_chain: &'a SC,
/// Digest provider for inclusion in blocks. /// Digest provider for inclusion in blocks.
pub consensus_data_provider: pub consensus_data_provider:
Option<&'a dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>, Option<&'a dyn ConsensusDataProvider<B, Proof = P, Transaction = TransactionFor<C, B>>>,
/// block import object /// block import object
pub block_import: &'a mut BI, pub block_import: &'a mut BI,
/// Something that can create the inherent data providers. /// Something that can create the inherent data providers.
@@ -64,7 +64,7 @@ pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP
} }
/// seals a new block with the given params /// seals a new block with the given params
pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>( pub async fn seal_block<B, BI, SC, C, E, TP, CIDP, P>(
SealBlockParams { SealBlockParams {
create_empty, create_empty,
finalize, finalize,
@@ -77,7 +77,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
create_inherent_data_providers, create_inherent_data_providers,
consensus_data_provider: digest_provider, consensus_data_provider: digest_provider,
mut sender, mut sender,
}: SealBlockParams<'_, B, BI, SC, C, E, TP, CIDP>, }: SealBlockParams<'_, B, BI, SC, C, E, TP, CIDP, P>,
) where ) where
B: BlockT, B: BlockT,
BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>> BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>>
@@ -86,11 +86,12 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
+ 'static, + 'static,
C: HeaderBackend<B> + ProvideRuntimeApi<B>, C: HeaderBackend<B> + ProvideRuntimeApi<B>,
E: Environment<B>, E: Environment<B>,
E::Proposer: Proposer<B, Transaction = TransactionFor<C, B>>, E::Proposer: Proposer<B, Proof = P, Transaction = TransactionFor<C, B>>,
TP: TransactionPool<Block = B>, TP: TransactionPool<Block = B>,
SC: SelectChain<B>, SC: SelectChain<B>,
TransactionFor<C, B>: 'static, TransactionFor<C, B>: 'static,
CIDP: CreateInherentDataProviders<B, ()>, CIDP: CreateInherentDataProviders<B, ()>,
P: Send + Sync + 'static,
{ {
let future = async { let future = async {
if pool.status().ready == 0 && !create_empty { if pool.status().ready == 0 && !create_empty {
@@ -138,6 +139,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
} }
let (header, body) = proposal.block.deconstruct(); let (header, body) = proposal.block.deconstruct();
let proof = proposal.proof;
let mut params = BlockImportParams::new(BlockOrigin::Own, header.clone()); let mut params = BlockImportParams::new(BlockOrigin::Own, header.clone());
params.body = Some(body); params.body = Some(body);
params.finalized = finalize; params.finalized = finalize;
@@ -147,7 +149,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
)); ));
if let Some(digest_provider) = digest_provider { if let Some(digest_provider) = digest_provider {
digest_provider.append_block_import(&parent, &mut params, &inherent_data)?; digest_provider.append_block_import(&parent, &mut params, &inherent_data, proof)?;
} }
// Make sure we return the same post-hash that will be calculated when importing the block // Make sure we return the same post-hash that will be calculated when importing the block