mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 20:07:56 +00:00
Relative slots (#2820)
* Initial work on relative slots for BABE * More work * Update core/consensus/babe/src/lib.rs `Aura` → `Babe` Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com> * More work on relative slots * Add missing field in test-runtime * Bump `impl_version` and `authoring_version` * Fix compile errors and warnings * Upgrade dependencies * Update dependencies more * Revert some updates to dependencies Somehow, those broke the build * Fix compilation errors * `Duration` → `u128` in calculations * `slot_duration` is in milleseconds, not seconds * Median algorithm: ignore blocks with slot_num < sl * Fix silly compile error * Store a duration, rather than an instant It is more useful * Fix compilation errors * `INVERSE_NANO` → `NANOS_PER_SEC` Also: `1000_000_000` → `1_000_000_000` Suggested-by: Niklas Adolfsson <niklasadolfsson1@gmail.com> * Un-bump `authoring_version` * Disable median algorithm when `median_required_blocks` is 0 Otherwise it would panic. * Apply suggestions from code review Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Simplify panic * Fix build error * Create `SignedDuration` struct for signed `Duration` values. Suggested-by: Bastian Köcher * Refactor median algorithm into separate function * Add issues for FIXMEs and respond to code review * Fix minor warnings
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
mod slots;
|
||||
mod aux_schema;
|
||||
|
||||
pub use slots::{slot_now, SlotInfo, Slots};
|
||||
pub use slots::{SignedDuration, SlotInfo, Slots};
|
||||
pub use aux_schema::{check_equivocation, MAX_SLOT_CAPACITY, PRUNING_BOUND};
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
|
||||
@@ -23,29 +23,44 @@ use consensus_common::Error;
|
||||
use futures::prelude::*;
|
||||
use futures::try_ready;
|
||||
use inherents::{InherentData, InherentDataProviders};
|
||||
use log::warn;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio_timer::Delay;
|
||||
|
||||
/// Returns current duration since unix epoch.
|
||||
pub fn duration_now() -> Option<Duration> {
|
||||
pub fn duration_now() -> Duration {
|
||||
use std::time::SystemTime;
|
||||
|
||||
let now = SystemTime::now();
|
||||
now.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|e| {
|
||||
warn!(
|
||||
"Current time {:?} is before unix epoch. Something is wrong: {:?}",
|
||||
now, e
|
||||
);
|
||||
})
|
||||
.ok()
|
||||
now.duration_since(SystemTime::UNIX_EPOCH).unwrap_or_else(|e| panic!(
|
||||
"Current time {:?} is before unix epoch. Something is wrong: {:?}",
|
||||
now,
|
||||
e,
|
||||
))
|
||||
}
|
||||
|
||||
/// Get the slot for now.
|
||||
pub fn slot_now(slot_duration: u64) -> Option<u64> {
|
||||
duration_now().map(|s| s.as_secs() / slot_duration)
|
||||
|
||||
/// A `Duration` with a sign (before or after). Immutable.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
||||
pub struct SignedDuration {
|
||||
offset: Duration,
|
||||
is_positive: bool,
|
||||
}
|
||||
|
||||
impl SignedDuration {
|
||||
/// Construct a `SignedDuration`
|
||||
pub fn new(offset: Duration, is_positive: bool) -> Self {
|
||||
Self { offset, is_positive }
|
||||
}
|
||||
|
||||
/// Get the slot for now. Panics if `slot_duration` is 0.
|
||||
pub fn slot_now(&self, slot_duration: u64) -> u64 {
|
||||
if self.is_positive {
|
||||
duration_now() + self.offset
|
||||
} else {
|
||||
duration_now() - self.offset
|
||||
}.as_secs() / slot_duration
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the duration until the next slot, based on current duration since
|
||||
@@ -112,11 +127,7 @@ impl<SC: SlotCompatible> Stream for Slots<SC> {
|
||||
self.inner_delay = match self.inner_delay.take() {
|
||||
None => {
|
||||
// schedule wait.
|
||||
let wait_until = match duration_now() {
|
||||
None => return Ok(Async::Ready(None)),
|
||||
Some(now) => Instant::now() + time_until_next(now, slot_duration),
|
||||
};
|
||||
|
||||
let wait_until = Instant::now() + time_until_next(duration_now(), slot_duration);
|
||||
Some(Delay::new(wait_until))
|
||||
}
|
||||
Some(d) => Some(d),
|
||||
|
||||
Reference in New Issue
Block a user