54 lines
2.1 KiB
Rust
54 lines
2.1 KiB
Rust
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// This file is part of Pezkuwi.
|
|
|
|
// Pezkuwi is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Pezkuwi is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! `ControlledValidatorIndices` implementation.
|
|
|
|
use pezkuwi_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex};
|
|
use pezsp_keystore::KeystorePtr;
|
|
use schnellru::{ByLength, LruMap};
|
|
|
|
/// Keeps track of the validator indices controlled by the local validator in a given session. For
|
|
/// better performance, the values for each session are cached.
|
|
pub struct ControlledValidatorIndices {
|
|
/// The indices of the controlled validators, cached by session.
|
|
controlled_validator_indices: LruMap<SessionIndex, Option<ValidatorIndex>>,
|
|
keystore: KeystorePtr,
|
|
}
|
|
|
|
impl ControlledValidatorIndices {
|
|
/// Create a new instance of `ControlledValidatorIndices`.
|
|
pub fn new(keystore: KeystorePtr, cache_size: u32) -> Self {
|
|
let controlled_validator_indices = LruMap::new(ByLength::new(cache_size));
|
|
Self { controlled_validator_indices, keystore }
|
|
}
|
|
|
|
/// Get the controlled validator indices for a given session. If the indices are not known they
|
|
/// will be fetched from `session_validators` and cached.
|
|
pub fn get(
|
|
&mut self,
|
|
session: SessionIndex,
|
|
session_validators: &IndexedVec<ValidatorIndex, ValidatorId>,
|
|
) -> Option<ValidatorIndex> {
|
|
self.controlled_validator_indices
|
|
.get_or_insert(session, || {
|
|
crate::signing_key_and_index(session_validators.iter(), &self.keystore)
|
|
.map(|(_, index)| index)
|
|
})
|
|
.copied()
|
|
.expect("We just inserted the controlled indices; qed")
|
|
}
|
|
}
|