This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix the BABE epoch logic in SRML #3611
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b04cfee
rejig babe APIs
rphmeier be5ce8c
introduce next-epoch-descriptor type
rphmeier 32c2147
overhaul srml-BABE epoch logic
rphmeier 0bd813a
ensure VRF outputs end up in the right epoch-randomness
rphmeier 8ec1f48
rewrite `do_initialize` to remove unnecessary loop
rphmeier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ use substrate_client::decl_runtime_apis; | |
|
||
#[cfg(feature = "std")] | ||
pub use digest::{BabePreDigest, CompatibleDigestItem}; | ||
pub use digest::{BABE_VRF_PREFIX, RawBabePreDigest}; | ||
pub use digest::{BABE_VRF_PREFIX, RawBabePreDigest, NextEpochDescriptor}; | ||
|
||
mod app { | ||
use app_crypto::{app_crypto, key_types::BABE, sr25519}; | ||
|
@@ -59,6 +59,11 @@ pub const VRF_PROOF_LENGTH: usize = 64; | |
/// The length of the public key | ||
pub const PUBLIC_KEY_LENGTH: usize = 32; | ||
|
||
/// How many blocks to wait before running the median algorithm for relative time | ||
/// This will not vary from chain to chain as it is not dependent on slot duration | ||
/// or epoch length. | ||
pub const MEDIAN_ALGORITHM_CARDINALITY: usize = 1200; // arbitrary suggestion by w3f-research. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'll become some number of slots maybe, yes? |
||
|
||
/// The index of an authority. | ||
pub type AuthorityIndex = u32; | ||
|
||
|
@@ -87,57 +92,55 @@ pub struct Epoch { | |
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>, | ||
/// Randomness for this epoch | ||
pub randomness: [u8; VRF_OUTPUT_LENGTH], | ||
/// Whether secondary slot assignments should be used during the epoch. | ||
pub secondary_slots: bool, | ||
} | ||
|
||
/// An consensus log item for BABE. | ||
#[derive(Decode, Encode, Clone, PartialEq, Eq)] | ||
pub enum ConsensusLog { | ||
/// The epoch has changed. This provides information about the | ||
/// epoch _after_ next: what slot number it will start at, who are the authorities (and their weights) | ||
/// and the next epoch randomness. The information for the _next_ epoch should already | ||
/// be available. | ||
/// The epoch has changed. This provides information about the _next_ | ||
/// epoch - information about the _current_ epoch (i.e. the one we've just | ||
/// entered) should already be available earlier in the chain. | ||
#[codec(index = "1")] | ||
NextEpochData(Epoch), | ||
NextEpochData(NextEpochDescriptor), | ||
/// Disable the authority with given index. | ||
#[codec(index = "2")] | ||
OnDisabled(AuthorityIndex), | ||
} | ||
|
||
/// Configuration data used by the BABE consensus engine. | ||
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug, Encode, Decode)] | ||
#[derive(Clone, PartialEq, Eq, Encode, Decode)] | ||
#[cfg_attr(any(feature = "std", test), derive(Debug))] | ||
pub struct BabeConfiguration { | ||
/// The slot duration in milliseconds for BABE. Currently, only | ||
/// the value provided by this type at genesis will be used. | ||
/// | ||
/// Dynamic slot duration may be supported in the future. | ||
pub slot_duration: u64, | ||
|
||
/// The duration of epochs in slots. | ||
pub epoch_length: SlotNumber, | ||
|
||
/// A constant value that is used in the threshold calculation formula. | ||
/// Expressed as a fraction where the first member of the tuple is the | ||
/// numerator and the second is the denominator. The fraction should | ||
/// Expressed as a rational where the first member of the tuple is the | ||
/// numerator and the second is the denominator. The rational should | ||
/// represent a value between 0 and 1. | ||
/// In the threshold formula calculation, `1 - c` represents the probability | ||
/// of a slot being empty. | ||
pub c: (u64, u64), | ||
|
||
/// The minimum number of blocks that must be received before running the | ||
/// median algorithm to compute the offset between the on-chain time and the | ||
/// local time. Currently, only the value provided by this type at genesis | ||
/// will be used, but this is subject to change. | ||
/// | ||
/// Blocks less than `self.median_required_blocks` must be generated by an | ||
/// *initial validator* ― that is, a node that was a validator at genesis. | ||
pub median_required_blocks: u64, | ||
/// The authorities for the genesis epoch. | ||
pub genesis_authorities: Vec<(AuthorityId, BabeAuthorityWeight)>, | ||
|
||
/// The randomness for this epoch. | ||
pub randomness: [u8; VRF_OUTPUT_LENGTH], | ||
|
||
/// Whether this chain should run with secondary slots, which are assigned | ||
/// in round-robin manner. | ||
pub secondary_slots: bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option might be |
||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl slots::SlotData for BabeConfiguration { | ||
/// Return the slot duration in milliseconds for BABE. Currently, only | ||
/// the value provided by this type at genesis will be used. | ||
/// | ||
/// Dynamic slot duration may be supported in the future. | ||
fn slot_duration(&self) -> u64 { | ||
self.slot_duration | ||
} | ||
|
@@ -152,9 +155,6 @@ decl_runtime_apis! { | |
/// only the value provided by this type at genesis will be used. | ||
/// | ||
/// Dynamic configuration may be supported in the future. | ||
fn startup_data() -> BabeConfiguration; | ||
|
||
/// Get the current epoch data for Babe. | ||
fn epoch() -> Epoch; | ||
fn configuration() -> BabeConfiguration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where should this go then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'll be maintained off-chain; it's a bit redundant to put it in the block header.