Skip to content

Commit 37c9bed

Browse files
Cumulus: Simplify parent search for block-building (#10998)
While reviewing #10973 I found once more that our parent search is totally overengineered: - It offers the option to search branches that do not contain the pending block -> These branches can never be taken - It returns a list of potential parents -> Nobody uses the list, we only care about the latest block that we should build on By eliminating these two annoyances, the code is a lot more simple and easier to follow. There are still some defensive checks that are not strictly necessary, but does not hurt to keep them. In summary, the mental model is: Build on the latest descendant of the pending block that is still inside the relay parent ancestry. If no pending block is available, use the included block in its place. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 65cdbb2 commit 37c9bed

File tree

7 files changed

+285
-738
lines changed

7 files changed

+285
-738
lines changed

cumulus/client/consensus/aura/src/collators/lookahead.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ use sp_consensus_aura::{AuraApi, Slot};
6363
use sp_core::crypto::Pair;
6464
use sp_inherents::CreateInherentDataProviders;
6565
use sp_keystore::KeystorePtr;
66-
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member};
66+
use sp_runtime::{
67+
traits::{Block as BlockT, Header as HeaderT, Member},
68+
Saturating,
69+
};
6770
use sp_timestamp::Timestamp;
6871
use std::{path::PathBuf, sync::Arc, time::Duration};
6972

@@ -314,18 +317,19 @@ where
314317
},
315318
};
316319

317-
let (included_block, initial_parent) = match crate::collators::find_parent(
320+
let parent_search_result = match crate::collators::find_parent(
318321
relay_parent,
319322
params.para_id,
320323
&*params.para_backend,
321324
&params.relay_client,
322325
)
323326
.await
324327
{
325-
Some(value) => value,
328+
Some(result) => result,
326329
None => continue,
327330
};
328331

332+
let included_header = &parent_search_result.included_header;
329333
let para_client = &*params.para_client;
330334
let keystore = &params.keystore;
331335
let can_build_upon = |block_hash| {
@@ -341,16 +345,19 @@ where
341345
relay_slot,
342346
timestamp,
343347
block_hash,
344-
included_block.hash(),
348+
included_header.hash(),
345349
para_client,
346350
&keystore,
347351
))
348352
};
349353

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

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

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

cumulus/client/consensus/aura/src/collators/mod.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,14 @@ where
257257
.then(|| SlotClaim::unchecked::<P>(author_pub, para_slot, timestamp))
258258
}
259259

260-
/// Use [`cumulus_client_consensus_common::find_potential_parents`] to find parachain blocks that
261-
/// we can build on. Once a list of potential parents is retrieved, return the last one of the
262-
/// longest chain.
260+
/// Use [`cumulus_client_consensus_common::find_parent_for_building`] to find the best parachain
261+
/// block to build on.
263262
async fn find_parent<Block>(
264263
relay_parent: RelayHash,
265264
para_id: ParaId,
266265
para_backend: &impl sc_client_api::Backend<Block>,
267266
relay_client: &impl RelayChainInterface,
268-
) -> Option<(<Block as BlockT>::Header, consensus_common::PotentialParent<Block>)>
267+
) -> Option<consensus_common::ParentSearchResult<Block>>
269268
where
270269
Block: BlockT,
271270
{
@@ -276,35 +275,26 @@ where
276275
.await
277276
.unwrap_or(DEFAULT_SCHEDULING_LOOKAHEAD)
278277
.saturating_sub(1) as usize,
279-
ignore_alternative_branches: true,
280278
};
281279

