Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 986fab5

Browse files
yjhmelodydavxy
andauthored
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 <[email protected]> * Update client/consensus/manual-seal/src/consensus/babe.rs Co-authored-by: Davide Galassi <[email protected]> * Update client/consensus/manual-seal/src/consensus/aura.rs Co-authored-by: Davide Galassi <[email protected]> Co-authored-by: Davide Galassi <[email protected]>
1 parent 8f4d1b3 commit 986fab5

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub trait ConsensusDataProvider<B: BlockT>: Send + Sync {
3333
/// Block import transaction type
3434
type Transaction;
3535

36+
/// The proof type.
37+
type Proof;
38+
3639
/// Attempt to create a consensus digest.
3740
fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error>;
3841

@@ -42,5 +45,6 @@ pub trait ConsensusDataProvider<B: BlockT>: Send + Sync {
4245
parent: &B::Header,
4346
params: &mut BlockImportParams<B, Self::Transaction>,
4447
inherents: &InherentData,
48+
proof: Self::Proof,
4549
) -> Result<(), Error>;
4650
}

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ use sp_timestamp::TimestampInherentData;
3535
use std::{marker::PhantomData, sync::Arc};
3636

3737
/// Consensus data provider for Aura.
38-
pub struct AuraConsensusDataProvider<B, C> {
38+
pub struct AuraConsensusDataProvider<B, C, P> {
3939
// slot duration
4040
slot_duration: SlotDuration,
4141
// phantom data for required generics
42-
_phantom: PhantomData<(B, C)>,
42+
_phantom: PhantomData<(B, C, P)>,
4343
}
4444

45-
impl<B, C> AuraConsensusDataProvider<B, C>
45+
impl<B, C, P> AuraConsensusDataProvider<B, C, P>
4646
where
4747
B: BlockT,
4848
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
@@ -58,7 +58,7 @@ where
5858
}
5959
}
6060

61-
impl<B, C> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, C>
61+
impl<B, C, P> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, C, P>
6262
where
6363
B: BlockT,
6464
C: AuxStore
@@ -67,8 +67,10 @@ where
6767
+ UsageProvider<B>
6868
+ ProvideRuntimeApi<B>,
6969
C::Api: AuraApi<B, AuthorityId>,
70+
P: Send + Sync,
7071
{
7172
type Transaction = TransactionFor<C, B>;
73+
type Proof = P;
7274

7375
fn create_digest(
7476
&self,
@@ -92,6 +94,7 @@ where
9294
_parent: &B::Header,
9395
_params: &mut BlockImportParams<B, Self::Transaction>,
9496
_inherents: &InherentData,
97+
_proof: Self::Proof,
9598
) -> Result<(), Error> {
9699
Ok(())
97100
}

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use sc_consensus_epochs::{
3131
descendent_query, EpochHeader, SharedEpochChanges, ViableEpochDescriptor,
3232
};
3333
use sp_keystore::SyncCryptoStorePtr;
34-
use std::{borrow::Cow, sync::Arc};
34+
use std::{borrow::Cow, marker::PhantomData, sync::Arc};
3535

3636
use sc_consensus::{BlockImportParams, ForkChoiceStrategy, Verifier};
3737
use sp_api::{ProvideRuntimeApi, TransactionFor};
@@ -53,7 +53,7 @@ use sp_timestamp::TimestampInherentData;
5353

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

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

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

7374
/// Verifier to be used for babe chains
@@ -131,7 +132,7 @@ where
131132
}
132133
}
133134

134-
impl<B, C> BabeConsensusDataProvider<B, C>
135+
impl<B, C, P> BabeConsensusDataProvider<B, C, P>
135136
where
136137
B: BlockT,
137138
C: AuxStore
@@ -153,7 +154,14 @@ where
153154

154155
let config = Config::get(&*client)?;
155156

156-
Ok(Self { config, client, keystore, epoch_changes, authorities })
157+
Ok(Self {
158+
config,
159+
client,
160+
keystore,
161+
epoch_changes,
162+
authorities,
163+
_phantom: Default::default(),
164+
})
157165
}
158166

159167
fn epoch(&self, parent: &B::Header, slot: Slot) -> Result<Epoch, Error> {
@@ -181,7 +189,7 @@ where
181189
}
182190
}
183191

