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

Use a more typesafe approach for managing indexed data #6150

Merged
merged 46 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
b5c02e8
Fix for issue #2403
tifecool Oct 13, 2022
954d392
Merge branch 'paritytech:master' into fix2403
tifecool Oct 13, 2022
ce8aba9
Nightly fmt
tifecool Oct 13, 2022
b5a7bfc
Quick documentation fixes
tifecool Oct 14, 2022
5da6693
Default Implementation
tifecool Oct 14, 2022
b90fc1e
iter() function integrated
tifecool Oct 14, 2022
557eebb
Implemented iter functionalities
tifecool Oct 15, 2022
09cc765
Fmt
tifecool Oct 15, 2022
3a6e327
small change
tifecool Oct 15, 2022
aba5837
updates node-network
tifecool Oct 15, 2022
dbac388
updates in dispute-coordinator
tifecool Oct 16, 2022
f1b8b4f
Updates
tifecool Oct 18, 2022
8696f93
benchmarking fix
tifecool Oct 18, 2022
4d10ca1
minor fix
tifecool Oct 18, 2022
95a3881
test fixes in runtime api
tifecool Oct 18, 2022
71fc436
Merge branch 'paritytech:master' into fix2403
tifecool Oct 18, 2022
4951b54
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
35c1304
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
a0edab4
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
3823be1
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
5714ad4
Update primitives/src/v2/mod.rs
tifecool Oct 19, 2022
7a8be34
Removal of [index], shorting of FromIterator, Renaming of GroupValida…
tifecool Oct 19, 2022
c3a1e72
Merge remote-tracking branch 'origin/fix2403' into fix2403
tifecool Oct 19, 2022
56b3904
Removal of ops import
tifecool Oct 19, 2022
a0bb41b
documentation fixes for spell check
tifecool Oct 19, 2022
ef5438c
implementation of generic type
tifecool Oct 20, 2022
4e825d0
Refactoring
tifecool Oct 21, 2022
f127cb4
Test and documentation fixes
tifecool Oct 21, 2022
06ecd6a
minor test fix
tifecool Oct 21, 2022
efa2278
minor test fix
tifecool Oct 21, 2022
cfca9b4
minor test fix
tifecool Oct 21, 2022
e6f685f
Update node/network/statement-distribution/src/lib.rs
tifecool Oct 21, 2022
33e3102
Update primitives/src/v2/mod.rs
tifecool Oct 21, 2022
e4d548b
Update primitives/src/v2/mod.rs
tifecool Oct 21, 2022
dee6396
removed IterMut
tifecool Oct 21, 2022
956a62e
Update node/core/dispute-coordinator/src/import.rs
tifecool Oct 21, 2022
1aba477
Update node/core/dispute-coordinator/src/initialized.rs
tifecool Oct 21, 2022
9025b12
Merge remote-tracking branch 'origin/fix2403' into fix2403
tifecool Oct 21, 2022
37cccf6
Update primitives/src/v2/mod.rs
tifecool Oct 21, 2022
f03adc0
fmt
tifecool Oct 21, 2022
bdc316c
IterMut
tifecool Oct 21, 2022
5ca768f
documentation update
tifecool Oct 21, 2022
2ee57d4
minor adjustments and new TypeIndex trait
tifecool Oct 21, 2022
be785f9
Merge remote-tracking branch 'origin/fix2403' into fix2403
tifecool Oct 21, 2022
6d87954
spelling fix
tifecool Oct 21, 2022
a2ae881
TypeIndex fix
tifecool Oct 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions node/primitives/src/disputes/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.0 as usize).ok_or(())?.clone();
let validator_public = session_info.validators.get(validator_index).ok_or(())?.clone();