282-
let potential_parents = cumulus_client_consensus_common::find_potential_parents::<Block>(
280+
match cumulus_client_consensus_common::find_parent_for_building::<Block>(
283281
parent_search_params,
284282
para_backend,
285283
relay_client,
286284
)
287-
.await;
288-
289-
let potential_parents = match potential_parents {
285+
.await
286+
{
287+
Ok(result) => result,
290288
Err(e) => {
291289
tracing::error!(
292290
target: crate::LOG_TARGET,
293291
?relay_parent,
294292
err = ?e,
295-
"Could not fetch potential parents to build upon"
293+
"Could not find parent to build upon"
296294
);
297-
298-
return None;
295+
None
299296
},
300-
Ok(x) => x,
301-
};
302-
303-
let included_block = potential_parents.iter().find(|x| x.depth == 0)?.header.clone();
304-
potential_parents
305-
.into_iter()
306-
.max_by_key(|a| a.depth)
307-
.map(|parent| (included_block, parent))
297+
}
308298
}
309299

310300
#[cfg(test)]

cumulus/client/consensus/aura/src/collators/slot_based/block_builder_task.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ use sp_consensus_aura::AuraApi;
5454
use sp_core::crypto::Pair;
5555
use sp_inherents::CreateInherentDataProviders;
5656
use sp_keystore::KeystorePtr;
57-
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member, Zero};
57+
use sp_runtime::{
58+
traits::{Block as BlockT, Header as HeaderT, Member, Zero},
59+
Saturating,
60+
};
5861
use std::{collections::VecDeque, sync::Arc, time::Duration};
5962

6063
/// Parameters for [`run_block_builder`].
@@ -237,15 +240,19 @@ where
237240
let relay_parent = rp_data.relay_parent().hash();
238241
let relay_parent_header = rp_data.relay_parent().clone();
239242

240-
let Some((included_header, parent)) =
243+
let Some(parent_search_result) =
241244
crate::collators::find_parent(relay_parent, para_id, &*para_backend, &relay_client)
242245
.await
243246
else {
244247
continue;
245248
};
246249

247-
let parent_hash = parent.hash;
248-
let parent_header = &parent.header;
250+
let parent_hash = parent_search_result.best_parent_header.hash();
251+
let included_header = parent_search_result.included_header;
252+
let parent_header = &parent_search_result.best_parent_header;
253+
// Distance from included block to best parent (unincluded segment length).
254+
let unincluded_segment_len =
255+
parent_header.number().saturating_sub(*included_header.number());
249256

250257
// Retrieve the core.
251258
let core = match determine_core(
@@ -329,7 +336,7 @@ where
329336
None => {
330337
tracing::debug!(
331338
target: crate::LOG_TARGET,
332-
unincluded_segment_len = parent.depth,
339+
?unincluded_segment_len,
333340
relay_parent = ?relay_parent,
334341
relay_parent_num = %relay_parent_header.number(),
335342
included_hash = ?included_header_hash,
@@ -344,7 +351,7 @@ where
344351

345352
tracing::debug!(
346353
target: crate::LOG_TARGET,
347-
unincluded_segment_len = parent.depth,
354+
?unincluded_segment_len,
348355
relay_parent = %relay_parent,
349356
relay_parent_num = %relay_parent_header.number(),
350357
relay_parent_offset,
@@ -417,7 +424,7 @@ where
417424
let Some(adjusted_authoring_duration) = adjusted_authoring_duration else {
418425
tracing::debug!(
419426
target: crate::LOG_TARGET,
420-
unincluded_segment_len = parent.depth,
427+
?unincluded_segment_len,
421428
relay_parent = ?relay_parent,
422429
relay_parent_num = %relay_parent_header.number(),
423430
included_hash = ?included_header_hash,

cumulus/client/consensus/aura/src/collators/slot_based/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
//!
3030
//! 1. Awaits the next production signal from the internal timer
3131
//! 2. Retrieves the current best relay chain block and identifies a valid parent block (see
32-
//! [find_potential_parents][cumulus_client_consensus_common::find_potential_parents] for parent
33-
//! selection criteria)
32+
//! [find_parent_for_building][cumulus_client_consensus_common::find_parent_for_building] for
33+
//! parent selection criteria)
3434
//! 3. Validates that:
3535
//! - The parachain has an assigned core on the relay chain
3636
//! - No block has been previously built on the target core

0 commit comments

Comments
 (0)