Skip to content

Commit 84af538

Browse files
bkchrdarkfriend77
authored andcommitted
Skip slot lenience on first block in BABE (paritytech#7515)
The genesis header doesn't have the BABE pre-digest and we insert `0` as slot number. The slot lenience calculation will return the maximum in this situation. Besides returning the maximum which is not bad at all, it also prints some a debug message that can be confusing in the first moment. To prevent printing this debug message, we now just return early when we see that the parent block is the genesis block.
1 parent c7d40f1 commit 84af538

File tree

1 file changed

+10
-5
lines changed
  • client/consensus/babe/src

1 file changed

+10
-5
lines changed

client/consensus/babe/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,20 +669,26 @@ impl<B, C, E, I, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for BabeSlot
669669

670670
fn proposing_remaining_duration(
671671
&self,
672-
head: &B::Header,
672+
parent_head: &B::Header,
673673
slot_info: &SlotInfo,
674674
) -> Option<std::time::Duration> {
675675
let slot_remaining = self.slot_remaining_duration(slot_info);
676676

677-
let parent_slot = match find_pre_digest::<B>(head) {
677+
// If parent is genesis block, we don't require any lenience factor.
678+
if parent_head.number().is_zero() {
679+
return Some(slot_remaining)
680+
}
681+
682+
let parent_slot = match find_pre_digest::<B>(parent_head) {
678683
Err(_) => return Some(slot_remaining),
679684
Ok(d) => d.slot_number(),
680685
};
681686

682687
if let Some(slot_lenience) =
683688
sc_consensus_slots::slot_lenience_exponential(parent_slot, slot_info)
684689
{
685-
debug!(target: "babe",
690+
debug!(
691+
target: "babe",
686692
"No block for {} slots. Applying exponential lenience of {}s",
687693
slot_info.number.saturating_sub(parent_slot + 1),
688694
slot_lenience.as_secs(),
@@ -697,8 +703,7 @@ impl<B, C, E, I, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for BabeSlot
697703

698704
/// Extract the BABE pre digest from the given header. Pre-runtime digests are
699705
/// mandatory, the function will return `Err` if none is found.
700-
pub fn find_pre_digest<B: BlockT>(header: &B::Header) -> Result<PreDigest, Error<B>>
701-
{
706+
pub fn find_pre_digest<B: BlockT>(header: &B::Header) -> Result<PreDigest, Error<B>> {
702707
// genesis block doesn't contain a pre digest so let's generate a
703708
// dummy one to not break any invariants in the rest of the code
704709
if header.number().is_zero() {

0 commit comments

Comments
 (0)