#![forbid(unsafe_code, missing_docs)]
mod slots;
mod aux_schema;
pub use slots::{SignedDuration, SlotInfo};
use slots::Slots;
pub use aux_schema::{check_equivocation, MAX_SLOT_CAPACITY, PRUNING_BOUND};
use codec::{Decode, Encode};
use sp_consensus::{BlockImport, Proposer, SyncOracle, SelectChain, CanAuthorWith, SlotData, RecordProof};
use futures::{prelude::*, future::{self, Either}};
use futures_timer::Delay;
use sp_inherents::{InherentData, InherentDataProviders};
use log::{debug, error, info, warn};
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Header, HashFor, NumberFor};
use sp_api::{ProvideRuntimeApi, ApiRef};
use std::{fmt::Debug, ops::Deref, pin::Pin, sync::Arc, time::{Instant, Duration}};
use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO};
use parking_lot::Mutex;
pub type StorageChanges<Transaction, Block> =
sp_state_machine::StorageChanges<Transaction, HashFor<Block>, NumberFor<Block>>;
pub trait SlotWorker<B: BlockT> {
type OnSlot: Future<Output = Result<(), sp_consensus::Error>>;
fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot;
}
pub trait SimpleSlotWorker<B: BlockT> {
type BlockImport: BlockImport<B, Transaction = <Self::Proposer as Proposer<B>>::Transaction>
+ Send + 'static;
type SyncOracle: SyncOracle;
type CreateProposer: Future<Output = Result<Self::Proposer, sp_consensus::Error>>
+ Send + Unpin + 'static;
type Proposer: Proposer<B>;
type Claim: Send + 'static;
type EpochData: Send + 'static;
fn logging_target(&self) -> &'static str;
fn block_import(&self) -> Arc<Mutex<Self::BlockImport>>;
fn epoch_data(&self, header: &B::Header, slot_number: u64) -> Result<Self::EpochData, sp_consensus::Error>;
fn authorities_len(&self, epoch_data: &Self::EpochData) -> Option<usize>;
fn claim_slot(
&self,
header: &B::Header,
slot_number: u64,
epoch_data: &Self::EpochData,
) -> Option<Self::Claim>;
fn notify_slot(
&self,
_header: &B::Header,
_slot_number: u64,
_epoch_data: &Self::EpochData,
) { }
fn pre_digest_data(
&self,
slot_number: u64,
claim: &Self::Claim,
) -> Vec<sp_runtime::DigestItem<B::Hash>>;
fn block_import_params(&self) -> Box<
dyn Fn(
B::Header,
&B::Hash,
Vec<B::Extrinsic>,
StorageChanges<<Self::BlockImport as BlockImport<B>>::Transaction, B>,
Self::Claim,
Self::EpochData,
) -> Result<
sp_consensus::BlockImportParams<B, <Self::BlockImport as BlockImport<B>>::Transaction>,
sp_consensus::Error
> + Send + 'static
>;
fn force_authoring(&self) -> bool;
fn sync_oracle(&mut self) -> &mut Self::SyncOracle;
fn proposer(&mut self, block: &B::Header) -> Self::CreateProposer;
fn slot_remaining_duration(&self, slot_info: &SlotInfo) -> Duration {
let now = Instant::now();
if now < slot_info.ends_at {
slot_info.ends_at.duration_since(now)
} else {
Duration::from_millis(0)
}
}
fn proposing_remaining_duration(
&self,
_head: &B::Header,
slot_info: &SlotInfo
) -> Option<Duration> {
Some(self.slot_remaining_duration(slot_info))
}
fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo)
-> Pin<Box<dyn Future<Output = Result<(), sp_consensus::Error>> + Send>> where
Self: Send + Sync,
<Self::Proposer as Proposer<B>>::Proposal: Unpin + Send + 'static,
{
let (timestamp, slot_number, slot_duration) =
(slot_info.timestamp, slot_info.number, slot_info.duration);
{
let slot_now = SignedDuration::default().slot_now(slot_duration);
if slot_now > slot_number {
debug!(target: self.logging_target(),
"Skipping proposal slot {} since our current view is {}",
slot_number, slot_now,
);
return Box::pin(future::ready(Ok(())));
}
}
let epoch_data = match self.epoch_data(&chain_head, slot_number) {
Ok(epoch_data) => epoch_data,
Err(err) => {
warn!("Unable to fetch epoch data at block {:?}: {:?}", chain_head.hash(), err);
telemetry!(
CONSENSUS_WARN; "slots.unable_fetching_authorities";
"slot" => ?chain_head.hash(),
"err" => ?err,
);
return Box::pin(future::ready(Ok(())));
}
};
self.notify_slot(&chain_head, slot_number, &epoch_data);
let authorities_len = self.authorities_len(&epoch_data);
if !self.force_authoring() &&
self.sync_oracle().is_offline() &&
authorities_len.map(|a| a > 1).unwrap_or(false)
{
debug!(target: self.logging_target(), "Skipping proposal slot. Waiting for the network.");
telemetry!(
CONSENSUS_DEBUG;
"slots.skipping_proposal_slot";
"authorities_len" => authorities_len,
);
return Box::pin(future::ready(Ok(())));
}
let claim = match self.claim_slot(&chain_head, slot_number, &epoch_data) {
None => return Box::pin(future::ready(Ok(()))),
Some(claim) => claim,
};
debug!(
target: self.logging_target(), "Starting authorship at slot {}; timestamp = {}",
slot_number,
timestamp,
);
telemetry!(CONSENSUS_DEBUG; "slots.starting_authorship";
"slot_num" => slot_number,
"timestamp" => timestamp,
);
let awaiting_proposer = self.proposer(&chain_head).map_err(move |err| {
warn!("Unable to author block in slot {:?}: {:?}", slot_number, err);
telemetry!(CONSENSUS_WARN; "slots.unable_authoring_block";
"slot" => slot_number, "err" => ?err
);
err
});
let slot_remaining_duration = self.slot_remaining_duration(&slot_info);
let proposing_remaining_duration = self.proposing_remaining_duration(&chain_head, &slot_info);
let logs = self.pre_digest_data(slot_number, &claim);
let proposing = awaiting_proposer.and_then(move |proposer| proposer.propose(
slot_info.inherent_data,
sp_runtime::generic::Digest {
logs,
},
slot_remaining_duration,
RecordProof::No,
).map_err(|e| sp_consensus::Error::ClientImport(format!("{:?}", e))));
let delay: Box<dyn Future<Output=()> + Unpin + Send> = match proposing_remaining_duration {
Some(r) => Box::new(Delay::new(r)),
None => Box::new(future::pending()),
};
let proposal_work =
Box::new(futures::future::select(proposing, delay).map(move |v| match v {
futures::future::Either::Left((b, _)) => b.map(|b| (b, claim)),
futures::future::Either::Right(_) => {
info!("⌛️ Discarding proposal for slot {}; block production took too long", slot_number);
#[cfg(build_type="debug")]
info!("👉 Recompile your node in `--release` mode to mitigate this problem.");
telemetry!(CONSENSUS_INFO; "slots.discarding_proposal_took_too_long";
"slot" => slot_number,
);
Err(sp_consensus::Error::ClientImport("Timeout in the Slots proposer".into()))
},
}));
let block_import_params_maker = self.block_import_params();
let block_import = self.block_import();
let logging_target = self.logging_target();
Box::pin(proposal_work.and_then(move |(proposal, claim)| {
let (header, body) = proposal.block.deconstruct();
let header_num = *header.number();
let header_hash = header.hash();
let parent_hash = *header.parent_hash();
let block_import_params = block_import_params_maker(
header,
&header_hash,
body,
proposal.storage_changes,
claim,
epoch_data,
);
let block_import_params = match block_import_params {
Ok(params) => params,
Err(e) => return future::err(e),
};
info!(
"🔖 Pre-sealed block for proposal at {}. Hash now {:?}, previously {:?}.",
header_num,
block_import_params.post_hash(),
header_hash,
);
telemetry!(CONSENSUS_INFO; "slots.pre_sealed_block";
"header_num" => ?header_num,
"hash_now" => ?block_import_params.post_hash(),
"hash_previously" => ?header_hash,
);
if let Err(err) = block_import.lock().import_block(block_import_params, Default::default()) {
warn!(target: logging_target,
"Error with block built on {:?}: {:?}",
parent_hash,
err,
);
telemetry!(CONSENSUS_WARN; "slots.err_with_block_built_on";
"hash" => ?parent_hash, "err" => ?err,
);
}
future::ready(Ok(()))
}))
}
}
pub trait SlotCompatible {
fn extract_timestamp_and_slot(
&self,
inherent: &InherentData,
) -> Result<(u64, u64, std::time::Duration), sp_consensus::Error>;
fn time_offset() -> SignedDuration { Default::default() }
}
pub fn start_slot_worker<B, C, W, T, SO, SC, CAW>(
slot_duration: SlotDuration<T>,
client: C,
mut worker: W,
mut sync_oracle: SO,
inherent_data_providers: InherentDataProviders,
timestamp_extractor: SC,
can_author_with: CAW,
) -> impl Future<Output = ()>
where
B: BlockT,
C: SelectChain<B>,
W: SlotWorker<B>,
W::OnSlot: Unpin,
SO: SyncOracle + Send,
SC: SlotCompatible + Unpin,
T: SlotData + Clone,
CAW: CanAuthorWith<B> + Send,
{
let SlotDuration(slot_duration) = slot_duration;
Slots::<SC>::new(
slot_duration.slot_duration(),
inherent_data_providers,
timestamp_extractor,
).inspect_err(|e| debug!(target: "slots", "Faulty timer: {:?}", e))
.try_for_each(move |slot_info| {
if sync_oracle.is_major_syncing() {
debug!(target: "slots", "Skipping proposal slot due to sync.");
return Either::Right(future::ready(Ok(())));
}
let slot_num = slot_info.number;
let chain_head = match client.best_chain() {
Ok(x) => x,
Err(e) => {
warn!(target: "slots", "Unable to author block in slot {}. \
no best block header: {:?}", slot_num, e);
return Either::Right(future::ready(Ok(())));
}
};
if let Err(err) = can_author_with.can_author_with(&BlockId::Hash(chain_head.hash())) {
warn!(
target: "slots",
"Unable to author block in slot {},. `can_author_with` returned: {} \
Probably a node update is required!",
slot_num,
err,
);
Either::Right(future::ready(Ok(())))
} else {
Either::Left(
worker.on_slot(chain_head, slot_info)
.map_err(|e| {
warn!(target: "slots", "Encountered consensus error: {:?}", e);
})
.or_else(|_| future::ready(Ok(())))
)
}
}).then(|res| {
if let Err(err) = res {
warn!(target: "slots", "Slots stream terminated with an error: {:?}", err);
}
future::ready(())
})
}
pub enum CheckedHeader<H, S> {
Deferred(H, u64),
Checked(H, S),
}
#[derive(Clone, Copy, Debug, Encode, Decode, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct SlotDuration<T>(T);
impl<T> Deref for SlotDuration<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
impl<T: SlotData + Clone> SlotData for SlotDuration<T> {
fn slot_duration(&self) -> u64
where T: SlotData,
{
self.0.slot_duration()
}
const SLOT_KEY: &'static [u8] = T::SLOT_KEY;
}
impl<T: Clone> SlotDuration<T> {
pub fn get_or_compute<B: BlockT, C, CB>(client: &C, cb: CB) -> sp_blockchain::Result<Self> where
C: sc_client_api::backend::AuxStore,
C: ProvideRuntimeApi<B>,
CB: FnOnce(ApiRef<C::Api>, &BlockId<B>) -> sp_blockchain::Result<T>,
T: SlotData + Encode + Decode + Debug,
{
let slot_duration = match client.get_aux(T::SLOT_KEY)? {
Some(v) => <T as codec::Decode>::decode(&mut &v[..])
.map(SlotDuration)
.map_err(|_| {
sp_blockchain::Error::Backend({
error!(target: "slots", "slot duration kept in invalid format");
"slot duration kept in invalid format".to_string()
})
}),
None => {
use sp_runtime::traits::Zero;
let genesis_slot_duration =
cb(client.runtime_api(), &BlockId::number(Zero::zero()))?;
info!(
"⏱ Loaded block-time = {:?} milliseconds from genesis on first-launch",
genesis_slot_duration.slot_duration()
);
genesis_slot_duration
.using_encoded(|s| client.insert_aux(&[(T::SLOT_KEY, &s[..])], &[]))?;
Ok(SlotDuration(genesis_slot_duration))
}
}?;
if slot_duration.slot_duration() == 0 {
return Err(sp_blockchain::Error::Msg(
"Invalid value for slot_duration: the value must be greater than 0.".into(),
))
}
Ok(slot_duration)
}
pub fn get(&self) -> T {
self.0.clone()
}
}
pub fn slot_lenience_exponential(parent_slot: u64, slot_info: &SlotInfo) -> Option<Duration> {
const BACKOFF_CAP: u64 = 7;
const BACKOFF_STEP: u64 = 2;
let skipped_slots = slot_info.number.saturating_sub(parent_slot + 1);
if skipped_slots == 0 {
None
} else {
let slot_lenience = skipped_slots / BACKOFF_STEP;
let slot_lenience = std::cmp::min(slot_lenience, BACKOFF_CAP);
let slot_lenience = 1 << slot_lenience;
Some(Duration::from_millis(slot_lenience * slot_info.duration))
}
}
pub fn slot_lenience_linear(parent_slot: u64, slot_info: &SlotInfo) -> Option<Duration> {
const BACKOFF_CAP: u64 = 20;
let skipped_slots = slot_info.number.saturating_sub(parent_slot + 1);
if skipped_slots == 0 {
None
} else {
let slot_lenience = std::cmp::min(skipped_slots, BACKOFF_CAP);
Some(Duration::from_millis(slot_lenience * slot_info.duration))
}
}
#[cfg(test)]
mod test {
use std::time::{Duration, Instant};
const SLOT_DURATION: Duration = Duration::from_millis(6000);
fn slot(n: u64) -> super::slots::SlotInfo {
super::slots::SlotInfo {
number: n,
last_number: n - 1,
duration: SLOT_DURATION.as_millis() as u64,
timestamp: Default::default(),
inherent_data: Default::default(),
ends_at: Instant::now(),
}
}
#[test]
fn linear_slot_lenience() {
assert_eq!(super::slot_lenience_linear(1, &slot(2)), None);
for n in 3..=22 {
assert_eq!(
super::slot_lenience_linear(1, &slot(n)),
Some(SLOT_DURATION * (n - 2) as u32),
);
}
assert_eq!(
super::slot_lenience_linear(1, &slot(23)),
Some(SLOT_DURATION * 20),
);
}
#[test]
fn exponential_slot_lenience() {
assert_eq!(super::slot_lenience_exponential(1, &slot(2)), None);
for n in 3..=17 {
assert_eq!(
super::slot_lenience_exponential(1, &slot(n)),
Some(SLOT_DURATION * 2u32.pow((n / 2 - 1) as u32)),
);
}
assert_eq!(
super::slot_lenience_exponential(1, &slot(18)),
Some(SLOT_DURATION * 2u32.pow(7)),
);
assert_eq!(
super::slot_lenience_exponential(1, &slot(19)),
Some(SLOT_DURATION * 2u32.pow(7)),
);
}
}