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 all commits
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
26 changes: 25 additions & 1 deletion frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ decl_storage! {
/// Temporary value (cleared at block finalization) which is `Some`
/// if per-block initialization has already been called for current block.
Initialized get(fn initialized): Option<MaybeVrf>;

/// How late the current block is compared to its parent.
///
/// This entry is populated as part of block execution and is cleaned up
/// on block finalization. Querying this storage entry outside of block
/// execution context should always yield zero.
Lateness get(fn lateness): T::BlockNumber;
}
add_extra_genesis {
config(authorities): Vec<(AuthorityId, BabeAuthorityWeight)>;
Expand Down Expand Up @@ -190,6 +197,9 @@ decl_module! {
if let Some(Some(vrf_output)) = Initialized::take() {
Self::deposit_vrf_output(&vrf_output);
}

// remove temporary "environment" entry from storage
Lateness::<T>::kill();
}
}
}
Expand Down Expand Up @@ -443,7 +453,15 @@ impl<T: Trait> Module<T> {
Self::deposit_consensus(ConsensusLog::NextEpochData(next))
}

CurrentSlot::put(digest.slot_number());
// the slot number of the current block being initialized
let current_slot = digest.slot_number();

// how many slots were skipped between current and last block
let lateness = current_slot.saturating_sub(CurrentSlot::get() + 1);
let lateness = T::BlockNumber::from(lateness as u32);

Lateness::<T>::put(lateness);
Copy link
Member

Choose a reason for hiding this comment

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

We're trying to cut down on extra DB I/O as it's pretty slow. Could we get this merged into CurrentSlot?

Copy link
Contributor Author

@andresilva andresilva Apr 3, 2020

Choose a reason for hiding this comment

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

This is always killed in on_finalize and should never reach the DB. Added some comments on this.

CurrentSlot::put(current_slot);

if let RawPreDigest::Primary(primary) = digest {
// place the VRF output into the `Initialized` storage item
Expand Down Expand Up @@ -498,6 +516,12 @@ impl<T: Trait> frame_support::traits::EstimateNextSessionRotation<T::BlockNumber
}
}

impl<T: Trait> frame_support::traits::Lateness<T::BlockNumber> for Module<T> {
fn lateness(&self) -> T::BlockNumber {
Self::lateness()
}
}

impl<T: Trait> sp_runtime::BoundToRuntimeAppPublic for Module<T> {
type Public = AuthorityId;
}
Expand Down
17 changes: 16 additions & 1 deletion frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sp_core::u32_trait::Value as U32;
use sp_runtime::{
RuntimeDebug,
ConsensusEngineId, DispatchResult, DispatchError,
traits::{MaybeSerializeDeserialize, AtLeast32Bit, Saturating, TrailingZeroInput, Bounded},
traits::{MaybeSerializeDeserialize, AtLeast32Bit, Saturating, TrailingZeroInput, Bounded, Zero},
};
use crate::dispatch::Parameter;
use crate::storage::StorageMap;
Expand Down Expand Up @@ -1032,6 +1032,21 @@ impl<Output: Decode + Default> Randomness<Output> for () {
}
}

/// Trait to be used by block producing consensus engine modules to determine
/// how late the current block is (e.g. in a slot-based proposal mechanism how
/// many slots were skipped since the previous block).
pub trait Lateness<N> {
/// Returns a generic measure of how late the current block is compared to
/// its parent.
fn lateness(&self) -> N;
}

impl<N: Zero> Lateness<N> for () {
fn lateness(&self) -> N {
Zero::zero()
}
}

/// Implementors of this trait provide information about whether or not some validator has
/// been registered with them. The [Session module](../../pallet_session/index.html) is an implementor.
pub trait ValidatorRegistration<ValidatorId> {
Expand Down