Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

slots: incrementally backoff claiming slots if finality lags behind #7186

Merged
45 commits merged into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
250fbeb
babe: backoff authoring blocks when finality lags
octol Sep 16, 2020
d531efa
babe: move backoff authoring params to default constructor
octol Sep 22, 2020
b060e9b
babe: deduplicate the test a bit
octol Sep 22, 2020
0f95c44
babe: set backoff constants in service
octol Sep 23, 2020
df1951c
babe: use better names for backoff authoring block parameters
octol Sep 23, 2020
c17ca03
babe: remove last unwrap
octol Sep 23, 2020
2892da0
babe: slight style tweak
octol Sep 23, 2020
01f1ac3
babe: fix comment
octol Sep 23, 2020
9794ffa
slots: move backoff block authorship logic to SimpleSlotWorker
octol Sep 28, 2020
d2ee780
aura: append SlotInfo in on_slot
octol Sep 30, 2020
026a8d0
slots: use the correct types for parameters
octol Sep 30, 2020
f428a35
Merge branch 'master' into jon/incremental-backoff-on-finality
octol Oct 14, 2020
6903837
slots: fix review comments
octol Oct 23, 2020
4a6b8b7
Merge branch 'master' into jon/incremental-backoff-on-finality
octol Oct 23, 2020
92b506a
aura: add missing backoff authoring blocks parameters
octol Oct 23, 2020
cda5150
slots: add comments for default values
octol Oct 27, 2020
62f97dc
slots: add additional checks in test
octol Oct 27, 2020
7fa2084
Merge remote-tracking branch 'upstream/master' into jon/incremental-b…
octol Oct 28, 2020
af8d30b
slots: update implementation for new master
octol Oct 28, 2020
76b745a
slots: revert the change to SlotInfo
octol Oct 28, 2020
dafd2e5
Merge remote-tracking branch 'upstream/master' into jon/incremental-b…
octol Oct 28, 2020
dc04a1f
Merge remote-tracking branch 'upstream/master' into jon/incremental-b…
octol Oct 29, 2020
cb6d24c
Fix review comments
octol Oct 30, 2020
55827c3
slots: rework unit tests for backing off claiming slots
octol Nov 2, 2020
83538f0
slots: add test for asymptotic behaviour for slot claims
octol Nov 3, 2020
c87145d
Merge remote-tracking branch 'upstream/master' into jon/incremental-b…
octol Nov 3, 2020
0435142
slots: address review comments
octol Nov 4, 2020
38e61e5
slots: add test for max_interval
octol Nov 4, 2020
1228aef
slots: add assertion for intervals between between claimed slots
octol Nov 4, 2020
f7b8c76
slots: remove rustfmt directive
octol Nov 4, 2020
9790f60
slots: another attempt at explaining authoring_rate
octol Nov 4, 2020
f95225a
Merge remote-tracking branch 'upstream/master' into jon/incremental-b…
octol Nov 4, 2020
461d5f9
slots: up unfinalized_slack to 50 by default
octol Nov 6, 2020
796baf7
slots: add tests for time to reach max_interval
octol Nov 6, 2020
0d11df5
slots: fix typo in comments
octol Nov 6, 2020
9a968c6
Apply suggestions from code review
octol Nov 10, 2020
a607bc4
slots: additional tweaks to comments and info calls
octol Nov 10, 2020
bc32a7c
slots: rename to BackoffAuthoringOnFinalizedHeadLagging
octol Nov 10, 2020
eb45ed7
slots: make the backing off strategy generic
octol Nov 10, 2020
5a40da8
Merge remote-tracking branch 'upstream/master' into jon/incremental-b…
octol Nov 10, 2020
4f44b62
Apply suggestions from code review
octol Nov 10, 2020
1423649
slots: implement backoff trait for () for simplicity
octol Nov 10, 2020
44de2cf
slots: move logging inside backing off function to make it more specific
octol Nov 11, 2020
90d0ad3
Merge remote-tracking branch 'upstream/master' into jon/incremental-b…
octol Nov 11, 2020
b6435ff
aura: add missing function parameter
octol Nov 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ pub fn new_full_base(
let can_author_with =
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());

let backoff_authoring_blocks = Some(sc_consensus_babe::BackoffAuthoringBlocksParam {
max_interval: 100,
unfinalized_slack: 5,
authoring_bias: 2,
});

