Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: make the candidate relay parent progression check more strict #5113

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 13 additions & 1 deletion polkadot/runtime/parachains/src/inclusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ impl<T: Config> Pallet<T> {
for (candidate, core) in para_candidates.iter() {
let candidate_hash = candidate.candidate().hash();

// The previous context is None, as it's already checked during candidate
// sanitization.
let check_ctx = CandidateCheckContext::<T>::new(None);
let relay_parent_number = check_ctx.verify_backed_candidate(
&allowed_relay_parents,
Expand Down Expand Up @@ -729,6 +731,16 @@ impl<T: Config> Pallet<T> {
}
}

// Get the relay parent number of the most recent candidate.
alindima marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn para_most_recent_context(para_id: &ParaId) -> Option<BlockNumberFor<T>> {
match PendingAvailability::<T>::get(para_id)
.and_then(|pending_candidates| pending_candidates.back().map(|x| x.relay_parent_number))
{
Some(relay_parent_number) => Some(relay_parent_number),
None => paras::MostRecentContext::<T>::get(para_id),
}
}

fn check_backing_votes(
backed_candidate: &BackedCandidate<T::Hash>,
validators: &[ValidatorId],
Expand Down Expand Up @@ -798,7 +810,7 @@ impl<T: Config> Pallet<T> {
relay_parent_number: BlockNumberFor<T>,
validation_outputs: polkadot_primitives::CandidateCommitments,
) -> bool {
let prev_context = paras::MostRecentContext::<T>::get(para_id);
let prev_context = Self::para_most_recent_context(&para_id);
let check_ctx = CandidateCheckContext::<T>::new(prev_context);

if check_ctx
Expand Down
71 changes: 39 additions & 32 deletions polkadot/runtime/parachains/src/paras_inherent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,18 +1164,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 Expand Up @@ -1243,22 +1244,27 @@ fn filter_unchained_candidates<T: inclusion::Config + paras::Config + inclusion:
candidates: &mut BTreeMap<ParaId, Vec<BackedCandidate<T::Hash>>>,
allowed_relay_parents: &AllowedRelayParentsTracker<T::Hash, BlockNumberFor<T>>,
) {
let mut para_latest_head_data: BTreeMap<ParaId, HeadData> = BTreeMap::new();
let mut para_latest_context: BTreeMap<ParaId, (HeadData, BlockNumberFor<T>)> = BTreeMap::new();
for para_id in candidates.keys() {
let latest_head_data = match inclusion::Pallet::<T>::para_latest_head_data(&para_id) {
None => {
defensive!("Latest included head data for paraid {:?} is None", para_id);
continue
},
Some(latest_head_data) => latest_head_data,
let Some(latest_head_data) = inclusion::Pallet::<T>::para_latest_head_data(&para_id) else {
defensive!("Latest included head data for paraid {:?} is None", para_id);
continue
};
let Some(latest_relay_parent) = inclusion::Pallet::<T>::para_most_recent_context(&para_id)
else {
defensive!("Latest relay parent for paraid {:?} is None", para_id);
continue
};
para_latest_head_data.insert(*para_id, latest_head_data);
para_latest_context.insert(*para_id, (latest_head_data, latest_relay_parent));
}

let mut para_visited_candidates: BTreeMap<ParaId, BTreeSet<CandidateHash>> = BTreeMap::new();

retain_candidates::<T, _, _>(candidates, |para_id, candidate| {
let Some(latest_head_data) = para_latest_head_data.get(&para_id) else { return false };
let Some((latest_head_data, latest_relay_parent)) = para_latest_context.get(&para_id)
else {
return false
};
let candidate_hash = candidate.candidate().hash();

let visited_candidates =
Expand All @@ -1277,15 +1283,23 @@ fn filter_unchained_candidates<T: inclusion::Config + paras::Config + inclusion:
visited_candidates.insert(candidate_hash);
}

let prev_context = paras::MostRecentContext::<T>::get(para_id);
let check_ctx = CandidateCheckContext::<T>::new(prev_context);
let check_ctx = CandidateCheckContext::<T>::new(Some(*latest_relay_parent));

let res = match check_ctx.verify_backed_candidate(
match check_ctx.verify_backed_candidate(
&allowed_relay_parents,
candidate.candidate(),
latest_head_data.clone(),
) {
Ok(_) => true,
Ok(relay_parent_block_number) => {
para_latest_context.insert(
para_id,
(
candidate.candidate().commitments.head_data.clone(),
relay_parent_block_number,
),
);
true
},
Err(err) => {
log::debug!(
target: LOG_TARGET,
Expand All @@ -1296,14 +1310,7 @@ fn filter_unchained_candidates<T: inclusion::Config + paras::Config + inclusion:
);
false
},
};

if res {
para_latest_head_data
.insert(para_id, candidate.candidate().commitments.head_data.clone());
}

res
});
}

Expand Down
Loading
Loading