referrerpolicy=no-referrer-when-downgrade

polkadot_node_subsystem_util/
controlled_validator_indices.rs

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
19use polkadot_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex};
20use schnellru::{ByLength, LruMap};
21use sp_keystore::KeystorePtr;
22
23/// Keeps track of the validator indices controlled by the local validator in a given session. For
24/// better performance, the values for each session are cached.
25pub struct ControlledValidatorIndices {
26	/// The indices of the controlled validators, cached by session.
27	controlled_validator_indices: LruMap<SessionIndex, Option<ValidatorIndex>>,
28	keystore: KeystorePtr,
29}
30
31impl ControlledValidatorIndices {
32	/// Create a new instance of `ControlledValidatorIndices`.
33	pub fn new(keystore: KeystorePtr, cache_size: u32) -> Self {
34		let controlled_validator_indices = LruMap::new(ByLength::new(cache_size));
35		Self { controlled_validator_indices, keystore }
36	}
37
38	/// Get the controlled validator indices for a given session. If the indices are not known they
39	/// will be fetched from `session_validators` and cached.
40	pub fn get(
41		&mut self,
42		session: SessionIndex,
43		session_validators: &IndexedVec<ValidatorIndex, ValidatorId>,
44	) -> Option<ValidatorIndex> {
45		self.controlled_validator_indices
46			.get_or_insert(session, || {
47				crate::signing_key_and_index(session_validators.iter(), &self.keystore)
48					.map(|(_, index)| index)
49			})
50			.copied()
51			.expect("We just inserted the controlled indices; qed")
52	}
53}