Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Use a more typesafe approach for managing indexed data #6150

Merged
merged 46 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b5c02e8
Fix for issue #2403
tifecool Oct 13, 2022
954d392
Merge branch 'paritytech:master' into fix2403
tifecool Oct 13, 2022
ce8aba9
Nightly fmt
tifecool Oct 13, 2022
b5a7bfc
Quick documentation fixes
tifecool Oct 14, 2022
5da6693
Default Implementation
tifecool Oct 14, 2022
b90fc1e
iter() function integrated
tifecool Oct 14, 2022
557eebb
Implemented iter functionalities
tifecool Oct 15, 2022
09cc765
Fmt
tifecool Oct 15, 2022
3a6e327
small change
tifecool Oct 15, 2022
aba5837
updates node-network
tifecool Oct 15, 2022
dbac388
updates in dispute-coordinator
tifecool Oct 16, 2022
f1b8b4f
Updates
tifecool Oct 18, 2022
8696f93
benchmarking fix
tifecool Oct 18, 2022
4d10ca1
minor fix
tifecool Oct 18, 2022
95a3881
test fixes in runtime api
tifecool Oct 18, 2022
71fc436
Merge branch 'paritytech:master' into fix2403
tifecool Oct 18, 2022
4951b54
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
35c1304
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
a0edab4
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
3823be1
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
5714ad4
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
7a8be34
Removal of [index], shorting of FromIterator, Renaming of GroupValida…
tifecool Oct 19, 2022
c3a1e72
Merge remote-tracking branch 'origin/fix2403' into fix2403
tifecool Oct 19, 2022
56b3904
Removal of ops import
tifecool Oct 19, 2022
a0bb41b
documentation fixes for spell check
tifecool Oct 19, 2022
ef5438c
implementation of generic type
tifecool Oct 20, 2022
4e825d0
Refactoring
tifecool Oct 21, 2022
f127cb4
Test and documentation fixes
tifecool Oct 21, 2022
06ecd6a
minor test fix
tifecool Oct 21, 2022
efa2278
minor test fix
tifecool Oct 21, 2022
cfca9b4
minor test fix
tifecool Oct 21, 2022
e6f685f
Update node/network/statement-distribution/src/lib.rs
tifecool Oct 21, 2022
33e3102
Update primitives/src/v2/mod.rs
tifecool Oct 21, 2022
e4d548b
Update primitives/src/v2/mod.rs
tifecool Oct 21, 2022
dee6396
removed IterMut
tifecool Oct 21, 2022
956a62e
Update node/core/dispute-coordinator/src/import.rs
tifecool Oct 21, 2022
1aba477
Update node/core/dispute-coordinator/src/initialized.rs
tifecool Oct 21, 2022
9025b12
Merge remote-tracking branch 'origin/fix2403' into fix2403
tifecool Oct 21, 2022
37cccf6
Update primitives/src/v2/mod.rs
tifecool Oct 21, 2022
f03adc0
fmt
tifecool Oct 21, 2022
bdc316c
IterMut
tifecool Oct 21, 2022
5ca768f
documentation update
tifecool Oct 21, 2022
2ee57d4
minor adjustments and new TypeIndex trait
tifecool Oct 21, 2022
be785f9
Merge remote-tracking branch 'origin/fix2403' into fix2403
tifecool Oct 21, 2022
6d87954
spelling fix
tifecool Oct 21, 2022
a2ae881
TypeIndex fix
tifecool Oct 21, 2022
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
24 changes: 14 additions & 10 deletions node/core/approval-voting/src/criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use polkadot_node_primitives::approval::{
self as approval_types, AssignmentCert, AssignmentCertKind, DelayTranche, RelayVRFStory,
};
use polkadot_primitives::v2::{
AssignmentId, AssignmentPair, CandidateHash, CoreIndex, GroupIndex, SessionInfo, ValidatorIndex,
AssignmentId, AssignmentPair, CandidateHash, CoreIndex, GroupIndex, IndexedVec, SessionInfo,
ValidatorIndex,
};
use sc_keystore::LocalKeystore;
use sp_application_crypto::ByteArray;
Expand Down Expand Up @@ -138,7 +139,7 @@ pub(crate) struct Config {
/// The assignment public keys for validators.
assignment_keys: Vec<AssignmentId>,
/// The groups of validators assigned to each core.
validator_groups: Vec<Vec<ValidatorIndex>>,
validator_groups: IndexedVec<GroupIndex, Vec<ValidatorIndex>>,
/// The number of availability cores used by the protocol during this session.
n_cores: u32,
/// The zeroth delay tranche width.
Expand Down Expand Up @@ -541,11 +542,11 @@ pub(crate) fn check_assignment_cert(
}

fn is_in_backing_group(
validator_groups: &[Vec<ValidatorIndex>],
validator_groups: &IndexedVec<GroupIndex, Vec<ValidatorIndex>>,
validator: ValidatorIndex,
group: GroupIndex,
) -> bool {
validator_groups.get(group.0 as usize).map_or(false, |g| g.contains(&validator))
validator_groups.get(group).map_or(false, |g| g.contains(&validator))
}

#[cfg(test)]
Expand Down Expand Up @@ -590,7 +591,10 @@ mod tests {
.collect()
}

fn basic_groups(n_validators: usize, n_groups: usize) -> Vec<Vec<ValidatorIndex>> {
fn basic_groups(
n_validators: usize,
n_groups: usize,
) -> IndexedVec<GroupIndex, Vec<ValidatorIndex>> {
let size = n_validators / n_groups;
let big_groups = n_validators % n_groups;
let scraps = n_groups * size;
Expand Down Expand Up @@ -631,10 +635,10 @@ mod tests {
Sr25519Keyring::Bob,
Sr25519Keyring::Charlie,
]),
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0)],
vec![ValidatorIndex(1), ValidatorIndex(2)],
],
]),
n_cores: 2,
zeroth_delay_tranche_width: 10,
relay_vrf_modulo_samples: 3,
Expand Down Expand Up @@ -666,10 +670,10 @@ mod tests {
Sr25519Keyring::Bob,
Sr25519Keyring::Charlie,
]),
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0)],
vec![ValidatorIndex(1), ValidatorIndex(2)],
],
]),
n_cores: 2,
zeroth_delay_tranche_width: 10,
relay_vrf_modulo_samples: 3,
Expand All @@ -696,7 +700,7 @@ mod tests {
Sr25519Keyring::Bob,
Sr25519Keyring::Charlie,
]),
validator_groups: vec![],
validator_groups: Default::default(),
n_cores: 0,
zeroth_delay_tranche_width: 10,
relay_vrf_modulo_samples: 3,
Expand Down
44 changes: 26 additions & 18 deletions node/core/approval-voting/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,9 @@ pub(crate) mod tests {
use polkadot_node_subsystem::messages::{AllMessages, ApprovalVotingMessage};
use polkadot_node_subsystem_test_helpers::make_subsystem_context;
use polkadot_node_subsystem_util::database::Database;
use polkadot_primitives::v2::{Id as ParaId, SessionInfo, ValidatorIndex};
use polkadot_primitives::v2::{
Id as ParaId, IndexedVec, SessionInfo, ValidatorId, ValidatorIndex,
};
pub(crate) use sp_consensus_babe::{
digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest},
AllowedSlots, BabeEpochConfiguration, Epoch as BabeEpoch,
Expand Down Expand Up @@ -713,10 +715,10 @@ pub(crate) mod tests {

fn dummy_session_info(index: SessionIndex) -> SessionInfo {
SessionInfo {
validators: Vec::new(),
validators: Default::default(),
discovery_keys: Vec::new(),
assignment_keys: Vec::new(),
validator_groups: Vec::new(),
validator_groups: Default::default(),
n_cores: index as _,
zeroth_delay_tranche_width: index as _,
relay_vrf_modulo_samples: index as _,
Expand Down Expand Up @@ -1174,21 +1176,27 @@ pub(crate) mod tests {

let session = 5;
let irrelevant = 666;
let session_info = SessionInfo {
validators: vec![Sr25519Keyring::Alice.public().into(); 6],
discovery_keys: Vec::new(),
assignment_keys: Vec::new(),
validator_groups: vec![vec![ValidatorIndex(0); 5], vec![ValidatorIndex(0); 2]],
n_cores: 6,
needed_approvals: 2,
zeroth_delay_tranche_width: irrelevant,
relay_vrf_modulo_samples: irrelevant,
n_delay_tranches: irrelevant,
no_show_slots: irrelevant,
active_validator_indices: Vec::new(),
dispute_period: 6,
random_seed: [0u8; 32],
};
let session_info =
SessionInfo {
validators: IndexedVec::<ValidatorIndex, ValidatorId>::from(
vec![Sr25519Keyring::Alice.public().into(); 6],
),
discovery_keys: Vec::new(),
assignment_keys: Vec::new(),
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0); 5],
vec![ValidatorIndex(0); 2],
]),
n_cores: 6,
needed_approvals: 2,
zeroth_delay_tranche_width: irrelevant,
relay_vrf_modulo_samples: irrelevant,
n_delay_tranches: irrelevant,
no_show_slots: irrelevant,
active_validator_indices: Vec::new(),
dispute_period: 6,
random_seed: [0u8; 32],
};

let slot = Slot::from(10);

Expand Down
4 changes: 2 additions & 2 deletions node/core/approval-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,7 @@ fn check_and_import_approval<T>(
)),
};