(
SignedDisputeStatement::new_checked(
Expand All @@ -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.0 as usize).ok_or(())?.clone();
let validator_public = session_info.validators.get(validator_index).ok_or(())?.clone();

(
SignedDisputeStatement::new_checked(
Expand Down
6 changes: 3 additions & 3 deletions node/subsystem-util/src/rolling_session_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{GroupValidators, Header, Validators};
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()),
tifecool marked this conversation as resolved.
Show resolved Hide resolved
n_cores: index as _,
zeroth_delay_tranche_width: index as _,
relay_vrf_modulo_samples: index as _,
Expand Down
6 changes: 3 additions & 3 deletions node/subsystem-util/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ impl RuntimeInfo {
///
/// Returns: `None` if not a parachain validator.
async fn get_validator_info(&self, session_info: &SessionInfo) -> Result<ValidatorInfo> {
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 {
tifecool marked this conversation as resolved.
Show resolved Hide resolved
// 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)| {
tifecool marked this conversation as resolved.
Show resolved Hide resolved
g.iter().find_map(|v| {
if *v == our_index {
Some(GroupIndex(i as u32))
Expand Down Expand Up @@ -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))
}
Expand Down
82 changes: 77 additions & 5 deletions primitives/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +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, prelude::*};

use application_crypto::KeyTypeId;
use inherents::InherentIdentifier;
Expand Down Expand Up @@ -1569,6 +1569,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<ValidatorId>);

impl Index<ValidatorIndex> for Validators {
type Output = ValidatorId;

fn index(&self, index: ValidatorIndex) -> &Self::Output {
&self.0.get(index.0 as usize).unwrap()
tifecool marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl From<Vec<ValidatorId>> for Validators {
fn from(validators: Vec<ValidatorId>) -> Self {
Validators(validators)
}
}

impl Validators {
tifecool marked this conversation as resolved.
Show resolved Hide resolved
/// 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.
tifecool marked this conversation as resolved.
Show resolved Hide resolved
pub fn len(&self) -> usize {
self.0.len()
}

///Returns contained vector
tifecool marked this conversation as resolved.
Show resolved Hide resolved
pub fn to_vec(&self) -> Vec<ValidatorId> {
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<Vec<ValidatorIndex>>);
tifecool marked this conversation as resolved.
Show resolved Hide resolved

impl Index<GroupIndex> for GroupValidators {
type Output = Vec<ValidatorIndex>;

fn index(&self, index: GroupIndex) -> &Self::Output {
self.0.get(index.0 as usize).unwrap()
}
}

impl From<Vec<Vec<ValidatorIndex>>> for GroupValidators {
fn from(group_validators: Vec<Vec<ValidatorIndex>>) -> Self {
GroupValidators(group_validators)
}
}

impl GroupValidators {
/// Returns a reference to an element indexed using GroupIndex.
tifecool marked this conversation as resolved.
Show resolved Hide resolved
pub fn get(&self, index: GroupIndex) -> Option<&Vec<ValidatorIndex>> {
self.0.get(index.0 as usize)
}

///Returns number of elements in vector.
tifecool marked this conversation as resolved.
Show resolved Hide resolved
pub fn len(&self) -> usize {
self.0.len()
}

///Returns contained vector
tifecool marked this conversation as resolved.
Show resolved Hide resolved
pub fn to_vec(&self) -> Vec<Vec<ValidatorIndex>> {
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 }`.
Expand Down Expand Up @@ -1603,7 +1675,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<ValidatorId>,
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
Expand All @@ -1626,7 +1698,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<Vec<ValidatorIndex>>,
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.
Expand Down Expand Up @@ -1679,7 +1751,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<ValidatorId>,
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
Expand All @@ -1702,7 +1774,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<Vec<ValidatorIndex>>,
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.
Expand Down
12 changes: 7 additions & 5 deletions runtime/parachains/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use primitives::v2::{
CompactStatement, CoreIndex, CoreOccupied, DisputeStatement, DisputeStatementSet, GroupIndex,
HeadData, Id as ParaId, InherentData as ParachainsInherentData, InvalidDisputeStatementKind,
PersistedValidationData, SessionIndex, SigningContext, UncheckedSigned,
ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, ValidityAttestation,
ValidDisputeStatementKind, ValidationCode, ValidatorId, ValidatorIndex, Validators,
ValidityAttestation,
};
use sp_core::{sr25519, H256};
use sp_runtime::{
Expand Down Expand Up @@ -65,7 +66,7 @@ fn byte32_slice_from(n: u32) -> [u8; 32] {
/// Paras inherent `enter` benchmark scenario builder.
pub(crate) struct BenchBuilder<T: paras_inherent::Config> {
/// Active validators. Validators should be declared prior to all other setup.
validators: Option<Vec<ValidatorId>>,
validators: Option<Validators>,
/// 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.
Expand Down Expand Up @@ -410,7 +411,7 @@ impl<T: paras_inherent::Config> BenchBuilder<T> {
assert_eq!(<shared::Pallet<T>>::session_index(), target_session);

// We need to refetch validators since they have been shuffled.
let validators_shuffled: Vec<_> = session_info::Pallet::<T>::session_info(target_session)
let validators_shuffled = session_info::Pallet::<T>::session_info(target_session)
.unwrap()
.validators
.clone();
Expand Down Expand Up @@ -438,6 +439,7 @@ impl<T: paras_inherent::Config> BenchBuilder<T> {
let availability_bitvec = Self::availability_bitvec(concluding_cores, total_cores);

let bitfields: Vec<UncheckedSigned<AvailabilityBitfield>> = validators
.to_vec()
.iter()
.enumerate()
.map(|(i, public)| {
Expand Down Expand Up @@ -549,7 +551,7 @@ impl<T: paras_inherent::Config> BenchBuilder<T> {
.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::<CompactStatement>::benchmark_sign(
public,
CompactStatement::Valid(candidate_hash.clone()),
Expand Down Expand Up @@ -606,7 +608,7 @@ impl<T: paras_inherent::Config> BenchBuilder<T> {
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.
Expand Down
3 changes: 1 addition & 2 deletions runtime/parachains/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,7 @@ impl<T: Config> Pallet<T> {
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);
continue
Expand Down
2 changes: 1 addition & 1 deletion runtime/parachains/src/disputes/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
<UnappliedSlashes<T>>::insert(session_index, candidate_hash, unapplied);
Expand Down
4 changes: 2 additions & 2 deletions runtime/parachains/src/session_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ impl<T: Config> Pallet<T> {

let dispute_period = config.dispute_period;

let validators = notification.validators.clone();
let validators = notification.validators.clone().into();
let discovery_keys = <T as AuthorityDiscoveryConfig>::authorities();
let assignment_keys = AssignmentKeysUnsafe::<T>::get();
let active_set = <shared::Pallet<T>>::active_validator_indices();

let validator_groups = <scheduler::Pallet<T>>::validator_groups();
let validator_groups = <scheduler::Pallet<T>>::validator_groups().into();
let n_cores = <scheduler::Pallet<T>>::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;
Expand Down
2 changes: 1 addition & 1 deletion runtime/parachains/src/session_info/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn session_info_active_subsets() {
});
let session = Sessions::<Test>::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),
Expand Down