octol marked this conversation as resolved.
Show resolved Hide resolved
let babe_config = sc_consensus_babe::BabeParams {
keystore: keystore.clone(),
client: client.clone(),
Expand All @@ -247,6 +253,7 @@ pub fn new_full_base(
sync_oracle: network.clone(),
inherent_data_providers: inherent_data_providers.clone(),
force_authoring,
backoff_authoring_blocks,
babe_link,
can_author_with,
};
Expand Down
151 changes: 150 additions & 1 deletion client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ use sp_core::{crypto::Public, traits::BareCryptoStore};
use sp_application_crypto::AppKey;
use sp_runtime::{
generic::{BlockId, OpaqueDigestItemId}, Justification,
traits::{Block as BlockT, Header, DigestItemFor, Zero},
traits::{
Block as BlockT, Header, DigestItemFor, Zero,
UniqueSaturatedInto, Saturating,
},
};
use sp_api::{ProvideRuntimeApi, NumberFor};
use sc_keystore::KeyStorePtr;
Expand Down Expand Up @@ -114,6 +117,7 @@ use log::{debug, info, log, trace, warn};
use prometheus_endpoint::Registry;
use sc_consensus_slots::{
SlotWorker, SlotInfo, SlotCompatible, StorageChanges, CheckedHeader, check_equivocation,
SignedDuration,
};
use sc_consensus_epochs::{
descendent_query, SharedEpochChanges, EpochChangesFor, Epoch as EpochT, ViableEpochDescriptor,
Expand Down Expand Up @@ -353,13 +357,33 @@ pub struct BabeParams<B: BlockT, C, E, I, SO, SC, CAW> {
/// Force authoring of blocks even if we are offline
pub force_authoring: bool,

/// Parameters that control BABE's functionality for backing off block production if finality
/// starts to lag behind.
pub backoff_authoring_blocks: Option<BackoffAuthoringBlocksParam>,

/// The source of timestamps for relative slots
pub babe_link: BabeLink<B>,

/// Checks if the current native implementation can author with a runtime at a given block.
pub can_author_with: CAW,
}

/// Parameters used by BABE to decide how backoff authoring blocks if the number of unfinalized
/// blocks grows too large.
#[derive(Clone)]
pub struct BackoffAuthoringBlocksParam {
/// The max interval to backoff when authoring blocks, regardless of delay in finality.
pub max_interval: u32,
/// The number of unfinalized blocks allowed before the BABE starts to consider to backoff
/// authoring blocks. Note that due to the `authoring_bias` BABE might still wait longer until it
/// decides to decline to author a block.
pub unfinalized_slack: u32,
/// How aggressively BABE should start to decline authoring locks. A small value for
/// `authoring_bias` means BABE will quickly start to backoff block authorship as the length of
/// the unfinalized blocks grows.
pub authoring_bias: u32,
}

/// Start the babe worker.
pub fn start_babe<B, C, SC, E, I, SO, CAW, Error>(BabeParams {
keystore,
Expand All @@ -370,6 +394,7 @@ pub fn start_babe<B, C, SC, E, I, SO, CAW, Error>(BabeParams {
sync_oracle,
inherent_data_providers,
force_authoring,
backoff_authoring_blocks,
babe_link,
can_author_with,
}: BabeParams<B, C, E, I, SO, SC, CAW>) -> Result<
Expand Down Expand Up @@ -398,6 +423,7 @@ pub fn start_babe<B, C, SC, E, I, SO, CAW, Error>(BabeParams {
env,
sync_oracle: sync_oracle.clone(),
force_authoring,
backoff_authoring_blocks,
keystore,
epoch_changes: babe_link.epoch_changes.clone(),
slot_notification_sinks: slot_notification_sinks.clone(),
Expand Down Expand Up @@ -468,6 +494,7 @@ struct BabeSlotWorker<B: BlockT, C, E, I, SO> {
env: E,
sync_oracle: SO,
force_authoring: bool,
backoff_authoring_blocks: Option<BackoffAuthoringBlocksParam>,
keystore: KeyStorePtr,
epoch_changes: SharedEpochChanges<B, Epoch>,
slot_notification_sinks: SlotNotificationSinks<B>,
Expand Down Expand Up @@ -683,10 +710,59 @@ impl<B, C, E, I, Error, SO> SlotWorker<B> for BabeSlotWorker<B, C, E, I, SO> whe
type OnSlot = Pin<Box<dyn Future<Output = Result<(), sp_consensus::Error>> + Send>>;

fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot {
if let Some(ref backoff_param) = self.backoff_authoring_blocks {
if let Ok(chain_head_slot) = find_pre_digest::<B>(&chain_head)
.map(|digest| digest.slot_number())
{
if should_backoff_authoring_blocks::<B>(
*chain_head.number(),
chain_head_slot,
self.client.info().finalized_number,
SignedDuration::default().slot_now(slot_info.duration),
backoff_param,
) {
return Box::pin(future::ready(Ok(())))
}
}
}

<Self as sc_consensus_slots::SimpleSlotWorker<B>>::on_slot(self, chain_head, slot_info)
}
}

// The criterion for backing off block authoring when finality is lagging
fn should_backoff_authoring_blocks<B>(
chain_head_number: NumberFor<B>,
chain_head_slot: u64,
finalized_number: NumberFor<B>,
slot_now: u64,
param: &BackoffAuthoringBlocksParam,
) -> bool
where
B: BlockT,
{
let BackoffAuthoringBlocksParam {
max_interval,
unfinalized_slack,
authoring_bias
} = param.clone();

let unfinalized_block_length = chain_head_number - finalized_number;
let interval = unfinalized_block_length.saturating_sub(unfinalized_slack.into())
/ authoring_bias.into();
let interval = interval.min(max_interval.into());

// We're doing arithmetic between block and slot numbers.
let interval = interval.unique_saturated_into();

if u128::from(slot_now) <= u128::from(chain_head_slot) + interval {
info!(target: "babe", "Backing off authoring blocks due to too many unfinalized blocks");
true
} else {
false
}
}

/// Extract the BABE pre digest from the given header. Pre-runtime digests are
/// mandatory, the function will return `Err` if none is found.
fn find_pre_digest<B: BlockT>(header: &B::Header) -> Result<PreDigest, Error<B>>
Expand Down Expand Up @@ -1518,3 +1594,76 @@ pub mod test_helpers {
).map(|(digest, _)| digest)
}
}

#[cfg(test)]
mod test {
use crate::{should_backoff_authoring_blocks, BackoffAuthoringBlocksParam};
use substrate_test_runtime_client::runtime::Block;

struct HeadState {
head_number: u64,
head_slot: u64,
slot_now: u64,
}

impl HeadState {
fn author_block(&mut self) {
self.head_number += 1;
self.head_slot += 1;
self.slot_now += 1;
}

fn dont_author_block(&mut self) {
self.slot_now += 1;
}
}

#[test]
fn should_backoff_authoring_when_finality_lags() {
let finalized_number = 2u32.into();
let param = BackoffAuthoringBlocksParam {
max_interval: 100,
unfinalized_slack: 5,
authoring_bias: 2,
};

let mut head_state = HeadState {
head_number: 3,
head_slot: 10,
slot_now: 11
};

let should_backoff = |head_state: &HeadState| -> bool {
should_backoff_authoring_blocks::<Block>(
head_state.head_number,
head_state.head_slot,
finalized_number,
head_state.slot_now,
&param,
)
};

while head_state.slot_now < 17 {
assert!(!should_backoff(&head_state));
head_state.author_block();
}

// Once the unfinalized head of the chain grows too long we start backing off block
// production
assert_eq!(head_state.head_number, 9);
assert_eq!(head_state.head_slot, 16);
assert_eq!(head_state.slot_now, 17);
assert!(should_backoff(&head_state));
head_state.dont_author_block();

// But we don't stop entirely
while head_state.slot_now < 20 {
assert!(!should_backoff(&head_state));
head_state.author_block();
}

// Back off again
assert!(should_backoff(&head_state));
}
}

1 change: 1 addition & 0 deletions client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ fn run_one_test(
sync_oracle: DummyOracle,
inherent_data_providers: data.inherent_data_providers.clone(),
force_authoring: false,
backoff_authoring_blocks: None,
babe_link: data.link.clone(),
keystore,
can_author_with: sp_consensus::AlwaysCanAuthor,
Expand Down