-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Use a more typesafe approach for managing indexed data #5503
Changes from 14 commits
41a64b8
53ede12
927900f
0531b9d
4db6034
5b5875a
1864bfd
ea5ff34
9cf63d2
e0c332f
3fd4e1c
4f8f464
8d89add
1ca2a12
3525a4e
731bd7a
72087ec
176800c
8d9a856
49ebd7a
6e459e1
d3ebca9
a601db8
d51852f
d4a584c
ff5a99e
c51ca88
6343736
dcd6e8e
e441256
5ff6d05
6ed1f84
42883ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,9 +17,11 @@ | |||||
| //! `V1` Primitives. | ||||||
|
|
||||||
| use bitvec::vec::BitVec; | ||||||
| use parity_scale_codec::{Decode, Encode}; | ||||||
| use scale_info::TypeInfo; | ||||||
| use parity_scale_codec::{Decode, Encode, WrapperTypeDecode, WrapperTypeEncode}; | ||||||
| use scale_info::{Type, TypeInfo}; | ||||||
| use sp_std::prelude::*; | ||||||
| use std::ops::{Deref, DerefMut}; | ||||||
| use typed_index_collections::TiVec; | ||||||
|
|
||||||
| use application_crypto::KeyTypeId; | ||||||
| use inherents::InherentIdentifier; | ||||||
|
|
@@ -53,9 +55,11 @@ pub use sp_staking::SessionIndex; | |||||
|
|
||||||
| /// Signed data. | ||||||
| mod signed; | ||||||
|
|
||||||
| pub use signed::{EncodeAs, Signed, UncheckedSigned}; | ||||||
|
|
||||||
| mod metrics; | ||||||
|
|
||||||
| pub use metrics::{ | ||||||
| metric_definitions, RuntimeMetricLabel, RuntimeMetricLabelValue, RuntimeMetricLabelValues, | ||||||
| RuntimeMetricLabels, RuntimeMetricOp, RuntimeMetricUpdate, | ||||||
|
|
@@ -135,6 +139,20 @@ impl From<u32> for ValidatorIndex { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl From<ValidatorIndex> for usize { | ||||||
| fn from(n: ValidatorIndex) -> Self { | ||||||
| n.0 as usize | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl From<usize> for ValidatorIndex { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need an impl for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While working on this, the error: came up. This is why I likely implemented the |
||||||
| fn from(n: usize) -> Self { | ||||||
| // can't panic, so need to truncate | ||||||
| let n = n.try_into().unwrap_or(u32::MAX); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. debug_assert would be good.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would I implement this? |
||||||
| ValidatorIndex(n) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| application_crypto::with_pair! { | ||||||
| /// A Parachain validator keypair. | ||||||
| pub type ValidatorPair = validator_app::Pair; | ||||||
|
|
@@ -790,6 +808,14 @@ impl From<u32> for GroupIndex { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl From<usize> for GroupIndex { | ||||||
| fn from(n: usize) -> Self { | ||||||
| // can't panic, so need to truncate | ||||||
| let n = n.try_into().unwrap_or(u32::MAX); | ||||||
| GroupIndex(n) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// A claim on authoring the next block for a given parathread. | ||||||
| #[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)] | ||||||
| #[cfg_attr(feature = "std", derive(PartialEq))] | ||||||
|
|
@@ -1394,9 +1420,11 @@ pub type CheckedMultiDisputeStatementSet = Vec<CheckedDisputeStatementSet>; | |||||
| #[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, TypeInfo)] | ||||||
| pub struct DisputeState<N = BlockNumber> { | ||||||
| /// A bitfield indicating all validators for the candidate. | ||||||
| pub validators_for: BitVec<u8, bitvec::order::Lsb0>, // one bit per validator. | ||||||
| pub validators_for: BitVec<u8, bitvec::order::Lsb0>, | ||||||
| // one bit per validator. | ||||||
|
||||||
| /// A bitfield indicating all validators against the candidate. | ||||||
| pub validators_against: BitVec<u8, bitvec::order::Lsb0>, // one bit per validator. | ||||||
| pub validators_against: BitVec<u8, bitvec::order::Lsb0>, | ||||||
| // one bit per validator. | ||||||
| /// The block number at which the dispute started on-chain. | ||||||
| pub start: N, | ||||||
| /// The block number at which the dispute concluded on-chain. | ||||||
|
|
@@ -1582,6 +1610,64 @@ pub fn supermajority_threshold(n: usize) -> usize { | |||||
| n - byzantine_threshold(n) | ||||||
| } | ||||||
|
|
||||||
| #[derive(Clone, RuntimeDebug)] | ||||||
| #[cfg_attr(feature = "std", derive(PartialEq))] | ||||||
| pub struct TypeVec<K: From<usize>, V: Clone>(TiVec<K, V>); | ||||||
|
|
||||||
| impl<K: From<usize>, V: Clone> TypeVec<K, V> { | ||||||
| pub fn new() -> Self { | ||||||
| TypeVec::<K, V> { 0: TiVec::<K, V>::new() } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl<K: From<usize>, V: Clone> From<Vec<V>> for TypeVec<K, V> { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| fn from(vec: Vec<V>) -> Self { | ||||||
tifecool marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| let mut type_vec: TypeVec<K, V> = TypeVec::new(); | ||||||
| for value in vec.iter() { | ||||||
| type_vec.0.push(value.clone()) | ||||||
tifecool marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
| type_vec | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[cfg(feature = "std")] | ||||||
| impl<K: From<usize>, V: Clone> MallocSizeOf for TypeVec<K, V> { | ||||||
| fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { | ||||||
| 0 | ||||||
|
||||||
| } | ||||||
| fn constant_size() -> Option<usize> { | ||||||
| Some(0) | ||||||
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl<K: From<usize>, V: Clone> Deref for TypeVec<K, V> { | ||||||
| type Target = Vec<V>; | ||||||
|
|
||||||
| fn deref(&self) -> &Self::Target { | ||||||
| &self.0.raw | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl<K: From<usize>, V: Clone> DerefMut for TypeVec<K, V> { | ||||||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||||||
| &mut self.0.raw | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl<K: From<usize>, V: Clone> WrapperTypeEncode for TypeVec<K, V> {} | ||||||
|
|
||||||
| impl<K: From<usize>, V: Clone> WrapperTypeDecode for TypeVec<K, V> { | ||||||
| type Wrapped = Vec<V>; | ||||||
| } | ||||||
|
|
||||||
| impl<K: From<usize>, V: Clone> TypeInfo for TypeVec<K, V> { | ||||||
| type Identity = (); | ||||||
|
|
||||||
| fn type_info() -> Type { | ||||||
| todo!() | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Must be fixed before merge
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not quite sure how to implement this trait. |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Information about validator sets of a session. | ||||||
| #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] | ||||||
| #[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))] | ||||||
|
|
@@ -1603,7 +1689,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: TypeVec<ValidatorIndex, ValidatorId>, | ||||||
| /// Validators' authority discovery keys for the session in canonical ordering. | ||||||
| /// | ||||||
| /// NOTE: The first `validators.len()` entries will match the corresponding validators in | ||||||
|
|
@@ -1620,13 +1706,13 @@ pub struct SessionInfo { | |||||
| /// | ||||||
| /// Therefore: | ||||||
| /// ```ignore | ||||||
| /// assignment_keys.len() == validators.len() && validators.len() <= discovery_keys.len() | ||||||
| /// ``` | ||||||
| /// assignment_keys.len() == validators.len() && validators.len() <= discovery_keys.len() | ||||||
| /// ``` | ||||||
| pub assignment_keys: Vec<AssignmentId>, | ||||||
| /// 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: TypeVec<GroupIndex, Vec<ValidatorIndex>>, | ||||||
| /// The number of availability cores used by the protocol during this session. | ||||||
| pub n_cores: u32, | ||||||
| /// The zeroth delay tranche width. | ||||||
|
|
@@ -1679,7 +1765,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: TypeVec<ValidatorIndex, ValidatorId>, | ||||||
| /// Validators' authority discovery keys for the session in canonical ordering. | ||||||
| /// | ||||||
| /// NOTE: The first `validators.len()` entries will match the corresponding validators in | ||||||
|
|
@@ -1696,13 +1782,13 @@ pub struct OldV1SessionInfo { | |||||
| /// | ||||||
| /// Therefore: | ||||||
| /// ```ignore | ||||||
| /// assignment_keys.len() == validators.len() && validators.len() <= discovery_keys.len() | ||||||
| /// ``` | ||||||
| /// assignment_keys.len() == validators.len() && validators.len() <= discovery_keys.len() | ||||||
| /// ``` | ||||||
| pub assignment_keys: Vec<AssignmentId>, | ||||||
| /// 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: TypeVec<GroupIndex, Vec<ValidatorIndex>>, | ||||||
| /// The number of availability cores used by the protocol during this session. | ||||||
| pub n_cores: u32, | ||||||
| /// The zeroth delay tranche width. | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should consider adding a custom macro that resembles the ergonomy of
vec![..]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't write macros.