slots: fix slot lenience methods (#5742)

* slots: extract slot lenience from babe and aura

* slots: add tests for slot lenience

* slots: fix comment in test
This commit is contained in:
André Silva
2020-04-23 08:42:53 +01:00
committed by GitHub
parent 733a4250aa
commit 96b7cec1ce
3 changed files with 145 additions and 37 deletions
+15 -14
View File
@@ -299,27 +299,28 @@ impl<B, C, E, I, P, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for AuraW
fn proposing_remaining_duration(
&self,
head: &B::Header,
slot_info: &SlotInfo
slot_info: &SlotInfo,
) -> Option<std::time::Duration> {
// never give more than 20 times more lenience.
const BACKOFF_CAP: u64 = 20;
let slot_remaining = self.slot_remaining_duration(slot_info);
let parent_slot = match find_pre_digest::<B, P>(head) {
Err(_) => return Some(slot_remaining),
Ok(d) => d,
};
// we allow a lenience of the number of slots since the head of the
// chain was produced, minus 1 (since there is always a difference of at least 1)
//
// linear back-off.
// in normal cases we only attempt to issue blocks up to the end of the slot.
// when the chain has been stalled for a few slots, we give more lenience.
let slot_lenience = slot_info.number.saturating_sub(parent_slot + 1);
let slot_lenience = std::cmp::min(slot_lenience, BACKOFF_CAP);
let slot_lenience = Duration::from_secs(slot_lenience * slot_info.duration);
Some(slot_lenience + slot_remaining)
if let Some(slot_lenience) =
sc_consensus_slots::slot_lenience_exponential(parent_slot, slot_info)
{
debug!(target: "aura",
"No block for {} slots. Applying linear lenience of {}s",
slot_info.number.saturating_sub(parent_slot + 1),
slot_lenience.as_secs(),
);
Some(slot_remaining + slot_lenience)
} else {
Some(slot_remaining)
}
}
}