let pubkey = match session_info.validators.get(approval.validator.0 as usize) {
let pubkey = match session_info.validators.get(approval.validator) {
Some(k) => k,
None => respond_early!(ApprovalCheckResult::Bad(
ApprovalCheckError::InvalidValidatorIndex(approval.validator),
Expand Down Expand Up @@ -2503,7 +2503,7 @@ async fn issue_approval<Context>(
},
};

let validator_pubkey = match session_info.validators.get(validator_index.0 as usize) {
let validator_pubkey = match session_info.validators.get(validator_index) {
Some(p) => p,
None => {
gum::warn!(
Expand Down
35 changes: 19 additions & 16 deletions node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use polkadot_node_subsystem_test_helpers as test_helpers;
use polkadot_node_subsystem_util::TimeoutExt;
use polkadot_overseer::HeadSupportsParachains;
use polkadot_primitives::v2::{
CandidateCommitments, CandidateEvent, CoreIndex, GroupIndex, Header, Id as ParaId,
CandidateCommitments, CandidateEvent, CoreIndex, GroupIndex, Header, Id as ParaId, IndexedVec,
ValidationCode, ValidatorSignature,
};
use std::time::Duration;
Expand Down Expand Up @@ -739,7 +739,10 @@ fn session_info(keys: &[Sr25519Keyring]) -> SessionInfo {
validators: keys.iter().map(|v| v.public().into()).collect(),
discovery_keys: keys.iter().map(|v| v.public().into()).collect(),
assignment_keys: keys.iter().map(|v| v.public().into()).collect(),
validator_groups: vec![vec![ValidatorIndex(0)], vec![ValidatorIndex(1)]],
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0)],
vec![ValidatorIndex(1)],
]),
n_cores: keys.len() as _,
needed_approvals: 2,
zeroth_delay_tranche_width: 5,
Expand Down Expand Up @@ -1552,11 +1555,11 @@ fn subsystem_second_approval_import_only_schedules_wakeups() {
Sr25519Keyring::Eve,
];
let session_info = SessionInfo {
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0), ValidatorIndex(1)],
vec![ValidatorIndex(2)],
vec![ValidatorIndex(3), ValidatorIndex(4)],
],
]),
needed_approvals: 1,
..session_info(&validators)
};
Expand Down Expand Up @@ -1889,11 +1892,11 @@ fn import_checked_approval_updates_entries_and_schedules() {
Sr25519Keyring::Eve,
];
let session_info = SessionInfo {
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0), ValidatorIndex(1)],
vec![ValidatorIndex(2)],
vec![ValidatorIndex(3), ValidatorIndex(4)],
],
]),
..session_info(&validators)
};

