Filter votes from disabled validators in BackedCandidates in process_inherent_data (#2889)

Backport of https://github.com/paritytech/polkadot-sdk/pull/1863 to
master

Extend candidate sanitation in paras_inherent by removing backing votes
from disabled validators. Check
https://github.com/paritytech/polkadot-sdk/issues/1592 for more details.

This change is related to the disabling strategy implementation
(https://github.com/paritytech/polkadot-sdk/pull/2226).

---------

Co-authored-by: ordian <noreply@reusable.software>
Co-authored-by: ordian <write@reusable.software>
Co-authored-by: Maciej <maciej.zyszkiewicz@parity.io>
This commit is contained in:
Tsvetomir Dimitrov
2024-01-18 09:33:58 +02:00
committed by GitHub
parent f574868822
commit f8954093b4
21 changed files with 506 additions and 64 deletions
+39 -3
View File
@@ -19,11 +19,14 @@
//! To avoid cyclic dependencies, it is important that this pallet is not
//! dependent on any of the other pallets.
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, traits::DisabledValidators};
use frame_system::pallet_prelude::BlockNumberFor;
use primitives::{SessionIndex, ValidatorId, ValidatorIndex};
use sp_runtime::traits::AtLeast32BitUnsigned;
use sp_std::{collections::vec_deque::VecDeque, vec::Vec};
use sp_std::{
collections::{btree_map::BTreeMap, vec_deque::VecDeque},
vec::Vec,
};
use rand::{seq::SliceRandom, SeedableRng};
use rand_chacha::ChaCha20Rng;
@@ -129,7 +132,9 @@ pub mod pallet {
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {}
pub trait Config: frame_system::Config {
type DisabledValidators: frame_support::traits::DisabledValidators;
}
/// The current session index.
#[pallet::storage]
@@ -216,6 +221,25 @@ impl<T: Config> Pallet<T> {
Self::session_index().saturating_add(SESSION_DELAY)
}
/// Fetches disabled validators list from session pallet.
/// CAVEAT: this might produce incorrect results on session boundaries
pub fn disabled_validators() -> Vec<ValidatorIndex> {
let shuffled_indices = Pallet::<T>::active_validator_indices();
// mapping from raw validator index to `ValidatorIndex`
// this computation is the same within a session, but should be cheap
let reverse_index = shuffled_indices
.iter()
.enumerate()
.map(|(i, v)| (v.0, ValidatorIndex(i as u32)))
.collect::<BTreeMap<u32, ValidatorIndex>>();
// we might have disabled validators who are not parachain validators
T::DisabledValidators::disabled_validators()
.iter()
.filter_map(|v| reverse_index.get(v).cloned())
.collect()
}
/// Test function for setting the current session index.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn set_session_index(index: SessionIndex) {
@@ -239,4 +263,16 @@ impl<T: Config> Pallet<T> {
ActiveValidatorIndices::<T>::set(indices);
ActiveValidatorKeys::<T>::set(keys);
}
#[cfg(test)]
pub(crate) fn add_allowed_relay_parent(
relay_parent: T::Hash,
state_root: T::Hash,
number: BlockNumberFor<T>,
max_ancestry_len: u32,
) {
AllowedRelayParents::<T>::mutate(|tracker| {
tracker.update(relay_parent, state_root, number, max_ancestry_len)
})
}
}