Skip to content

Commit

Permalink
compute concluded invalid lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
ordian committed Aug 14, 2024
1 parent f75dcd7 commit 683d0ea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 43 deletions.
18 changes: 0 additions & 18 deletions polkadot/runtime/parachains/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ pub trait DisputesHandler<BlockNumber: Ord> {
included_in: BlockNumber,
);

/// Get a list of disputes in a session that concluded invalid.
fn disputes_concluded_invalid(session: SessionIndex) -> BTreeSet<CandidateHash>;

/// 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<BlockNumber>;
Expand Down Expand Up @@ -275,10 +272,6 @@ impl<BlockNumber: Ord> DisputesHandler<BlockNumber> for () {
Ok(Vec::new())
}

fn disputes_concluded_invalid(_session: SessionIndex) -> BTreeSet<CandidateHash> {
BTreeSet::new()
}

fn note_included(
_session: SessionIndex,
_candidate_hash: CandidateHash,
Expand Down Expand Up @@ -328,10 +321,6 @@ where
pallet::Pallet::<T>::process_checked_multi_dispute_data(statement_sets)
}

fn disputes_concluded_invalid(session: SessionIndex) -> BTreeSet<CandidateHash> {
pallet::Pallet::<T>::disputes_concluded_invalid(session)
}

fn note_included(
session: SessionIndex,
candidate_hash: CandidateHash,
Expand Down Expand Up @@ -1237,13 +1226,6 @@ impl<T: Config> Pallet<T> {
Included::<T>::insert(&session, &candidate_hash, revert_to);
}

pub(crate) fn disputes_concluded_invalid(session: SessionIndex) -> BTreeSet<CandidateHash> {
Disputes::<T>::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,
Expand Down
11 changes: 4 additions & 7 deletions polkadot/runtime/parachains/src/inclusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,14 +1032,11 @@ impl<T: Config> Pallet<T> {
///
/// Returns a vector of cleaned-up core IDs, along with the evicted candidate hashes.
pub(crate) fn free_disputed(
disputed: &BTreeSet<CandidateHash>,
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
Expand Down
40 changes: 22 additions & 18 deletions polkadot/runtime/parachains/src/paras_inherent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,19 @@ impl<T: Config> Pallet<T> {
// 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 =
<T>::DisputesHandler::disputes_concluded_invalid(current_session);
let current_concluded_invalid_disputes =
|hash: CandidateHash| <T>::DisputesHandler::concluded_invalid(current_session, hash);

// Get the cores freed as a result of concluded invalid candidates.
let (freed_disputed, concluded_invalid_hashes): (Vec<CoreIndex>, BTreeSet<CandidateHash>) =
inclusion::Pallet::<T>::free_disputed(&current_concluded_invalid_disputes)
inclusion::Pallet::<T>::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) ||
<T>::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.
Expand Down Expand Up @@ -946,7 +949,7 @@ pub(crate) fn sanitize_bitfields<T: crate::inclusion::Config>(
fn sanitize_backed_candidates<T: crate::inclusion::Config>(
backed_candidates: Vec<BackedCandidate<T::Hash>>,
allowed_relay_parents: &AllowedRelayParentsTracker<T::Hash, BlockNumberFor<T>>,
concluded_invalid_with_descendants: BTreeSet<CandidateHash>,
concluded_invalid_with_descendants: impl Fn(CandidateHash) -> bool,
scheduled: BTreeMap<ParaId, BTreeSet<CoreIndex>>,
core_index_enabled: bool,
) -> BTreeMap<ParaId, Vec<(BackedCandidate<T::Hash>, CoreIndex)>> {
Expand All @@ -967,7 +970,7 @@ fn sanitize_backed_candidates<T: crate::inclusion::Config>(
// Remove any candidates that were concluded invalid or who are descendants of concluded invalid
// candidates (along with their descendants).
retain_candidates::<T, _, _>(&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!(
Expand Down Expand Up @@ -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::<T>::group_assigned_to_core(
Expand Down

0 comments on commit 683d0ea

Please sign in to comment.