mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-09 01:18:00 +00:00
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:
@@ -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>;
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
@@ -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>,
|
||||
@@ -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
|
||||
@@ -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,
|
||||
@@ -92,6 +94,7 @@ where
|
||||
_parent: &B::Header,
|
||||
_params: &mut BlockImportParams<B, Self::Transaction>,
|
||||
_inherents: &InherentData,
|
||||
_proof: Self::Proof,
|
||||
) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
@@ -131,7 +132,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, C> BabeConsensusDataProvider<B, C>
|
||||
impl<B, C, P> BabeConsensusDataProvider<B, C, P>
|
||||
where
|
||||
B: BlockT,
|
||||
C: AuxStore
|
||||
@@ -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> {
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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()?
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
@@ -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>>
|
||||
@@ -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 {
|
||||
@@ -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,
|
||||
@@ -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>>
|
||||
@@ -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.
|
||||
@@ -275,6 +277,7 @@ mod tests {
|
||||
C: ProvideRuntimeApi<B> + Send + Sync,
|
||||
{
|
||||
type Transaction = TransactionFor<C, B>;
|
||||
type Proof = ();
|
||||
|
||||
fn create_digest(
|
||||
&self,
|
||||
@@ -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(())
|
||||
|
||||
@@ -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,
|
||||
@@ -56,7 +56,7 @@ 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.
|
||||
@@ -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
|
||||
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,
|
||||
@@ -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>>
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user