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

Refactory of next_slot method #13155

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions client/consensus/slots/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,20 @@ where
/// Returns a future that fires when the next slot starts.
pub async fn next_slot(&mut self) -> Result<SlotInfo<Block>, Error> {
davxy marked this conversation as resolved.
Show resolved Hide resolved
loop {
self.inner_delay = match self.inner_delay.take() {
None => {
// schedule wait.
// Wait for slot timeout
self.inner_delay
davxy marked this conversation as resolved.
Show resolved Hide resolved
.take()
.unwrap_or_else(|| {
// Schedule first timeout.
let wait_dur = time_until_next_slot(self.slot_duration);
Some(Delay::new(wait_dur))
},
Some(d) => Some(d),
};

if let Some(inner_delay) = self.inner_delay.take() {
inner_delay.await;
}
// timeout has fired.
Delay::new(wait_dur)
})
.await;

let ends_in = time_until_next_slot(self.slot_duration);
// Schedule delay for next slot.
let wait_dur = time_until_next_slot(self.slot_duration);
self.inner_delay = Some(Delay::new(wait_dur));

// reschedule delay for next slot.
self.inner_delay = Some(Delay::new(ends_in));
let chain_head = match self.select_chain.best_chain().await {
Ok(x) => x,
Err(e) => {
Expand All @@ -150,20 +146,31 @@ where
"Unable to author block in slot. No best block header: {}",
e,
);
// Let's try at the next slot..
self.inner_delay.take();
// Let's retry at the next slot.
continue
},
};

let inherent_data_providers = self
let inherent_data_providers = match self
.create_inherent_data_providers
.create_inherent_data_providers(chain_head.hash(), ())
.await?;
.await
{
Ok(x) => x,
Err(e) => {
log::warn!(
target: LOG_TARGET,
"Unable to author block in slot. Failure creating inherent data provider: {}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to print out which exact slot it is.

e,
);
// Let's retry at the next slot.
continue
},
};

let slot = inherent_data_providers.slot();

// never yield the same slot twice.
// Never yield the same slot twice.
if slot > self.last_slot {
self.last_slot = slot;

Expand Down