Skip to content

Commit

Permalink
feat: generalize ConsensusDataProvider for manual-seal (paritytech#11827
Browse files Browse the repository at this point in the history
)

* 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>
  • Loading branch information
yjhmelody and davxy authored Jul 29, 2022
1 parent 8f4d1b3 commit 986fab5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
4 changes: 4 additions & 0 deletions client/consensus/manual-seal/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub trait ConsensusDataProvider<B: BlockT>: Send + Sync {
/// Block import transaction type
type Transaction;

/// The proof type.
type Proof;

/// Attempt to create a consensus digest.
fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error>;

Expand All @@ -42,5 +45,6 @@ pub trait ConsensusDataProvider<B: BlockT>: Send + Sync {
parent: &B::Header,
params: &mut BlockImportParams<B, Self::Transaction>,
inherents: &InherentData,
proof: Self::Proof,
) -> Result<(), Error>;
}
11 changes: 7 additions & 4 deletions client/consensus/manual-seal/src/consensus/aura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ use sp_timestamp::TimestampInherentData;
use std::{marker::PhantomData, sync::Arc};

/// Consensus data provider for Aura.
pub struct AuraConsensusDataProvider<B, C> {
pub struct AuraConsensusDataProvider<B, C, P> {
// slot duration
slot_duration: SlotDuration,
// 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
B: BlockT,
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
Expand All @@ -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
B: BlockT,
C: AuxStore
Expand All @@ -67,8 +67,10 @@ where
+ UsageProvider<B>
+ ProvideRuntimeApi<B>,
C::Api: AuraApi<B, AuthorityId>,
P: Send + Sync,
{
type Transaction = TransactionFor<C, B>;
type Proof = P;

fn create_digest(
&self,
Expand All @@ -92,6 +94,7 @@ where
_parent: &B::Header,
_params: &mut BlockImportParams<B, Self::Transaction>,
_inherents: &InherentData,
_proof: Self::Proof,
) -> Result<(), Error> {
Ok(())
}
Expand Down
21 changes: 16 additions & 5 deletions client/consensus/manual-seal/src/consensus/babe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use sc_consensus_epochs::{
descendent_query, EpochHeader, SharedEpochChanges, ViableEpochDescriptor,
};
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 sp_api::{ProvideRuntimeApi, TransactionFor};
Expand All @@ -53,7 +53,7 @@ use sp_timestamp::TimestampInherentData;

/// Provides BABE-compatible predigests and BlockImportParams.
/// Intended for use with BABE runtimes.
pub struct BabeConsensusDataProvider<B: BlockT, C> {
pub struct BabeConsensusDataProvider<B: BlockT, C, P> {
/// shared reference to keystore
keystore: SyncCryptoStorePtr,

Expand All @@ -68,6 +68,7 @@ pub struct BabeConsensusDataProvider<B: BlockT, C> {

/// Authorities to be used for this babe chain.
authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
_phantom: PhantomData<P>,
}

/// Verifier to be used for babe chains
Expand Down Expand Up @@ -131,7 +132,7 @@ where
}
}

impl<B, C> BabeConsensusDataProvider<B, C>
impl<B, C, P> BabeConsensusDataProvider<B, C, P>
where
B: BlockT,
C: AuxStore
Expand All @@ -153,7 +154,14 @@ where

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> {
Expand Down Expand Up @@ -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
B: BlockT,
C: AuxStore
Expand All @@ -190,8 +198,10 @@ where
+ UsageProvider<B>
+ ProvideRuntimeApi<B>,
C::Api: BabeApi<B>,
P: Send + Sync,
{
type Transaction = TransactionFor<C, B>;
type Proof = P;

fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error> {
let slot = inherents
Expand Down Expand Up @@ -259,6 +269,7 @@ where
parent: &B::Header,
params: &mut BlockImportParams<B, Self::Transaction>,
inherents: &InherentData,
_proof: Self::Proof,
) -> Result<(), Error> {
let slot = inherents
.babe_inherent_data()?
Expand Down
24 changes: 14 additions & 10 deletions client/consensus/manual-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
}

/// 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.
pub block_import: BI,

Expand All @@ -103,14 +103,14 @@ pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, C

/// Digest provider for inclusion in blocks.
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.
pub create_inherent_data_providers: CIDP,
}

/// 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.
pub block_import: BI,

Expand All @@ -128,14 +128,14 @@ pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC,

/// Digest provider for inclusion in blocks.
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.
pub create_inherent_data_providers: CIDP,
}

/// 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 {
mut block_import,
mut env,
Expand All @@ -145,7 +145,7 @@ pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>(
select_chain,
consensus_data_provider,
create_inherent_data_providers,
}: ManualSealParams<B, BI, E, C, TP, SC, CS, CIDP>,
}: ManualSealParams<B, BI, E, C, TP, SC, CS, CIDP, P>,
) where
B: BlockT + 'static,
BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>>
Expand All @@ -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,
CB: ClientBackend<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,
SC: SelectChain<B> + 'static,
TransactionFor<C, B>: 'static,
TP: TransactionPool<Block = B>,
CIDP: CreateInherentDataProviders<B, ()>,
P: Send + Sync + 'static,
{
while let Some(command) = commands_stream.next().await {
match command {
Expand Down Expand Up @@ -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.
/// instant-seal creates a new block for every transaction imported into
/// 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 {
block_import,
env,
Expand All @@ -207,7 +208,7 @@ pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP>(
select_chain,
consensus_data_provider,
create_inherent_data_providers,
}: InstantSealParams<B, BI, E, C, TP, SC, CIDP>,
}: InstantSealParams<B, BI, E, C, TP, SC, CIDP, P>,
) where
B: BlockT + 'static,
BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>>
Expand All @@ -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,
CB: ClientBackend<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,
TransactionFor<C, B>: 'static,
TP: TransactionPool<Block = B>,
CIDP: CreateInherentDataProviders<B, ()>,
P: Send + Sync + 'static,
{
// instant-seal creates blocks as soon as transactions are imported
// into the transaction pool.
Expand Down Expand Up @@ -275,6 +277,7 @@ mod tests {
C: ProvideRuntimeApi<B> + Send + Sync,
{
type Transaction = TransactionFor<C, B>;
type Proof = ();

fn create_digest(
&self,
Expand All @@ -289,6 +292,7 @@ mod tests {
_parent: &B::Header,
params: &mut BlockImportParams<B, Self::Transaction>,
_inherents: &InherentData,
_proof: Self::Proof,
) -> Result<(), Error> {
params.post_digests.push(DigestItem::Other(vec![1]));
Ok(())
Expand Down
14 changes: 8 additions & 6 deletions client/consensus/manual-seal/src/seal_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use std::{collections::HashMap, sync::Arc, time::Duration};
pub const MAX_PROPOSAL_DURATION: u64 = 10;

/// 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.
/// otherwise, will return Error::EmptyTransactionPool.
pub create_empty: bool,
Expand All @@ -56,15 +56,15 @@ pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP
pub select_chain: &'a SC,
/// Digest provider for inclusion in blocks.
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
pub block_import: &'a mut BI,
/// Something that can create the inherent data providers.
pub create_inherent_data_providers: &'a CIDP,
}

/// 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 {
create_empty,
finalize,
Expand All @@ -77,7 +77,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
create_inherent_data_providers,
consensus_data_provider: digest_provider,
mut sender,
}: SealBlockParams<'_, B, BI, SC, C, E, TP, CIDP>,
}: SealBlockParams<'_, B, BI, SC, C, E, TP, CIDP, P>,
) where
B: BlockT,
BI: BlockImport<B, Error = sp_consensus::Error, Transaction = sp_api::TransactionFor<C, B>>
Expand All @@ -86,11 +86,12 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
+ 'static,
C: HeaderBackend<B> + ProvideRuntimeApi<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>,
SC: SelectChain<B>,
TransactionFor<C, B>: 'static,
CIDP: CreateInherentDataProviders<B, ()>,
P: Send + Sync + 'static,
{
let future = async {
if pool.status().ready == 0 && !create_empty {
Expand Down Expand Up @@ -138,6 +139,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
}

let (header, body) = proposal.block.deconstruct();
let proof = proposal.proof;
let mut params = BlockImportParams::new(BlockOrigin::Own, header.clone());
params.body = Some(body);
params.finalized = finalize;
Expand All @@ -147,7 +149,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
));

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
Expand Down

0 comments on commit 986fab5

Please sign in to comment.