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

Commit 37bb3ae

Browse files
authored
consensus: handle justification sync for blocks authored locally (#8698)
* consensus: add trait to control justification sync process * network: implement JustificationSyncLink for NetworkService * slots: handle justification sync in slot worker * babe: fix slot worker instantiation * aura: fix slot worker instantiation * pow: handle justification sync in miner * babe: fix tests * aura: fix tests * node: fix compilation * node-template: fix compilation * consensus: rename justification sync link parameter * aura: fix test compilation * consensus: slots: move JustificationSyncLink out of on_slot
1 parent 2cff60c commit 37bb3ae

File tree

13 files changed

+228
-70
lines changed

13 files changed

+228
-70
lines changed

bin/node-template/node/src/service.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
220220
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
221221
let raw_slot_duration = slot_duration.slot_duration();
222222

223-
let aura = sc_consensus_aura::start_aura::<AuraPair, _, _, _, _, _, _, _, _, _, _>(
223+
let aura = sc_consensus_aura::start_aura::<AuraPair, _, _, _, _, _, _, _, _, _, _, _>(
224224
StartAuraParams {
225225
slot_duration,
226226
client: client.clone(),
@@ -243,6 +243,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
243243
keystore: keystore_container.sync_keystore(),
244244
can_author_with,
245245
sync_oracle: network.clone(),
246+
justification_sync_link: network.clone(),
246247
block_proposal_slot_portion: SlotProportion::new(2f32 / 3f32),
247248
telemetry: telemetry.as_ref().map(|x| x.handle()),
248249
},

bin/node/cli/src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ pub fn new_full_base(
308308
env: proposer,
309309
block_import,
310310
sync_oracle: network.clone(),
311+
justification_sync_link: network.clone(),
311312
create_inherent_data_providers: move |parent, ()| {
312313
let client_clone = client_clone.clone();
313314
async move {

client/consensus/aura/src/lib.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn slot_author<P: Pair>(slot: Slot, authorities: &[AuthorityId<P>]) -> Option<&A
109109
}
110110

111111
/// Parameters of [`start_aura`].
112-
pub struct StartAuraParams<C, SC, I, PF, SO, BS, CAW, IDP> {
112+
pub struct StartAuraParams<C, SC, I, PF, SO, L, CIDP, BS, CAW> {
113113
/// The duration of a slot.
114114
pub slot_duration: SlotDuration,
115115
/// The client to interact with the chain.
@@ -122,8 +122,10 @@ pub struct StartAuraParams<C, SC, I, PF, SO, BS, CAW, IDP> {
122122
pub proposer_factory: PF,
123123
/// The sync oracle that can give us the current sync status.
124124
pub sync_oracle: SO,
125+
/// Hook into the sync module to control the justification sync process.
126+
pub justification_sync_link: L,
125127
/// Something that can create the inherent data providers.
126-
pub create_inherent_data_providers: IDP,
128+
pub create_inherent_data_providers: CIDP,
127129
/// Should we force the authoring of blocks?
128130
pub force_authoring: bool,
129131
/// The backoff strategy when we miss slots.
@@ -143,46 +145,49 @@ pub struct StartAuraParams<C, SC, I, PF, SO, BS, CAW, IDP> {
143145
}
144146

145147
/// Start the aura worker. The returned future should be run in a futures executor.
146-
pub fn start_aura<P, B, C, SC, PF, I, SO, CAW, BS, Error, IDP>(
148+
pub fn start_aura<P, B, C, SC, I, PF, SO, L, CIDP, BS, CAW, Error>(
147149
StartAuraParams {
148150
slot_duration,
149151
client,
150152
select_chain,
151153
block_import,
152154
proposer_factory,
153155
sync_oracle,
156+
justification_sync_link,
154157
create_inherent_data_providers,
155158
force_authoring,
156159
backoff_authoring_blocks,
157160
keystore,
158161
can_author_with,
159162
block_proposal_slot_portion,
160163
telemetry,
161-
}: StartAuraParams<C, SC, I, PF, SO, BS, CAW, IDP>,
164+
}: StartAuraParams<C, SC, I, PF, SO, L, CIDP, BS, CAW>,
162165
) -> Result<impl Future<Output = ()>, sp_consensus::Error> where
166+
P: Pair + Send + Sync,
167+
P::Public: AppPublic + Hash + Member + Encode + Decode,
168+
P::Signature: TryFrom<Vec<u8>> + Hash + Member + Encode + Decode,
163169
B: BlockT,
164170
C: ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + AuxStore + HeaderBackend<B> + Send + Sync,
165171
C::Api: AuraApi<B, AuthorityId<P>>,
166172
SC: SelectChain<B>,
173+
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
167174
PF: Environment<B, Error = Error> + Send + Sync + 'static,
168175
PF::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
169-
P: Pair + Send + Sync,
170-
P::Public: AppPublic + Hash + Member + Encode + Decode,
171-
P::Signature: TryFrom<Vec<u8>> + Hash + Member + Encode + Decode,
172-
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
173-
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
174176
SO: SyncOracle + Send + Sync + Clone,
175-
CAW: CanAuthorWith<B> + Send,
177+
L: sp_consensus::JustificationSyncLink<B>,
178+
CIDP: CreateInherentDataProviders<B, ()> + Send,
179+
CIDP::InherentDataProviders: InherentDataProviderExt + Send,
176180
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
177-
IDP: CreateInherentDataProviders<B, ()> + Send,
178-
IDP::InherentDataProviders: InherentDataProviderExt + Send,
181+
CAW: CanAuthorWith<B> + Send,
182+
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
179183
{
180-
let worker = build_aura_worker::<P, _, _, _, _, _, _, _>(BuildAuraWorkerParams {
184+
let worker = build_aura_worker::<P, _, _, _, _, _, _, _, _>(BuildAuraWorkerParams {
181185
client: client.clone(),
182186
block_import,
183187
proposer_factory,
184188
keystore,
185189
sync_oracle: sync_oracle.clone(),
190+
justification_sync_link,
186191
force_authoring,
187192
backoff_authoring_blocks,
188193
telemetry,
@@ -200,7 +205,7 @@ pub fn start_aura<P, B, C, SC, PF, I, SO, CAW, BS, Error, IDP>(
200205
}
201206

202207
/// Parameters of [`build_aura_worker`].
203-
pub struct BuildAuraWorkerParams<C, I, PF, SO, BS> {
208+
pub struct BuildAuraWorkerParams<C, I, PF, SO, L, BS> {
204209
/// The client to interact with the chain.
205210
pub client: Arc<C>,
206211
/// The block import.
@@ -209,6 +214,8 @@ pub struct BuildAuraWorkerParams<C, I, PF, SO, BS> {
209214
pub proposer_factory: PF,
210215
/// The sync oracle that can give us the current sync status.
211216
pub sync_oracle: SO,
217+
/// Hook into the sync module to control the justification sync process.
218+
pub justification_sync_link: L,
212219
/// Should we force the authoring of blocks?
213220
pub force_authoring: bool,
214221
/// The backoff strategy when we miss slots.
@@ -228,18 +235,19 @@ pub struct BuildAuraWorkerParams<C, I, PF, SO, BS> {
228235
/// Build the aura worker.
229236
///
230237
/// The caller is responsible for running this worker, otherwise it will do nothing.
231-
pub fn build_aura_worker<P, B, C, PF, I, SO, BS, Error>(
238+
pub fn build_aura_worker<P, B, C, PF, I, SO, L, BS, Error>(
232239
BuildAuraWorkerParams {
233240
client,
234241
block_import,
235242
proposer_factory,
236243
sync_oracle,
244+
justification_sync_link,
237245
backoff_authoring_blocks,
238246
keystore,
239247
block_proposal_slot_portion,
240248
telemetry,
241249
force_authoring,
242-
}: BuildAuraWorkerParams<C, I, PF, SO, BS>,
250+
}: BuildAuraWorkerParams<C, I, PF, SO, L, BS>,
243251
) -> impl sc_consensus_slots::SlotWorker<B, <PF::Proposer as Proposer<B>>::Proof> where
244252
B: BlockT,
245253
C: ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + AuxStore + HeaderBackend<B> + Send + Sync,
@@ -252,6 +260,7 @@ pub fn build_aura_worker<P, B, C, PF, I, SO, BS, Error>(
252260
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
253261
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
254262
SO: SyncOracle + Send + Sync + Clone,
263+
L: sp_consensus::JustificationSyncLink<B>,
255264
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
256265
{
257266
AuraWorker {
@@ -260,6 +269,7 @@ pub fn build_aura_worker<P, B, C, PF, I, SO, BS, Error>(
260269
env: proposer_factory,
261270
keystore,
262271
sync_oracle,
272+
justification_sync_link,
263273
force_authoring,
264274
backoff_authoring_blocks,
265275
telemetry,
@@ -268,21 +278,22 @@ pub fn build_aura_worker<P, B, C, PF, I, SO, BS, Error>(
268278
}
269279
}
270280

271-
struct AuraWorker<C, E, I, P, SO, BS> {
281+
struct AuraWorker<C, E, I, P, SO, L, BS> {
272282
client: Arc<C>,
273283
block_import: I,
274284
env: E,
275285
keystore: SyncCryptoStorePtr,
276286
sync_oracle: SO,
287+
justification_sync_link: L,
277288
force_authoring: bool,
278289
backoff_authoring_blocks: Option<BS>,
279290
block_proposal_slot_portion: SlotProportion,
280291
telemetry: Option<TelemetryHandle>,
281292
_key_type: PhantomData<P>,
282293
}
283294

284-
impl<B, C, E, I, P, Error, SO, BS> sc_consensus_slots::SimpleSlotWorker<B>
285-
for AuraWorker<C, E, I, P, SO, BS>
295+
impl<B, C, E, I, P, Error, SO, L, BS> sc_consensus_slots::SimpleSlotWorker<B>
296+
for AuraWorker<C, E, I, P, SO, L, BS>
286297
where
287298
B: BlockT,
288299
C: ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + HeaderBackend<B> + Sync,
@@ -294,11 +305,13 @@ where
294305
P::Public: AppPublic + Public + Member + Encode + Decode + Hash,
295306
P::Signature: TryFrom<Vec<u8>> + Member + Encode + Decode + Hash + Debug,
296307
SO: SyncOracle + Send + Clone,
308+
L: sp_consensus::JustificationSyncLink<B>,
297309
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
298310
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
299311
{
300312
type BlockImport = I;
301313
type SyncOracle = SO;
314+
type JustificationSyncLink = L;
302315
type CreateProposer = Pin<Box<
303316
dyn Future<Output = Result<E::Proposer, sp_consensus::Error>> + Send + 'static
304317
>>;
@@ -425,6 +438,10 @@ where
425438
&mut self.sync_oracle
426439
}
427440

441+
fn justification_sync_link(&mut self) -> &mut Self::JustificationSyncLink {
442+
&mut self.justification_sync_link
443+
}
444+
428445
fn proposer(&mut self, block: &B::Header) -> Self::CreateProposer {
429446
Box::pin(self.env.init(block).map_err(|e| {
430447
sp_consensus::Error::ClientImport(format!("{:?}", e)).into()
@@ -725,13 +742,14 @@ mod tests {
725742

726743
let slot_duration = slot_duration(&*client).expect("slot duration available");
727744

728-
aura_futures.push(start_aura::<AuthorityPair, _, _, _, _, _, _, _, _, _, _>(StartAuraParams {
745+
aura_futures.push(start_aura::<AuthorityPair, _, _, _, _, _, _, _, _, _, _, _>(StartAuraParams {
729746
slot_duration,
730747
block_import: client.clone(),
731748
select_chain,
732749
client,
733750
proposer_factory: environ,
734751
sync_oracle: DummyOracle,
752+
justification_sync_link: (),
735753
create_inherent_data_providers: |_, _| async {
736754
let timestamp = TimestampInherentDataProvider::from_system_time();
737755
let slot = InherentDataProvider::from_timestamp_and_duration(
@@ -804,6 +822,7 @@ mod tests {
804822
env: environ,
805823
keystore: keystore.into(),
806824
sync_oracle: DummyOracle.clone(),
825+
justification_sync_link: (),
807826
force_authoring: false,
808827
backoff_authoring_blocks: Some(BackoffAuthoringOnFinalizedHeadLagging::default()),
809828
telemetry: None,
@@ -853,6 +872,7 @@ mod tests {
853872
env: environ,
854873
keystore: keystore.into(),
855874
sync_oracle: DummyOracle.clone(),
875+
justification_sync_link: (),
856876
force_authoring: false,
857877
backoff_authoring_blocks: Option::<()>::None,
858878
telemetry: None,
@@ -871,7 +891,7 @@ mod tests {
871891
duration: Duration::from_millis(1000),
872892
chain_head: head,
873893
block_size_limit: None,
874-
},
894+
}
875895
)).unwrap();
876896

877897
// The returned block should be imported and we should be able to get its header by now.

client/consensus/babe/src/lib.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl std::ops::Deref for Config {
363363
}
364364

365365
/// Parameters for BABE.
366-
pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS, IDP> {
366+
pub struct BabeParams<B: BlockT, C, SC, E, I, SO, L, CIDP, BS, CAW> {
367367
/// The keystore that manages the keys of the node.
368368
pub keystore: SyncCryptoStorePtr,
369369

@@ -384,8 +384,11 @@ pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS, IDP> {
384384
/// A sync oracle
385385
pub sync_oracle: SO,
386386

387+
/// Hook into the sync module to control the justification sync process.
388+
pub justification_sync_link: L,
389+
387390
/// Something that can create the inherent data providers.
388-
pub create_inherent_data_providers: IDP,
391+
pub create_inherent_data_providers: CIDP,
389392

390393
/// Force authoring of blocks even if we are offline
391394
pub force_authoring: bool,
@@ -411,40 +414,50 @@ pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS, IDP> {
411414
}
412415

413416
/// Start the babe worker.
414-
pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error, IDP>(BabeParams {
417+
pub fn start_babe<B, C, SC, E, I, SO, CIDP, BS, CAW, L, Error>(BabeParams {
415418
keystore,
416419
client,
417420
select_chain,
418421
env,
419422
block_import,
420423
sync_oracle,
424+
justification_sync_link,
421425
create_inherent_data_providers,
422426
force_authoring,
423427
backoff_authoring_blocks,
424428
babe_link,
425429
can_author_with,
426430
block_proposal_slot_portion,
427431
telemetry,
428-
}: BabeParams<B, C, E, I, SO, SC, CAW, BS, IDP>) -> Result<
432+
}: BabeParams<B, C, SC, E, I, SO, L, CIDP, BS, CAW>) -> Result<
429433
BabeWorker<B>,
430434
sp_consensus::Error,
431435
> where
432436
B: BlockT,
433-
C: ProvideRuntimeApi<B> + ProvideCache<B> + ProvideUncles<B> + BlockchainEvents<B>
434-
+ HeaderBackend<B> + HeaderMetadata<B, Error = ClientError>
435-
+ Send + Sync + 'static,
437+
C: ProvideRuntimeApi<B>
438+
+ ProvideCache<B>
439+
+ ProvideUncles<B>
440+
+ BlockchainEvents<B>
441+
+ HeaderBackend<B>
442+
+ HeaderMetadata<B, Error = ClientError>
443+
+ Send
444+
+ Sync
445+
+ 'static,
436446
C::Api: BabeApi<B>,
437447
SC: SelectChain<B> + 'static,
438448
E: Environment<B, Error = Error> + Send + Sync + 'static,
439449
E::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
440-
I: BlockImport<B, Error = ConsensusError, Transaction = sp_api::TransactionFor<C, B>> + Send
441-
+ Sync + 'static,
442-
Error: std::error::Error + Send + From<ConsensusError> + From<I::Error> + 'static,
450+
I: BlockImport<B, Error = ConsensusError, Transaction = sp_api::TransactionFor<C, B>>
451+
+ Send
452+
+ Sync
453+
+ 'static,
443454
SO: SyncOracle + Send + Sync + Clone + 'static,
444-
CAW: CanAuthorWith<B> + Send + Sync + 'static,
455+
L: sp_consensus::JustificationSyncLink<B> + 'static,
456+
CIDP: CreateInherentDataProviders<B, ()> + Send + Sync + 'static,
457+
CIDP::InherentDataProviders: InherentDataProviderExt + Send,
445458
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + 'static,
446-
IDP: CreateInherentDataProviders<B, ()> + Send + Sync + 'static,
447-
IDP::InherentDataProviders: InherentDataProviderExt + Send,
459+
CAW: CanAuthorWith<B> + Send + Sync + 'static,
460+
Error: std::error::Error + Send + From<ConsensusError> + From<I::Error> + 'static,
448461
{
449462
const HANDLE_BUFFER_SIZE: usize = 1024;
450463

@@ -456,6 +469,7 @@ pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error, IDP>(BabeParams {
456469
block_import,
457470
env,
458471
sync_oracle: sync_oracle.clone(),
472+
justification_sync_link,
459473
force_authoring,
460474
backoff_authoring_blocks,
461475
keystore,
@@ -600,11 +614,12 @@ type SlotNotificationSinks<B> = Arc<
600614
Mutex<Vec<Sender<(Slot, ViableEpochDescriptor<<B as BlockT>::Hash, NumberFor<B>, Epoch>)>>>
601615
>;
602616

603-
struct BabeSlotWorker<B: BlockT, C, E, I, SO, BS> {
617+
struct BabeSlotWorker<B: BlockT, C, E, I, SO, L, BS> {
604618
client: Arc<C>,
605619
block_import: I,
606620
env: E,
607621
sync_oracle: SO,
622+
justification_sync_link: L,
608623
force_authoring: bool,
609624
backoff_authoring_blocks: Option<BS>,
610625
keystore: SyncCryptoStorePtr,
@@ -615,8 +630,8 @@ struct BabeSlotWorker<B: BlockT, C, E, I, SO, BS> {
615630
telemetry: Option<TelemetryHandle>,
616631
}
617632

618-
impl<B, C, E, I, Error, SO, BS> sc_consensus_slots::SimpleSlotWorker<B>
619-
for BabeSlotWorker<B, C, E, I, SO, BS>
633+
impl<B, C, E, I, Error, SO, L, BS> sc_consensus_slots::SimpleSlotWorker<B>
634+
for BabeSlotWorker<B, C, E, I, SO, L, BS>
620635
where
621636
B: BlockT,
622637
C: ProvideRuntimeApi<B> +
@@ -628,12 +643,14 @@ where
628643
E::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
629644
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
630645
SO: SyncOracle + Send + Clone,
646+
L: sp_consensus::JustificationSyncLink<B>,
631647
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>>,
632648
Error: std::error::Error + Send + From<ConsensusError> + From<I::Error> + 'static,
633649
{
634650
type EpochData = ViableEpochDescriptor<B::Hash, NumberFor<B>, Epoch>;
635651
type Claim = (PreDigest, AuthorityId);
636652
type SyncOracle = SO;
653+
type JustificationSyncLink = L;
637654
type CreateProposer = Pin<Box<
638655
dyn Future<Output = Result<E::Proposer, sp_consensus::Error>> + Send + 'static
639656
>>;
@@ -798,6 +815,10 @@ where
798815
&mut self.sync_oracle
799816
}
800817

818+
fn justification_sync_link(&mut self) -> &mut Self::JustificationSyncLink {
819+
&mut self.justification_sync_link
820+
}
821+
801822
fn proposer(&mut self, block: &B::Header) -> Self::CreateProposer {
802823
Box::pin(self.env.init(block).map_err(|e| {
803824
sp_consensus::Error::ClientImport(format!("{:?}", e))

0 commit comments

Comments
 (0)