Skip to content

Commit

Permalink
Skip slot before creating inherent data providers during major sync (#…
Browse files Browse the repository at this point in the history
…5344)

# Description

Moves `create_inherent_data_provider` after checking if major sync is in
progress.

## Integration

Change is internal to sc-consensus-slots. It should be no-op unless
someone is using fork of this SDK.

## Review Notes

Motivation for this change is to avoid calling
`create_inherent_data_providers` if it's result is going to be discarded
anyway during major sync. This has potential to speed up node operations
during major sync by not calling possibly expensive
`create_inherent_data_provider`.

TODO: labels T0-node D0-simple
TODO: there is no tests for `Slots`, should I add one for this case?

# Checklist

* [x] My PR includes a detailed description as outlined in the
"Description" and its two subsections above.
* [x] My PR follows the [labeling requirements](CONTRIBUTING.md#Process)
of this project (at minimum one label for `T`
  required)
* External contributors: ask maintainers to put the right label on your
PR.
* [ ] I have made corresponding changes to the documentation (if
applicable)
* [ ] I have added tests that prove my fix is effective or that my
feature works (if applicable)
  • Loading branch information
LGLO committed Aug 25, 2024
1 parent 91b5a49 commit 178e699
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
10 changes: 10 additions & 0 deletions prdoc/pr_5344.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Fix storage weight reclaim bug.

doc:
- audience: Node Dev
description: |
Improvement in slot worker loop that will not call create inherent data providers if the major sync is in progress. Before it was called every slot and the results were discarded during major sync.

crates:
- name: sc-consensus-slots
bump: minor
13 changes: 6 additions & 7 deletions substrate/client/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,15 @@ pub async fn start_slot_worker<B, C, W, SO, CIDP, Proof>(
CIDP: CreateInherentDataProviders<B, ()> + Send + 'static,
CIDP::InherentDataProviders: InherentDataProviderExt + Send,
{
let mut slots = Slots::new(slot_duration.as_duration(), create_inherent_data_providers, client);
let mut slots = Slots::new(
slot_duration.as_duration(),
create_inherent_data_providers,
client,
sync_oracle,
);

loop {
let slot_info = slots.next_slot().await;

if sync_oracle.is_major_syncing() {
debug!(target: LOG_TARGET, "Skipping proposal slot due to sync.");
continue
}

let _ = worker.on_slot(slot_info).await;
}
}
Expand Down
17 changes: 13 additions & 4 deletions substrate/client/consensus/slots/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! This is used instead of `futures_timer::Interval` because it was unreliable.

use super::{InherentDataProviderExt, Slot, LOG_TARGET};
use sp_consensus::SelectChain;
use sp_consensus::{SelectChain, SyncOracle};
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};

Expand Down Expand Up @@ -87,39 +87,43 @@ impl<B: BlockT> SlotInfo<B> {
}

/// A stream that returns every time there is a new slot.
pub(crate) struct Slots<Block, SC, IDP> {
pub(crate) struct Slots<Block, SC, IDP, SO> {
last_slot: Slot,
slot_duration: Duration,
until_next_slot: Option<Delay>,
create_inherent_data_providers: IDP,
select_chain: SC,
sync_oracle: SO,
_phantom: std::marker::PhantomData<Block>,
}

impl<Block, SC, IDP> Slots<Block, SC, IDP> {
impl<Block, SC, IDP, SO> Slots<Block, SC, IDP, SO> {
/// Create a new `Slots` stream.
pub fn new(
slot_duration: Duration,
create_inherent_data_providers: IDP,
select_chain: SC,
sync_oracle: SO,
) -> Self {
Slots {
last_slot: 0.into(),
slot_duration,
until_next_slot: None,
create_inherent_data_providers,
select_chain,
sync_oracle,
_phantom: Default::default(),
}
}
}

impl<Block, SC, IDP> Slots<Block, SC, IDP>
impl<Block, SC, IDP, SO> Slots<Block, SC, IDP, SO>
where
Block: BlockT,
SC: SelectChain<Block>,
IDP: CreateInherentDataProviders<Block, ()> + 'static,
IDP::InherentDataProviders: crate::InherentDataProviderExt,
SO: SyncOracle,
{
/// Returns a future that fires when the next slot starts.
pub async fn next_slot(&mut self) -> SlotInfo<Block> {
Expand All @@ -138,6 +142,11 @@ where
let wait_dur = time_until_next_slot(self.slot_duration);
self.until_next_slot = Some(Delay::new(wait_dur));

if self.sync_oracle.is_major_syncing() {
log::debug!(target: LOG_TARGET, "Skipping slot: major sync is in progress.");
continue;
}

let chain_head = match self.select_chain.best_chain().await {
Ok(x) => x,
Err(e) => {
Expand Down

0 comments on commit 178e699

Please sign in to comment.