From 683d0ea23e559ff7875a4c84bbae9f98e0a50abd Mon Sep 17 00:00:00 2001 From: ordian Date: Wed, 14 Aug 2024 21:57:41 +0200 Subject: [PATCH] compute concluded invalid lazily --- polkadot/runtime/parachains/src/disputes.rs | 18 --------- .../runtime/parachains/src/inclusion/mod.rs | 11 ++--- .../parachains/src/paras_inherent/mod.rs | 40 ++++++++++--------- 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/polkadot/runtime/parachains/src/disputes.rs b/polkadot/runtime/parachains/src/disputes.rs index b5ff9802e446..00c9dba24f20 100644 --- a/polkadot/runtime/parachains/src/disputes.rs +++ b/polkadot/runtime/parachains/src/disputes.rs @@ -230,9 +230,6 @@ pub trait DisputesHandler { included_in: BlockNumber, ); - /// Get a list of disputes in a session that concluded invalid. - fn disputes_concluded_invalid(session: SessionIndex) -> BTreeSet; - /// Retrieve the included state of a given candidate in a particular session. If it /// returns `Some`, then we have a local dispute for the given `candidate_hash`. fn included_state(session: SessionIndex, candidate_hash: CandidateHash) -> Option; @@ -275,10 +272,6 @@ impl DisputesHandler for () { Ok(Vec::new()) } - fn disputes_concluded_invalid(_session: SessionIndex) -> BTreeSet { - BTreeSet::new() - } - fn note_included( _session: SessionIndex, _candidate_hash: CandidateHash, @@ -328,10 +321,6 @@ where pallet::Pallet::::process_checked_multi_dispute_data(statement_sets) } - fn disputes_concluded_invalid(session: SessionIndex) -> BTreeSet { - pallet::Pallet::::disputes_concluded_invalid(session) - } - fn note_included( session: SessionIndex, candidate_hash: CandidateHash, @@ -1237,13 +1226,6 @@ impl Pallet { Included::::insert(&session, &candidate_hash, revert_to); } - pub(crate) fn disputes_concluded_invalid(session: SessionIndex) -> BTreeSet { - Disputes::::iter_prefix(session) - .filter(|(_hash, state)| has_supermajority_against(state)) - .map(|(hash, _state)| hash) - .collect() - } - pub(crate) fn included_state( session: SessionIndex, candidate_hash: CandidateHash, diff --git a/polkadot/runtime/parachains/src/inclusion/mod.rs b/polkadot/runtime/parachains/src/inclusion/mod.rs index 281dc5d0c5f4..9bc7be654014 100644 --- a/polkadot/runtime/parachains/src/inclusion/mod.rs +++ b/polkadot/runtime/parachains/src/inclusion/mod.rs @@ -1032,14 +1032,11 @@ impl Pallet { /// /// Returns a vector of cleaned-up core IDs, along with the evicted candidate hashes. pub(crate) fn free_disputed( - disputed: &BTreeSet, + disputed: impl Fn(CandidateHash) -> bool, ) -> Vec<(CoreIndex, CandidateHash)> { - Self::free_failed_cores( - |candidate| disputed.contains(&candidate.hash), - Some(disputed.len()), - ) - .map(|candidate| (candidate.core, candidate.hash)) - .collect() + Self::free_failed_cores(|candidate| disputed(candidate.hash), None) + .map(|candidate| (candidate.core, candidate.hash)) + .collect() } // Clean up cores whose candidates are deemed as failed by the predicate. `pred` returns true if diff --git a/polkadot/runtime/parachains/src/paras_inherent/mod.rs b/polkadot/runtime/parachains/src/paras_inherent/mod.rs index 2749fddc99a4..06b511072a3e 100644 --- a/polkadot/runtime/parachains/src/paras_inherent/mod.rs +++ b/polkadot/runtime/parachains/src/paras_inherent/mod.rs @@ -490,16 +490,19 @@ impl Pallet { // Contains the disputes that are concluded in the current session only, // since these are the only ones that are relevant for the occupied cores // and lightens the load on `free_disputed` significantly. - let mut current_concluded_invalid_disputes = - ::DisputesHandler::disputes_concluded_invalid(current_session); + let current_concluded_invalid_disputes = + |hash: CandidateHash| ::DisputesHandler::concluded_invalid(current_session, hash); // Get the cores freed as a result of concluded invalid candidates. let (freed_disputed, concluded_invalid_hashes): (Vec, BTreeSet) = - inclusion::Pallet::::free_disputed(¤t_concluded_invalid_disputes) + inclusion::Pallet::::free_disputed(current_concluded_invalid_disputes) .into_iter() .unzip(); // Also include descendants of the concluded invalid candidates. - current_concluded_invalid_disputes.extend(concluded_invalid_hashes); + let current_concluded_invalid_disputes = |hash: CandidateHash| { + concluded_invalid_hashes.contains(&hash) || + ::DisputesHandler::concluded_invalid(current_session, hash) + }; // Create a bit index from the set of core indices where each index corresponds to // a core index that was freed due to a dispute. @@ -946,7 +949,7 @@ pub(crate) fn sanitize_bitfields( fn sanitize_backed_candidates( backed_candidates: Vec>, allowed_relay_parents: &AllowedRelayParentsTracker>, - concluded_invalid_with_descendants: BTreeSet, + concluded_invalid_with_descendants: impl Fn(CandidateHash) -> bool, scheduled: BTreeMap>, core_index_enabled: bool, ) -> BTreeMap, CoreIndex)>> { @@ -967,7 +970,7 @@ fn sanitize_backed_candidates( // Remove any candidates that were concluded invalid or who are descendants of concluded invalid // candidates (along with their descendants). retain_candidates::(&mut candidates_per_para, |_, candidate| { - let keep = !concluded_invalid_with_descendants.contains(&candidate.candidate().hash()); + let keep = !concluded_invalid_with_descendants(candidate.candidate().hash()); if !keep { log::debug!( @@ -1157,18 +1160,19 @@ fn filter_backed_statements_from_disabled_validators< // Get relay parent block number of the candidate. We need this to get the group index // assigned to this core at this block number - let relay_parent_block_number = - match allowed_relay_parents.acquire_info(bc.descriptor().relay_parent, None) { - Some((_, block_num)) => block_num, - None => { - log::debug!( - target: LOG_TARGET, - "Relay parent {:?} for candidate is not in the allowed relay parents. Dropping the candidate.", - bc.descriptor().relay_parent - ); - return false - }, - }; + let relay_parent_block_number = match allowed_relay_parents + .acquire_info(bc.descriptor().relay_parent, None) + { + Some((_, block_num)) => block_num, + None => { + log::debug!( + target: LOG_TARGET, + "Relay parent {:?} for candidate is not in the allowed relay parents. Dropping the candidate.", + bc.descriptor().relay_parent + ); + return false + }, + }; // Get the group index for the core let group_idx = match scheduler::Pallet::::group_assigned_to_core(