Introduce new concept of "slot portion for proposing" (#8280)

* Introduce new concept of "slot portion for proposing"

Currently when building a block we actually give the proposer all of the
time in the slot, while this is wrong. The slot is actually split in at
least two phases proposing and propagation or in the polkadot case into
three phases validating pov's, proposing and propagation. As we don't
want to bring that much polkadot concepts into Substrate, we only
support splitting the slot into proposing and propagation. The portion
can now be passed as parameter to AuRa and BABE to configure this value.
However, this slot portion for propagation doesn't mean that the
proposer can not go over this limit. When we miss slots we still apply
the lenience factor to increase the proposing time, so that we have
enough time to build a heavy block.

Besides all what was said above, this is especially required for
parachains. Parachains have a much more constraint proposing window.
Currently the slot duration is at minimum 12 seconds, but we only have
around 500ms for proposing. So, this slot portion for proposing is
really required to make it working without hacks.

* Offgit feedback

* Cast cast cast
This commit is contained in:
Bastian Köcher
2021-03-09 20:14:54 +01:00
committed by GitHub
parent 7f17082002
commit 7599e0d6e8
7 changed files with 112 additions and 57 deletions
+23 -6
View File
@@ -74,6 +74,7 @@ pub use sp_consensus_babe::{
},
};
pub use sp_consensus::SyncOracle;
pub use sc_consensus_slots::SlotProportion;
use std::{
collections::HashMap, sync::Arc, u64, pin::Pin, time::{Instant, Duration},
any::Any, borrow::Cow, convert::TryInto,
@@ -394,6 +395,13 @@ pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW, BS> {
/// Checks if the current native implementation can author with a runtime at a given block.
pub can_author_with: CAW,
/// The proportion of the slot dedicated to proposing.
///
/// The block proposing will be limited to this proportion of the slot from the starting of the
/// slot. However, the proposing can still take longer when there is some lenience factor applied,
/// because there were no blocks produced for some slots.
pub block_proposal_slot_portion: SlotProportion,
}
/// Start the babe worker.
@@ -409,6 +417,7 @@ pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error>(BabeParams {
backoff_authoring_blocks,
babe_link,
can_author_with,
block_proposal_slot_portion,
}: BabeParams<B, C, E, I, SO, SC, CAW, BS>) -> Result<
BabeWorker<B>,
sp_consensus::Error,
@@ -443,6 +452,7 @@ pub fn start_babe<B, C, SC, E, I, SO, CAW, BS, Error>(BabeParams {
epoch_changes: babe_link.epoch_changes.clone(),
slot_notification_sinks: slot_notification_sinks.clone(),
config: config.clone(),
block_proposal_slot_portion,
};
register_babe_inherent_data_provider(&inherent_data_providers, config.slot_duration())?;
@@ -597,6 +607,7 @@ struct BabeSlotWorker<B: BlockT, C, E, I, SO, BS> {
epoch_changes: SharedEpochChanges<B, Epoch>,
slot_notification_sinks: SlotNotificationSinks<B>,
config: Config,
block_proposal_slot_portion: SlotProportion,
}
impl<B, C, E, I, Error, SO, BS> sc_consensus_slots::SimpleSlotWorker<B>
@@ -791,16 +802,22 @@ where
&self,
parent_head: &B::Header,
slot_info: &SlotInfo,
) -> Option<std::time::Duration> {
let slot_remaining = self.slot_remaining_duration(slot_info);
) -> std::time::Duration {
let max_proposing = slot_info.duration.mul_f32(self.block_proposal_slot_portion.get());
let slot_remaining = slot_info.ends_at
.checked_duration_since(Instant::now())
.unwrap_or_default();
let slot_remaining = std::cmp::min(slot_remaining, max_proposing);
// If parent is genesis block, we don't require any lenience factor.
if parent_head.number().is_zero() {
return Some(slot_remaining)
return slot_remaining
}
let parent_slot = match find_pre_digest::<B>(parent_head) {
Err(_) => return Some(slot_remaining),
Err(_) => return slot_remaining,
Ok(d) => d.slot(),
};
@@ -814,9 +831,9 @@ where
slot_lenience.as_secs(),
);
Some(slot_remaining + slot_lenience)
slot_remaining + slot_lenience
} else {
Some(slot_remaining)
slot_remaining
}
}
}
@@ -431,6 +431,7 @@ fn run_one_test(
babe_link: data.link.clone(),
keystore,
can_author_with: sp_consensus::AlwaysCanAuthor,
block_proposal_slot_portion: SlotProportion::new(0.5),
}).expect("Starts babe"));
}
futures::executor::block_on(future::select(