Expand Down Expand Up @@ -2046,11 +2049,11 @@ fn subsystem_import_checked_approval_sets_one_block_bit_at_a_time() {
Sr25519Keyring::Eve,
];
let session_info = SessionInfo {
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0), ValidatorIndex(1)],
vec![ValidatorIndex(2)],
vec![ValidatorIndex(3), ValidatorIndex(4)],
],
]),
..session_info(&validators)
};

Expand Down Expand Up @@ -2336,11 +2339,11 @@ fn subsystem_validate_approvals_cache() {
Sr25519Keyring::Eve,
];
let session_info = SessionInfo {
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0), ValidatorIndex(1)],
vec![ValidatorIndex(2)],
vec![ValidatorIndex(3), ValidatorIndex(4)],
],
]),
..session_info(&validators)
};

Expand Down Expand Up @@ -2548,11 +2551,11 @@ where
Sr25519Keyring::Ferdie,
];
let session_info = SessionInfo {
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0), ValidatorIndex(1)],
vec![ValidatorIndex(2), ValidatorIndex(3)],
vec![ValidatorIndex(4), ValidatorIndex(5)],
],
]),
relay_vrf_modulo_samples: 2,
no_show_slots,
..session_info(&validators)
Expand Down Expand Up @@ -2868,11 +2871,11 @@ fn pre_covers_dont_stall_approval() {
Sr25519Keyring::One,
];
let session_info = SessionInfo {
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0), ValidatorIndex(1)],
vec![ValidatorIndex(2), ValidatorIndex(5)],
vec![ValidatorIndex(3), ValidatorIndex(4)],
],
]),
..session_info(&validators)
};

Expand Down Expand Up @@ -3045,11 +3048,11 @@ fn waits_until_approving_assignments_are_old_enough() {
Sr25519Keyring::One,
];
let session_info = SessionInfo {
validator_groups: vec![
validator_groups: IndexedVec::<GroupIndex, Vec<ValidatorIndex>>::from(vec![
vec![ValidatorIndex(0), ValidatorIndex(1)],
vec![ValidatorIndex(2), ValidatorIndex(5)],
vec![ValidatorIndex(3), ValidatorIndex(4)],
],
]),
..session_info(&validators)
};

