mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 01:11:10 +00:00
srml: babe: add expected block time and epoch duration constants (#3241)
* srml: babe: add expected block time constant * srml: babe: expose epoch duration constant * node: bump spec_version * core: don't use moment type in test-runtime * babe: add docs regarding c parameter
This commit is contained in:
@@ -104,7 +104,10 @@ pub struct BabeConfiguration {
|
||||
|
||||
/// 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.
|
||||
/// numerator and the second is the denominator. The fraction 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
|
||||
|
||||
@@ -362,10 +362,12 @@ impl srml_timestamp::Trait for Runtime {
|
||||
|
||||
parameter_types! {
|
||||
pub const EpochDuration: u64 = 6;
|
||||
pub const ExpectedBlockTime: u64 = 10_000;
|
||||
}
|
||||
|
||||
impl srml_babe::Trait for Runtime {
|
||||
type EpochDuration = EpochDuration;
|
||||
type ExpectedBlockTime = ExpectedBlockTime;
|
||||
}
|
||||
|
||||
/// Adds one to the given input and returns the final result.
|
||||
|
||||
@@ -34,7 +34,8 @@ pub mod time {
|
||||
/// by `SLOT_DURATION`, but some slots will not be allocated to any
|
||||
/// authority and hence no block will be produced. We expect to have this
|
||||
/// block time on average following the defined slot duration and the value
|
||||
/// of `c` configured for BABE.
|
||||
/// of `c` configured for BABE (where `1 - c` represents the probability of
|
||||
/// a slot being empty).
|
||||
/// This value is only used indirectly to define the unit constants below
|
||||
/// that are expressed in blocks. The rest of the code should use
|
||||
/// `SLOT_DURATION` instead (like the timestamp module for calculating the
|
||||
|
||||
@@ -79,7 +79,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
// and set impl_version to equal spec_version. If only runtime
|
||||
// implementation changes and behavior does not, then leave spec_version as
|
||||
// is and increment impl_version.
|
||||
spec_version: 122,
|
||||
spec_version: 123,
|
||||
impl_version: 123,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
};
|
||||
@@ -128,10 +128,12 @@ impl system::Trait for Runtime {
|
||||
|
||||
parameter_types! {
|
||||
pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS;
|
||||
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
|
||||
}
|
||||
|
||||
impl babe::Trait for Runtime {
|
||||
type EpochDuration = EpochDuration;
|
||||
type ExpectedBlockTime = ExpectedBlockTime;
|
||||
}
|
||||
|
||||
impl indices::Trait for Runtime {
|
||||
@@ -517,9 +519,10 @@ impl_runtime_apis! {
|
||||
|
||||
impl babe_primitives::BabeApi<Block> for Runtime {
|
||||
fn startup_data() -> babe_primitives::BabeConfiguration {
|
||||
// The choice of `c` parameter is done in accordance to
|
||||
// the slot duration and expected target block time, for
|
||||
// safely resisting network delays of maximum two seconds.
|
||||
// The choice of `c` parameter (where `1 - c` represents the
|
||||
// probability of a slot being empty), is done in accordance to the
|
||||
// slot duration and expected target block time, for safely
|
||||
// resisting network delays of maximum two seconds.
|
||||
// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
|
||||
babe_primitives::BabeConfiguration {
|
||||
median_required_blocks: 1000,
|
||||
|
||||
@@ -109,6 +109,7 @@ impl ProvideInherentData for InherentDataProvider {
|
||||
|
||||
pub trait Trait: timestamp::Trait {
|
||||
type EpochDuration: Get<u64>;
|
||||
type ExpectedBlockTime: Get<Self::Moment>;
|
||||
}
|
||||
|
||||
/// The length of the BABE randomness
|
||||
@@ -156,6 +157,17 @@ decl_storage! {
|
||||
decl_module! {
|
||||
/// The BABE SRML module
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
/// The number of **slots** that an epoch takes. We couple sessions to
|
||||
/// epochs, i.e. we start a new session once the new epoch begins.
|
||||
const EpochDuration: u64 = T::EpochDuration::get();
|
||||
|
||||
/// The expected average block time at which BABE should be creating
|
||||
/// blocks. Since BABE is probabilistic it is not trivial to figure out
|
||||
/// what the expected average block time should be based on the slot
|
||||
/// duration and the security parameter `c` (where `1 - c` represents
|
||||
/// the probability of a slot being empty).
|
||||
const ExpectedBlockTime: T::Moment = T::ExpectedBlockTime::get();
|
||||
|
||||
/// Initialization
|
||||
fn on_initialize() {
|
||||
for digest in Self::get_inherent_digests()
|
||||
|
||||
Reference in New Issue
Block a user