Skip to content

Commit 6e3f912

Browse files
authored
Cleanup and improvements for ControlledValidatorIndices (#8896)
Improvements for `ControlledValidatorIndices` from #8837: - remove unneeded dependency - more readable implementations for `get` and `find_controlled_validator_indices`
1 parent 2ad96b7 commit 6e3f912

5 files changed

Lines changed: 27 additions & 38 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

polkadot/node/core/dispute-coordinator/src/import.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ impl<'a> CandidateEnvironment<'a> {
104104
d
105105
};
106106

107-
let controlled_indices = controlled_indices.get(session_index, &session.validators).clone();
107+
let controlled_indices = controlled_indices
108+
.get(session_index, &session.validators)
109+
.map_or(HashSet::new(), |index| HashSet::from([index]));
108110

109111
Some(Self { session_index, session, executor_params, controlled_indices, disabled_indices })
110112
}

polkadot/node/subsystem-util/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ polkadot-node-subsystem-types = { workspace = true, default-features = true }
3232
polkadot-overseer = { workspace = true, default-features = true }
3333
polkadot-primitives = { workspace = true, default-features = true }
3434

35-
sc-keystore = { workspace = true, default-features = true }
3635
sp-application-crypto = { workspace = true, default-features = true }
3736
sp-core = { workspace = true, default-features = true }
3837
sp-keystore = { workspace = true, default-features = true }

polkadot/node/subsystem-util/src/controlled_validator_indices.rs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,21 @@
1616

1717
//! `ControlledValidatorIndices` implementation.
1818
19-
use polkadot_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex, ValidatorPair};
20-
use sc_keystore::LocalKeystore;
19+
use polkadot_primitives::{IndexedVec, SessionIndex, ValidatorId, ValidatorIndex};
2120
use schnellru::{ByLength, LruMap};
22-
use sp_application_crypto::{AppCrypto, ByteArray};
23-
use sp_keystore::Keystore;
24-
use std::{collections::HashSet, sync::Arc};
21+
use sp_keystore::KeystorePtr;
2522

2623
/// Keeps track of the validator indices controlled by the local validator in a given session. For
2724
/// better performance, the values for each session are cached.
2825
pub struct ControlledValidatorIndices {
2926
/// The indices of the controlled validators, cached by session.
30-
controlled_validator_indices: LruMap<SessionIndex, HashSet<ValidatorIndex>>,
31-
keystore: Arc<LocalKeystore>,
27+
controlled_validator_indices: LruMap<SessionIndex, Option<ValidatorIndex>>,
28+
keystore: KeystorePtr,
3229
}
3330

3431
impl ControlledValidatorIndices {
3532
/// Create a new instance of `ControlledValidatorIndices`.
36-
pub fn new(keystore: Arc<LocalKeystore>, cache_size: u32) -> Self {
33+
pub fn new(keystore: KeystorePtr, cache_size: u32) -> Self {
3734
let controlled_validator_indices = LruMap::new(ByLength::new(cache_size));
3835
Self { controlled_validator_indices, keystore }
3936
}
@@ -44,34 +41,13 @@ impl ControlledValidatorIndices {
4441
&mut self,
4542
session: SessionIndex,
4643
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-
44+
) -> Option<ValidatorIndex> {
5445
self.controlled_validator_indices
55-
.get(&session)
46+
.get_or_insert(session, || {
47+
crate::signing_key_and_index(session_validators.iter(), &self.keystore)
48+
.map(|(_, index)| index)
49+
})
50+
.copied()
5651
.expect("We just inserted the controlled indices; qed")
5752
}
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-
}
7753
}

prdoc/pr_8896.prdoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
title: Improvements for `ControlledValidatorIndices`
2+
doc:
3+
- audience: Node Dev
4+
description: |
5+
Improvements for `ControlledValidatorIndices`:
6+
- remove unneeded dependency
7+
- more readable implementations for `get` and `find_controlled_validator_indices`
8+
9+
crates:
10+
- name: polkadot-node-subsystem-util
11+
bump: patch
12+
- name: polkadot-node-core-dispute-coordinator
13+
bump: patch

0 commit comments

Comments
 (0)