From b5c02e89ba166c7b8ed3c3de6b5efe9bdddce9e2 Mon Sep 17 00:00:00 2001 From: Tife Date: Thu, 13 Oct 2022 17:54:51 +0100 Subject: [PATCH 01/41] Fix for issue #2403 --- node/primitives/src/disputes/message.rs | 8 +- .../src/rolling_session_window.rs | 6 +- node/subsystem-util/src/runtime/mod.rs | 6 +- primitives/src/v2/mod.rs | 81 ++++++++++++++++++- runtime/parachains/src/builder.rs | 18 ++--- runtime/parachains/src/disputes.rs | 2 +- runtime/parachains/src/disputes/slashing.rs | 2 +- runtime/parachains/src/session_info.rs | 4 +- runtime/parachains/src/session_info/tests.rs | 2 +- 9 files changed, 98 insertions(+), 31 deletions(-) diff --git a/node/primitives/src/disputes/message.rs b/node/primitives/src/disputes/message.rs index 88a65ab04015..fcdfdede421f 100644 --- a/node/primitives/src/disputes/message.rs +++ b/node/primitives/src/disputes/message.rs @@ -135,11 +135,11 @@ impl DisputeMessage { let valid_id = session_info .validators - .get(valid_index.0 as usize) + .get(valid_index) .ok_or(Error::ValidStatementInvalidValidatorIndex)?; let invalid_id = session_info .validators - .get(invalid_index.0 as usize) + .get(invalid_index) .ok_or(Error::InvalidStatementInvalidValidatorIndex)?; if valid_id != valid_statement.validator_public() { @@ -224,7 +224,7 @@ impl UncheckedDisputeMessage { let vote_valid = { let ValidDisputeVote { validator_index, signature, kind } = valid_vote; let validator_public = - session_info.validators.get(validator_index.0 as usize).ok_or(())?.clone(); + session_info.validators.get(validator_index).ok_or(())?.clone(); ( SignedDisputeStatement::new_checked( @@ -241,7 +241,7 @@ impl UncheckedDisputeMessage { let vote_invalid = { let InvalidDisputeVote { validator_index, signature, kind } = invalid_vote; let validator_public = - session_info.validators.get(validator_index.0 as usize).ok_or(())?.clone(); + session_info.validators.get(validator_index).ok_or(())?.clone(); ( SignedDisputeStatement::new_checked( diff --git a/node/subsystem-util/src/rolling_session_window.rs b/node/subsystem-util/src/rolling_session_window.rs index 0ff2dc6deb13..56d84e2241ba 100644 --- a/node/subsystem-util/src/rolling_session_window.rs +++ b/node/subsystem-util/src/rolling_session_window.rs @@ -392,17 +392,17 @@ mod tests { SubsystemContext, }; use polkadot_node_subsystem_test_helpers::make_subsystem_context; - use polkadot_primitives::v2::Header; + use polkadot_primitives::v2::{Header, Validators, GroupValidators}; use sp_core::testing::TaskExecutor; pub const TEST_WINDOW_SIZE: SessionWindowSize = new_session_window_size!(6); fn dummy_session_info(index: SessionIndex) -> SessionInfo { SessionInfo { - validators: Vec::new(), + validators: Validators::from(Vec::new()), discovery_keys: Vec::new(), assignment_keys: Vec::new(), - validator_groups: Vec::new(), + validator_groups: GroupValidators::from(Vec::new()), n_cores: index as _, zeroth_delay_tranche_width: index as _, relay_vrf_modulo_samples: index as _, diff --git a/node/subsystem-util/src/runtime/mod.rs b/node/subsystem-util/src/runtime/mod.rs index 7fcae2c57b09..e31139b9655c 100644 --- a/node/subsystem-util/src/runtime/mod.rs +++ b/node/subsystem-util/src/runtime/mod.rs @@ -207,10 +207,10 @@ impl RuntimeInfo { /// /// Returns: `None` if not a parachain validator. async fn get_validator_info(&self, session_info: &SessionInfo) -> Result { - if let Some(our_index) = self.get_our_index(&session_info.validators).await { + if let Some(our_index) = self.get_our_index(&session_info.validators.to_vec()).await { // Get our group index: let our_group = - session_info.validator_groups.iter().enumerate().find_map(|(i, g)| { + session_info.validator_groups.to_vec().iter().enumerate().find_map(|(i, g)| { g.iter().find_map(|v| { if *v == our_index { Some(GroupIndex(i as u32)) @@ -254,7 +254,7 @@ where session_info .validators - .get(signed.unchecked_validator_index().0 as usize) + .get(signed.unchecked_validator_index()) .ok_or_else(|| signed.clone()) .and_then(|v| signed.try_into_checked(&signing_context, v)) } diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index e31d0e4d36f9..519acbb576f0 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -20,6 +20,7 @@ use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_std::prelude::*; +use sp_std::ops::Index; use application_crypto::KeyTypeId; use inherents::InherentIdentifier; @@ -1569,6 +1570,78 @@ impl CompactStatement { } } +/// Validators struct indexed by ValidatorIndex. +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +#[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] +pub struct Validators (Vec); + +impl Index for Validators{ + type Output = ValidatorId; + + fn index(&self, index: ValidatorIndex) -> &Self::Output { + &self.0.get(index.0 as usize).unwrap() + } +} + +impl From> for Validators{ + fn from(validators: Vec) -> Self { + Validators(validators) + } +} + +impl Validators { + /// Returns a reference to an element indexed using ValidatorIndex. + pub fn get(&self, index: ValidatorIndex) -> Option<&ValidatorId>{ + self.0.get(index.0 as usize) + } + + ///Returns number of elements in vector. + pub fn len(&self) -> usize{ + self.0.len() + } + + ///Returns contained vector + pub fn to_vec(&self) -> Vec { + self.0.clone() + } +} + +/// GroupValidators struct indexed by GroupIndex. +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +#[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] +pub struct GroupValidators (Vec>); + +impl Index for GroupValidators{ + type Output = Vec; + + fn index(&self, index: GroupIndex) -> &Self::Output { + self.0.get(index.0 as usize).unwrap() + } +} + +impl From>> for GroupValidators{ + fn from(group_validators: Vec>) -> Self { + GroupValidators(group_validators) + } +} + +impl GroupValidators { + /// Returns a reference to an element indexed using GroupIndex. + pub fn get(&self, index: GroupIndex) -> Option<&Vec>{ + self.0.get(index.0 as usize) + } + + ///Returns number of elements in vector. + pub fn len(&self) -> usize{ + self.0.len() + } + + ///Returns contained vector + pub fn to_vec(&self) -> Vec> { + self.0.clone() + } +} + /// The maximum number of validators `f` which may safely be faulty. /// /// The total number of validators is `n = 3f + e` where `e in { 1, 2, 3 }`. @@ -1603,7 +1676,7 @@ pub struct SessionInfo { /// [`max_validators`](https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148). /// /// `SessionInfo::validators` will be limited to to `max_validators` when set. - pub validators: Vec, + pub validators: Validators, /// Validators' authority discovery keys for the session in canonical ordering. /// /// NOTE: The first `validators.len()` entries will match the corresponding validators in @@ -1626,7 +1699,7 @@ pub struct SessionInfo { /// Validators in shuffled ordering - these are the validator groups as produced /// by the `Scheduler` module for the session and are typically referred to by /// `GroupIndex`. - pub validator_groups: Vec>, + pub validator_groups: GroupValidators, /// The number of availability cores used by the protocol during this session. pub n_cores: u32, /// The zeroth delay tranche width. @@ -1679,7 +1752,7 @@ pub struct OldV1SessionInfo { /// [`max_validators`](https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148). /// /// `SessionInfo::validators` will be limited to to `max_validators` when set. - pub validators: Vec, + pub validators: Validators, /// Validators' authority discovery keys for the session in canonical ordering. /// /// NOTE: The first `validators.len()` entries will match the corresponding validators in @@ -1702,7 +1775,7 @@ pub struct OldV1SessionInfo { /// Validators in shuffled ordering - these are the validator groups as produced /// by the `Scheduler` module for the session and are typically referred to by /// `GroupIndex`. - pub validator_groups: Vec>, + pub validator_groups: GroupValidators, /// The number of availability cores used by the protocol during this session. pub n_cores: u32, /// The zeroth delay tranche width. diff --git a/runtime/parachains/src/builder.rs b/runtime/parachains/src/builder.rs index 42f54ecb3417..36097dc7a278 100644 --- a/runtime/parachains/src/builder.rs +++ b/runtime/parachains/src/builder.rs @@ -21,14 +21,7 @@ use crate::{ }; use bitvec::{order::Lsb0 as BitOrderLsb0, vec::BitVec}; use frame_support::pallet_prelude::*; -use primitives::v2::{ - collator_signature_payload, AvailabilityBitfield, BackedCandidate, CandidateCommitments, - CandidateDescriptor, CandidateHash, CollatorId, CollatorSignature, CommittedCandidateReceipt, - CompactStatement, CoreIndex, CoreOccupied, DisputeStatement, DisputeStatementSet, GroupIndex, - HeadData, Id as ParaId, InherentData as ParachainsInherentData, InvalidDisputeStatementKind, - PersistedValidationData, SessionIndex, SigningContext, UncheckedSigned, - ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, ValidityAttestation, -}; +use primitives::v2::{collator_signature_payload, AvailabilityBitfield, BackedCandidate, CandidateCommitments, CandidateDescriptor, CandidateHash, CollatorId, CollatorSignature, CommittedCandidateReceipt, CompactStatement, CoreIndex, CoreOccupied, DisputeStatement, DisputeStatementSet, GroupIndex, HeadData, Id as ParaId, InherentData as ParachainsInherentData, InvalidDisputeStatementKind, PersistedValidationData, SessionIndex, SigningContext, UncheckedSigned, ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, ValidityAttestation, Validators}; use sp_core::{sr25519, H256}; use sp_runtime::{ generic::Digest, @@ -65,7 +58,7 @@ fn byte32_slice_from(n: u32) -> [u8; 32] { /// Paras inherent `enter` benchmark scenario builder. pub(crate) struct BenchBuilder { /// Active validators. Validators should be declared prior to all other setup. - validators: Option>, + validators: Option, /// Starting block number; we expect it to get incremented on session setup. block_number: T::BlockNumber, /// Starting session; we expect it to get incremented on session setup. @@ -410,7 +403,7 @@ impl BenchBuilder { assert_eq!(>::session_index(), target_session); // We need to refetch validators since they have been shuffled. - let validators_shuffled: Vec<_> = session_info::Pallet::::session_info(target_session) + let validators_shuffled = session_info::Pallet::::session_info(target_session) .unwrap() .validators .clone(); @@ -438,6 +431,7 @@ impl BenchBuilder { let availability_bitvec = Self::availability_bitvec(concluding_cores, total_cores); let bitfields: Vec> = validators + .to_vec() .iter() .enumerate() .map(|(i, public)| { @@ -549,7 +543,7 @@ impl BenchBuilder { .iter() .take(*num_votes as usize) .map(|val_idx| { - let public = validators.get(val_idx.0 as usize).unwrap(); + let public = validators.get(*val_idx).unwrap(); let sig = UncheckedSigned::::benchmark_sign( public, CompactStatement::Valid(candidate_hash.clone()), @@ -606,7 +600,7 @@ impl BenchBuilder { self.dispute_statements.get(&seed).cloned().unwrap_or(validators.len() as u32); let statements = (0..statements_len) .map(|validator_index| { - let validator_public = &validators.get(validator_index as usize).expect("Test case is not borked. `ValidatorIndex` out of bounds of `ValidatorId`s."); + let validator_public = &validators.get(ValidatorIndex::from(validator_index)).expect("Test case is not borked. `ValidatorIndex` out of bounds of `ValidatorId`s."); // We need dispute statements on each side. And we don't want a revert log // so we make sure that we have a super majority with valid statements. diff --git a/runtime/parachains/src/disputes.rs b/runtime/parachains/src/disputes.rs index a26eccad0daf..d07c0b7df3d9 100644 --- a/runtime/parachains/src/disputes.rs +++ b/runtime/parachains/src/disputes.rs @@ -985,7 +985,7 @@ impl Pallet { let mut importer = DisputeStateImporter::new(dispute_state, now); for (i, (statement, validator_index, signature)) in set.statements.iter().enumerate() { // assure the validator index and is present in the session info - let validator_public = match session_info.validators.get(validator_index.0 as usize) + let validator_public = match session_info.validators.get(*validator_index) { None => { filter.remove_index(i); diff --git a/runtime/parachains/src/disputes/slashing.rs b/runtime/parachains/src/disputes/slashing.rs index fd6708de6ba0..2dfdc87c4b4e 100644 --- a/runtime/parachains/src/disputes/slashing.rs +++ b/runtime/parachains/src/disputes/slashing.rs @@ -257,7 +257,7 @@ where let keys = losers .into_iter() - .filter_map(|i| session_info.validators.get(i.0 as usize).cloned().map(|id| (i, id))) + .filter_map(|i| session_info.validators.get(i).cloned().map(|id| (i, id))) .collect(); let unapplied = PendingSlashes { keys, kind }; >::insert(session_index, candidate_hash, unapplied); diff --git a/runtime/parachains/src/session_info.rs b/runtime/parachains/src/session_info.rs index c903638423c2..cbd67a200a16 100644 --- a/runtime/parachains/src/session_info.rs +++ b/runtime/parachains/src/session_info.rs @@ -126,12 +126,12 @@ impl Pallet { let dispute_period = config.dispute_period; - let validators = notification.validators.clone(); + let validators = notification.validators.clone().into(); let discovery_keys = ::authorities(); let assignment_keys = AssignmentKeysUnsafe::::get(); let active_set = >::active_validator_indices(); - let validator_groups = >::validator_groups(); + let validator_groups = >::validator_groups().into(); let n_cores = >::availability_cores().len() as u32; let zeroth_delay_tranche_width = config.zeroth_delay_tranche_width; let relay_vrf_modulo_samples = config.relay_vrf_modulo_samples; diff --git a/runtime/parachains/src/session_info/tests.rs b/runtime/parachains/src/session_info/tests.rs index 168bd3d1cc27..7bf68736b572 100644 --- a/runtime/parachains/src/session_info/tests.rs +++ b/runtime/parachains/src/session_info/tests.rs @@ -201,7 +201,7 @@ fn session_info_active_subsets() { }); let session = Sessions::::get(&1).unwrap(); - assert_eq!(session.validators, validators); + assert_eq!(session.validators.to_vec(), validators); assert_eq!( session.discovery_keys, take_active_subset_and_inactive(&active_set, &unscrambled_discovery), From ce8aba9da63b52288eb2c252e1e63fc3e7a2240d Mon Sep 17 00:00:00 2001 From: Tife Date: Thu, 13 Oct 2022 18:38:28 +0100 Subject: [PATCH 02/41] Nightly fmt --- node/primitives/src/disputes/message.rs | 6 ++--- .../src/rolling_session_window.rs | 2 +- primitives/src/v2/mod.rs | 23 +++++++++---------- runtime/parachains/src/builder.rs | 10 +++++++- runtime/parachains/src/disputes.rs | 3 +-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/node/primitives/src/disputes/message.rs b/node/primitives/src/disputes/message.rs index fcdfdede421f..93d4e804f906 100644 --- a/node/primitives/src/disputes/message.rs +++ b/node/primitives/src/disputes/message.rs @@ -223,8 +223,7 @@ impl UncheckedDisputeMessage { let vote_valid = { let ValidDisputeVote { validator_index, signature, kind } = valid_vote; - let validator_public = - session_info.validators.get(validator_index).ok_or(())?.clone(); + let validator_public = session_info.validators.get(validator_index).ok_or(())?.clone(); ( SignedDisputeStatement::new_checked( @@ -240,8 +239,7 @@ impl UncheckedDisputeMessage { let vote_invalid = { let InvalidDisputeVote { validator_index, signature, kind } = invalid_vote; - let validator_public = - session_info.validators.get(validator_index).ok_or(())?.clone(); + let validator_public = session_info.validators.get(validator_index).ok_or(())?.clone(); ( SignedDisputeStatement::new_checked( diff --git a/node/subsystem-util/src/rolling_session_window.rs b/node/subsystem-util/src/rolling_session_window.rs index 56d84e2241ba..f3697fe2c2a9 100644 --- a/node/subsystem-util/src/rolling_session_window.rs +++ b/node/subsystem-util/src/rolling_session_window.rs @@ -392,7 +392,7 @@ mod tests { SubsystemContext, }; use polkadot_node_subsystem_test_helpers::make_subsystem_context; - use polkadot_primitives::v2::{Header, Validators, GroupValidators}; + use polkadot_primitives::v2::{GroupValidators, Header, Validators}; use sp_core::testing::TaskExecutor; pub const TEST_WINDOW_SIZE: SessionWindowSize = new_session_window_size!(6); diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 519acbb576f0..00c207ac9c11 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -19,8 +19,7 @@ use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; -use sp_std::prelude::*; -use sp_std::ops::Index; +use sp_std::{ops::Index, prelude::*}; use application_crypto::KeyTypeId; use inherents::InherentIdentifier; @@ -1573,9 +1572,9 @@ impl CompactStatement { /// Validators struct indexed by ValidatorIndex. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] -pub struct Validators (Vec); +pub struct Validators(Vec); -impl Index for Validators{ +impl Index for Validators { type Output = ValidatorId; fn index(&self, index: ValidatorIndex) -> &Self::Output { @@ -1583,7 +1582,7 @@ impl Index for Validators{ } } -impl From> for Validators{ +impl From> for Validators { fn from(validators: Vec) -> Self { Validators(validators) } @@ -1591,12 +1590,12 @@ impl From> for Validators{ impl Validators { /// Returns a reference to an element indexed using ValidatorIndex. - pub fn get(&self, index: ValidatorIndex) -> Option<&ValidatorId>{ + pub fn get(&self, index: ValidatorIndex) -> Option<&ValidatorId> { self.0.get(index.0 as usize) } ///Returns number of elements in vector. - pub fn len(&self) -> usize{ + pub fn len(&self) -> usize { self.0.len() } @@ -1609,9 +1608,9 @@ impl Validators { /// GroupValidators struct indexed by GroupIndex. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] -pub struct GroupValidators (Vec>); +pub struct GroupValidators(Vec>); -impl Index for GroupValidators{ +impl Index for GroupValidators { type Output = Vec; fn index(&self, index: GroupIndex) -> &Self::Output { @@ -1619,7 +1618,7 @@ impl Index for GroupValidators{ } } -impl From>> for GroupValidators{ +impl From>> for GroupValidators { fn from(group_validators: Vec>) -> Self { GroupValidators(group_validators) } @@ -1627,12 +1626,12 @@ impl From>> for GroupValidators{ impl GroupValidators { /// Returns a reference to an element indexed using GroupIndex. - pub fn get(&self, index: GroupIndex) -> Option<&Vec>{ + pub fn get(&self, index: GroupIndex) -> Option<&Vec> { self.0.get(index.0 as usize) } ///Returns number of elements in vector. - pub fn len(&self) -> usize{ + pub fn len(&self) -> usize { self.0.len() } diff --git a/runtime/parachains/src/builder.rs b/runtime/parachains/src/builder.rs index 36097dc7a278..b8c099e32728 100644 --- a/runtime/parachains/src/builder.rs +++ b/runtime/parachains/src/builder.rs @@ -21,7 +21,15 @@ use crate::{ }; use bitvec::{order::Lsb0 as BitOrderLsb0, vec::BitVec}; use frame_support::pallet_prelude::*; -use primitives::v2::{collator_signature_payload, AvailabilityBitfield, BackedCandidate, CandidateCommitments, CandidateDescriptor, CandidateHash, CollatorId, CollatorSignature, CommittedCandidateReceipt, CompactStatement, CoreIndex, CoreOccupied, DisputeStatement, DisputeStatementSet, GroupIndex, HeadData, Id as ParaId, InherentData as ParachainsInherentData, InvalidDisputeStatementKind, PersistedValidationData, SessionIndex, SigningContext, UncheckedSigned, ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, ValidityAttestation, Validators}; +use primitives::v2::{ + collator_signature_payload, AvailabilityBitfield, BackedCandidate, CandidateCommitments, + CandidateDescriptor, CandidateHash, CollatorId, CollatorSignature, CommittedCandidateReceipt, + CompactStatement, CoreIndex, CoreOccupied, DisputeStatement, DisputeStatementSet, GroupIndex, + HeadData, Id as ParaId, InherentData as ParachainsInherentData, InvalidDisputeStatementKind, + PersistedValidationData, SessionIndex, SigningContext, UncheckedSigned, + ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, Validators, + ValidityAttestation, +}; use sp_core::{sr25519, H256}; use sp_runtime::{ generic::Digest, diff --git a/runtime/parachains/src/disputes.rs b/runtime/parachains/src/disputes.rs index d07c0b7df3d9..9f458421e2ed 100644 --- a/runtime/parachains/src/disputes.rs +++ b/runtime/parachains/src/disputes.rs @@ -985,8 +985,7 @@ impl Pallet { let mut importer = DisputeStateImporter::new(dispute_state, now); for (i, (statement, validator_index, signature)) in set.statements.iter().enumerate() { // assure the validator index and is present in the session info - let validator_public = match session_info.validators.get(*validator_index) - { + let validator_public = match session_info.validators.get(*validator_index) { None => { filter.remove_index(i); continue From b5a7bfc401668752152b981825568a6c7b4b6273 Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 14 Oct 2022 18:27:38 +0100 Subject: [PATCH 03/41] Quick documentation fixes --- primitives/src/v2/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 00c207ac9c11..c6374590e666 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1594,12 +1594,12 @@ impl Validators { self.0.get(index.0 as usize) } - ///Returns number of elements in vector. + /// Returns number of elements in vector. pub fn len(&self) -> usize { self.0.len() } - ///Returns contained vector + /// Returns contained vector pub fn to_vec(&self) -> Vec { self.0.clone() } @@ -1630,12 +1630,12 @@ impl GroupValidators { self.0.get(index.0 as usize) } - ///Returns number of elements in vector. + /// Returns number of elements in vector. pub fn len(&self) -> usize { self.0.len() } - ///Returns contained vector + /// Returns contained vector pub fn to_vec(&self) -> Vec> { self.0.clone() } From 5da66936a07ebc3f6f8ce7ae03713f2bfd70fee5 Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 14 Oct 2022 19:48:12 +0100 Subject: [PATCH 04/41] Default Implementation --- node/subsystem-util/src/rolling_session_window.rs | 4 ++-- primitives/src/v2/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/node/subsystem-util/src/rolling_session_window.rs b/node/subsystem-util/src/rolling_session_window.rs index f3697fe2c2a9..3be681fbc75f 100644 --- a/node/subsystem-util/src/rolling_session_window.rs +++ b/node/subsystem-util/src/rolling_session_window.rs @@ -399,10 +399,10 @@ mod tests { fn dummy_session_info(index: SessionIndex) -> SessionInfo { SessionInfo { - validators: Validators::from(Vec::new()), + validators: Default::default(), discovery_keys: Vec::new(), assignment_keys: Vec::new(), - validator_groups: GroupValidators::from(Vec::new()), + validator_groups: Default::default(), n_cores: index as _, zeroth_delay_tranche_width: index as _, relay_vrf_modulo_samples: index as _, diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index c6374590e666..7b8544e73879 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1570,7 +1570,7 @@ impl CompactStatement { } /// Validators struct indexed by ValidatorIndex. -#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] pub struct Validators(Vec); @@ -1606,7 +1606,7 @@ impl Validators { } /// GroupValidators struct indexed by GroupIndex. -#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] pub struct GroupValidators(Vec>); From b90fc1e82c1d82defed09345692e0c7429c0f9ef Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 14 Oct 2022 20:06:33 +0100 Subject: [PATCH 05/41] iter() function integrated --- node/subsystem-util/src/runtime/mod.rs | 8 ++++---- primitives/src/v2/mod.rs | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/node/subsystem-util/src/runtime/mod.rs b/node/subsystem-util/src/runtime/mod.rs index e31139b9655c..791d67eb2f7d 100644 --- a/node/subsystem-util/src/runtime/mod.rs +++ b/node/subsystem-util/src/runtime/mod.rs @@ -29,7 +29,7 @@ use polkadot_node_subsystem::{messages::RuntimeApiMessage, overseer, SubsystemSe use polkadot_primitives::v2::{ CandidateEvent, CoreState, EncodeAs, GroupIndex, GroupRotationInfo, Hash, OccupiedCore, ScrapedOnChainVotes, SessionIndex, SessionInfo, Signed, SigningContext, UncheckedSigned, - ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, + ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, Validators, }; use crate::{ @@ -207,10 +207,10 @@ impl RuntimeInfo { /// /// Returns: `None` if not a parachain validator. async fn get_validator_info(&self, session_info: &SessionInfo) -> Result { - if let Some(our_index) = self.get_our_index(&session_info.validators.to_vec()).await { + if let Some(our_index) = self.get_our_index(&session_info.validators).await { // Get our group index: let our_group = - session_info.validator_groups.to_vec().iter().enumerate().find_map(|(i, g)| { + session_info.validator_groups.iter().enumerate().find_map(|(i, g)| { g.iter().find_map(|v| { if *v == our_index { Some(GroupIndex(i as u32)) @@ -228,7 +228,7 @@ impl RuntimeInfo { /// Get our `ValidatorIndex`. /// /// Returns: None if we are not a validator. - async fn get_our_index(&self, validators: &[ValidatorId]) -> Option { + async fn get_our_index(&self, validators: &Validators) -> Option { let keystore = self.keystore.as_ref()?; for (i, v) in validators.iter().enumerate() { if CryptoStore::has_keys(&**keystore, &[(v.to_raw_vec(), ValidatorId::ID)]).await { diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 7b8544e73879..3ee477df8742 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -20,6 +20,7 @@ use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_std::{ops::Index, prelude::*}; +use std::slice::Iter; use application_crypto::KeyTypeId; use inherents::InherentIdentifier; @@ -1603,6 +1604,11 @@ impl Validators { pub fn to_vec(&self) -> Vec { self.0.clone() } + + /// Returns Iterator of contained vector + pub fn iter(&self) -> Iter<'_, ValidatorId> { + self.0.iter() + } } /// GroupValidators struct indexed by GroupIndex. @@ -1639,6 +1645,11 @@ impl GroupValidators { pub fn to_vec(&self) -> Vec> { self.0.clone() } + + /// Returns Iterator of contained vector + pub fn iter(&self) -> Iter<'_, Vec> { + self.0.iter() + } } /// The maximum number of validators `f` which may safely be faulty. From 557eebb5dde4d83142b1f130670dd72d8795f8b6 Mon Sep 17 00:00:00 2001 From: Tife Date: Sat, 15 Oct 2022 01:25:56 +0100 Subject: [PATCH 06/41] Implemented iter functionalities --- .../src/tests/mock.rs | 8 +-- primitives/src/v2/mod.rs | 54 +++++++++++++++++-- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/node/network/availability-distribution/src/tests/mock.rs b/node/network/availability-distribution/src/tests/mock.rs index 86be0a1e230d..f3391d60ec52 100644 --- a/node/network/availability-distribution/src/tests/mock.rs +++ b/node/network/availability-distribution/src/tests/mock.rs @@ -22,11 +22,7 @@ use sp_keyring::Sr25519Keyring; use polkadot_erasure_coding::{branches, obtain_chunks_v1 as obtain_chunks}; use polkadot_node_primitives::{AvailableData, BlockData, ErasureChunk, PoV, Proof}; -use polkadot_primitives::v2::{ - CandidateCommitments, CandidateDescriptor, CandidateHash, CommittedCandidateReceipt, - GroupIndex, Hash, HeadData, Id as ParaId, OccupiedCore, PersistedValidationData, SessionInfo, - ValidatorIndex, -}; +use polkadot_primitives::v2::{CandidateCommitments, CandidateDescriptor, CandidateHash, CommittedCandidateReceipt, GroupIndex, GroupValidators, Hash, HeadData, Id as ParaId, OccupiedCore, PersistedValidationData, SessionInfo, ValidatorIndex}; use polkadot_primitives_test_helpers::{ dummy_collator, dummy_collator_signature, dummy_hash, dummy_validation_code, }; @@ -43,7 +39,7 @@ pub fn make_session_info() -> SessionInfo { Sr25519Keyring::One, ]; - let validator_groups: Vec> = [vec![5, 0, 3], vec![1, 6, 2, 4]] + let validator_groups: GroupValidators = [vec![5, 0, 3], vec![1, 6, 2, 4]] .iter() .map(|g| g.into_iter().map(|v| ValidatorIndex(*v)).collect()) .collect(); diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 3ee477df8742..1f1c8c6adace 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -16,11 +16,13 @@ //! `V1` Primitives. +use sp_std::slice::IterMut; +use sp_std::vec::IntoIter; use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_std::{ops::Index, prelude::*}; -use std::slice::Iter; +use sp_std::slice::Iter; use application_crypto::KeyTypeId; use inherents::InherentIdentifier; @@ -1589,6 +1591,17 @@ impl From> for Validators { } } +impl FromIterator for Validators{ + fn from_iter>(iter: T) -> Self { + let mut validators = Vec::new(); + + for i in iter{ + validators.push(i); + } + Validators(validators) + } +} + impl Validators { /// Returns a reference to an element indexed using ValidatorIndex. pub fn get(&self, index: ValidatorIndex) -> Option<&ValidatorId> { @@ -1600,15 +1613,25 @@ impl Validators { self.0.len() } - /// Returns contained vector + /// Returns contained vector. pub fn to_vec(&self) -> Vec { self.0.clone() } - /// Returns Iterator of contained vector + /// Returns Iterator of contained vector. pub fn iter(&self) -> Iter<'_, ValidatorId> { self.0.iter() } + + /// Returns a mutable Iterator. + pub fn iter_mut(&mut self) -> IterMut<'_, ValidatorId> { + self.0.iter_mut() + } + + /// Returns an Iterator with ownership. + pub fn into_iter(self) -> IntoIter { + self.0.into_iter() + } } /// GroupValidators struct indexed by GroupIndex. @@ -1630,6 +1653,17 @@ impl From>> for GroupValidators { } } +impl FromIterator> for GroupValidators{ + fn from_iter>>(iter: T) -> Self { + let mut validator_groups = Vec::new(); + + for i in iter{ + validator_groups.push(i); + } + GroupValidators(validator_groups) + } +} + impl GroupValidators { /// Returns a reference to an element indexed using GroupIndex. pub fn get(&self, index: GroupIndex) -> Option<&Vec> { @@ -1641,15 +1675,25 @@ impl GroupValidators { self.0.len() } - /// Returns contained vector + /// Returns contained vector. pub fn to_vec(&self) -> Vec> { self.0.clone() } - /// Returns Iterator of contained vector + /// Returns Iterator of contained vector. pub fn iter(&self) -> Iter<'_, Vec> { self.0.iter() } + + /// Returns a mutable Iterator. + pub fn iter_mut(&mut self) -> IterMut<'_, Vec> { + self.0.iter_mut() + } + + /// Returns an Iterator with ownership. + pub fn into_iter(self) -> IntoIter> { + self.0.into_iter() + } } /// The maximum number of validators `f` which may safely be faulty. From 09cc7653e007d4726348b7647bd9734150917d15 Mon Sep 17 00:00:00 2001 From: Tife Date: Sat, 15 Oct 2022 01:42:33 +0100 Subject: [PATCH 07/41] Fmt --- .../src/tests/mock.rs | 6 ++++- primitives/src/v2/mod.rs | 22 ++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/node/network/availability-distribution/src/tests/mock.rs b/node/network/availability-distribution/src/tests/mock.rs index f3391d60ec52..794b23ac3996 100644 --- a/node/network/availability-distribution/src/tests/mock.rs +++ b/node/network/availability-distribution/src/tests/mock.rs @@ -22,7 +22,11 @@ use sp_keyring::Sr25519Keyring; use polkadot_erasure_coding::{branches, obtain_chunks_v1 as obtain_chunks}; use polkadot_node_primitives::{AvailableData, BlockData, ErasureChunk, PoV, Proof}; -use polkadot_primitives::v2::{CandidateCommitments, CandidateDescriptor, CandidateHash, CommittedCandidateReceipt, GroupIndex, GroupValidators, Hash, HeadData, Id as ParaId, OccupiedCore, PersistedValidationData, SessionInfo, ValidatorIndex}; +use polkadot_primitives::v2::{ + CandidateCommitments, CandidateDescriptor, CandidateHash, CommittedCandidateReceipt, + GroupIndex, GroupValidators, Hash, HeadData, Id as ParaId, OccupiedCore, + PersistedValidationData, SessionInfo, ValidatorIndex, +}; use polkadot_primitives_test_helpers::{ dummy_collator, dummy_collator_signature, dummy_hash, dummy_validation_code, }; diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 1f1c8c6adace..c8fca86dabc6 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -16,13 +16,15 @@ //! `V1` Primitives. -use sp_std::slice::IterMut; -use sp_std::vec::IntoIter; use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; -use sp_std::{ops::Index, prelude::*}; -use sp_std::slice::Iter; +use sp_std::{ + ops::Index, + prelude::*, + slice::{Iter, IterMut}, + vec::IntoIter, +}; use application_crypto::KeyTypeId; use inherents::InherentIdentifier; @@ -1591,11 +1593,11 @@ impl From> for Validators { } } -impl FromIterator for Validators{ - fn from_iter>(iter: T) -> Self { +impl FromIterator for Validators { + fn from_iter>(iter: T) -> Self { let mut validators = Vec::new(); - for i in iter{ + for i in iter { validators.push(i); } Validators(validators) @@ -1653,11 +1655,11 @@ impl From>> for GroupValidators { } } -impl FromIterator> for GroupValidators{ - fn from_iter>>(iter: T) -> Self { +impl FromIterator> for GroupValidators { + fn from_iter>>(iter: T) -> Self { let mut validator_groups = Vec::new(); - for i in iter{ + for i in iter { validator_groups.push(i); } GroupValidators(validator_groups) From 3a6e3279dd2380248af9a5e54fe8ffbe09bbf0b7 Mon Sep 17 00:00:00 2001 From: Tife Date: Sat, 15 Oct 2022 01:56:34 +0100 Subject: [PATCH 08/41] small change --- runtime/parachains/src/builder.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/parachains/src/builder.rs b/runtime/parachains/src/builder.rs index b8c099e32728..045fb27857ec 100644 --- a/runtime/parachains/src/builder.rs +++ b/runtime/parachains/src/builder.rs @@ -439,7 +439,6 @@ impl BenchBuilder { let availability_bitvec = Self::availability_bitvec(concluding_cores, total_cores); let bitfields: Vec> = validators - .to_vec() .iter() .enumerate() .map(|(i, public)| { From aba583708764cde2f037914c961bb0267a51e2f1 Mon Sep 17 00:00:00 2001 From: Tife Date: Sat, 15 Oct 2022 23:54:05 +0100 Subject: [PATCH 09/41] updates node-network --- .../network/dispute-distribution/src/sender/mod.rs | 4 ++-- .../network/dispute-distribution/src/tests/mock.rs | 6 +++--- node/network/statement-distribution/src/lib.rs | 14 +++++--------- node/network/statement-distribution/src/tests.rs | 8 ++++---- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/node/network/dispute-distribution/src/sender/mod.rs b/node/network/dispute-distribution/src/sender/mod.rs index 09b902173ede..331fe12c61e2 100644 --- a/node/network/dispute-distribution/src/sender/mod.rs +++ b/node/network/dispute-distribution/src/sender/mod.rs @@ -299,7 +299,7 @@ impl DisputeSender { let valid_public = info .session_info .validators - .get(valid_index.0 as usize) + .get(*valid_index) .ok_or(JfyiError::InvalidStatementFromCoordinator)?; let valid_signed = SignedDisputeStatement::new_checked( DisputeStatement::Valid(kind.clone()), @@ -314,7 +314,7 @@ impl DisputeSender { let invalid_public = info .session_info .validators - .get(invalid_index.0 as usize) + .get(*invalid_index) .ok_or(JfyiError::InvalidValidatorIndexFromCoordinator)?; let invalid_signed = SignedDisputeStatement::new_checked( DisputeStatement::Invalid(kind.clone()), diff --git a/node/network/dispute-distribution/src/tests/mock.rs b/node/network/dispute-distribution/src/tests/mock.rs index aa2a4485d480..bc64734d57a0 100644 --- a/node/network/dispute-distribution/src/tests/mock.rs +++ b/node/network/dispute-distribution/src/tests/mock.rs @@ -84,7 +84,7 @@ pub static ref MOCK_SESSION_INFO: SessionInfo = .map(|k| MOCK_VALIDATORS_DISCOVERY_KEYS.get(&k).unwrap().clone()) .collect(), assignment_keys: vec![], - validator_groups: vec![], + validator_groups: Default::default(), n_cores: 0, zeroth_delay_tranche_width: 0, relay_vrf_modulo_samples: 0, @@ -104,9 +104,9 @@ pub static ref MOCK_NEXT_SESSION_INFO: SessionInfo = .iter() .map(|k| MOCK_VALIDATORS_DISCOVERY_KEYS.get(&k).unwrap().clone()) .collect(), - validators: vec![], + validators: Default::default(), assignment_keys: vec![], - validator_groups: vec![], + validator_groups: Default::default(), n_cores: 0, zeroth_delay_tranche_width: 0, relay_vrf_modulo_samples: 0, diff --git a/node/network/statement-distribution/src/lib.rs b/node/network/statement-distribution/src/lib.rs index 17ca5d8ea4ac..c61b653e9684 100644 --- a/node/network/statement-distribution/src/lib.rs +++ b/node/network/statement-distribution/src/lib.rs @@ -45,11 +45,7 @@ use polkadot_node_subsystem::{ overseer, ActiveLeavesUpdate, FromOrchestra, OverseerSignal, PerLeafSpan, SpawnedSubsystem, SubsystemError, }; -use polkadot_primitives::v2::{ - AuthorityDiscoveryId, CandidateHash, CommittedCandidateReceipt, CompactStatement, Hash, - SignedStatement, SigningContext, UncheckedSignedStatement, ValidatorId, ValidatorIndex, - ValidatorSignature, -}; +use polkadot_primitives::v2::{AuthorityDiscoveryId, CandidateHash, CommittedCandidateReceipt, CompactStatement, Hash, SignedStatement, SigningContext, UncheckedSignedStatement, ValidatorIndex, Validators, ValidatorSignature}; use futures::{ channel::{mpsc, oneshot}, @@ -665,7 +661,7 @@ struct ActiveHeadData { /// Large statements we are waiting for with associated meta data. waiting_large_statements: HashMap, /// The parachain validators at the head's child session index. - validators: Vec, + validators: Validators, /// The current session index of this fork. session_index: sp_staking::SessionIndex, /// How many `Seconded` statements we've seen per validator. @@ -676,7 +672,7 @@ struct ActiveHeadData { impl ActiveHeadData { fn new( - validators: Vec, + validators: Validators, session_index: sp_staking::SessionIndex, span: PerLeafSpan, ) -> Self { @@ -878,7 +874,7 @@ fn check_statement_signature( SigningContext { session_index: head.session_index, parent_hash: relay_parent }; head.validators - .get(statement.unchecked_validator_index().0 as usize) + .get(statement.unchecked_validator_index()) .ok_or_else(|| statement.clone()) .and_then(|v| statement.try_into_checked(&signing_context, v)) } @@ -2072,7 +2068,7 @@ impl StatementDistributionSubsystem { // directly: let group_peers = { if let Some(our_group) = validator_info.our_group { - let our_group = &session_info.validator_groups[our_group.0 as usize]; + let our_group = &session_info.validator_groups[our_group]; our_group .into_iter() diff --git a/node/network/statement-distribution/src/tests.rs b/node/network/statement-distribution/src/tests.rs index f3b9db00aef4..a5729afd467d 100644 --- a/node/network/statement-distribution/src/tests.rs +++ b/node/network/statement-distribution/src/tests.rs @@ -35,7 +35,7 @@ use polkadot_node_subsystem::{ ActivatedLeaf, LeafStatus, }; use polkadot_node_subsystem_test_helpers::mock::make_ferdie_keystore; -use polkadot_primitives::v2::{Hash, Id as ParaId, SessionInfo, ValidationCode}; +use polkadot_primitives::v2::{GroupValidators, Hash, Id as ParaId, SessionInfo, ValidationCode, ValidatorId}; use polkadot_primitives_test_helpers::{ dummy_committed_candidate_receipt, dummy_hash, AlwaysZeroRng, }; @@ -83,7 +83,7 @@ fn active_head_accepts_only_2_seconded_per_validator() { }; let mut head_data = ActiveHeadData::new( - validators, + Validators::from(validators), session_index, PerLeafSpan::new(Arc::new(jaeger::Span::Disabled), "test"), ); @@ -429,7 +429,7 @@ fn peer_view_update_sends_messages() { let new_head_data = { let mut data = ActiveHeadData::new( - validators, + Validators::from(validators), session_index, PerLeafSpan::new(Arc::new(jaeger::Span::Disabled), "test"), ); @@ -2319,7 +2319,7 @@ fn handle_multiple_seconded_statements() { } fn make_session_info(validators: Vec, groups: Vec>) -> SessionInfo { - let validator_groups: Vec> = groups + let validator_groups: GroupValidators = groups .iter() .map(|g| g.into_iter().map(|v| ValidatorIndex(*v)).collect()) .collect(); From dbac3888857373a0b4086b52019af0a9d691fa95 Mon Sep 17 00:00:00 2001 From: Tife Date: Sun, 16 Oct 2022 19:55:21 +0100 Subject: [PATCH 10/41] updates in dispute-coordinator --- node/core/dispute-coordinator/src/import.rs | 13 +++++-------- .../core/dispute-coordinator/src/initialized.rs | 12 ++++++------ node/core/dispute-coordinator/src/tests.rs | 17 ++++++----------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/node/core/dispute-coordinator/src/import.rs b/node/core/dispute-coordinator/src/import.rs index 020d04792191..1dda94968363 100644 --- a/node/core/dispute-coordinator/src/import.rs +++ b/node/core/dispute-coordinator/src/import.rs @@ -30,10 +30,7 @@ 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, -}; +use polkadot_primitives::v2::{CandidateReceipt, DisputeStatement, SessionIndex, SessionInfo, ValidDisputeStatementKind, ValidatorIndex, ValidatorPair, ValidatorSignature, Validators}; use sc_keystore::LocalKeystore; use crate::LOG_TARGET; @@ -63,7 +60,7 @@ impl<'a> CandidateEnvironment<'a> { } /// Validators in the candidate's session. - pub fn validators(&self) -> &Vec { + pub fn validators(&self) -> &Validators { &self.session.validators } @@ -229,7 +226,7 @@ impl CandidateVoteState { 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!( @@ -488,7 +485,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[index]; let candidate_hash = votes.candidate_receipt.hash(); let session_index = env.session_index(); DisputeStatement::Valid(ValidDisputeStatementKind::ApprovalChecking) @@ -538,7 +535,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: &Validators, ) -> HashSet { let mut controlled = HashSet::new(); for (index, validator) in validators.iter().enumerate() { diff --git a/node/core/dispute-coordinator/src/initialized.rs b/node/core/dispute-coordinator/src/initialized.rs index 5f29245f33f8..07b74cb2a684 100644 --- a/node/core/dispute-coordinator/src/initialized.rs +++ b/node/core/dispute-coordinator/src/initialized.rs @@ -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, @@ -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, @@ -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, @@ -1097,7 +1097,7 @@ impl Initialized { valid, candidate_hash, session, - env.validators()[index.0 as usize].clone(), + env.validators()[*index].clone(), ) .await; @@ -1238,7 +1238,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(), @@ -1253,7 +1253,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(), diff --git a/node/core/dispute-coordinator/src/tests.rs b/node/core/dispute-coordinator/src/tests.rs index aaef00999259..c2874e6d5011 100644 --- a/node/core/dispute-coordinator/src/tests.rs +++ b/node/core/dispute-coordinator/src/tests.rs @@ -56,12 +56,7 @@ use polkadot_node_subsystem::{ ActivatedLeaf, ActiveLeavesUpdate, LeafStatus, }; use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; -use polkadot_primitives::v2::{ - ApprovalVote, BlockNumber, CandidateCommitments, CandidateHash, CandidateReceipt, - DisputeStatement, Hash, Header, MultiDisputeStatementSet, ScrapedOnChainVotes, SessionIndex, - SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorId, ValidatorIndex, - ValidatorSignature, -}; +use polkadot_primitives::v2::{ApprovalVote, BlockNumber, CandidateCommitments, CandidateHash, CandidateReceipt, DisputeStatement, Hash, Header, MultiDisputeStatementSet, ScrapedOnChainVotes, SessionIndex, SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorId, ValidatorIndex, ValidatorSignature, Validators, GroupValidators}; use crate::{ backend::Backend, @@ -125,8 +120,8 @@ impl MockClock { struct TestState { validators: Vec, - validator_public: Vec, - validator_groups: Vec>, + validator_public: Validators, + validator_groups: GroupValidators, master_keystore: Arc, subsystem_keystore: Arc, db: Arc, @@ -163,11 +158,11 @@ impl Default for TestState { .map(|k| ValidatorId::from(k.0.public())) .collect(); - let validator_groups = vec![ + let validator_groups = GroupValidators::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(3)], vec![ValidatorIndex(4), ValidatorIndex(5), ValidatorIndex(6)], - ]; + ]); let master_keystore = make_keystore(validators.iter().map(|v| v.1.clone())).into(); let subsystem_keystore = @@ -431,7 +426,7 @@ impl TestState { session: SessionIndex, valid: bool, ) -> SignedDisputeStatement { - let public = self.validator_public[index.0 as usize].clone(); + let public = self.validator_public[index].clone(); let keystore = self.master_keystore.clone() as SyncCryptoStorePtr; From f1b8b4f7ede3d9ed5bf13ad5eec0c25d12c4a5aa Mon Sep 17 00:00:00 2001 From: Tife Date: Tue, 18 Oct 2022 15:33:00 +0100 Subject: [PATCH 11/41] Updates --- node/core/approval-voting/src/criteria.rs | 21 ++++++----- node/core/approval-voting/src/import.rs | 15 +++++--- node/core/approval-voting/src/lib.rs | 4 +- node/core/approval-voting/src/tests.rs | 37 ++++++++++--------- node/core/dispute-coordinator/src/import.rs | 5 ++- node/core/dispute-coordinator/src/tests.rs | 7 +++- node/network/availability-recovery/src/lib.rs | 6 +-- .../availability-recovery/src/tests.rs | 10 +++-- .../src/collator_side/mod.rs | 6 +-- .../src/collator_side/tests.rs | 10 ++--- .../network/statement-distribution/src/lib.rs | 6 ++- .../statement-distribution/src/tests.rs | 4 +- primitives/src/v2/mod.rs | 10 +++++ 13 files changed, 87 insertions(+), 54 deletions(-) diff --git a/node/core/approval-voting/src/criteria.rs b/node/core/approval-voting/src/criteria.rs index d7bedc9b0a7f..fd675ec805e4 100644 --- a/node/core/approval-voting/src/criteria.rs +++ b/node/core/approval-voting/src/criteria.rs @@ -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, GroupValidators, + SessionInfo, ValidatorIndex, }; use sc_keystore::LocalKeystore; use sp_application_crypto::ByteArray; @@ -138,7 +139,7 @@ pub(crate) struct Config { /// The assignment public keys for validators. assignment_keys: Vec, /// The groups of validators assigned to each core. - validator_groups: Vec>, + validator_groups: GroupValidators, /// The number of availability cores used by the protocol during this session. n_cores: u32, /// The zeroth delay tranche width. @@ -541,11 +542,11 @@ pub(crate) fn check_assignment_cert( } fn is_in_backing_group( - validator_groups: &[Vec], + validator_groups: &GroupValidators, 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)] @@ -590,7 +591,7 @@ mod tests { .collect() } - fn basic_groups(n_validators: usize, n_groups: usize) -> Vec> { + fn basic_groups(n_validators: usize, n_groups: usize) -> GroupValidators { let size = n_validators / n_groups; let big_groups = n_validators % n_groups; let scraps = n_groups * size; @@ -631,10 +632,10 @@ mod tests { Sr25519Keyring::Bob, Sr25519Keyring::Charlie, ]), - validator_groups: vec![ + validator_groups: GroupValidators::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1), ValidatorIndex(2)], - ], + ]), n_cores: 2, zeroth_delay_tranche_width: 10, relay_vrf_modulo_samples: 3, @@ -666,10 +667,10 @@ mod tests { Sr25519Keyring::Bob, Sr25519Keyring::Charlie, ]), - validator_groups: vec![ + validator_groups: GroupValidators::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1), ValidatorIndex(2)], - ], + ]), n_cores: 2, zeroth_delay_tranche_width: 10, relay_vrf_modulo_samples: 3, @@ -696,7 +697,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, diff --git a/node/core/approval-voting/src/import.rs b/node/core/approval-voting/src/import.rs index 5413c271e0d6..c612b858badd 100644 --- a/node/core/approval-voting/src/import.rs +++ b/node/core/approval-voting/src/import.rs @@ -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::{ + GroupValidators, Id as ParaId, SessionInfo, ValidatorIndex, Validators, + }; pub(crate) use sp_consensus_babe::{ digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest}, AllowedSlots, BabeEpochConfiguration, Epoch as BabeEpoch, @@ -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 _, @@ -1175,10 +1177,13 @@ pub(crate) mod tests { let session = 5; let irrelevant = 666; let session_info = SessionInfo { - validators: vec![Sr25519Keyring::Alice.public().into(); 6], + validators: Validators::from(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]], + validator_groups: GroupValidators::from(vec![ + vec![ValidatorIndex(0); 5], + vec![ValidatorIndex(0); 2], + ]), n_cores: 6, needed_approvals: 2, zeroth_delay_tranche_width: irrelevant, diff --git a/node/core/approval-voting/src/lib.rs b/node/core/approval-voting/src/lib.rs index 467d8be612e9..cff420fa46b6 100644 --- a/node/core/approval-voting/src/lib.rs +++ b/node/core/approval-voting/src/lib.rs @@ -1803,7 +1803,7 @@ fn check_and_import_approval( )), }; - 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), @@ -2503,7 +2503,7 @@ async fn issue_approval( }, }; - 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!( diff --git a/node/core/approval-voting/src/tests.rs b/node/core/approval-voting/src/tests.rs index bdb7a8c929b3..e39c2abd3ea0 100644 --- a/node/core/approval-voting/src/tests.rs +++ b/node/core/approval-voting/src/tests.rs @@ -32,8 +32,8 @@ 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, - ValidationCode, ValidatorSignature, + CandidateCommitments, CandidateEvent, CoreIndex, GroupIndex, GroupValidators, Header, + Id as ParaId, ValidationCode, ValidatorSignature, }; use std::time::Duration; @@ -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: GroupValidators::from(vec![ + vec![ValidatorIndex(0)], + vec![ValidatorIndex(1)], + ]), n_cores: keys.len() as _, needed_approvals: 2, zeroth_delay_tranche_width: 5, @@ -1552,11 +1555,11 @@ fn subsystem_second_approval_import_only_schedules_wakeups() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: vec![ + validator_groups: GroupValidators::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], - ], + ]), needed_approvals: 1, ..session_info(&validators) }; @@ -1889,11 +1892,11 @@ fn import_checked_approval_updates_entries_and_schedules() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: vec![ + validator_groups: GroupValidators::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], - ], + ]), ..session_info(&validators) }; @@ -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: GroupValidators::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], - ], + ]), ..session_info(&validators) }; @@ -2336,11 +2339,11 @@ fn subsystem_validate_approvals_cache() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: vec![ + validator_groups: GroupValidators::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], - ], + ]), ..session_info(&validators) }; @@ -2548,11 +2551,11 @@ where Sr25519Keyring::Ferdie, ]; let session_info = SessionInfo { - validator_groups: vec![ + validator_groups: GroupValidators::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) @@ -2868,11 +2871,11 @@ fn pre_covers_dont_stall_approval() { Sr25519Keyring::One, ]; let session_info = SessionInfo { - validator_groups: vec![ + validator_groups: GroupValidators::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(5)], vec![ValidatorIndex(3), ValidatorIndex(4)], - ], + ]), ..session_info(&validators) }; @@ -3045,11 +3048,11 @@ fn waits_until_approving_assignments_are_old_enough() { Sr25519Keyring::One, ]; let session_info = SessionInfo { - validator_groups: vec![ + validator_groups: GroupValidators::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(5)], vec![ValidatorIndex(3), ValidatorIndex(4)], - ], + ]), ..session_info(&validators) }; diff --git a/node/core/dispute-coordinator/src/import.rs b/node/core/dispute-coordinator/src/import.rs index 1dda94968363..44799ef70ac9 100644 --- a/node/core/dispute-coordinator/src/import.rs +++ b/node/core/dispute-coordinator/src/import.rs @@ -30,7 +30,10 @@ 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, ValidatorIndex, ValidatorPair, ValidatorSignature, Validators}; +use polkadot_primitives::v2::{ + CandidateReceipt, DisputeStatement, SessionIndex, SessionInfo, ValidDisputeStatementKind, + ValidatorIndex, ValidatorPair, ValidatorSignature, Validators, +}; use sc_keystore::LocalKeystore; use crate::LOG_TARGET; diff --git a/node/core/dispute-coordinator/src/tests.rs b/node/core/dispute-coordinator/src/tests.rs index c2874e6d5011..14a44a511c0a 100644 --- a/node/core/dispute-coordinator/src/tests.rs +++ b/node/core/dispute-coordinator/src/tests.rs @@ -56,7 +56,12 @@ use polkadot_node_subsystem::{ ActivatedLeaf, ActiveLeavesUpdate, LeafStatus, }; use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; -use polkadot_primitives::v2::{ApprovalVote, BlockNumber, CandidateCommitments, CandidateHash, CandidateReceipt, DisputeStatement, Hash, Header, MultiDisputeStatementSet, ScrapedOnChainVotes, SessionIndex, SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorId, ValidatorIndex, ValidatorSignature, Validators, GroupValidators}; +use polkadot_primitives::v2::{ + ApprovalVote, BlockNumber, CandidateCommitments, CandidateHash, CandidateReceipt, + DisputeStatement, GroupValidators, Hash, Header, MultiDisputeStatementSet, ScrapedOnChainVotes, + SessionIndex, SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorId, + ValidatorIndex, ValidatorSignature, Validators, +}; use crate::{ backend::Backend, diff --git a/node/network/availability-recovery/src/lib.rs b/node/network/availability-recovery/src/lib.rs index f2f92cc54490..111393e2c817 100644 --- a/node/network/availability-recovery/src/lib.rs +++ b/node/network/availability-recovery/src/lib.rs @@ -58,7 +58,7 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_util::request_session_info; use polkadot_primitives::v2::{ AuthorityDiscoveryId, BlakeTwo256, BlockNumber, CandidateHash, CandidateReceipt, GroupIndex, - Hash, HashT, SessionIndex, SessionInfo, ValidatorId, ValidatorIndex, + Hash, HashT, SessionIndex, SessionInfo, ValidatorIndex, Validators, }; mod error; @@ -134,7 +134,7 @@ struct RecoveryParams { validator_authority_keys: Vec, /// Validators relevant to this `RecoveryTask`. - validators: Vec, + validators: Validators, /// The number of pieces needed. threshold: usize, @@ -871,7 +871,7 @@ async fn launch_recovery_task( }; let phase = backing_group - .and_then(|g| session_info.validator_groups.get(g.0 as usize)) + .and_then(|g| session_info.validator_groups.get(g)) .map(|group| Source::RequestFromBackers(RequestFromBackers::new(group.clone()))) .unwrap_or_else(|| { Source::RequestChunks(RequestChunksFromValidators::new(params.validators.len() as _)) diff --git a/node/network/availability-recovery/src/tests.rs b/node/network/availability-recovery/src/tests.rs index 8a5191e70b26..69412bee384e 100644 --- a/node/network/availability-recovery/src/tests.rs +++ b/node/network/availability-recovery/src/tests.rs @@ -36,7 +36,9 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; use polkadot_node_subsystem_util::TimeoutExt; -use polkadot_primitives::v2::{AuthorityDiscoveryId, Hash, HeadData, PersistedValidationData}; +use polkadot_primitives::v2::{ + AuthorityDiscoveryId, GroupValidators, Hash, HeadData, PersistedValidationData, +}; use polkadot_primitives_test_helpers::{dummy_candidate_receipt, dummy_hash}; type VirtualOverseer = TestSubsystemContextHandle; @@ -179,7 +181,7 @@ impl Has { #[derive(Clone)] struct TestState { validators: Vec, - validator_public: Vec, + validator_public: Validators, validator_authority_id: Vec, current: Hash, candidate: CandidateReceipt, @@ -218,7 +220,7 @@ impl TestState { validators: self.validator_public.clone(), discovery_keys: self.validator_authority_id.clone(), // all validators in the same group. - validator_groups: vec![(0..self.validators.len()).map(|i| ValidatorIndex(i as _)).collect()], + validator_groups: GroupValidators::from(vec![(0..self.validators.len()).map(|i| ValidatorIndex(i as _)).collect()]), assignment_keys: vec![], n_cores: 0, zeroth_delay_tranche_width: 0, @@ -402,7 +404,7 @@ impl TestState { } } -fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Vec { +fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Validators { val_ids.iter().map(|v| v.public().into()).collect() } diff --git a/node/network/collator-protocol/src/collator_side/mod.rs b/node/network/collator-protocol/src/collator_side/mod.rs index 4f2eea2ca747..7a603a8a404a 100644 --- a/node/network/collator-protocol/src/collator_side/mod.rs +++ b/node/network/collator-protocol/src/collator_side/mod.rs @@ -448,10 +448,8 @@ async fn determine_our_validators( let rotation_info = get_group_rotation_info(ctx.sender(), relay_parent).await?; let current_group_index = rotation_info.group_for_core(core_index, cores); - let current_validators = groups - .get(current_group_index.0 as usize) - .map(|v| v.as_slice()) - .unwrap_or_default(); + let current_validators = + groups.get(current_group_index).map(|v| v.as_slice()).unwrap_or_default(); let validators = &info.discovery_keys; diff --git a/node/network/collator-protocol/src/collator_side/tests.rs b/node/network/collator-protocol/src/collator_side/tests.rs index c20a2d6c97a5..28a2bb83366f 100644 --- a/node/network/collator-protocol/src/collator_side/tests.rs +++ b/node/network/collator-protocol/src/collator_side/tests.rs @@ -44,8 +44,8 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_test_helpers as test_helpers; use polkadot_node_subsystem_util::TimeoutExt; use polkadot_primitives::v2::{ - AuthorityDiscoveryId, CollatorPair, GroupRotationInfo, ScheduledCore, SessionIndex, - SessionInfo, ValidatorId, ValidatorIndex, + AuthorityDiscoveryId, CollatorPair, GroupIndex, GroupRotationInfo, ScheduledCore, SessionIndex, + SessionInfo, ValidatorIndex, Validators, }; use polkadot_primitives_test_helpers::TestCandidateBuilder; @@ -62,7 +62,7 @@ struct TestState { session_index: SessionIndex, } -fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Vec { +fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Validators { val_ids.iter().map(|v| v.public().into()).collect() } @@ -135,7 +135,7 @@ impl TestState { fn current_group_validator_indices(&self) -> &[ValidatorIndex] { let core_num = self.availability_cores.len(); let GroupIndex(group_idx) = self.group_rotation_info.group_for_core(CoreIndex(0), core_num); - &self.session_info.validator_groups[group_idx as usize] + &self.session_info.validator_groups[GroupIndex::from(group_idx)] } fn current_session_index(&self) -> SessionIndex { @@ -367,7 +367,7 @@ async fn distribute_collation( )) => { assert_eq!(relay_parent, test_state.relay_parent); tx.send(Ok(( - test_state.session_info.validator_groups.clone(), + test_state.session_info.validator_groups.to_vec(), test_state.group_rotation_info.clone(), ))) .unwrap(); diff --git a/node/network/statement-distribution/src/lib.rs b/node/network/statement-distribution/src/lib.rs index c61b653e9684..3de8fba0082b 100644 --- a/node/network/statement-distribution/src/lib.rs +++ b/node/network/statement-distribution/src/lib.rs @@ -45,7 +45,11 @@ use polkadot_node_subsystem::{ overseer, ActiveLeavesUpdate, FromOrchestra, OverseerSignal, PerLeafSpan, SpawnedSubsystem, SubsystemError, }; -use polkadot_primitives::v2::{AuthorityDiscoveryId, CandidateHash, CommittedCandidateReceipt, CompactStatement, Hash, SignedStatement, SigningContext, UncheckedSignedStatement, ValidatorIndex, Validators, ValidatorSignature}; +use polkadot_primitives::v2::{ + AuthorityDiscoveryId, CandidateHash, CommittedCandidateReceipt, CompactStatement, Hash, + SignedStatement, SigningContext, UncheckedSignedStatement, ValidatorIndex, ValidatorSignature, + Validators, +}; use futures::{ channel::{mpsc, oneshot}, diff --git a/node/network/statement-distribution/src/tests.rs b/node/network/statement-distribution/src/tests.rs index a5729afd467d..e186025d60df 100644 --- a/node/network/statement-distribution/src/tests.rs +++ b/node/network/statement-distribution/src/tests.rs @@ -35,7 +35,9 @@ use polkadot_node_subsystem::{ ActivatedLeaf, LeafStatus, }; use polkadot_node_subsystem_test_helpers::mock::make_ferdie_keystore; -use polkadot_primitives::v2::{GroupValidators, Hash, Id as ParaId, SessionInfo, ValidationCode, ValidatorId}; +use polkadot_primitives::v2::{ + GroupValidators, Hash, Id as ParaId, SessionInfo, ValidationCode, ValidatorId, +}; use polkadot_primitives_test_helpers::{ dummy_committed_candidate_receipt, dummy_hash, AlwaysZeroRng, }; diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index c8fca86dabc6..9a71552cbc94 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1634,6 +1634,11 @@ impl Validators { pub fn into_iter(self) -> IntoIter { self.0.into_iter() } + + /// Returns true if contained Vector is empty + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } } /// GroupValidators struct indexed by GroupIndex. @@ -1696,6 +1701,11 @@ impl GroupValidators { pub fn into_iter(self) -> IntoIter> { self.0.into_iter() } + + /// Returns true if contained Vector is empty + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } } /// The maximum number of validators `f` which may safely be faulty. From 8696f936aa02473b64e864b11e9ba5655c81cd8a Mon Sep 17 00:00:00 2001 From: Tife Date: Tue, 18 Oct 2022 15:59:25 +0100 Subject: [PATCH 12/41] benchmarking fix --- runtime/parachains/src/disputes/slashing/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/parachains/src/disputes/slashing/benchmarking.rs b/runtime/parachains/src/disputes/slashing/benchmarking.rs index 2a21c3a0f62b..daaff8d0501f 100644 --- a/runtime/parachains/src/disputes/slashing/benchmarking.rs +++ b/runtime/parachains/src/disputes/slashing/benchmarking.rs @@ -80,7 +80,7 @@ where let session_index = crate::shared::Pallet::::session_index(); let session_info = crate::session_info::Pallet::::session_info(session_index); let session_info = session_info.unwrap(); - let validator_id = session_info.validators[0].clone(); + let validator_id = session_info.validators[ValidatorIndex::from(0)].clone(); let key = (PARACHAIN_KEY_TYPE_ID, validator_id.clone()); let key_owner_proof = pallet_session::historical::Pallet::::prove(key).unwrap(); From 4d10ca1185fdb0b6c7882d5ba8e0b028ae0e851b Mon Sep 17 00:00:00 2001 From: Tife Date: Tue, 18 Oct 2022 16:27:39 +0100 Subject: [PATCH 13/41] minor fix --- node/network/gossip-support/src/tests.rs | 3 ++- node/subsystem-util/src/rolling_session_window.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/node/network/gossip-support/src/tests.rs b/node/network/gossip-support/src/tests.rs index 79f2a9a6db42..dc807c304aa2 100644 --- a/node/network/gossip-support/src/tests.rs +++ b/node/network/gossip-support/src/tests.rs @@ -37,6 +37,7 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers as test_helpers; use polkadot_node_subsystem_util::TimeoutExt as _; +use polkadot_primitives::v2::GroupValidators; use test_helpers::mock::make_ferdie_keystore; use super::*; @@ -219,7 +220,7 @@ fn make_session_info() -> SessionInfo { validators: AUTHORITY_KEYRINGS.iter().map(|k| k.public().into()).collect(), discovery_keys: AUTHORITIES.clone(), assignment_keys: AUTHORITY_KEYRINGS.iter().map(|k| k.public().into()).collect(), - validator_groups: vec![all_validator_indices], + validator_groups: GroupValidators::from(vec![all_validator_indices]), n_cores: 1, zeroth_delay_tranche_width: 1, relay_vrf_modulo_samples: 1, diff --git a/node/subsystem-util/src/rolling_session_window.rs b/node/subsystem-util/src/rolling_session_window.rs index 3be681fbc75f..700feb2ccff8 100644 --- a/node/subsystem-util/src/rolling_session_window.rs +++ b/node/subsystem-util/src/rolling_session_window.rs @@ -392,7 +392,7 @@ mod tests { SubsystemContext, }; use polkadot_node_subsystem_test_helpers::make_subsystem_context; - use polkadot_primitives::v2::{GroupValidators, Header, Validators}; + use polkadot_primitives::v2::Header; use sp_core::testing::TaskExecutor; pub const TEST_WINDOW_SIZE: SessionWindowSize = new_session_window_size!(6); From 95a388182db2b694e6a793c32661c5978ac1f5a8 Mon Sep 17 00:00:00 2001 From: Tife Date: Tue, 18 Oct 2022 19:55:25 +0100 Subject: [PATCH 14/41] test fixes in runtime api --- node/core/runtime-api/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/node/core/runtime-api/src/tests.rs b/node/core/runtime-api/src/tests.rs index 2fab84179433..45286431e49d 100644 --- a/node/core/runtime-api/src/tests.rs +++ b/node/core/runtime-api/src/tests.rs @@ -522,10 +522,10 @@ fn requests_session_index_for_child() { fn dummy_session_info() -> SessionInfo { SessionInfo { - validators: vec![], + validators: Default::default(), discovery_keys: vec![], assignment_keys: vec![], - validator_groups: vec![], + validator_groups: Default::default(), n_cores: 4u32, zeroth_delay_tranche_width: 0u32, relay_vrf_modulo_samples: 0u32, From 4951b5454836b95c9e78b5b86e5fb42395bbb9db Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Wed, 19 Oct 2022 11:42:21 +0100 Subject: [PATCH 15/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 9a71552cbc94..9609aaeb6d3b 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1687,7 +1687,7 @@ impl GroupValidators { self.0.clone() } - /// Returns Iterator of contained vector. + /// Returns an iterator over the groups. pub fn iter(&self) -> Iter<'_, Vec> { self.0.iter() } From 35c13044e354aaa5ef0d03b29d0c220bdba69a95 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Wed, 19 Oct 2022 11:42:34 +0100 Subject: [PATCH 16/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 9609aaeb6d3b..8439cd54b171 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1692,7 +1692,7 @@ impl GroupValidators { self.0.iter() } - /// Returns a mutable Iterator. + /// Returns an iterator that allows modifying each group. pub fn iter_mut(&mut self) -> IterMut<'_, Vec> { self.0.iter_mut() } From a0edab4f81037bc39e243d8574f499526db8d8d3 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Wed, 19 Oct 2022 11:42:44 +0100 Subject: [PATCH 17/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 8439cd54b171..bcbfad8f0049 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1697,7 +1697,7 @@ impl GroupValidators { self.0.iter_mut() } - /// Returns an Iterator with ownership. + /// Creates a consuming iterator. pub fn into_iter(self) -> IntoIter> { self.0.into_iter() } From 3823be133e1fa45c94361e8ac1b2a6ceb6d492c5 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Wed, 19 Oct 2022 11:42:56 +0100 Subject: [PATCH 18/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index bcbfad8f0049..6d0f544b4c67 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1702,7 +1702,7 @@ impl GroupValidators { self.0.into_iter() } - /// Returns true if contained Vector is empty + /// Returns true if the underlying container is empty. pub fn is_empty(&self) -> bool { self.0.is_empty() } From 5714ad4b52376ab82ea50b9ee0d079bfbd4a294e Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Wed, 19 Oct 2022 11:43:12 +0100 Subject: [PATCH 19/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 6d0f544b4c67..336625fec569 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1677,7 +1677,7 @@ impl GroupValidators { self.0.get(index.0 as usize) } - /// Returns number of elements in vector. + /// Returns the number of validator groups. pub fn len(&self) -> usize { self.0.len() } From 7a8be345ad12b8e85c124345a0a12ea520a5345b Mon Sep 17 00:00:00 2001 From: Tife Date: Wed, 19 Oct 2022 23:52:18 +0100 Subject: [PATCH 20/41] Removal of [index], shorting of FromIterator, Renaming of GroupValidators to ValidatorGroups --- node/core/approval-voting/src/criteria.rs | 14 +++--- node/core/approval-voting/src/import.rs | 4 +- node/core/approval-voting/src/tests.rs | 20 ++++---- node/core/dispute-coordinator/src/import.rs | 2 +- .../dispute-coordinator/src/initialized.rs | 2 +- node/core/dispute-coordinator/src/tests.rs | 10 ++-- .../src/tests/mock.rs | 6 +-- .../availability-recovery/src/tests.rs | 4 +- .../src/collator_side/tests.rs | 2 +- node/network/gossip-support/src/tests.rs | 4 +- .../network/statement-distribution/src/lib.rs | 5 +- .../statement-distribution/src/tests.rs | 4 +- primitives/src/v2/mod.rs | 46 ++++--------------- .../src/disputes/slashing/benchmarking.rs | 2 +- 14 files changed, 51 insertions(+), 74 deletions(-) diff --git a/node/core/approval-voting/src/criteria.rs b/node/core/approval-voting/src/criteria.rs index fd675ec805e4..ee19a37ee379 100644 --- a/node/core/approval-voting/src/criteria.rs +++ b/node/core/approval-voting/src/criteria.rs @@ -21,8 +21,8 @@ use polkadot_node_primitives::approval::{ self as approval_types, AssignmentCert, AssignmentCertKind, DelayTranche, RelayVRFStory, }; use polkadot_primitives::v2::{ - AssignmentId, AssignmentPair, CandidateHash, CoreIndex, GroupIndex, GroupValidators, - SessionInfo, ValidatorIndex, + AssignmentId, AssignmentPair, CandidateHash, CoreIndex, GroupIndex, SessionInfo, + ValidatorGroups, ValidatorIndex, }; use sc_keystore::LocalKeystore; use sp_application_crypto::ByteArray; @@ -139,7 +139,7 @@ pub(crate) struct Config { /// The assignment public keys for validators. assignment_keys: Vec, /// The groups of validators assigned to each core. - validator_groups: GroupValidators, + validator_groups: ValidatorGroups, /// The number of availability cores used by the protocol during this session. n_cores: u32, /// The zeroth delay tranche width. @@ -542,7 +542,7 @@ pub(crate) fn check_assignment_cert( } fn is_in_backing_group( - validator_groups: &GroupValidators, + validator_groups: &ValidatorGroups, validator: ValidatorIndex, group: GroupIndex, ) -> bool { @@ -591,7 +591,7 @@ mod tests { .collect() } - fn basic_groups(n_validators: usize, n_groups: usize) -> GroupValidators { + fn basic_groups(n_validators: usize, n_groups: usize) -> ValidatorGroups { let size = n_validators / n_groups; let big_groups = n_validators % n_groups; let scraps = n_groups * size; @@ -632,7 +632,7 @@ mod tests { Sr25519Keyring::Bob, Sr25519Keyring::Charlie, ]), - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1), ValidatorIndex(2)], ]), @@ -667,7 +667,7 @@ mod tests { Sr25519Keyring::Bob, Sr25519Keyring::Charlie, ]), - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1), ValidatorIndex(2)], ]), diff --git a/node/core/approval-voting/src/import.rs b/node/core/approval-voting/src/import.rs index c612b858badd..587466182ee3 100644 --- a/node/core/approval-voting/src/import.rs +++ b/node/core/approval-voting/src/import.rs @@ -621,7 +621,7 @@ pub(crate) mod tests { use polkadot_node_subsystem_test_helpers::make_subsystem_context; use polkadot_node_subsystem_util::database::Database; use polkadot_primitives::v2::{ - GroupValidators, Id as ParaId, SessionInfo, ValidatorIndex, Validators, + Id as ParaId, SessionInfo, ValidatorGroups, ValidatorIndex, Validators, }; pub(crate) use sp_consensus_babe::{ digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest}, @@ -1180,7 +1180,7 @@ pub(crate) mod tests { validators: Validators::from(vec![Sr25519Keyring::Alice.public().into(); 6]), discovery_keys: Vec::new(), assignment_keys: Vec::new(), - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0); 5], vec![ValidatorIndex(0); 2], ]), diff --git a/node/core/approval-voting/src/tests.rs b/node/core/approval-voting/src/tests.rs index e39c2abd3ea0..cfd6ecc4ef91 100644 --- a/node/core/approval-voting/src/tests.rs +++ b/node/core/approval-voting/src/tests.rs @@ -32,8 +32,8 @@ 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, GroupValidators, Header, - Id as ParaId, ValidationCode, ValidatorSignature, + CandidateCommitments, CandidateEvent, CoreIndex, GroupIndex, Header, Id as ParaId, + ValidationCode, ValidatorGroups, ValidatorSignature, }; use std::time::Duration; @@ -739,7 +739,7 @@ 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: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1)], ]), @@ -1555,7 +1555,7 @@ fn subsystem_second_approval_import_only_schedules_wakeups() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -1892,7 +1892,7 @@ fn import_checked_approval_updates_entries_and_schedules() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -2049,7 +2049,7 @@ fn subsystem_import_checked_approval_sets_one_block_bit_at_a_time() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -2339,7 +2339,7 @@ fn subsystem_validate_approvals_cache() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -2551,7 +2551,7 @@ where Sr25519Keyring::Ferdie, ]; let session_info = SessionInfo { - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(3)], vec![ValidatorIndex(4), ValidatorIndex(5)], @@ -2871,7 +2871,7 @@ fn pre_covers_dont_stall_approval() { Sr25519Keyring::One, ]; let session_info = SessionInfo { - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(5)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -3048,7 +3048,7 @@ fn waits_until_approving_assignments_are_old_enough() { Sr25519Keyring::One, ]; let session_info = SessionInfo { - validator_groups: GroupValidators::from(vec![ + validator_groups: ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(5)], vec![ValidatorIndex(3), ValidatorIndex(4)], diff --git a/node/core/dispute-coordinator/src/import.rs b/node/core/dispute-coordinator/src/import.rs index 44799ef70ac9..fc861cf80553 100644 --- a/node/core/dispute-coordinator/src/import.rs +++ b/node/core/dispute-coordinator/src/import.rs @@ -488,7 +488,7 @@ impl ImportResult { for (index, sig) in approval_votes.into_iter() { debug_assert!( { - let pub_key = &env.session_info().validators[index]; + let pub_key = &env.session_info().validators.get(index).expect(""); let candidate_hash = votes.candidate_receipt.hash(); let session_index = env.session_index(); DisputeStatement::Valid(ValidDisputeStatementKind::ApprovalChecking) diff --git a/node/core/dispute-coordinator/src/initialized.rs b/node/core/dispute-coordinator/src/initialized.rs index 07b74cb2a684..b33baaece47a 100644 --- a/node/core/dispute-coordinator/src/initialized.rs +++ b/node/core/dispute-coordinator/src/initialized.rs @@ -1097,7 +1097,7 @@ impl Initialized { valid, candidate_hash, session, - env.validators()[*index].clone(), + env.validators().get(*index).expect("").clone(), ) .await; diff --git a/node/core/dispute-coordinator/src/tests.rs b/node/core/dispute-coordinator/src/tests.rs index 14a44a511c0a..6bbe81eca8d9 100644 --- a/node/core/dispute-coordinator/src/tests.rs +++ b/node/core/dispute-coordinator/src/tests.rs @@ -58,8 +58,8 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; use polkadot_primitives::v2::{ ApprovalVote, BlockNumber, CandidateCommitments, CandidateHash, CandidateReceipt, - DisputeStatement, GroupValidators, Hash, Header, MultiDisputeStatementSet, ScrapedOnChainVotes, - SessionIndex, SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorId, + DisputeStatement, Hash, Header, MultiDisputeStatementSet, ScrapedOnChainVotes, SessionIndex, + SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorGroups, ValidatorId, ValidatorIndex, ValidatorSignature, Validators, }; @@ -126,7 +126,7 @@ impl MockClock { struct TestState { validators: Vec, validator_public: Validators, - validator_groups: GroupValidators, + validator_groups: ValidatorGroups, master_keystore: Arc, subsystem_keystore: Arc, db: Arc, @@ -163,7 +163,7 @@ impl Default for TestState { .map(|k| ValidatorId::from(k.0.public())) .collect(); - let validator_groups = GroupValidators::from(vec![ + let validator_groups = ValidatorGroups::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(3)], vec![ValidatorIndex(4), ValidatorIndex(5), ValidatorIndex(6)], @@ -431,7 +431,7 @@ impl TestState { session: SessionIndex, valid: bool, ) -> SignedDisputeStatement { - let public = self.validator_public[index].clone(); + let public = self.validator_public.get(index).unwrap().clone(); let keystore = self.master_keystore.clone() as SyncCryptoStorePtr; diff --git a/node/network/availability-distribution/src/tests/mock.rs b/node/network/availability-distribution/src/tests/mock.rs index 794b23ac3996..5a2d738df0be 100644 --- a/node/network/availability-distribution/src/tests/mock.rs +++ b/node/network/availability-distribution/src/tests/mock.rs @@ -24,8 +24,8 @@ use polkadot_erasure_coding::{branches, obtain_chunks_v1 as obtain_chunks}; use polkadot_node_primitives::{AvailableData, BlockData, ErasureChunk, PoV, Proof}; use polkadot_primitives::v2::{ CandidateCommitments, CandidateDescriptor, CandidateHash, CommittedCandidateReceipt, - GroupIndex, GroupValidators, Hash, HeadData, Id as ParaId, OccupiedCore, - PersistedValidationData, SessionInfo, ValidatorIndex, + GroupIndex, Hash, HeadData, Id as ParaId, OccupiedCore, PersistedValidationData, SessionInfo, + ValidatorGroups, ValidatorIndex, }; use polkadot_primitives_test_helpers::{ dummy_collator, dummy_collator_signature, dummy_hash, dummy_validation_code, @@ -43,7 +43,7 @@ pub fn make_session_info() -> SessionInfo { Sr25519Keyring::One, ]; - let validator_groups: GroupValidators = [vec![5, 0, 3], vec![1, 6, 2, 4]] + let validator_groups: ValidatorGroups = [vec![5, 0, 3], vec![1, 6, 2, 4]] .iter() .map(|g| g.into_iter().map(|v| ValidatorIndex(*v)).collect()) .collect(); diff --git a/node/network/availability-recovery/src/tests.rs b/node/network/availability-recovery/src/tests.rs index 2456afd4e399..0deaa6362212 100644 --- a/node/network/availability-recovery/src/tests.rs +++ b/node/network/availability-recovery/src/tests.rs @@ -37,7 +37,7 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; use polkadot_node_subsystem_util::TimeoutExt; use polkadot_primitives::v2::{ - AuthorityDiscoveryId, GroupValidators, Hash, HeadData, PersistedValidationData, + AuthorityDiscoveryId, Hash, HeadData, PersistedValidationData, ValidatorGroups, }; use polkadot_primitives_test_helpers::{dummy_candidate_receipt, dummy_hash}; @@ -220,7 +220,7 @@ impl TestState { validators: self.validator_public.clone(), discovery_keys: self.validator_authority_id.clone(), // all validators in the same group. - validator_groups: GroupValidators::from(vec![(0..self.validators.len()).map(|i| ValidatorIndex(i as _)).collect()]), + validator_groups: ValidatorGroups::from(vec![(0..self.validators.len()).map(|i| ValidatorIndex(i as _)).collect()]), assignment_keys: vec![], n_cores: 0, zeroth_delay_tranche_width: 0, diff --git a/node/network/collator-protocol/src/collator_side/tests.rs b/node/network/collator-protocol/src/collator_side/tests.rs index 28a2bb83366f..c468e2596e36 100644 --- a/node/network/collator-protocol/src/collator_side/tests.rs +++ b/node/network/collator-protocol/src/collator_side/tests.rs @@ -135,7 +135,7 @@ impl TestState { fn current_group_validator_indices(&self) -> &[ValidatorIndex] { let core_num = self.availability_cores.len(); let GroupIndex(group_idx) = self.group_rotation_info.group_for_core(CoreIndex(0), core_num); - &self.session_info.validator_groups[GroupIndex::from(group_idx)] + &self.session_info.validator_groups.get(GroupIndex::from(group_idx)).unwrap() } fn current_session_index(&self) -> SessionIndex { diff --git a/node/network/gossip-support/src/tests.rs b/node/network/gossip-support/src/tests.rs index dc807c304aa2..f69c8fd9c51d 100644 --- a/node/network/gossip-support/src/tests.rs +++ b/node/network/gossip-support/src/tests.rs @@ -37,7 +37,7 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers as test_helpers; use polkadot_node_subsystem_util::TimeoutExt as _; -use polkadot_primitives::v2::GroupValidators; +use polkadot_primitives::v2::ValidatorGroups; use test_helpers::mock::make_ferdie_keystore; use super::*; @@ -220,7 +220,7 @@ fn make_session_info() -> SessionInfo { validators: AUTHORITY_KEYRINGS.iter().map(|k| k.public().into()).collect(), discovery_keys: AUTHORITIES.clone(), assignment_keys: AUTHORITY_KEYRINGS.iter().map(|k| k.public().into()).collect(), - validator_groups: GroupValidators::from(vec![all_validator_indices]), + validator_groups: ValidatorGroups::from(vec![all_validator_indices]), n_cores: 1, zeroth_delay_tranche_width: 1, relay_vrf_modulo_samples: 1, diff --git a/node/network/statement-distribution/src/lib.rs b/node/network/statement-distribution/src/lib.rs index 3de8fba0082b..0d4a505b7fc8 100644 --- a/node/network/statement-distribution/src/lib.rs +++ b/node/network/statement-distribution/src/lib.rs @@ -2072,7 +2072,10 @@ impl StatementDistributionSubsystem { // directly: let group_peers = { if let Some(our_group) = validator_info.our_group { - let our_group = &session_info.validator_groups[our_group]; + let our_group = &session_info + .validator_groups + .get(our_group) + .expect(""); our_group .into_iter() diff --git a/node/network/statement-distribution/src/tests.rs b/node/network/statement-distribution/src/tests.rs index e186025d60df..4fffb0a6ddf5 100644 --- a/node/network/statement-distribution/src/tests.rs +++ b/node/network/statement-distribution/src/tests.rs @@ -36,7 +36,7 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers::mock::make_ferdie_keystore; use polkadot_primitives::v2::{ - GroupValidators, Hash, Id as ParaId, SessionInfo, ValidationCode, ValidatorId, + Hash, Id as ParaId, SessionInfo, ValidationCode, ValidatorGroups, ValidatorId, }; use polkadot_primitives_test_helpers::{ dummy_committed_candidate_receipt, dummy_hash, AlwaysZeroRng, @@ -2321,7 +2321,7 @@ fn handle_multiple_seconded_statements() { } fn make_session_info(validators: Vec, groups: Vec>) -> SessionInfo { - let validator_groups: GroupValidators = groups + let validator_groups: ValidatorGroups = groups .iter() .map(|g| g.into_iter().map(|v| ValidatorIndex(*v)).collect()) .collect(); diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 9a71552cbc94..21bbe5b94b71 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1579,14 +1579,6 @@ impl CompactStatement { #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] pub struct Validators(Vec); -impl Index for Validators { - type Output = ValidatorId; - - fn index(&self, index: ValidatorIndex) -> &Self::Output { - &self.0.get(index.0 as usize).unwrap() - } -} - impl From> for Validators { fn from(validators: Vec) -> Self { Validators(validators) @@ -1595,12 +1587,7 @@ impl From> for Validators { impl FromIterator for Validators { fn from_iter>(iter: T) -> Self { - let mut validators = Vec::new(); - - for i in iter { - validators.push(i); - } - Validators(validators) + Self(Vec::from_iter(iter)) } } @@ -1641,37 +1628,24 @@ impl Validators { } } -/// GroupValidators struct indexed by GroupIndex. +/// ValidatorGroups struct indexed by GroupIndex. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] -pub struct GroupValidators(Vec>); +pub struct ValidatorGroups(Vec>); -impl Index for GroupValidators { - type Output = Vec; - - fn index(&self, index: GroupIndex) -> &Self::Output { - self.0.get(index.0 as usize).unwrap() - } -} - -impl From>> for GroupValidators { +impl From>> for ValidatorGroups { fn from(group_validators: Vec>) -> Self { - GroupValidators(group_validators) + ValidatorGroups(group_validators) } } -impl FromIterator> for GroupValidators { +impl FromIterator> for ValidatorGroups { fn from_iter>>(iter: T) -> Self { - let mut validator_groups = Vec::new(); - - for i in iter { - validator_groups.push(i); - } - GroupValidators(validator_groups) + Self(Vec::from_iter(iter)) } } -impl GroupValidators { +impl ValidatorGroups { /// Returns a reference to an element indexed using GroupIndex. pub fn get(&self, index: GroupIndex) -> Option<&Vec> { self.0.get(index.0 as usize) @@ -1765,7 +1739,7 @@ pub struct SessionInfo { /// Validators in shuffled ordering - these are the validator groups as produced /// by the `Scheduler` module for the session and are typically referred to by /// `GroupIndex`. - pub validator_groups: GroupValidators, + pub validator_groups: ValidatorGroups, /// The number of availability cores used by the protocol during this session. pub n_cores: u32, /// The zeroth delay tranche width. @@ -1841,7 +1815,7 @@ pub struct OldV1SessionInfo { /// Validators in shuffled ordering - these are the validator groups as produced /// by the `Scheduler` module for the session and are typically referred to by /// `GroupIndex`. - pub validator_groups: GroupValidators, + pub validator_groups: ValidatorGroups, /// The number of availability cores used by the protocol during this session. pub n_cores: u32, /// The zeroth delay tranche width. diff --git a/runtime/parachains/src/disputes/slashing/benchmarking.rs b/runtime/parachains/src/disputes/slashing/benchmarking.rs index daaff8d0501f..552172dc1901 100644 --- a/runtime/parachains/src/disputes/slashing/benchmarking.rs +++ b/runtime/parachains/src/disputes/slashing/benchmarking.rs @@ -80,7 +80,7 @@ where let session_index = crate::shared::Pallet::::session_index(); let session_info = crate::session_info::Pallet::::session_info(session_index); let session_info = session_info.unwrap(); - let validator_id = session_info.validators[ValidatorIndex::from(0)].clone(); + let validator_id = session_info.validators.get(ValidatorIndex::from(0)).unwrap().clone(); let key = (PARACHAIN_KEY_TYPE_ID, validator_id.clone()); let key_owner_proof = pallet_session::historical::Pallet::::prove(key).unwrap(); From 56b3904a9e0283a0cb45e03f5ee2ac3e8dbb99ff Mon Sep 17 00:00:00 2001 From: Tife Date: Thu, 20 Oct 2022 00:00:29 +0100 Subject: [PATCH 21/41] Removal of ops import --- primitives/src/v2/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 785b16f1aeda..9402184eceea 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -20,7 +20,6 @@ use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_std::{ - ops::Index, prelude::*, slice::{Iter, IterMut}, vec::IntoIter, From a0bb41badb0a3bb767015f20d12c24a2340ced12 Mon Sep 17 00:00:00 2001 From: Tife Date: Thu, 20 Oct 2022 00:05:54 +0100 Subject: [PATCH 22/41] documentation fixes for spell check --- primitives/src/v2/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 9402184eceea..a505f3ff975f 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1573,7 +1573,7 @@ impl CompactStatement { } } -/// Validators struct indexed by ValidatorIndex. +/// `Validators` struct indexed by `ValidatorIndex`. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] pub struct Validators(Vec); @@ -1591,7 +1591,7 @@ impl FromIterator for Validators { } impl Validators { - /// Returns a reference to an element indexed using ValidatorIndex. + /// Returns a reference to an element indexed using `ValidatorIndex`. pub fn get(&self, index: ValidatorIndex) -> Option<&ValidatorId> { self.0.get(index.0 as usize) } @@ -1627,7 +1627,7 @@ impl Validators { } } -/// ValidatorGroups struct indexed by GroupIndex. +/// `ValidatorGroups` struct indexed by `GroupIndex`. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] pub struct ValidatorGroups(Vec>); @@ -1645,7 +1645,7 @@ impl FromIterator> for ValidatorGroups { } impl ValidatorGroups { - /// Returns a reference to an element indexed using GroupIndex. + /// Returns a reference to an element indexed using `GroupIndex`. pub fn get(&self, index: GroupIndex) -> Option<&Vec> { self.0.get(index.0 as usize) } From ef5438cd7a0ddb986f35bc865cf8b14a9eb30ba1 Mon Sep 17 00:00:00 2001 From: Tife Date: Thu, 20 Oct 2022 16:43:53 +0100 Subject: [PATCH 23/41] implementation of generic type --- primitives/src/v2/mod.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index a505f3ff975f..99ad06dbf9bf 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -16,6 +16,7 @@ //! `V1` Primitives. +use std::marker::PhantomData; use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; @@ -1576,24 +1577,25 @@ impl CompactStatement { /// `Validators` struct indexed by `ValidatorIndex`. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] -pub struct Validators(Vec); +pub struct IndexedVec(Vec, PhantomData K>); -impl From> for Validators { - fn from(validators: Vec) -> Self { - Validators(validators) +impl From> for IndexedVec { + fn from(validators: Vec) -> Self { + Self(validators, PhantomData) } } -impl FromIterator for Validators { - fn from_iter>(iter: T) -> Self { - Self(Vec::from_iter(iter)) +impl FromIterator for IndexedVec { + fn from_iter>(iter: T) -> Self { + Self(Vec::from_iter(iter), PhantomData) } } -impl Validators { +impl IndexedVec { /// Returns a reference to an element indexed using `ValidatorIndex`. - pub fn get(&self, index: ValidatorIndex) -> Option<&ValidatorId> { - self.0.get(index.0 as usize) + pub fn get(&self, index: K) -> Option<&V> + where K: From{ + self.0.get(u32::from(index)) } /// Returns number of elements in vector. @@ -1602,22 +1604,22 @@ impl Validators { } /// Returns contained vector. - pub fn to_vec(&self) -> Vec { + pub fn to_vec(&self) -> Vec { self.0.clone() } /// Returns Iterator of contained vector. - pub fn iter(&self) -> Iter<'_, ValidatorId> { + pub fn iter(&self) -> Iter<'_, V> { self.0.iter() } /// Returns a mutable Iterator. - pub fn iter_mut(&mut self) -> IterMut<'_, ValidatorId> { + pub fn iter_mut(&mut self) -> IterMut<'_, V> { self.0.iter_mut() } /// Returns an Iterator with ownership. - pub fn into_iter(self) -> IntoIter { + pub fn into_iter(self) -> IntoIter { self.0.into_iter() } From 4e825d012482c5654e2da9104965e343d7a3b551 Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 02:11:50 +0100 Subject: [PATCH 24/41] Refactoring --- node/core/approval-voting/src/criteria.rs | 17 +-- node/core/approval-voting/src/import.rs | 41 ++++--- node/core/approval-voting/src/tests.rs | 20 ++-- node/core/dispute-coordinator/src/import.rs | 8 +- node/core/dispute-coordinator/src/tests.rs | 12 +- .../src/tests/mock.rs | 13 +- node/network/availability-recovery/src/lib.rs | 4 +- .../availability-recovery/src/tests.rs | 8 +- .../src/collator_side/tests.rs | 6 +- node/network/gossip-support/src/tests.rs | 6 +- .../network/statement-distribution/src/lib.rs | 8 +- .../statement-distribution/src/tests.rs | 4 +- node/subsystem-util/src/runtime/mod.rs | 11 +- primitives/src/v2/mod.rs | 113 +++++++----------- 14 files changed, 130 insertions(+), 141 deletions(-) diff --git a/node/core/approval-voting/src/criteria.rs b/node/core/approval-voting/src/criteria.rs index ee19a37ee379..fea71d79c098 100644 --- a/node/core/approval-voting/src/criteria.rs +++ b/node/core/approval-voting/src/criteria.rs @@ -21,8 +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, - ValidatorGroups, ValidatorIndex, + AssignmentId, AssignmentPair, CandidateHash, CoreIndex, GroupIndex, IndexedVec, SessionInfo, + ValidatorIndex, }; use sc_keystore::LocalKeystore; use sp_application_crypto::ByteArray; @@ -139,7 +139,7 @@ pub(crate) struct Config { /// The assignment public keys for validators. assignment_keys: Vec, /// The groups of validators assigned to each core. - validator_groups: ValidatorGroups, + validator_groups: IndexedVec>, /// The number of availability cores used by the protocol during this session. n_cores: u32, /// The zeroth delay tranche width. @@ -542,7 +542,7 @@ pub(crate) fn check_assignment_cert( } fn is_in_backing_group( - validator_groups: &ValidatorGroups, + validator_groups: &IndexedVec>, validator: ValidatorIndex, group: GroupIndex, ) -> bool { @@ -591,7 +591,10 @@ mod tests { .collect() } - fn basic_groups(n_validators: usize, n_groups: usize) -> ValidatorGroups { + fn basic_groups( + n_validators: usize, + n_groups: usize, + ) -> IndexedVec> { let size = n_validators / n_groups; let big_groups = n_validators % n_groups; let scraps = n_groups * size; @@ -632,7 +635,7 @@ mod tests { Sr25519Keyring::Bob, Sr25519Keyring::Charlie, ]), - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1), ValidatorIndex(2)], ]), @@ -667,7 +670,7 @@ mod tests { Sr25519Keyring::Bob, Sr25519Keyring::Charlie, ]), - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1), ValidatorIndex(2)], ]), diff --git a/node/core/approval-voting/src/import.rs b/node/core/approval-voting/src/import.rs index 587466182ee3..df713143750f 100644 --- a/node/core/approval-voting/src/import.rs +++ b/node/core/approval-voting/src/import.rs @@ -621,7 +621,7 @@ pub(crate) mod tests { use polkadot_node_subsystem_test_helpers::make_subsystem_context; use polkadot_node_subsystem_util::database::Database; use polkadot_primitives::v2::{ - Id as ParaId, SessionInfo, ValidatorGroups, ValidatorIndex, Validators, + Id as ParaId, IndexedVec, SessionInfo, ValidatorId, ValidatorIndex, }; pub(crate) use sp_consensus_babe::{ digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest}, @@ -1176,24 +1176,27 @@ pub(crate) mod tests { let session = 5; let irrelevant = 666; - let session_info = SessionInfo { - validators: Validators::from(vec![Sr25519Keyring::Alice.public().into(); 6]), - discovery_keys: Vec::new(), - assignment_keys: Vec::new(), - validator_groups: ValidatorGroups::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 session_info = + SessionInfo { + validators: IndexedVec::::from( + vec![Sr25519Keyring::Alice.public().into(); 6], + ), + discovery_keys: Vec::new(), + assignment_keys: Vec::new(), + validator_groups: IndexedVec::>::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); diff --git a/node/core/approval-voting/src/tests.rs b/node/core/approval-voting/src/tests.rs index cfd6ecc4ef91..d5c8d3c01da4 100644 --- a/node/core/approval-voting/src/tests.rs +++ b/node/core/approval-voting/src/tests.rs @@ -32,8 +32,8 @@ 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, - ValidationCode, ValidatorGroups, ValidatorSignature, + CandidateCommitments, CandidateEvent, CoreIndex, GroupIndex, Header, Id as ParaId, IndexedVec, + ValidationCode, ValidatorSignature, }; use std::time::Duration; @@ -739,7 +739,7 @@ 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: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0)], vec![ValidatorIndex(1)], ]), @@ -1555,7 +1555,7 @@ fn subsystem_second_approval_import_only_schedules_wakeups() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -1892,7 +1892,7 @@ fn import_checked_approval_updates_entries_and_schedules() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -2049,7 +2049,7 @@ fn subsystem_import_checked_approval_sets_one_block_bit_at_a_time() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -2339,7 +2339,7 @@ fn subsystem_validate_approvals_cache() { Sr25519Keyring::Eve, ]; let session_info = SessionInfo { - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -2551,7 +2551,7 @@ where Sr25519Keyring::Ferdie, ]; let session_info = SessionInfo { - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(3)], vec![ValidatorIndex(4), ValidatorIndex(5)], @@ -2871,7 +2871,7 @@ fn pre_covers_dont_stall_approval() { Sr25519Keyring::One, ]; let session_info = SessionInfo { - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(5)], vec![ValidatorIndex(3), ValidatorIndex(4)], @@ -3048,7 +3048,7 @@ fn waits_until_approving_assignments_are_old_enough() { Sr25519Keyring::One, ]; let session_info = SessionInfo { - validator_groups: ValidatorGroups::from(vec![ + validator_groups: IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(5)], vec![ValidatorIndex(3), ValidatorIndex(4)], diff --git a/node/core/dispute-coordinator/src/import.rs b/node/core/dispute-coordinator/src/import.rs index fc861cf80553..1c093e2d3563 100644 --- a/node/core/dispute-coordinator/src/import.rs +++ b/node/core/dispute-coordinator/src/import.rs @@ -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, - ValidatorIndex, ValidatorPair, ValidatorSignature, Validators, + CandidateReceipt, DisputeStatement, IndexedVec, SessionIndex, SessionInfo, + ValidDisputeStatementKind, ValidatorId, ValidatorIndex, ValidatorPair, ValidatorSignature, }; use sc_keystore::LocalKeystore; @@ -63,7 +63,7 @@ impl<'a> CandidateEnvironment<'a> { } /// Validators in the candidate's session. - pub fn validators(&self) -> &Validators { + pub fn validators(&self) -> &IndexedVec { &self.session.validators } @@ -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: &Validators, + validators: &IndexedVec, ) -> HashSet { let mut controlled = HashSet::new(); for (index, validator) in validators.iter().enumerate() { diff --git a/node/core/dispute-coordinator/src/tests.rs b/node/core/dispute-coordinator/src/tests.rs index 6bbe81eca8d9..6a2b3fb818bd 100644 --- a/node/core/dispute-coordinator/src/tests.rs +++ b/node/core/dispute-coordinator/src/tests.rs @@ -58,9 +58,9 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; use polkadot_primitives::v2::{ ApprovalVote, BlockNumber, CandidateCommitments, CandidateHash, CandidateReceipt, - DisputeStatement, Hash, Header, MultiDisputeStatementSet, ScrapedOnChainVotes, SessionIndex, - SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorGroups, ValidatorId, - ValidatorIndex, ValidatorSignature, Validators, + DisputeStatement, Hash, Header, IndexedVec, MultiDisputeStatementSet, ScrapedOnChainVotes, + SessionIndex, SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorId, + ValidatorIndex, ValidatorSignature, }; use crate::{ @@ -125,8 +125,8 @@ impl MockClock { struct TestState { validators: Vec, - validator_public: Validators, - validator_groups: ValidatorGroups, + validator_public: IndexedVec, + validator_groups: IndexedVec>, master_keystore: Arc, subsystem_keystore: Arc, db: Arc, @@ -163,7 +163,7 @@ impl Default for TestState { .map(|k| ValidatorId::from(k.0.public())) .collect(); - let validator_groups = ValidatorGroups::from(vec![ + let validator_groups = IndexedVec::>::from(vec![ vec![ValidatorIndex(0), ValidatorIndex(1)], vec![ValidatorIndex(2), ValidatorIndex(3)], vec![ValidatorIndex(4), ValidatorIndex(5), ValidatorIndex(6)], diff --git a/node/network/availability-distribution/src/tests/mock.rs b/node/network/availability-distribution/src/tests/mock.rs index 5a2d738df0be..15ea1ab1bc53 100644 --- a/node/network/availability-distribution/src/tests/mock.rs +++ b/node/network/availability-distribution/src/tests/mock.rs @@ -24,8 +24,8 @@ use polkadot_erasure_coding::{branches, obtain_chunks_v1 as obtain_chunks}; use polkadot_node_primitives::{AvailableData, BlockData, ErasureChunk, PoV, Proof}; use polkadot_primitives::v2::{ CandidateCommitments, CandidateDescriptor, CandidateHash, CommittedCandidateReceipt, - GroupIndex, Hash, HeadData, Id as ParaId, OccupiedCore, PersistedValidationData, SessionInfo, - ValidatorGroups, ValidatorIndex, + GroupIndex, Hash, HeadData, Id as ParaId, IndexedVec, OccupiedCore, PersistedValidationData, + SessionInfo, ValidatorIndex, }; use polkadot_primitives_test_helpers::{ dummy_collator, dummy_collator_signature, dummy_hash, dummy_validation_code, @@ -43,10 +43,11 @@ pub fn make_session_info() -> SessionInfo { Sr25519Keyring::One, ]; - let validator_groups: ValidatorGroups = [vec![5, 0, 3], vec![1, 6, 2, 4]] - .iter() - .map(|g| g.into_iter().map(|v| ValidatorIndex(*v)).collect()) - .collect(); + let validator_groups: IndexedVec> = + [vec![5, 0, 3], vec![1, 6, 2, 4]] + .iter() + .map(|g| g.into_iter().map(|v| ValidatorIndex(*v)).collect()) + .collect(); SessionInfo { discovery_keys: validators.iter().map(|k| k.public().into()).collect(), diff --git a/node/network/availability-recovery/src/lib.rs b/node/network/availability-recovery/src/lib.rs index 9b7eb3fc22bf..4ca28c955f9e 100644 --- a/node/network/availability-recovery/src/lib.rs +++ b/node/network/availability-recovery/src/lib.rs @@ -58,7 +58,7 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_util::request_session_info; use polkadot_primitives::v2::{ AuthorityDiscoveryId, BlakeTwo256, BlockNumber, CandidateHash, CandidateReceipt, GroupIndex, - Hash, HashT, SessionIndex, SessionInfo, ValidatorIndex, Validators, + Hash, HashT, IndexedVec, SessionIndex, SessionInfo, ValidatorId, ValidatorIndex, }; mod error; @@ -134,7 +134,7 @@ struct RecoveryParams { validator_authority_keys: Vec, /// Validators relevant to this `RecoveryTask`. - validators: Validators, + validators: IndexedVec, /// The number of pieces needed. threshold: usize, diff --git a/node/network/availability-recovery/src/tests.rs b/node/network/availability-recovery/src/tests.rs index 0deaa6362212..c77278f645c1 100644 --- a/node/network/availability-recovery/src/tests.rs +++ b/node/network/availability-recovery/src/tests.rs @@ -37,7 +37,7 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; use polkadot_node_subsystem_util::TimeoutExt; use polkadot_primitives::v2::{ - AuthorityDiscoveryId, Hash, HeadData, PersistedValidationData, ValidatorGroups, + AuthorityDiscoveryId, Hash, HeadData, IndexedVec, PersistedValidationData, ValidatorId, }; use polkadot_primitives_test_helpers::{dummy_candidate_receipt, dummy_hash}; @@ -181,7 +181,7 @@ impl Has { #[derive(Clone)] struct TestState { validators: Vec, - validator_public: Validators, + validator_public: IndexedVec, validator_authority_id: Vec, current: Hash, candidate: CandidateReceipt, @@ -220,7 +220,7 @@ impl TestState { validators: self.validator_public.clone(), discovery_keys: self.validator_authority_id.clone(), // all validators in the same group. - validator_groups: ValidatorGroups::from(vec![(0..self.validators.len()).map(|i| ValidatorIndex(i as _)).collect()]), + validator_groups: IndexedVec::>::from(vec![(0..self.validators.len()).map(|i| ValidatorIndex(i as _)).collect()]), assignment_keys: vec![], n_cores: 0, zeroth_delay_tranche_width: 0, @@ -404,7 +404,7 @@ impl TestState { } } -fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Validators { +fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> IndexedVec { val_ids.iter().map(|v| v.public().into()).collect() } diff --git a/node/network/collator-protocol/src/collator_side/tests.rs b/node/network/collator-protocol/src/collator_side/tests.rs index c468e2596e36..6575259b37b3 100644 --- a/node/network/collator-protocol/src/collator_side/tests.rs +++ b/node/network/collator-protocol/src/collator_side/tests.rs @@ -44,8 +44,8 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_test_helpers as test_helpers; use polkadot_node_subsystem_util::TimeoutExt; use polkadot_primitives::v2::{ - AuthorityDiscoveryId, CollatorPair, GroupIndex, GroupRotationInfo, ScheduledCore, SessionIndex, - SessionInfo, ValidatorIndex, Validators, + AuthorityDiscoveryId, CollatorPair, GroupIndex, GroupRotationInfo, IndexedVec, ScheduledCore, + SessionIndex, SessionInfo, ValidatorId, ValidatorIndex, }; use polkadot_primitives_test_helpers::TestCandidateBuilder; @@ -62,7 +62,7 @@ struct TestState { session_index: SessionIndex, } -fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Validators { +fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> IndexedVec { val_ids.iter().map(|v| v.public().into()).collect() } diff --git a/node/network/gossip-support/src/tests.rs b/node/network/gossip-support/src/tests.rs index f69c8fd9c51d..d5ce2accd770 100644 --- a/node/network/gossip-support/src/tests.rs +++ b/node/network/gossip-support/src/tests.rs @@ -37,7 +37,7 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers as test_helpers; use polkadot_node_subsystem_util::TimeoutExt as _; -use polkadot_primitives::v2::ValidatorGroups; +use polkadot_primitives::v2::IndexedVec; use test_helpers::mock::make_ferdie_keystore; use super::*; @@ -220,7 +220,9 @@ fn make_session_info() -> SessionInfo { validators: AUTHORITY_KEYRINGS.iter().map(|k| k.public().into()).collect(), discovery_keys: AUTHORITIES.clone(), assignment_keys: AUTHORITY_KEYRINGS.iter().map(|k| k.public().into()).collect(), - validator_groups: ValidatorGroups::from(vec![all_validator_indices]), + validator_groups: IndexedVec::>::from(vec![ + all_validator_indices, + ]), n_cores: 1, zeroth_delay_tranche_width: 1, relay_vrf_modulo_samples: 1, diff --git a/node/network/statement-distribution/src/lib.rs b/node/network/statement-distribution/src/lib.rs index 0d4a505b7fc8..e9380fb8b088 100644 --- a/node/network/statement-distribution/src/lib.rs +++ b/node/network/statement-distribution/src/lib.rs @@ -47,8 +47,8 @@ use polkadot_node_subsystem::{ }; use polkadot_primitives::v2::{ AuthorityDiscoveryId, CandidateHash, CommittedCandidateReceipt, CompactStatement, Hash, - SignedStatement, SigningContext, UncheckedSignedStatement, ValidatorIndex, ValidatorSignature, - Validators, + IndexedVec, SignedStatement, SigningContext, UncheckedSignedStatement, ValidatorId, + ValidatorIndex, ValidatorSignature, }; use futures::{ @@ -665,7 +665,7 @@ struct ActiveHeadData { /// Large statements we are waiting for with associated meta data. waiting_large_statements: HashMap, /// The parachain validators at the head's child session index. - validators: Validators, + validators: IndexedVec, /// The current session index of this fork. session_index: sp_staking::SessionIndex, /// How many `Seconded` statements we've seen per validator. @@ -676,7 +676,7 @@ struct ActiveHeadData { impl ActiveHeadData { fn new( - validators: Validators, + validators: IndexedVec, session_index: sp_staking::SessionIndex, span: PerLeafSpan, ) -> Self { diff --git a/node/network/statement-distribution/src/tests.rs b/node/network/statement-distribution/src/tests.rs index 4fffb0a6ddf5..ce3934063b26 100644 --- a/node/network/statement-distribution/src/tests.rs +++ b/node/network/statement-distribution/src/tests.rs @@ -36,7 +36,7 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers::mock::make_ferdie_keystore; use polkadot_primitives::v2::{ - Hash, Id as ParaId, SessionInfo, ValidationCode, ValidatorGroups, ValidatorId, + Hash, Id as ParaId, IndexedVec, SessionInfo, ValidationCode, ValidatorId, }; use polkadot_primitives_test_helpers::{ dummy_committed_candidate_receipt, dummy_hash, AlwaysZeroRng, @@ -2321,7 +2321,7 @@ fn handle_multiple_seconded_statements() { } fn make_session_info(validators: Vec, groups: Vec>) -> SessionInfo { - let validator_groups: ValidatorGroups = groups + let validator_groups: IndexedVec> = groups .iter() .map(|g| g.into_iter().map(|v| ValidatorIndex(*v)).collect()) .collect(); diff --git a/node/subsystem-util/src/runtime/mod.rs b/node/subsystem-util/src/runtime/mod.rs index 791d67eb2f7d..5b8baad0f94f 100644 --- a/node/subsystem-util/src/runtime/mod.rs +++ b/node/subsystem-util/src/runtime/mod.rs @@ -27,9 +27,9 @@ use sp_keystore::{CryptoStore, SyncCryptoStorePtr}; use polkadot_node_subsystem::{messages::RuntimeApiMessage, overseer, SubsystemSender}; use polkadot_primitives::v2::{ - CandidateEvent, CoreState, EncodeAs, GroupIndex, GroupRotationInfo, Hash, OccupiedCore, - ScrapedOnChainVotes, SessionIndex, SessionInfo, Signed, SigningContext, UncheckedSigned, - ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, Validators, + CandidateEvent, CoreState, EncodeAs, GroupIndex, GroupRotationInfo, Hash, IndexedVec, + OccupiedCore, ScrapedOnChainVotes, SessionIndex, SessionInfo, Signed, SigningContext, + UncheckedSigned, ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, }; use crate::{ @@ -228,7 +228,10 @@ impl RuntimeInfo { /// Get our `ValidatorIndex`. /// /// Returns: None if we are not a validator. - async fn get_our_index(&self, validators: &Validators) -> Option { + async fn get_our_index( + &self, + validators: &IndexedVec, + ) -> Option { let keystore = self.keystore.as_ref()?; for (i, v) in validators.iter().enumerate() { if CryptoStore::has_keys(&**keystore, &[(v.to_raw_vec(), ValidatorId::ID)]).await { diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 99ad06dbf9bf..f757238226cd 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -16,11 +16,11 @@ //! `V1` Primitives. -use std::marker::PhantomData; use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_std::{ + marker::PhantomData, prelude::*, slice::{Iter, IterMut}, vec::IntoIter, @@ -129,7 +129,9 @@ impl MallocSizeOf for ValidatorId { } /// Index of the validator is used as a lightweight replacement of the `ValidatorId` when appropriate. -#[derive(Eq, Ord, PartialEq, PartialOrd, Copy, Clone, Encode, Decode, TypeInfo, RuntimeDebug)] +#[derive( + Eq, Ord, PartialEq, PartialOrd, Copy, Clone, Encode, Decode, TypeInfo, RuntimeDebug, Default, +)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, MallocSizeOf))] pub struct ValidatorIndex(pub u32); @@ -140,6 +142,12 @@ impl From for ValidatorIndex { } } +impl From for u32 { + fn from(i: ValidatorIndex) -> Self { + i.0 + } +} + application_crypto::with_pair! { /// A Parachain validator keypair. pub type ValidatorPair = validator_app::Pair; @@ -784,6 +792,12 @@ impl From for CoreIndex { } } +impl From for u32 { + fn from(i: CoreIndex) -> Self { + i.0 + } +} + /// The unique (during session) index of a validator group. #[derive(Encode, Decode, Default, Clone, Copy, Debug, PartialEq, Eq, TypeInfo)] #[cfg_attr(feature = "std", derive(Hash, MallocSizeOf))] @@ -795,6 +809,12 @@ impl From for GroupIndex { } } +impl From for u32 { + fn from(i: GroupIndex) -> Self { + i.0 + } +} + /// A claim on authoring the next block for a given parathread. #[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)] #[cfg_attr(feature = "std", derive(PartialEq))] @@ -1574,28 +1594,39 @@ impl CompactStatement { } } -/// `Validators` struct indexed by `ValidatorIndex`. -#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] +/// `IndexedVec` struct indexed by type specific indexes. +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] -pub struct IndexedVec(Vec, PhantomData K>); +pub struct IndexedVec(Vec, PhantomData K>); -impl From> for IndexedVec { +impl Default for IndexedVec { + fn default() -> Self { + Self(vec![], PhantomData) + } +} + +impl From> for IndexedVec { fn from(validators: Vec) -> Self { Self(validators, PhantomData) } } -impl FromIterator for IndexedVec { +impl FromIterator for IndexedVec { fn from_iter>(iter: T) -> Self { Self(Vec::from_iter(iter), PhantomData) } } -impl IndexedVec { +impl IndexedVec +where + V: Clone, +{ /// Returns a reference to an element indexed using `ValidatorIndex`. pub fn get(&self, index: K) -> Option<&V> - where K: From{ - self.0.get(u32::from(index)) + where + K: Into, + { + self.0.get(index.into() as usize) } /// Returns number of elements in vector. @@ -1629,60 +1660,6 @@ impl IndexedVec { } } -/// `ValidatorGroups` struct indexed by `GroupIndex`. -#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)] -#[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] -pub struct ValidatorGroups(Vec>); - -impl From>> for ValidatorGroups { - fn from(group_validators: Vec>) -> Self { - ValidatorGroups(group_validators) - } -} - -impl FromIterator> for ValidatorGroups { - fn from_iter>>(iter: T) -> Self { - Self(Vec::from_iter(iter)) - } -} - -impl ValidatorGroups { - /// Returns a reference to an element indexed using `GroupIndex`. - pub fn get(&self, index: GroupIndex) -> Option<&Vec> { - self.0.get(index.0 as usize) - } - - /// Returns the number of validator groups. - pub fn len(&self) -> usize { - self.0.len() - } - - /// Returns contained vector. - pub fn to_vec(&self) -> Vec> { - self.0.clone() - } - - /// Returns an iterator over the groups. - pub fn iter(&self) -> Iter<'_, Vec> { - self.0.iter() - } - - /// Returns an iterator that allows modifying each group. - pub fn iter_mut(&mut self) -> IterMut<'_, Vec> { - self.0.iter_mut() - } - - /// Creates a consuming iterator. - pub fn into_iter(self) -> IntoIter> { - self.0.into_iter() - } - - /// Returns true if the underlying container is empty. - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } -} - /// The maximum number of validators `f` which may safely be faulty. /// /// The total number of validators is `n = 3f + e` where `e in { 1, 2, 3 }`. @@ -1717,7 +1694,7 @@ pub struct SessionInfo { /// [`max_validators`](https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148). /// /// `SessionInfo::validators` will be limited to to `max_validators` when set. - pub validators: Validators, + pub validators: IndexedVec, /// Validators' authority discovery keys for the session in canonical ordering. /// /// NOTE: The first `validators.len()` entries will match the corresponding validators in @@ -1740,7 +1717,7 @@ pub struct SessionInfo { /// Validators in shuffled ordering - these are the validator groups as produced /// by the `Scheduler` module for the session and are typically referred to by /// `GroupIndex`. - pub validator_groups: ValidatorGroups, + pub validator_groups: IndexedVec>, /// The number of availability cores used by the protocol during this session. pub n_cores: u32, /// The zeroth delay tranche width. @@ -1793,7 +1770,7 @@ pub struct OldV1SessionInfo { /// [`max_validators`](https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148). /// /// `SessionInfo::validators` will be limited to to `max_validators` when set. - pub validators: Validators, + pub validators: IndexedVec, /// Validators' authority discovery keys for the session in canonical ordering. /// /// NOTE: The first `validators.len()` entries will match the corresponding validators in @@ -1816,7 +1793,7 @@ pub struct OldV1SessionInfo { /// Validators in shuffled ordering - these are the validator groups as produced /// by the `Scheduler` module for the session and are typically referred to by /// `GroupIndex`. - pub validator_groups: ValidatorGroups, + pub validator_groups: IndexedVec>, /// The number of availability cores used by the protocol during this session. pub n_cores: u32, /// The zeroth delay tranche width. From f127cb4397dfc4f088e73ecaf9d12b5c0f17e2ad Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 02:38:47 +0100 Subject: [PATCH 25/41] Test and documentation fixes --- node/network/statement-distribution/src/tests.rs | 4 ++-- primitives/src/v2/mod.rs | 2 +- runtime/parachains/src/builder.rs | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/node/network/statement-distribution/src/tests.rs b/node/network/statement-distribution/src/tests.rs index ce3934063b26..08c432277cda 100644 --- a/node/network/statement-distribution/src/tests.rs +++ b/node/network/statement-distribution/src/tests.rs @@ -85,7 +85,7 @@ fn active_head_accepts_only_2_seconded_per_validator() { }; let mut head_data = ActiveHeadData::new( - Validators::from(validators), + IndexedVec::::from(validators), session_index, PerLeafSpan::new(Arc::new(jaeger::Span::Disabled), "test"), ); @@ -431,7 +431,7 @@ fn peer_view_update_sends_messages() { let new_head_data = { let mut data = ActiveHeadData::new( - Validators::from(validators), + IndexedVec::::from(validators), session_index, PerLeafSpan::new(Arc::new(jaeger::Span::Disabled), "test"), ); diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index f757238226cd..25b7c3cff50c 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1621,7 +1621,7 @@ impl IndexedVec where V: Clone, { - /// Returns a reference to an element indexed using `ValidatorIndex`. + /// Returns a reference to an element indexed using `K`. pub fn get(&self, index: K) -> Option<&V> where K: Into, diff --git a/runtime/parachains/src/builder.rs b/runtime/parachains/src/builder.rs index 045fb27857ec..f4bc58f2d4f2 100644 --- a/runtime/parachains/src/builder.rs +++ b/runtime/parachains/src/builder.rs @@ -25,9 +25,9 @@ use primitives::v2::{ collator_signature_payload, AvailabilityBitfield, BackedCandidate, CandidateCommitments, CandidateDescriptor, CandidateHash, CollatorId, CollatorSignature, CommittedCandidateReceipt, CompactStatement, CoreIndex, CoreOccupied, DisputeStatement, DisputeStatementSet, GroupIndex, - HeadData, Id as ParaId, InherentData as ParachainsInherentData, InvalidDisputeStatementKind, - PersistedValidationData, SessionIndex, SigningContext, UncheckedSigned, - ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, Validators, + HeadData, Id as ParaId, IndexedVec, InherentData as ParachainsInherentData, + InvalidDisputeStatementKind, PersistedValidationData, SessionIndex, SigningContext, + UncheckedSigned, ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, ValidityAttestation, }; use sp_core::{sr25519, H256}; @@ -66,7 +66,7 @@ fn byte32_slice_from(n: u32) -> [u8; 32] { /// Paras inherent `enter` benchmark scenario builder. pub(crate) struct BenchBuilder { /// Active validators. Validators should be declared prior to all other setup. - validators: Option, + validators: Option>, /// Starting block number; we expect it to get incremented on session setup. block_number: T::BlockNumber, /// Starting session; we expect it to get incremented on session setup. From 06ecd6ad56f5d2e32f615cab7675fdcecb8ad6b5 Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 02:51:46 +0100 Subject: [PATCH 26/41] minor test fix --- node/network/statement-distribution/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/network/statement-distribution/src/tests.rs b/node/network/statement-distribution/src/tests.rs index 08c432277cda..c1636557fdca 100644 --- a/node/network/statement-distribution/src/tests.rs +++ b/node/network/statement-distribution/src/tests.rs @@ -36,7 +36,7 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers::mock::make_ferdie_keystore; use polkadot_primitives::v2::{ - Hash, Id as ParaId, IndexedVec, SessionInfo, ValidationCode, ValidatorId, + GroupIndex, Hash, Id as ParaId, IndexedVec, SessionInfo, ValidationCode, ValidatorId, }; use polkadot_primitives_test_helpers::{ dummy_committed_candidate_receipt, dummy_hash, AlwaysZeroRng, From efa227856d2b037cd973285b592bf6dc0789268d Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 03:02:22 +0100 Subject: [PATCH 27/41] minor test fix --- node/core/dispute-coordinator/src/tests.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node/core/dispute-coordinator/src/tests.rs b/node/core/dispute-coordinator/src/tests.rs index 6a2b3fb818bd..c6fe328d9537 100644 --- a/node/core/dispute-coordinator/src/tests.rs +++ b/node/core/dispute-coordinator/src/tests.rs @@ -58,9 +58,9 @@ use polkadot_node_subsystem::{ use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle}; use polkadot_primitives::v2::{ ApprovalVote, BlockNumber, CandidateCommitments, CandidateHash, CandidateReceipt, - DisputeStatement, Hash, Header, IndexedVec, MultiDisputeStatementSet, ScrapedOnChainVotes, - SessionIndex, SessionInfo, SigningContext, ValidDisputeStatementKind, ValidatorId, - ValidatorIndex, ValidatorSignature, + DisputeStatement, GroupIndex, Hash, Header, IndexedVec, MultiDisputeStatementSet, + ScrapedOnChainVotes, SessionIndex, SessionInfo, SigningContext, ValidDisputeStatementKind, + ValidatorId, ValidatorIndex, ValidatorSignature, }; use crate::{ From cfca9b449c43fb585ace6c2629664f84fd3675f4 Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 03:28:49 +0100 Subject: [PATCH 28/41] minor test fix --- node/network/gossip-support/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/network/gossip-support/src/tests.rs b/node/network/gossip-support/src/tests.rs index d5ce2accd770..4b2b91f7cdba 100644 --- a/node/network/gossip-support/src/tests.rs +++ b/node/network/gossip-support/src/tests.rs @@ -37,7 +37,7 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_test_helpers as test_helpers; use polkadot_node_subsystem_util::TimeoutExt as _; -use polkadot_primitives::v2::IndexedVec; +use polkadot_primitives::v2::{GroupIndex, IndexedVec}; use test_helpers::mock::make_ferdie_keystore; use super::*; From e6f685fb42d7c5874967cca08707623cd53620ba Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Fri, 21 Oct 2022 11:35:46 +0100 Subject: [PATCH 29/41] Update node/network/statement-distribution/src/lib.rs Co-authored-by: Andronik --- node/network/statement-distribution/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/network/statement-distribution/src/lib.rs b/node/network/statement-distribution/src/lib.rs index e9380fb8b088..c0ac09dbe3f8 100644 --- a/node/network/statement-distribution/src/lib.rs +++ b/node/network/statement-distribution/src/lib.rs @@ -2075,7 +2075,7 @@ impl StatementDistributionSubsystem { let our_group = &session_info .validator_groups .get(our_group) - .expect(""); + .expect("`our_group` is derived from `validator_groups`; qed"); our_group .into_iter() From 33e3102c6d2e78d8ca7e04815ff113196124bc34 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Fri, 21 Oct 2022 11:37:00 +0100 Subject: [PATCH 30/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 25b7c3cff50c..35c37711f95a 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1654,7 +1654,7 @@ where self.0.into_iter() } - /// Returns true if contained Vector is empty + /// Returns true if the underlying container is empty. pub fn is_empty(&self) -> bool { self.0.is_empty() } From e4d548b8d68b1f0c31d2e102aa88cf54a42ecb67 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Fri, 21 Oct 2022 11:37:38 +0100 Subject: [PATCH 31/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 35c37711f95a..5a534b500a00 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1639,7 +1639,7 @@ where self.0.clone() } - /// Returns Iterator of contained vector. + /// Returns an iterator over the underlying vector. pub fn iter(&self) -> Iter<'_, V> { self.0.iter() } From dee6396652c1e089d6c64be8cff468c474a7a6b2 Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 11:43:00 +0100 Subject: [PATCH 32/41] removed IterMut --- primitives/src/v2/mod.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 25b7c3cff50c..2b508c9196e3 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -19,12 +19,7 @@ use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; -use sp_std::{ - marker::PhantomData, - prelude::*, - slice::{Iter, IterMut}, - vec::IntoIter, -}; +use sp_std::{marker::PhantomData, prelude::*, slice::Iter, vec::IntoIter}; use application_crypto::KeyTypeId; use inherents::InherentIdentifier; @@ -1644,11 +1639,6 @@ where self.0.iter() } - /// Returns a mutable Iterator. - pub fn iter_mut(&mut self) -> IterMut<'_, V> { - self.0.iter_mut() - } - /// Returns an Iterator with ownership. pub fn into_iter(self) -> IntoIter { self.0.into_iter() From 956a62e546ff5d07a2fda5f339595a5258ea5f01 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Fri, 21 Oct 2022 11:43:17 +0100 Subject: [PATCH 33/41] Update node/core/dispute-coordinator/src/import.rs Co-authored-by: Andronik --- node/core/dispute-coordinator/src/import.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/core/dispute-coordinator/src/import.rs b/node/core/dispute-coordinator/src/import.rs index 1c093e2d3563..8eb3d173dcf7 100644 --- a/node/core/dispute-coordinator/src/import.rs +++ b/node/core/dispute-coordinator/src/import.rs @@ -488,7 +488,7 @@ impl ImportResult { for (index, sig) in approval_votes.into_iter() { debug_assert!( { - let pub_key = &env.session_info().validators.get(index).expect(""); + 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) From 1aba47756e7e7702986555fc96c9a638b4147be2 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Fri, 21 Oct 2022 11:43:35 +0100 Subject: [PATCH 34/41] Update node/core/dispute-coordinator/src/initialized.rs Co-authored-by: Andronik --- node/core/dispute-coordinator/src/initialized.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/core/dispute-coordinator/src/initialized.rs b/node/core/dispute-coordinator/src/initialized.rs index b33baaece47a..a11cd267779c 100644 --- a/node/core/dispute-coordinator/src/initialized.rs +++ b/node/core/dispute-coordinator/src/initialized.rs @@ -1097,7 +1097,7 @@ impl Initialized { valid, candidate_hash, session, - env.validators().get(*index).expect("").clone(), + env.validators().get(*index).expect("`controlled_indices` are derived from `validators`; qed").clone(), ) .await; From 37cccf67e408efcb4c4b9e9a3b993b091a9bfb2d Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Fri, 21 Oct 2022 11:46:31 +0100 Subject: [PATCH 35/41] Update primitives/src/v2/mod.rs Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index a4e7e4c375d2..749255685e55 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1639,7 +1639,7 @@ where self.0.iter() } - /// Returns an Iterator with ownership. + /// Creates a consuming iterator. pub fn into_iter(self) -> IntoIter { self.0.into_iter() } From f03adc08ae91ad92e41f86dfb09eda106a2cc71e Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 11:47:55 +0100 Subject: [PATCH 36/41] fmt --- node/core/dispute-coordinator/src/initialized.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/node/core/dispute-coordinator/src/initialized.rs b/node/core/dispute-coordinator/src/initialized.rs index a11cd267779c..9229f61f3a78 100644 --- a/node/core/dispute-coordinator/src/initialized.rs +++ b/node/core/dispute-coordinator/src/initialized.rs @@ -1097,7 +1097,10 @@ impl Initialized { valid, candidate_hash, session, - env.validators().get(*index).expect("`controlled_indices` are derived from `validators`; qed").clone(), + env.validators() + .get(*index) + .expect("`controlled_indices` are derived from `validators`; qed") + .clone(), ) .await; From bdc316c986f9d398e7840253571422a34b2a0946 Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 11:59:29 +0100 Subject: [PATCH 37/41] IterMut --- primitives/src/v2/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 749255685e55..88a904eba487 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -19,7 +19,12 @@ use bitvec::vec::BitVec; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; -use sp_std::{marker::PhantomData, prelude::*, slice::Iter, vec::IntoIter}; +use sp_std::{ + marker::PhantomData, + prelude::*, + slice::{Iter, IterMut}, + vec::IntoIter, +}; use application_crypto::KeyTypeId; use inherents::InherentIdentifier; @@ -1639,6 +1644,11 @@ where self.0.iter() } + /// Returns a mutable Iterator. + pub fn iter_mut(&mut self) -> IterMut<'_, V> { + self.0.iter_mut() + } + /// Creates a consuming iterator. pub fn into_iter(self) -> IntoIter { self.0.into_iter() From 5ca768fbbe833cdf835b1197c45ce0e86cd4e205 Mon Sep 17 00:00:00 2001 From: Boluwatife Bakre Date: Fri, 21 Oct 2022 13:08:48 +0100 Subject: [PATCH 38/41] documentation update Co-authored-by: Andronik --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 88a904eba487..d5e993fc0fa8 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -1644,7 +1644,7 @@ where self.0.iter() } - /// Returns a mutable Iterator. + /// Returns a mutable iterator over the underlying vector. pub fn iter_mut(&mut self) -> IterMut<'_, V> { self.0.iter_mut() } From 2ee57d4400a730bebb7ee7ec25489156329dc0dd Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 13:13:11 +0100 Subject: [PATCH 39/41] minor adjustments and new TypeIndex trait --- primitives/src/v2/mod.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 88a904eba487..d2f30c4c3e2d 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -128,10 +128,14 @@ impl MallocSizeOf for ValidatorId { } } +/// Trait required for type specific indices e.g. `ValidatorIndex` and `GroupIndex` +pub trait TypeIndex { + ///Converts `Self` to usize. + fn into_usize(self) -> usize; +} + /// Index of the validator is used as a lightweight replacement of the `ValidatorId` when appropriate. -#[derive( - Eq, Ord, PartialEq, PartialOrd, Copy, Clone, Encode, Decode, TypeInfo, RuntimeDebug, Default, -)] +#[derive(Eq, Ord, PartialEq, PartialOrd, Copy, Clone, Encode, Decode, TypeInfo, RuntimeDebug)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, MallocSizeOf))] pub struct ValidatorIndex(pub u32); @@ -142,9 +146,9 @@ impl From for ValidatorIndex { } } -impl From for u32 { - fn from(i: ValidatorIndex) -> Self { - i.0 +impl TypeIndex for ValidatorIndex { + fn into_usize(self) -> usize { + self.0 as usize } } @@ -792,9 +796,9 @@ impl From for CoreIndex { } } -impl From for u32 { - fn from(i: CoreIndex) -> Self { - i.0 +impl TypeIndex for CoreIndex { + fn into_usize(self) -> usize { + self.0 as usize } } @@ -809,9 +813,9 @@ impl From for GroupIndex { } } -impl From for u32 { - fn from(i: GroupIndex) -> Self { - i.0 +impl TypeIndex for GroupIndex { + fn into_usize(self) -> usize { + self.0 as usize } } @@ -1594,7 +1598,7 @@ impl CompactStatement { } } -/// `IndexedVec` struct indexed by type specific indexes. +/// `IndexedVec` struct indexed by type specific indices. #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] pub struct IndexedVec(Vec, PhantomData K>); @@ -1624,9 +1628,9 @@ where /// Returns a reference to an element indexed using `K`. pub fn get(&self, index: K) -> Option<&V> where - K: Into, + K: TypeIndex, { - self.0.get(index.into() as usize) + self.0.get(index.into_usize()) } /// Returns number of elements in vector. From 6d8795425c4275cb504b0bf59360cefbb59fb3ab Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 13:21:05 +0100 Subject: [PATCH 40/41] spelling fix --- primitives/src/v2/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index d44b1ab489b1..288291dd4429 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -130,7 +130,7 @@ impl MallocSizeOf for ValidatorId { /// Trait required for type specific indices e.g. `ValidatorIndex` and `GroupIndex` pub trait TypeIndex { - ///Converts `Self` to usize. + ///Converts `Self` to `usize`. fn into_usize(self) -> usize; } From a2ae881a9afb71009fd17d4c3c991ec4faba06be Mon Sep 17 00:00:00 2001 From: Tife Date: Fri, 21 Oct 2022 14:49:41 +0100 Subject: [PATCH 41/41] TypeIndex fix --- primitives/src/v2/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/primitives/src/v2/mod.rs b/primitives/src/v2/mod.rs index 288291dd4429..ea1f4fcca0b9 100644 --- a/primitives/src/v2/mod.rs +++ b/primitives/src/v2/mod.rs @@ -130,8 +130,8 @@ impl MallocSizeOf for ValidatorId { /// Trait required for type specific indices e.g. `ValidatorIndex` and `GroupIndex` pub trait TypeIndex { - ///Converts `Self` to `usize`. - fn into_usize(self) -> usize; + /// Returns the index associated to this value. + fn type_index(&self) -> usize; } /// Index of the validator is used as a lightweight replacement of the `ValidatorId` when appropriate. @@ -147,7 +147,7 @@ impl From for ValidatorIndex { } impl TypeIndex for ValidatorIndex { - fn into_usize(self) -> usize { + fn type_index(&self) -> usize { self.0 as usize } } @@ -797,7 +797,7 @@ impl From for CoreIndex { } impl TypeIndex for CoreIndex { - fn into_usize(self) -> usize { + fn type_index(&self) -> usize { self.0 as usize } } @@ -814,7 +814,7 @@ impl From for GroupIndex { } impl TypeIndex for GroupIndex { - fn into_usize(self) -> usize { + fn type_index(&self) -> usize { self.0 as usize } } @@ -1630,7 +1630,7 @@ where where K: TypeIndex, { - self.0.get(index.into_usize()) + self.0.get(index.type_index()) } /// Returns number of elements in vector.