Parachains shared.rs to Frame V2 (#3425)

* gotta migrate them all

* migrate rococo construct_runtime

* trigger ci

* fix warnings

* get mocks to work

* add pallet to test runtime

* comments

* calm down mr tabrizi lol
This commit is contained in:
ferrell-code
2021-07-22 17:59:31 -04:00
committed by GitHub
parent 26330640d2
commit 7788de8164
16 changed files with 114 additions and 106 deletions
+53 -48
View File
@@ -14,16 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! A module for any shared state that other pallets may want access to.
//! A pallet for any shared state that other pallets may want access to.
//!
//! To avoid cyclic dependencies, it is important that this module is not
//! dependent on any of the other modules.
//! To avoid cyclic dependencies, it is important that this pallet is not
//! dependent on any of the other pallets.
use primitives::v1::{SessionIndex, ValidatorId, ValidatorIndex};
use frame_support::{
decl_storage, decl_module, decl_error,
weights::Weight,
};
use frame_support::pallet_prelude::*;
use sp_std::vec::Vec;
use rand::{SeedableRng, seq::SliceRandom};
@@ -31,44 +28,52 @@ use rand_chacha::ChaCha20Rng;
use crate::configuration::HostConfiguration;
pub trait Config: frame_system::Config { }
pub use pallet::*;
// `SESSION_DELAY` is used to delay any changes to Paras registration or configurations.
// Wait until the session index is 2 larger then the current index to apply any changes,
// which guarantees that at least one full session has passed before any changes are applied.
pub(crate) const SESSION_DELAY: SessionIndex = 2;
decl_storage! {
trait Store for Module<T: Config> as ParasShared {
/// The current session index.
CurrentSessionIndex get(fn session_index): SessionIndex;
/// All the validators actively participating in parachain consensus.
/// Indices are into the broader validator set.
ActiveValidatorIndices get(fn active_validator_indices): Vec<ValidatorIndex>;
/// The parachain attestation keys of the validators actively participating in parachain consensus.
/// This should be the same length as `ActiveValidatorIndices`.
ActiveValidatorKeys get(fn active_validator_keys): Vec<ValidatorId>;
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {}
/// The current session index.
#[pallet::storage]
#[pallet::getter(fn session_index)]
pub(super) type CurrentSessionIndex<T: Config> = StorageValue<_, SessionIndex, ValueQuery>;
/// All the validators actively participating in parachain consensus.
/// Indices are into the broader validator set.
#[pallet::storage]
#[pallet::getter(fn active_validator_indices)]
pub(super) type ActiveValidatorIndices<T: Config> = StorageValue<_, Vec<ValidatorIndex>, ValueQuery>;
/// The parachain attestation keys of the validators actively participating in parachain consensus.
/// This should be the same length as `ActiveValidatorIndices`.
#[pallet::storage]
#[pallet::getter(fn active_validator_keys)]
pub(super) type ActiveValidatorKeys<T: Config> = StorageValue<_, Vec<ValidatorId>, ValueQuery>;
#[pallet::call]
impl<T: Config> Pallet<T> {}
}
decl_error! {
pub enum Error for Module<T: Config> { }
}
decl_module! {
/// The session info module.
pub struct Module<T: Config> for enum Call where origin: <T as frame_system::Config>::Origin {
type Error = Error<T>;
}
}
impl<T: Config> Module<T> {
/// Called by the initializer to initialize the configuration module.
impl<T: Config> Pallet<T> {
/// Called by the initializer to initialize the configuration pallet.
pub(crate) fn initializer_initialize(_now: T::BlockNumber) -> Weight {
0
}
/// Called by the initializer to finalize the configuration module.
/// Called by the initializer to finalize the configuration pallet.
pub(crate) fn initializer_finalize() { }
/// Called by the initializer to note that a new session has started.
@@ -80,7 +85,7 @@ impl<T: Config> Module<T> {
new_config: &HostConfiguration<T::BlockNumber>,
all_validators: Vec<ValidatorId>,
) -> Vec<ValidatorId> {
CurrentSessionIndex::set(session_index);
CurrentSessionIndex::<T>::set(session_index);
let mut rng: ChaCha20Rng = SeedableRng::from_seed(random_seed);
let mut shuffled_indices: Vec<_> = (0..all_validators.len())
@@ -99,8 +104,8 @@ impl<T: Config> Module<T> {
&all_validators,
);
ActiveValidatorIndices::set(shuffled_indices);
ActiveValidatorKeys::set(active_validator_keys.clone());
ActiveValidatorIndices::<T>::set(shuffled_indices);
ActiveValidatorKeys::<T>::set(active_validator_keys.clone());
active_validator_keys
}
@@ -113,15 +118,15 @@ impl<T: Config> Module<T> {
/// Test function for setting the current session index.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn set_session_index(index: SessionIndex) {
CurrentSessionIndex::set(index);
CurrentSessionIndex::<T>::set(index);
}
#[cfg(test)]
pub(crate) fn set_active_validators_ascending(active: Vec<ValidatorId>) {
ActiveValidatorIndices::set(
ActiveValidatorIndices::<T>::set(
(0..active.len()).map(|i| ValidatorIndex(i as _)).collect()
);
ActiveValidatorKeys::set(active);
ActiveValidatorKeys::<T>::set(active);
}
#[cfg(test)]
@@ -130,8 +135,8 @@ impl<T: Config> Module<T> {
keys: Vec<ValidatorId>,
) {
assert_eq!(indices.len(), keys.len());
ActiveValidatorIndices::set(indices);
ActiveValidatorKeys::set(keys);
ActiveValidatorIndices::<T>::set(indices);
ActiveValidatorKeys::<T>::set(keys);
}
}
@@ -139,7 +144,7 @@ impl<T: Config> Module<T> {
mod tests {
use super::*;
use crate::configuration::HostConfiguration;
use crate::mock::{new_test_ext, MockGenesisConfig, Shared};
use crate::mock::{new_test_ext, MockGenesisConfig, ParasShared};
use keyring::Sr25519Keyring;
fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Vec<ValidatorId> {
@@ -162,7 +167,7 @@ mod tests {
let pubkeys = validator_pubkeys(&validators);
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let validators = Shared::initializer_on_new_session(
let validators = ParasShared::initializer_on_new_session(
1,
[1; 32],
&config,
@@ -181,12 +186,12 @@ mod tests {
);
assert_eq!(
Shared::active_validator_keys(),
ParasShared::active_validator_keys(),
validators,
);
assert_eq!(
Shared::active_validator_indices(),
ParasShared::active_validator_indices(),
vec![
ValidatorIndex(4),
ValidatorIndex(1),
@@ -214,7 +219,7 @@ mod tests {
let pubkeys = validator_pubkeys(&validators);
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let validators = Shared::initializer_on_new_session(
let validators = ParasShared::initializer_on_new_session(
1,
[1; 32],
&config,
@@ -230,12 +235,12 @@ mod tests {
);
assert_eq!(
Shared::active_validator_keys(),
ParasShared::active_validator_keys(),
validators,
);
assert_eq!(
Shared::active_validator_indices(),
ParasShared::active_validator_indices(),
vec![
ValidatorIndex(4),
ValidatorIndex(1),