184-
impl<B, C> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C>
192+
impl<B, C, P> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C, P>
185193
where
186194
B: BlockT,
187195
C: AuxStore
@@ -190,8 +198,10 @@ where
190198
+ UsageProvider<B>
191199
+ ProvideRuntimeApi<B>,
192200
C::Api: BabeApi<B>,
201+
P: Send + Sync,
193202
{
194203
type Transaction = TransactionFor<C, B>;
204+
type Proof = P;
195205

196206
fn create_digest(&self, parent: &B::Header, inherents: &InherentData) -> Result<Digest, Error> {
197207
let slot = inherents
@@ -259,6 +269,7 @@ where
259269
parent: &B::Header,
260270
params: &mut BlockImportParams<B, Self::Transaction>,
261271
inherents: &InherentData,
272+
_proof: Self::Proof,
262273
) -> Result<(), Error> {
263274
let slot = inherents
264275
.babe_inherent_data()?

client/consensus/manual-seal/src/lib.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ where
8181
}
8282

8383
/// Params required to start the instant sealing authorship task.
84-
pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CS, CIDP> {
84+
pub struct ManualSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CS, CIDP, P> {
8585
/// Block import instance for well. importing blocks.
8686
pub block_import: BI,
8787

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

104104
/// Digest provider for inclusion in blocks.
105105
pub consensus_data_provider:
106-
Option<Box<dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>>,
106+
Option<Box<dyn ConsensusDataProvider<B, Proof = P, Transaction = TransactionFor<C, B>>>>,
107107

108108
/// Something that can create the inherent data providers.
109109
pub create_inherent_data_providers: CIDP,
110110
}
111111

112112
/// Params required to start the manual sealing authorship task.
113-
pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CIDP> {
113+
pub struct InstantSealParams<B: BlockT, BI, E, C: ProvideRuntimeApi<B>, TP, SC, CIDP, P> {
114114
/// Block import instance for well. importing blocks.
115115
pub block_import: BI,
116116

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

129129
/// Digest provider for inclusion in blocks.
130130
pub consensus_data_provider:
131-
Option<Box<dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>>,
131+
Option<Box<dyn ConsensusDataProvider<B, Proof = P, Transaction = TransactionFor<C, B>>>>,
132132

133133
/// Something that can create the inherent data providers.
134134
pub create_inherent_data_providers: CIDP,
135135
}
136136

137137
/// Creates the background authorship task for the manual seal engine.
138-
pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>(
138+
pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP, P>(
139139
ManualSealParams {
140140
mut block_import,
141141
mut env,
@@ -145,7 +145,7 @@ pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>(
145145
select_chain,
146146
consensus_data_provider,
147147
create_inherent_data_providers,
148-
}: ManualSealParams<B, BI, E, C, TP, SC, CS, CIDP>,
148+
}: ManualSealParams<B, BI, E, C, TP, SC, CS, CIDP, P>,
149149
) where
150150
B: BlockT + 'static,
151151
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>(
155155
C: HeaderBackend<B> + Finalizer<B, CB> + ProvideRuntimeApi<B> + 'static,
156156
CB: ClientBackend<B> + 'static,
157157
E: Environment<B> + 'static,
158-
E::Proposer: Proposer<B, Transaction = TransactionFor<C, B>>,
158+
E::Proposer: Proposer<B, Proof = P, Transaction = TransactionFor<C, B>>,
159159
CS: Stream<Item = EngineCommand<<B as BlockT>::Hash>> + Unpin + 'static,
160160
SC: SelectChain<B> + 'static,
161161
TransactionFor<C, B>: 'static,
162162
TP: TransactionPool<Block = B>,
163163
CIDP: CreateInherentDataProviders<B, ()>,
164+
P: Send + Sync + 'static,
164165
{
165166
while let Some(command) = commands_stream.next().await {
166167
match command {
@@ -198,7 +199,7 @@ pub async fn run_manual_seal<B, BI, CB, E, C, TP, SC, CS, CIDP>(
198199
/// runs the background authorship task for the instant seal engine.
199200
/// instant-seal creates a new block for every transaction imported into
200201
/// the transaction pool.
201-
pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP>(
202+
pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP, P>(
202203
InstantSealParams {
203204
block_import,
204205
env,
@@ -207,7 +208,7 @@ pub async fn run_instant_seal<B, BI, CB, E, C, TP, SC, CIDP>(
207208
select_chain,
208209
consensus_data_provider,
209210
create_inherent_data_providers,
210-
}: InstantSealParams<B, BI, E, C, TP, SC, CIDP>,
211+
}: InstantSealParams<B, BI, E, C, TP, SC, CIDP, P>,
211212
) where
212213
B: BlockT + 'static,
213214
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>(
217218
C: HeaderBackend<B> + Finalizer<B, CB> + ProvideRuntimeApi<B> + 'static,
218219
CB: ClientBackend<B> + 'static,
219220
E: Environment<B> + 'static,
220-
E::Proposer: Proposer<B, Transaction = TransactionFor<C, B>>,
221+
E::Proposer: Proposer<B, Proof = P, Transaction = TransactionFor<C, B>>,
221222
SC: SelectChain<B> + 'static,
222223
TransactionFor<C, B>: 'static,
223224
TP: TransactionPool<Block = B>,
224225
CIDP: CreateInherentDataProviders<B, ()>,
226+
P: Send + Sync + 'static,
225227
{
226228
// instant-seal creates blocks as soon as transactions are imported
227229
// into the transaction pool.
@@ -275,6 +277,7 @@ mod tests {
275277
C: ProvideRuntimeApi<B> + Send + Sync,
276278
{
277279
type Transaction = TransactionFor<C, B>;
280+
type Proof = ();
278281

279282
fn create_digest(
280283
&self,
@@ -289,6 +292,7 @@ mod tests {
289292
_parent: &B::Header,
290293
params: &mut BlockImportParams<B, Self::Transaction>,
291294
_inherents: &InherentData,
295+
_proof: Self::Proof,
292296
) -> Result<(), Error> {
293297
params.post_digests.push(DigestItem::Other(vec![1]));
294298
Ok(())

client/consensus/manual-seal/src/seal_block.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::{collections::HashMap, sync::Arc, time::Duration};
3636
pub const MAX_PROPOSAL_DURATION: u64 = 10;
3737

3838
/// params for sealing a new block
39-
pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP, CIDP> {
39+
pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP, CIDP, P> {
4040
/// if true, empty blocks(without extrinsics) will be created.
4141
/// otherwise, will return Error::EmptyTransactionPool.
4242
pub create_empty: bool,
@@ -56,15 +56,15 @@ pub struct SealBlockParams<'a, B: BlockT, BI, SC, C: ProvideRuntimeApi<B>, E, TP
5656
pub select_chain: &'a SC,
5757
/// Digest provider for inclusion in blocks.
5858
pub consensus_data_provider:
59-
Option<&'a dyn ConsensusDataProvider<B, Transaction = TransactionFor<C, B>>>,
59+
Option<&'a dyn ConsensusDataProvider<B, Proof = P, Transaction = TransactionFor<C, B>>>,
6060
/// block import object
6161
pub block_import: &'a mut BI,
6262
/// Something that can create the inherent data providers.
6363
pub create_inherent_data_providers: &'a CIDP,
6464
}
6565

6666
/// seals a new block with the given params
67-
pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
67+
pub async fn seal_block<B, BI, SC, C, E, TP, CIDP, P>(
6868
SealBlockParams {
6969
create_empty,
7070
finalize,
@@ -77,7 +77,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
7777
create_inherent_data_providers,
7878
consensus_data_provider: digest_provider,
7979
mut sender,
80-
}: SealBlockParams<'_, B, BI, SC, C, E, TP, CIDP>,
80+
}: SealBlockParams<'_, B, BI, SC, C, E, TP, CIDP, P>,
8181
) where
8282
B: BlockT,
8383
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>(
8686
+ 'static,
8787
C: HeaderBackend<B> + ProvideRuntimeApi<B>,
8888
E: Environment<B>,
89-
E::Proposer: Proposer<B, Transaction = TransactionFor<C, B>>,
89+
E::Proposer: Proposer<B, Proof = P, Transaction = TransactionFor<C, B>>,
9090
TP: TransactionPool<Block = B>,
9191
SC: SelectChain<B>,
9292
TransactionFor<C, B>: 'static,
9393
CIDP: CreateInherentDataProviders<B, ()>,
94+
P: Send + Sync + 'static,
9495
{
9596
let future = async {
9697
if pool.status().ready == 0 && !create_empty {
@@ -138,6 +139,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP>(
138139
}
139140

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

149151
if let Some(digest_provider) = digest_provider {
150-
digest_provider.append_block_import(&parent, &mut params, &inherent_data)?;
152+
digest_provider.append_block_import(&parent, &mut params, &inherent_data, proof)?;
151153
}
152154

153155
// Make sure we return the same post-hash that will be calculated when importing the block

0 commit comments

Comments
 (0)