Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 15 additions & 8 deletions cumulus/client/consensus/aura/src/collators/lookahead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ use sp_consensus_aura::{AuraApi, Slot};
use sp_core::crypto::Pair;
use sp_inherents::CreateInherentDataProviders;
use sp_keystore::KeystorePtr;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member};
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT, Member},
Saturating,
};
use sp_timestamp::Timestamp;
use std::{path::PathBuf, sync::Arc, time::Duration};

Expand Down Expand Up @@ -314,18 +317,19 @@ where
},
};

let (included_block, initial_parent) = match crate::collators::find_parent(
let parent_search_result = match crate::collators::find_parent(
relay_parent,
params.para_id,
&*params.para_backend,
&params.relay_client,
)
.await
{
Some(value) => value,
Some(result) => result,
None => continue,
};

let included_header = &parent_search_result.included_header;
let para_client = &*params.para_client;
let keystore = &params.keystore;
let can_build_upon = |block_hash| {
Expand All @@ -341,16 +345,19 @@ where
relay_slot,
timestamp,
block_hash,
included_block.hash(),
included_header.hash(),
para_client,
&keystore,
))
};

// Build in a loop until not allowed. Note that the authorities can change
// at any block, so we need to re-claim our slot every time.
let mut parent_hash = initial_parent.hash;
let mut parent_header = initial_parent.header;
let mut parent_hash = parent_search_result.best_parent_header.hash();
let mut parent_header = parent_search_result.best_parent_header;
// Distance from included block to best parent.
let initial_parent_depth =
(*parent_header.number()).saturating_sub(*included_header.number());
let overseer_handle = &mut params.overseer_handle;

// Do not try to build upon an unknown, pruned or bad block
Expand All @@ -373,7 +380,7 @@ where

// This needs to change to support elastic scaling, but for continuously
// scheduled chains this ensures that the backlog will grow steadily.
for n_built in 0..2 {
for n_built in 0..2u32 {
let slot_claim = match can_build_upon(parent_hash) {
Some(fut) => match fut.await {
None => break,
Expand All @@ -385,7 +392,7 @@ where
tracing::debug!(
target: crate::LOG_TARGET,
?relay_parent,
unincluded_segment_len = initial_parent.depth + n_built,
unincluded_segment_len = ?initial_parent_depth.saturating_add(n_built.into()),
"Slot claimed. Building"
);

Expand Down
30 changes: 10 additions & 20 deletions cumulus/client/consensus/aura/src/collators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,14 @@ where
.then(|| SlotClaim::unchecked::<P>(author_pub, para_slot, timestamp))
}

/// Use [`cumulus_client_consensus_common::find_potential_parents`] to find parachain blocks that
/// we can build on. Once a list of potential parents is retrieved, return the last one of the
/// longest chain.
/// Use [`cumulus_client_consensus_common::find_parent_for_building`] to find the best parachain
/// block to build on.
async fn find_parent<Block>(
relay_parent: RelayHash,
para_id: ParaId,
para_backend: &impl sc_client_api::Backend<Block>,
relay_client: &impl RelayChainInterface,
) -> Option<(<Block as BlockT>::Header, consensus_common::PotentialParent<Block>)>
) -> Option<consensus_common::ParentSearchResult<Block>>
where
Block: BlockT,
{
Expand All @@ -276,35 +275,26 @@ where
.await
.unwrap_or(DEFAULT_SCHEDULING_LOOKAHEAD)
.saturating_sub(1) as usize,
ignore_alternative_branches: true,
};

let potential_parents = cumulus_client_consensus_common::find_potential_parents::<Block>(
match cumulus_client_consensus_common::find_parent_for_building::<Block>(
parent_search_params,
para_backend,
relay_client,
)
.await;

let potential_parents = match potential_parents {
.await
{
Ok(result) => result,
Err(e) => {
tracing::error!(
target: crate::LOG_TARGET,
?relay_parent,
err = ?e,
"Could not fetch potential parents to build upon"
"Could not find parent to build upon"
);

return None;
None
},
Ok(x) => x,
};

let included_block = potential_parents.iter().find(|x| x.depth == 0)?.header.clone();
potential_parents
.into_iter()
.max_by_key(|a| a.depth)
.map(|parent| (included_block, parent))
}
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ use sp_consensus_aura::AuraApi;
use sp_core::crypto::Pair;
use sp_inherents::CreateInherentDataProviders;
use sp_keystore::KeystorePtr;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member, Zero};
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT, Member, Zero},
Saturating,
};
use std::{collections::VecDeque, sync::Arc, time::Duration};

/// Parameters for [`run_block_builder`].
Expand Down Expand Up @@ -237,15 +240,19 @@ where
let relay_parent = rp_data.relay_parent().hash();
let relay_parent_header = rp_data.relay_parent().clone();

let Some((included_header, parent)) =
let Some(parent_search_result) =
crate::collators::find_parent(relay_parent, para_id, &*para_backend, &relay_client)
.await
else {
continue;
};

let parent_hash = parent.hash;
let parent_header = &parent.header;
let parent_hash = parent_search_result.best_parent_header.hash();
let included_header = parent_search_result.included_header;
let parent_header = &parent_search_result.best_parent_header;
// Distance from included block to best parent (unincluded segment length).
let unincluded_segment_len =
parent_header.number().saturating_sub(*included_header.number());

// Retrieve the core.
let core = match determine_core(
Expand Down Expand Up @@ -329,7 +336,7 @@ where
None => {
tracing::debug!(
target: crate::LOG_TARGET,
unincluded_segment_len = parent.depth,
?unincluded_segment_len,
relay_parent = ?relay_parent,
relay_parent_num = %relay_parent_header.number(),
included_hash = ?included_header_hash,
Expand All @@ -344,7 +351,7 @@ where

tracing::debug!(
target: crate::LOG_TARGET,
unincluded_segment_len = parent.depth,
?unincluded_segment_len,
relay_parent = %relay_parent,
relay_parent_num = %relay_parent_header.number(),
relay_parent_offset,
Expand Down Expand Up @@ -417,7 +424,7 @@ where
let Some(adjusted_authoring_duration) = adjusted_authoring_duration else {
tracing::debug!(
target: crate::LOG_TARGET,
unincluded_segment_len = parent.depth,
?unincluded_segment_len,
relay_parent = ?relay_parent,
relay_parent_num = %relay_parent_header.number(),
included_hash = ?included_header_hash,
Expand Down
4 changes: 2 additions & 2 deletions cumulus/client/consensus/aura/src/collators/slot_based/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
//!
//! 1. Awaits the next production signal from the internal timer
//! 2. Retrieves the current best relay chain block and identifies a valid parent block (see
//! [find_potential_parents][cumulus_client_consensus_common::find_potential_parents] for parent
//! selection criteria)
//! [find_parent_for_building][cumulus_client_consensus_common::find_parent_for_building] for
//! parent selection criteria)
//! 3. Validates that:
//! - The parachain has an assigned core on the relay chain
//! - No block has been previously built on the target core
Expand Down
Loading
Loading