-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Filter votes from disabled validators in BackedCandidates in process_inherent_data
#1863
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
Changes from 2 commits
259cb4b
d451886
a3bafba
75d707a
cb0d8fc
8a8893e
7b868b0
a41d21c
34a3bf0
88eb018
305d5b8
e241312
2aa1323
a8efd15
3567a93
04dc32a
cce71ee
7a19f55
3260187
ff577a5
876655d
ed2d430
9a8985a
f7544d7
59fc176
cb2fe60
be85b5d
f472bb1
53aaffa
16823f4
452b202
cb60cf0
750f2db
7022003
8b5e5a8
a50800f
7a308cd
7ccb942
1834e20
610b45d
83adc93
158557d
79f4c40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,7 +121,11 @@ pub mod pallet { | |
| #[pallet::config] | ||
| #[pallet::disable_frame_system_supertrait_check] | ||
| pub trait Config: | ||
| inclusion::Config + scheduler::Config + initializer::Config + pallet_babe::Config | ||
| inclusion::Config | ||
| + scheduler::Config | ||
| + initializer::Config | ||
| + pallet_babe::Config | ||
| + pallet_session::Config | ||
| { | ||
| /// Weight information for extrinsics in this pallet. | ||
| type WeightInfo: WeightInfo; | ||
|
|
@@ -430,6 +434,9 @@ impl<T: Config> Pallet<T> { | |
| T::DisputesHandler::filter_dispute_data(set, post_conclusion_acceptance_period) | ||
| }; | ||
|
|
||
| // Filter out backing statements from disabled validators | ||
| let statements_were_dropped = filter_backed_statements::<T>(&mut backed_candidates)?; | ||
|
|
||
| // Limit the disputes first, since the following statements depend on the votes include | ||
| // here. | ||
| let (checked_disputes_sets, checked_disputes_sets_consumed_weight) = | ||
|
|
@@ -480,6 +487,7 @@ impl<T: Config> Pallet<T> { | |
| } | ||
|
|
||
| ensure!(all_weight_before.all_lte(max_block_weight), Error::<T>::InherentOverweight); | ||
| ensure!(!statements_were_dropped, Error::<T>::InherentOverweight); | ||
|
||
| all_weight_before | ||
| }; | ||
|
|
||
|
|
@@ -1029,3 +1037,122 @@ fn limit_and_sanitize_disputes< | |
| (checked, checked_disputes_weight) | ||
| } | ||
| } | ||
|
|
||
| // Filters statements from disabled validators in `BackedCandidate` and `MultiDisputeStatementSet`. | ||
tdimitrov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Returns `true` if at least one statement is removed and `false` otherwise. | ||
| fn filter_backed_statements<T: Config>( | ||
|
||
| backed_candidates: &mut Vec<BackedCandidate<<T as frame_system::Config>::Hash>>, | ||
| ) -> Result<bool, DispatchErrorWithPostInfo> { | ||
tdimitrov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // `active_validator_indices` contain 'raw indexes' aka indexes in the broad validator set. | ||
| // We want mapping from `ValidatorIndex` (which is an index within `active_validator_indices`) | ||
| // to 'raw indexes' to process disabled validators. | ||
| let raw_idx_to_val_index = shared::Pallet::<T>::active_validator_indices() | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(i, v)| (v.0, ValidatorIndex(i as u32))) | ||
| .collect::<BTreeMap<_, _>>(); | ||
| // `disabled_validators` in pallet session contains 'raw indexes' (see the previous comment). We | ||
| // want them converted to `ValidatorIndex` so that we can look them up easily when processing | ||
| // backed candidates. We skip disabled validators which are not found in the active set. | ||
| let disabled_validators = <pallet_session::Pallet<T>>::disabled_validators() | ||
| .iter() | ||
| .filter_map(|i| raw_idx_to_val_index.get(i)) | ||
| .collect::<BTreeSet<_>>(); | ||
|
|
||
| // `BackedCandidate` contains `validator_indices` which are indecies within the validator group. | ||
tdimitrov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // To obtain `ValidatorIndex` from them we do the following steps: | ||
| // 1. Get `para_id` from `CandidateDescriptor` | ||
| // 2. Get the `core_idx`` for the corresponding `para_id` | ||
| // 3. Get the validator group assigned to the corresponding core idx | ||
|
|
||
| // Map `para_id` to `core_idx` for step 2 from the above list | ||
| let para_id_to_core_idx = <scheduler::Pallet<T>>::scheduled_paras() | ||
| .map(|(core_idx, para_id)| (para_id, core_idx)) | ||
| .collect::<BTreeMap<_, _>>(); | ||
tdimitrov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Flag which will be returned. Set to `true` if at least one vote is filtered. | ||
| let mut filtered = false; | ||
|
|
||
| // Process all backed candidates. | ||
| backed_candidates.retain_mut(|bc| { | ||
| // Get `core_idx` assigned to the `para_id` of the candidate (step 2) | ||
| let core_idx = match para_id_to_core_idx.get(&bc.descriptor().para_id) { | ||
| Some(core_idx) => *core_idx, | ||
| None => { | ||
| log::debug!(target: LOG_TARGET, "Can't get core idx got a backed candidate for para id {:?}. Dropping the candidate.", bc.descriptor().para_id); | ||
| return false | ||
| } | ||
| }; | ||
|
|
||
| // 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 <shared::Pallet<T>>::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 = <scheduler::Pallet<T>>::group_assigned_to_core( | ||
| core_idx, | ||
| relay_parent_block_number + One::one(), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| // And finally get the validator group for this group index | ||
| let validator_group = match <scheduler::Pallet<T>>::group_validators(group_idx) { | ||
| Some(validator_group) => validator_group, | ||
| None => { | ||
| // TODO: this should be an assert? | ||
| log::debug!(target: LOG_TARGET, "Can't get the validators from group {:?}. Dropping the candidate.", group_idx); | ||
| return false | ||
| } | ||
| }; | ||
|
|
||
| // `validator_indices` in `BackedCandidate` are indecies within the validator group. Convert | ||
tdimitrov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // them to `ValidatorId`. | ||
| let voted_validator_ids = bc | ||
| .validator_indices | ||
| .iter() | ||
| .enumerate() | ||
| .filter_map(|(idx, bitval)| match *bitval { | ||
| true => validator_group.get(idx), // drop indecies not found in the validator group. This will lead to filtering the vote | ||
| false => None, | ||
| }) | ||
tdimitrov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .collect::<Vec<_>>(); | ||
|
|
||
| { | ||
| // `validity_votes` should match `validator_indices` | ||
| let mut idx = 0; | ||
| bc.validity_votes.retain(|_| { | ||
| let voted_validator_index = match voted_validator_ids.get(idx) { | ||
| Some(validator_index) => validator_index, | ||
| None => { | ||
| log::debug!(target: LOG_TARGET, "Can't find the voted validator index {:?} in the validator group. Dropping the vote.", group_idx); | ||
| idx += 1; | ||
| return false | ||
| } | ||
| }; | ||
| idx += 1; | ||
|
|
||
| filtered = disabled_validators.contains(voted_validator_index); | ||
|
|
||
| // If we are removing a validity vote - modify `validator_indices` too | ||
| if filtered { | ||
| bc.validator_indices.set(idx, false); | ||
| } | ||
|
|
||
| !filtered | ||
| }); | ||
| } | ||
|
|
||
| // Remove the candidate if all entries are filtered out | ||
| !bc.validity_votes.is_empty() | ||
| }); | ||
|
|
||
| Ok(filtered) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.