@@ -49,7 +49,9 @@ use polkadot_node_subsystem::messages::{
4949 CollationGenerationMessage , RuntimeApiMessage , RuntimeApiRequest ,
5050} ;
5151use polkadot_overseer:: Handle as OverseerHandle ;
52- use polkadot_primitives:: { CollatorPair , CoreIndex , Id as ParaId , OccupiedCoreAssumption } ;
52+ use polkadot_primitives:: {
53+ AsyncBackingParams , CollatorPair , CoreIndex , CoreState , Id as ParaId , OccupiedCoreAssumption ,
54+ } ;
5355
5456use futures:: { channel:: oneshot, prelude:: * } ;
5557use sc_client_api:: { backend:: AuxStore , BlockBackend , BlockOf } ;
@@ -186,10 +188,14 @@ where
186188
187189 // TODO: Currently we use just the first core here, but for elastic scaling
188190 // we iterate and build on all of the cores returned.
189- let core_index = if let Some ( core_index) =
190- cores_scheduled_for_para ( relay_parent, params. para_id , & mut params. overseer_handle )
191- . await
192- . get ( 0 )
191+ let core_index = if let Some ( core_index) = cores_scheduled_for_para (
192+ relay_parent,
193+ params. para_id ,
194+ & mut params. overseer_handle ,
195+ & mut params. relay_client ,
196+ )
197+ . await
198+ . get ( 0 )
193199 {
194200 * core_index
195201 } else {
@@ -223,7 +229,10 @@ where
223229 let parent_search_params = ParentSearchParams {
224230 relay_parent,
225231 para_id : params. para_id ,
226- ancestry_lookback : max_ancestry_lookback ( relay_parent, & params. relay_client ) . await ,
232+ ancestry_lookback : async_backing_params ( relay_parent, & params. relay_client )
233+ . await
234+ . map ( |c| c. allowed_ancestry_len as usize )
235+ . unwrap_or ( 0 ) ,
227236 max_depth : PARENT_SEARCH_DEPTH ,
228237 ignore_alternative_branches : true ,
229238 } ;
@@ -461,21 +470,19 @@ where
461470 Some ( SlotClaim :: unchecked :: < P > ( author_pub, slot, timestamp) )
462471}
463472
464- /// Reads allowed ancestry length parameter from the relay chain storage at the given relay parent.
465- ///
466- /// Falls back to 0 in case of an error.
467- async fn max_ancestry_lookback (
473+ /// Reads async backing parameters from the relay chain storage at the given relay parent.
474+ async fn async_backing_params (
468475 relay_parent : PHash ,
469476 relay_client : & impl RelayChainInterface ,
470- ) -> usize {
477+ ) -> Option < AsyncBackingParams > {
471478 match load_abridged_host_configuration ( relay_parent, relay_client) . await {
472- Ok ( Some ( config) ) => config. async_backing_params . allowed_ancestry_len as usize ,
479+ Ok ( Some ( config) ) => Some ( config. async_backing_params ) ,
473480 Ok ( None ) => {
474481 tracing:: error!(
475482 target: crate :: LOG_TARGET ,
476483 "Active config is missing in relay chain storage" ,
477484 ) ;
478- 0
485+ None
479486 } ,
480487 Err ( err) => {
481488 tracing:: error!(
@@ -484,7 +491,7 @@ async fn max_ancestry_lookback(
484491 ?relay_parent,
485492 "Failed to read active config from relay chain client" ,
486493 ) ;
487- 0
494+ None
488495 } ,
489496 }
490497}
@@ -494,7 +501,9 @@ async fn cores_scheduled_for_para(
494501 relay_parent : PHash ,
495502 para_id : ParaId ,
496503 overseer_handle : & mut OverseerHandle ,
504+ relay_client : & impl RelayChainInterface ,
497505) -> Vec < CoreIndex > {
506+ // Get `AvailabilityCores` from runtime
498507 let ( tx, rx) = oneshot:: channel ( ) ;
499508 let request = RuntimeApiRequest :: AvailabilityCores ( tx) ;
500509 overseer_handle
@@ -522,11 +531,25 @@ async fn cores_scheduled_for_para(
522531 } ,
523532 } ;
524533
534+ let max_candidate_depth = async_backing_params ( relay_parent, relay_client)
535+ . await
536+ . map ( |c| c. max_candidate_depth )
537+ . unwrap_or ( 0 ) ;
538+
525539 cores
526540 . iter ( )
527541 . enumerate ( )
528542 . filter_map ( |( index, core) | {
529- if core. para_id ( ) == Some ( para_id) {
543+ let core_para_id = match core {
544+ CoreState :: Scheduled ( scheduled_core) => Some ( scheduled_core. para_id ) ,
545+ CoreState :: Occupied ( occupied_core) if max_candidate_depth >= 1 => occupied_core
546+ . next_up_on_available
547+ . as_ref ( )
548+ . map ( |scheduled_core| scheduled_core. para_id ) ,
549+ CoreState :: Free | CoreState :: Occupied ( _) => None ,
550+ } ;
551+
552+ if core_para_id == Some ( para_id) {
530553 Some ( CoreIndex ( index as u32 ) )
531554 } else {
532555 None
0 commit comments