Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion polkadot/node/core/dispute-coordinator/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ impl<'a> CandidateEnvironment<'a> {
d
};

let controlled_indices = controlled_indices.get(session_index, &session.validators).clone();
let controlled_indices = controlled_indices
.get(session_index, &session.validators)
.map_or(HashSet::new(), |index| HashSet::from([index]));

Some(Self { session_index, session, executor_params, controlled_indices, disabled_indices })
}
Expand Down
1 change: 0 additions & 1 deletion polkadot/node/subsystem-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ polkadot-node-subsystem-types = { workspace = true, default-features = true }
polkadot-overseer = { workspace = true, default-features = true }
polkadot-primitives = { workspace = true, default-features = true }

sc-keystore = { workspace = true, default-features = true }
sp-application-crypto = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-keystore = { workspace = true, default-features = true }
Expand Down
46 changes: 11 additions & 35 deletions polkadot/node/subsystem-util/src/controlled_validator_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@

//! `ControlledValidatorIndices` implementation.

use polkadot_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex, ValidatorPair};
use sc_keystore::LocalKeystore;
use polkadot_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex};
use schnellru::{ByLength, LruMap};
use sp_application_crypto::{AppCrypto, ByteArray};
use sp_keystore::Keystore;
use std::{collections::HashSet, sync::Arc};
use sp_keystore::KeystorePtr;

/// 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>,
controlled_validator_indices: LruMap<SessionIndex, Option<ValidatorIndex>>,
keystore: KeystorePtr,
}

impl ControlledValidatorIndices {
/// Create a new instance of `ControlledValidatorIndices`.
pub fn new(keystore: Arc<LocalKeystore>, cache_size: u32) -> Self {
pub fn new(keystore: KeystorePtr, cache_size: u32) -> Self {
let controlled_validator_indices = LruMap::new(ByLength::new(cache_size));
Self { controlled_validator_indices, keystore }
}
Expand All @@ -44,34 +41,13 @@ impl ControlledValidatorIndices {
&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());
}

) -> Option<ValidatorIndex> {
self.controlled_validator_indices
.get(&session)
.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")
}

/// 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();
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
}
}
13 changes: 13 additions & 0 deletions prdoc/pr_8896.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title: Improvements for `ControlledValidatorIndices`
doc:
- audience: Node Dev
description: |
Improvements for `ControlledValidatorIndices`:
- remove unneeded dependency
- more readable implementations for `get` and `find_controlled_validator_indices`

crates:
- name: polkadot-node-subsystem-util
bump: patch
- name: polkadot-node-core-dispute-coordinator
bump: patch
Loading