|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Polkadot. |
| 3 | + |
| 4 | +// Polkadot is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Polkadot is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! `ControlledValidatorIndices` implementation. |
| 18 | +
|
| 19 | +use polkadot_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex, ValidatorPair}; |
| 20 | +use sc_keystore::LocalKeystore; |
| 21 | +use schnellru::{ByLength, LruMap}; |
| 22 | +use sp_application_crypto::{AppCrypto, ByteArray}; |
| 23 | +use sp_keystore::Keystore; |
| 24 | +use std::{collections::HashSet, sync::Arc}; |
| 25 | + |
| 26 | +/// Keeps track of the validator indices controlled by the local validator in a given session. For |
| 27 | +/// better performance, the values for each session are cached. |
| 28 | +pub struct ControlledValidatorIndices { |
| 29 | + /// The indices of the controlled validators, cached by session. |
| 30 | + controlled_validator_indices: LruMap<SessionIndex, HashSet<ValidatorIndex>>, |
| 31 | + keystore: Arc<LocalKeystore>, |
| 32 | +} |
| 33 | + |
| 34 | +impl ControlledValidatorIndices { |
| 35 | + /// Create a new instance of `ControlledValidatorIndices`. |
| 36 | + pub fn new(keystore: Arc<LocalKeystore>, cache_size: u32) -> Self { |
| 37 | + let controlled_validator_indices = LruMap::new(ByLength::new(cache_size)); |
| 38 | + Self { controlled_validator_indices, keystore } |
| 39 | + } |
| 40 | + |
| 41 | + /// Get the controlled validator indices for a given session. If the indices are not known they |
| 42 | + /// will be fetched from `session_validators` and cached. |
| 43 | + pub fn get( |
| 44 | + &mut self, |
| 45 | + session: SessionIndex, |
| 46 | + session_validators: &IndexedVec<ValidatorIndex, ValidatorId>, |
| 47 | + ) -> &HashSet<ValidatorIndex> { |
| 48 | + if self.controlled_validator_indices.get(&session).is_none() { |
| 49 | + let indices = |
| 50 | + Self::find_controlled_validator_indices(&self.keystore, session_validators); |
| 51 | + self.controlled_validator_indices.insert(session, indices.clone()); |
| 52 | + } |
| 53 | + |
| 54 | + self.controlled_validator_indices |
| 55 | + .get(&session) |
| 56 | + .expect("We just inserted the controlled indices; qed") |
| 57 | + } |
| 58 | + |
| 59 | + /// Find indices controlled by this validator. |
| 60 | + /// |
| 61 | + /// That is all `ValidatorIndex`es we have private keys for. Usually this will only be one. |
| 62 | + fn find_controlled_validator_indices( |
| 63 | + keystore: &LocalKeystore, |
| 64 | + validators: &IndexedVec<ValidatorIndex, ValidatorId>, |
| 65 | + ) -> HashSet<ValidatorIndex> { |
| 66 | + let mut controlled = HashSet::new(); |
| 67 | + for (index, validator) in validators.iter().enumerate() { |
| 68 | + if !Keystore::has_keys(keystore, &[(validator.to_raw_vec(), ValidatorPair::ID)]) { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + controlled.insert(ValidatorIndex(index as _)); |
| 73 | + } |
| 74 | + |
| 75 | + controlled |
| 76 | + } |
| 77 | +} |
0 commit comments