-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[stable2503] Backport #8837 #8884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot 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. | ||
|
|
||
| // Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! `ControlledValidatorIndices` implementation. | ||
|
|
||
| use polkadot_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex, ValidatorPair}; | ||
| use sc_keystore::LocalKeystore; | ||
| use schnellru::{ByLength, LruMap}; | ||
| use sp_application_crypto::{AppCrypto, ByteArray}; | ||
| use sp_keystore::Keystore; | ||
| use std::{collections::HashSet, sync::Arc}; | ||
|
|
||
| /// 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, HashSet<ValidatorIndex>>, | ||
| keystore: Arc<LocalKeystore>, | ||
| } | ||
|
|
||
| impl ControlledValidatorIndices { | ||
| /// Create a new instance of `ControlledValidatorIndices`. | ||
| pub fn new(keystore: Arc<LocalKeystore>, 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: This like this: pub fn get(
&mut self,
session: SessionIndex,
session_validators: &IndexedVec<ValidatorIndex, ValidatorId>,
) -> &HashSet<ValidatorIndex> {
self.controlled_validator_indices.get_or_insert(session, || {
Self::find_controlled_validator_indices(&self.keystore, session_validators)
}).expect("We just inserted the controlled indices; qed")
}If the key is If I'm not mistaken :) |
||
| &mut self, | ||
| session: SessionIndex, | ||
| session_validators: &IndexedVec<ValidatorIndex, ValidatorId>, | ||
| ) -> &HashSet<ValidatorIndex> { | ||
| if self.controlled_validator_indices.get(&session).is_none() { | ||
| let indices = | ||
| Self::find_controlled_validator_indices(&self.keystore, session_validators); | ||
| self.controlled_validator_indices.insert(session, indices.clone()); | ||
| } | ||
|
|
||
| self.controlled_validator_indices | ||
| .get(&session) | ||
| .expect("We just inserted the controlled indices; qed") | ||
| } | ||
|
|
||
| /// Find indices controlled by this validator. | ||
| /// | ||
| /// That is all `ValidatorIndex`es we have private keys for. Usually this will only be one. | ||
| fn find_controlled_validator_indices( | ||
| keystore: &LocalKeystore, | ||
| validators: &IndexedVec<ValidatorIndex, ValidatorId>, | ||
| ) -> HashSet<ValidatorIndex> { | ||
| let mut controlled = HashSet::new(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this not just a fn find_controlled_validator_indices(
keystore: &LocalKeystore,
validators: &IndexedVec<ValidatorIndex, ValidatorId>,
) -> HashSet<ValidatorIndex> {
validators
.iter()
.enumerate()
.filter(|(_, validator)| {
Keystore::has_keys(keystore, &[(validator.to_raw_vec(), ValidatorPair::ID)])
})
.map(|(index, _)| ValidatorIndex(index as _))
.collect()
}I think? |
||
| for (index, validator) in validators.iter().enumerate() { | ||
| if !Keystore::has_keys(keystore, &[(validator.to_raw_vec(), ValidatorPair::ID)]) { | ||
| continue | ||
| } | ||
|
|
||
| controlled.insert(ValidatorIndex(index as _)); | ||
| } | ||
|
|
||
| controlled | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| title: Cache locally controlled validator indices in dispute-coordinator | ||
| doc: | ||
| - audience: Node Dev | ||
| description: | | ||
| `dispute-coordinator` uses `keystore.key_pair()` to obtain the set of locally controlled | ||
| validator IDs. This operation happens on each import and is expensive because it involves key | ||
| generation from a seed phrase. This patch lazily determines the set of locally controlled | ||
| validator IDs and caches the result for each session. | ||
|
|
||
| crates: | ||
| - name: polkadot-node-core-dispute-coordinator | ||
| bump: minor | ||
| - name: polkadot-node-subsystem-util | ||
| bump: minor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we using some tool to insert copy right headers? Or are we using some tool, e.g. https://github.com/Lucas-C/pre-commit-hooks ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do it by hand :)