Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
840dde0
Initial work on relative slots for BABE
Demi-Marie Jun 6, 2019
525db62
Merge remote-tracking branch 'origin/master' into demi-relative-slots
Demi-Marie Jun 6, 2019
26c9216
Merge remote-tracking branch 'origin/master' into demi-relative-slots
Demi-Marie Jun 7, 2019
45cc84a
More work
Demi-Marie Jun 7, 2019
b9639c5
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 7, 2019
c0ba761
Update core/consensus/babe/src/lib.rs
Demi-Marie Jun 7, 2019
9deafa0
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 9, 2019
d2143eb
More work on relative slots
Demi-Marie Jun 10, 2019
5ac939b
Add missing field in test-runtime
Demi-Marie Jun 10, 2019
d67ec03
Bump `impl_version` and `authoring_version`
Demi-Marie Jun 10, 2019
527c460
Fix compile errors and warnings
Demi-Marie Jun 10, 2019
6dcb87a
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 11, 2019
948bd80
Upgrade dependencies
Demi-Marie Jun 11, 2019
dedf739
Update dependencies more
Demi-Marie Jun 11, 2019
286c1c1
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 11, 2019
5fb2586
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 11, 2019
ea97fe2
Revert some updates to dependencies
Demi-Marie Jun 11, 2019
65660fe
Merge remote-tracking branch 'origin/master' into demi-relative-slots
Demi-Marie Jun 12, 2019
3c8f864
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 12, 2019
93227b1
Fix compilation errors
Demi-Marie Jun 12, 2019
bd3b1e2
`Duration` → `u128` in calculations
Demi-Marie Jun 13, 2019
9f5292a
`slot_duration` is in milleseconds, not seconds
Demi-Marie Jun 13, 2019
07e2974
Median algorithm: ignore blocks with slot_num < sl
Demi-Marie Jun 13, 2019
0e9d63b
Fix silly compile error
Demi-Marie Jun 13, 2019
e62baec
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 13, 2019
3766cd6
Store a duration, rather than an instant
Demi-Marie Jun 14, 2019
581ad8e
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 14, 2019
f4cf8a7
Fix compilation errors
Demi-Marie Jun 14, 2019
b9ccfbf
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 17, 2019
051b9f6
`INVERSE_NANO` → `NANOS_PER_SEC`
Demi-Marie Jun 17, 2019
e65e46a
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 19, 2019
18fe2d0
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 19, 2019
bf4ac48
Un-bump `authoring_version`
Demi-Marie Jun 19, 2019
7a4c8ec
Disable median algorithm when `median_required_blocks` is 0
Demi-Marie Jun 19, 2019
94680c7
Apply suggestions from code review
Demi-Marie Jun 19, 2019
71f0c30
Simplify panic
Demi-Marie Jun 19, 2019
d761493
Merge branch 'demi-relative-slots' of github.com:paritytech/substrate…
Demi-Marie Jun 19, 2019
c4a6315
Fix build error
Demi-Marie Jun 19, 2019
426dd04
Create `SignedDuration` struct
Demi-Marie Jun 20, 2019
c73aa1a
Merge branch 'master' into demi-relative-slots
Demi-Marie Jun 20, 2019
8f71428
Refactor median algorithm into separate function
Demi-Marie Jun 21, 2019
69eb154
Add issues for FIXMEs and respond to code review
Demi-Marie Jun 22, 2019
d43d04e
Fix minor warnings
Demi-Marie Jun 22, 2019
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
Prev Previous commit
Next Next commit
Create SignedDuration struct
for signed `Duration` values.

Suggested-by: Bastian Köcher
  • Loading branch information
Demi-Marie committed Jun 20, 2019
commit 426dd04ddd4ea616c12379b51383529ab199a66d
5 changes: 3 additions & 2 deletions core/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ use srml_aura::{
};
use substrate_telemetry::{telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO};

use slots::{CheckedHeader, SlotData, SlotWorker, SlotInfo, SlotCompatible, slot_now, check_equivocation};
use slots::{CheckedHeader, SlotData, SlotWorker, SlotInfo, SlotCompatible};
use slots::{SignedDuration, check_equivocation};

pub use aura_primitives::*;
pub use consensus_common::SyncOracle;
Expand Down Expand Up @@ -283,7 +284,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
Box::new(proposal_work.map(move |b| {
// minor hack since we don't have access to the timestamp
// that is actually set by the proposer.
let slot_after_building = slot_now(slot_duration, Default::default(), true);
let slot_after_building = SignedDuration::default().slot_now(slot_duration);
if slot_after_building != slot_num {
info!(
"Discarding proposal for slot {}; block production took too long",
Expand Down
8 changes: 4 additions & 4 deletions core/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use futures::{Future, IntoFuture, future};
use tokio_timer::Timeout;
use log::{error, warn, debug, info, trace};

use slots::{SlotWorker, SlotData, SlotInfo, SlotCompatible, slot_now};
use slots::{SlotWorker, SlotData, SlotInfo, SlotCompatible, SignedDuration};

pub use babe_primitives::AuthorityId;

Expand Down Expand Up @@ -337,7 +337,7 @@ impl<Hash, H, B, C, E, I, Error, SO> SlotWorker<B> for BabeWorker<C, E, I, SO> w
Box::new(proposal_work.map(move |b| {
// minor hack since we don't have access to the timestamp
// that is actually set by the proposer.
let slot_after_building = slot_now(slot_duration, Default::default(), true);
let slot_after_building = SignedDuration::default().slot_now(slot_duration);
if slot_after_building != slot_num {
info!(
target: "babe",
Expand Down Expand Up @@ -639,7 +639,7 @@ impl<B: Block, C> Verifier<B> for BabeVerifier<C> where
let mut timestamps = self.timestamps.lock();
// Remainder of this block is a critical section.
let num_timestamps = timestamps.1.len();
let { median_required_blocks, .. } = self.config.0;
let median_required_blocks = self.config.0.median_required_blocks;
if num_timestamps as u64 >= median_required_blocks && median_required_blocks > 0 {
let mut new_list: Vec<_> = timestamps.1.iter().map(|&(t, sl)| {
let offset: u128 = u128::from(self.config.get())
Expand All @@ -655,7 +655,7 @@ impl<B: Block, C> Verifier<B> for BabeVerifier<C> where
let &median = new_list
.get(num_timestamps / 2)
.expect("we have at least one timestamp, so this is a valid index; qed");
timestamps.1.clear()
timestamps.1.clear();
// Compute the (relative!) start time of the blockchain ―
// that is, the issue time of the genesis block that would
// give the values we observed.
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
29 changes: 22 additions & 7 deletions core/consensus/slots/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,28 @@ pub fn duration_now() -> Duration {
))
}

/// Get the slot for now.
pub fn slot_now(slot_duration: u64, offset: Duration, is_positive: bool) -> u64 {
if is_positive {
duration_now() + offset
} else {
duration_now() - offset
}.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
Expand Down