use super::{InherentDataProviderExt, Slot, LOG_TARGET};
use sp_consensus::{Error, SelectChain};
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use futures_timer::Delay;
use std::time::{Duration, Instant};
pub fn duration_now() -> Duration {
use std::time::SystemTime;
let now = SystemTime::now();
now.duration_since(SystemTime::UNIX_EPOCH).unwrap_or_else(|e| {
panic!("Current time {:?} is before unix epoch. Something is wrong: {:?}", now, e)
})
}
pub fn time_until_next_slot(slot_duration: Duration) -> Duration {
let now = duration_now().as_millis();
let next_slot = (now + slot_duration.as_millis()) / slot_duration.as_millis();
let remaining_millis = next_slot * slot_duration.as_millis() - now;
Duration::from_millis(remaining_millis as u64)
}
pub struct SlotInfo<B: BlockT> {
pub slot: Slot,
pub ends_at: Instant,
pub create_inherent_data: Box<dyn InherentDataProvider>,
pub duration: Duration,
pub chain_head: B::Header,
pub block_size_limit: Option<usize>,
}
impl<B: BlockT> SlotInfo<B> {
pub fn new(
slot: Slot,
create_inherent_data: Box<dyn InherentDataProvider>,
duration: Duration,
chain_head: B::Header,
block_size_limit: Option<usize>,
) -> Self {
Self {
slot,
create_inherent_data,
duration,
chain_head,
block_size_limit,
ends_at: Instant::now() + time_until_next_slot(duration),
}
}
}
pub(crate) struct Slots<Block, SC, IDP> {
last_slot: Slot,
slot_duration: Duration,
inner_delay: Option<Delay>,
create_inherent_data_providers: IDP,
select_chain: SC,
_phantom: std::marker::PhantomData<Block>,
}
impl<Block, SC, IDP> Slots<Block, SC, IDP> {
pub fn new(
slot_duration: Duration,
create_inherent_data_providers: IDP,
select_chain: SC,
) -> Self {
Slots {
last_slot: 0.into(),
slot_duration,
inner_delay: None,
create_inherent_data_providers,
select_chain,
_phantom: Default::default(),
}
}
}
impl<Block, SC, IDP> Slots<Block, SC, IDP>
where
Block: BlockT,
SC: SelectChain<Block>,
IDP: CreateInherentDataProviders<Block, ()> + 'static,
IDP::InherentDataProviders: crate::InherentDataProviderExt,
{
pub async fn next_slot(&mut self) -> Result<SlotInfo<Block>, Error> {
loop {
self.inner_delay = match self.inner_delay.take() {
None => {
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;
}
let ends_in = time_until_next_slot(self.slot_duration);
self.inner_delay = Some(Delay::new(ends_in));
let chain_head = match self.select_chain.best_chain().await {
Ok(x) => x,
Err(e) => {
log::warn!(
target: LOG_TARGET,
"Unable to author block in slot. No best block header: {}",
e,
);
self.inner_delay.take();
continue
},
};
let inherent_data_providers = self
.create_inherent_data_providers
.create_inherent_data_providers(chain_head.hash(), ())
.await?;
let slot = inherent_data_providers.slot();
if slot > self.last_slot {
self.last_slot = slot;
break Ok(SlotInfo::new(
slot,
Box::new(inherent_data_providers),
self.slot_duration,
chain_head,
None,
))
}
}
}
}