diff --git a/substrate/node/cli/src/service.rs b/substrate/node/cli/src/service.rs index 57d986cb05..aeeb8e2061 100644 --- a/substrate/node/cli/src/service.rs +++ b/substrate/node/cli/src/service.rs @@ -371,7 +371,7 @@ mod tests { &parent_id, slot_num, &alice, - (3, 10), + (278, 1000), ) { break babe_pre_digest; } diff --git a/substrate/node/runtime/src/constants.rs b/substrate/node/runtime/src/constants.rs index c32709adf6..5c149b6d14 100644 --- a/substrate/node/runtime/src/constants.rs +++ b/substrate/node/runtime/src/constants.rs @@ -29,9 +29,28 @@ pub mod currency { pub mod time { use node_primitives::Moment; + /// Since BABE is probabilistic this is the average expected block time that + /// we are targetting. Blocks will be produced at a minimum duration defined + /// 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. + /// 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 + /// minimum period). + /// pub const MILLISECS_PER_BLOCK: Moment = 6000; pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000; - pub const SLOT_DURATION: Moment = 6000; + + pub const SLOT_DURATION: Moment = 1650; + + pub const EPOCH_DURATION_IN_BLOCKS: Moment = 10 * MINUTES; + pub const EPOCH_DURATION_IN_SLOTS: Moment = { + const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64; + + (EPOCH_DURATION_IN_BLOCKS as f64 * SLOT_FILL_RATE) as Moment + }; // These time units are defined in number of blocks. pub const MINUTES: Moment = 60 / SECS_PER_BLOCK; diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index 02d2a91851..700f3b20c7 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -127,7 +127,7 @@ impl system::Trait for Runtime { } parameter_types! { - pub const EpochDuration: u64 = 10 * MINUTES; + pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS; } impl babe::Trait for Runtime { @@ -517,10 +517,14 @@ impl_runtime_apis! { impl babe_primitives::BabeApi 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. + // babe_primitives::BabeConfiguration { median_required_blocks: 1000, slot_duration: Babe::slot_duration(), - c: (3, 10), + c: (278, 1000), } }