Disabled validators runtime API (#1257)

Exposes disabled validators list via a runtime API.

---------

Co-authored-by: ordian <noreply@reusable.software>
Co-authored-by: ordian <write@reusable.software>
This commit is contained in:
Tsvetomir Dimitrov
2023-10-12 16:01:07 +03:00
committed by GitHub
parent f0e6d2ad52
commit 7aace06b3d
13 changed files with 115 additions and 8 deletions
@@ -15,3 +15,30 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Put implementations of functions from staging APIs here.
use crate::shared;
use primitives::ValidatorIndex;
use sp_std::{collections::btree_map::BTreeMap, prelude::Vec};
/// Implementation for `DisabledValidators`
// CAVEAT: this should only be called on the node side
// as it might produce incorrect results on session boundaries
pub fn disabled_validators<T>() -> Vec<ValidatorIndex>
where
T: pallet_session::Config + shared::Config,
{
let shuffled_indices = <shared::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
<pallet_session::Pallet<T>>::disabled_validators()
.iter()
.filter_map(|v| reverse_index.get(v).cloned())
.collect()
}