Expand Down
12 changes: 6 additions & 6 deletions node/core/dispute-coordinator/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use polkadot_node_primitives::{CandidateVotes, SignedDisputeStatement};
use polkadot_node_subsystem_util::rolling_session_window::RollingSessionWindow;
use polkadot_primitives::v2::{
CandidateReceipt, DisputeStatement, SessionIndex, SessionInfo, ValidDisputeStatementKind,
ValidatorId, ValidatorIndex, ValidatorPair, ValidatorSignature,
CandidateReceipt, DisputeStatement, IndexedVec, SessionIndex, SessionInfo,
ValidDisputeStatementKind, ValidatorId, ValidatorIndex, ValidatorPair, ValidatorSignature,
};
use sc_keystore::LocalKeystore;

Expand Down Expand Up @@ -63,7 +63,7 @@ impl<'a> CandidateEnvironment<'a> {
}

/// Validators in the candidate's session.
pub fn validators(&self) -> &Vec<ValidatorId> {
pub fn validators(&self) -> &IndexedVec<ValidatorIndex, ValidatorId> {
&self.session.validators
}

Expand Down Expand Up @@ -229,7 +229,7 @@ impl CandidateVoteState<CandidateVotes> {
for (statement, val_index) in statements {
if env
.validators()
.get(val_index.0 as usize)
.get(val_index)
.map_or(true, |v| v != statement.validator_public())
{
gum::error!(
Expand Down Expand Up @@ -488,7 +488,7 @@ impl ImportResult {
for (index, sig) in approval_votes.into_iter() {
debug_assert!(
{
let pub_key = &env.session_info().validators[index.0 as usize];
let pub_key = &env.session_info().validators.get(index).expect("indices are validated by approval-voting subsystem; qed");
let candidate_hash = votes.candidate_receipt.hash();
let session_index = env.session_index();
DisputeStatement::Valid(ValidDisputeStatementKind::ApprovalChecking)
Expand Down Expand Up @@ -538,7 +538,7 @@ impl ImportResult {
/// That is all `ValidatorIndex`es we have private keys for. Usually this will only be one.
fn find_controlled_validator_indices(
keystore: &LocalKeystore,
validators: &[ValidatorId],
validators: &IndexedVec<ValidatorIndex, ValidatorId>,
) -> HashSet<ValidatorIndex> {
let mut controlled = HashSet::new();
for (index, validator) in validators.iter().enumerate() {
Expand Down
15 changes: 9 additions & 6 deletions node/core/dispute-coordinator/src/initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl Initialized {
.filter_map(|(validator_index, attestation)| {
let validator_public: ValidatorId = session_info
.validators
.get(validator_index.0 as usize)
.get(validator_index)
.or_else(|| {
gum::error!(
target: LOG_TARGET,
Expand Down Expand Up @@ -473,7 +473,7 @@ impl Initialized {

let validator_public: ValidatorId = session_info
.validators
.get(validator_index.0 as usize)
.get(validator_index)
.or_else(|| {
gum::error!(
target: LOG_TARGET,
Expand Down Expand Up @@ -903,7 +903,7 @@ impl Initialized {
let no_votes = Vec::new();
let our_approval_votes = new_state.own_approval_votes().unwrap_or(&no_votes);
for (validator_index, sig) in our_approval_votes {
let pub_key = match env.validators().get(validator_index.0 as usize) {
let pub_key = match env.validators().get(*validator_index) {
None => {
gum::error!(
target: LOG_TARGET,
Expand Down Expand Up @@ -1097,7 +1097,10 @@ impl Initialized {
valid,
candidate_hash,
session,
env.validators()[index.0 as usize].clone(),
env.validators()
.get(*index)
.expect("`controlled_indices` are derived from `validators`; qed")
.clone(),
)
.await;

Expand Down Expand Up @@ -1238,7 +1241,7 @@ fn make_dispute_message(
our_vote.candidate_hash().clone(),
our_vote.session_index(),
validators
.get(validator_index.0 as usize)
.get(*validator_index)
.ok_or(DisputeMessageCreationError::InvalidValidatorIndex)?
.clone(),
validator_signature.clone(),
Expand All @@ -1253,7 +1256,7 @@ fn make_dispute_message(
our_vote.candidate_hash().clone(),
our_vote.session_index(),
validators
.get(validator_index.0 as usize)
.get(*validator_index)
.ok_or(DisputeMessageCreationError::InvalidValidatorIndex)?
.clone(),
validator_signature.clone(),
Expand Down
Loading