mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 05:51:02 +00:00
Scheduler Module (#1162)
* scheduler module skeleton * update scheduler skeleton to match latest version of guide * better session change notification * add mock randomness and fix test compilation * shuffle validators into groups * finish implementing session change logic for scheduler * tweak core assignment type to track retries of parathread * reframe queued parathread core as offset * implement initialzation and finalization routines * implement parathread claim queuing * implement core_para * implement the group_validators routine and fix errors * add a reason for freeing cores * implement `schedule` function * add some docs to the scheduled function * implement `occupied` helper * implement availability predicate * fix some warnings * integrate scheduler into initializer * integrate scheduler into mock module * avoid conflict with Substrate's scheduler storage * add parathreads index to paras module * implement parathreads map in paras module * add is_parathread to paras * test adding parathread claim * test that you cannot add claims when no parathread cores exist * check session change parathread queue pruning * test validator shuffling * add allow_unused to scheduler items * add test for scheduling * add some more tests for scheduling logic * test core rotation * check parathread claim pruning after retries * add bound notes * Apply suggestions from code review Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> Co-authored-by: Bernhard Schuster <bernhard@ahoi.io> * more suggestions from review * test availability predicate, add box to please compiler * add changes to guide Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> Co-authored-by: Bernhard Schuster <bernhard@ahoi.io>
This commit is contained in:
committed by
GitHub
parent
053bfc2d0c
commit
04c8603042
@@ -35,6 +35,9 @@ frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch =
|
||||
primitives = { package = "polkadot-primitives", path = "../../primitives", default-features = false }
|
||||
libsecp256k1 = { version = "0.3.2", default-features = false, optional = true }
|
||||
|
||||
rand = { version = "0.7", default-features = false }
|
||||
rand_chacha = { version = "0.2.2", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
hex-literal = "0.2.1"
|
||||
keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
|
||||
@@ -33,7 +33,7 @@ use system::ensure_root;
|
||||
/// All configuration of the runtime with respect to parachains and parathreads.
|
||||
#[derive(Clone, Encode, Decode, PartialEq, Default)]
|
||||
#[cfg_attr(test, derive(Debug))]
|
||||
pub struct HostConfiguration<BlockNumber: Default> {
|
||||
pub struct HostConfiguration<BlockNumber> {
|
||||
/// The minimum frequency at which parachains can update their validation code.
|
||||
pub validation_upgrade_frequency: BlockNumber,
|
||||
/// The delay, in blocks, before a validation upgrade is applied.
|
||||
@@ -49,7 +49,7 @@ pub struct HostConfiguration<BlockNumber: Default> {
|
||||
pub parathread_cores: u32,
|
||||
/// The number of retries that a parathread author has to submit their block.
|
||||
pub parathread_retries: u32,
|
||||
/// How often parachain groups should be rotated across parachains.
|
||||
/// How often parachain groups should be rotated across parachains. Must be non-zero.
|
||||
pub parachain_rotation_frequency: BlockNumber,
|
||||
/// The availability period, in blocks, for parachains. This is the amount of blocks
|
||||
/// after inclusion that validators have to make the block available and signal its availability to
|
||||
|
||||
@@ -25,11 +25,29 @@ use primitives::{
|
||||
parachain::{ValidatorId},
|
||||
};
|
||||
use frame_support::{
|
||||
decl_storage, decl_module, decl_error,
|
||||
decl_storage, decl_module, decl_error, traits::Randomness,
|
||||
};
|
||||
use crate::{configuration, paras};
|
||||
use crate::{configuration::{self, HostConfiguration}, paras, scheduler};
|
||||
|
||||
pub trait Trait: system::Trait + configuration::Trait + paras::Trait { }
|
||||
/// Information about a session change that has just occurred.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct SessionChangeNotification<BlockNumber> {
|
||||
/// The new validators in the session.
|
||||
pub validators: Vec<ValidatorId>,
|
||||
/// The qeueud validators for the following session.
|
||||
pub queued: Vec<ValidatorId>,
|
||||
/// The configuration before handling the session change
|
||||
pub prev_config: HostConfiguration<BlockNumber>,
|
||||
/// The configuration after handling the session change.
|
||||
pub new_config: HostConfiguration<BlockNumber>,
|
||||
/// A secure random seed for the session, gathered from BABE.
|
||||
pub random_seed: [u8; 32],
|
||||
}
|
||||
|
||||
pub trait Trait: system::Trait + configuration::Trait + paras::Trait + scheduler::Trait {
|
||||
/// A randomness beacon.
|
||||
type Randomness: Randomness<Self::Hash>;
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Trait> as Initializer {
|
||||
@@ -62,7 +80,8 @@ decl_module! {
|
||||
// - Inclusion
|
||||
// - Validity
|
||||
let total_weight = configuration::Module::<T>::initializer_initialize(now) +
|
||||
paras::Module::<T>::initializer_initialize(now);
|
||||
paras::Module::<T>::initializer_initialize(now) +
|
||||
scheduler::Module::<T>::initializer_initialize(now);
|
||||
|
||||
HasInitialized::set(Some(()));
|
||||
|
||||
@@ -70,6 +89,9 @@ decl_module! {
|
||||
}
|
||||
|
||||
fn on_finalize() {
|
||||
// reverse initialization order.
|
||||
|
||||
scheduler::Module::<T>::initializer_finalize();
|
||||
paras::Module::<T>::initializer_finalize();
|
||||
configuration::Module::<T>::initializer_finalize();
|
||||
HasInitialized::take();
|
||||
@@ -90,8 +112,32 @@ impl<T: Trait> Module<T> {
|
||||
let validators: Vec<_> = validators.map(|(_, v)| v).collect();
|
||||
let queued: Vec<_> = queued.map(|(_, v)| v).collect();
|
||||
|
||||
let prev_config = <configuration::Module<T>>::config();
|
||||
|
||||
let random_seed = {
|
||||
let mut buf = [0u8; 32];
|
||||
let random_hash = T::Randomness::random(&b"paras"[..]);
|
||||
let len = sp_std::cmp::min(32, random_hash.as_ref().len());
|
||||
buf[..len].copy_from_slice(&random_hash.as_ref()[..len]);
|
||||
buf
|
||||
};
|
||||
|
||||
// We can't pass the new config into the thing that determines the new config,
|
||||
// so we don't pass the `SessionChangeNotification` into this module.
|
||||
configuration::Module::<T>::initializer_on_new_session(&validators, &queued);
|
||||
paras::Module::<T>::initializer_on_new_session(&validators, &queued);
|
||||
|
||||
let new_config = <configuration::Module<T>>::config();
|
||||
|
||||
let notification = SessionChangeNotification {
|
||||
validators,
|
||||
queued,
|
||||
prev_config,
|
||||
new_config,
|
||||
random_seed,
|
||||
};
|
||||
|
||||
paras::Module::<T>::initializer_on_new_session(¬ification);
|
||||
scheduler::Module::<T>::initializer_on_new_session(¬ification);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ use primitives::{
|
||||
};
|
||||
use frame_support::{
|
||||
impl_outer_origin, impl_outer_dispatch, parameter_types,
|
||||
weights::Weight,
|
||||
weights::Weight, traits::Randomness as RandomnessT,
|
||||
};
|
||||
|
||||
/// A test runtime struct.
|
||||
@@ -47,6 +47,14 @@ impl_outer_dispatch! {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TestRandomness;
|
||||
|
||||
impl RandomnessT<H256> for TestRandomness {
|
||||
fn random(_subject: &[u8]) -> H256 {
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u32 = 250;
|
||||
pub const MaximumBlockWeight: Weight = 4 * 1024 * 1024;
|
||||
@@ -80,12 +88,16 @@ impl system::Trait for Test {
|
||||
type OnKilledAccount = ();
|
||||
}
|
||||
|
||||
impl crate::initializer::Trait for Test { }
|
||||
impl crate::initializer::Trait for Test {
|
||||
type Randomness = TestRandomness;
|
||||
}
|
||||
|
||||
impl crate::configuration::Trait for Test { }
|
||||
|
||||
impl crate::paras::Trait for Test { }
|
||||
|
||||
impl crate::scheduler::Trait for Test { }
|
||||
|
||||
pub type System = system::Module<Test>;
|
||||
|
||||
/// Mocked initializer.
|
||||
@@ -97,6 +109,9 @@ pub type Configuration = crate::configuration::Module<Test>;
|
||||
/// Mocked paras.
|
||||
pub type Paras = crate::paras::Module<Test>;
|
||||
|
||||
/// Mocked scheduler.
|
||||
pub type Scheduler = crate::scheduler::Module<Test>;
|
||||
|
||||
/// Create a new set of test externalities.
|
||||
pub fn new_test_ext(state: GenesisConfig) -> TestExternalities {
|
||||
let mut t = state.system.build_storage::<Test>().unwrap();
|
||||
|
||||
@@ -27,7 +27,7 @@ use sp_std::prelude::*;
|
||||
use sp_std::marker::PhantomData;
|
||||
use sp_runtime::traits::One;
|
||||
use primitives::{
|
||||
parachain::{ValidatorId, Id as ParaId, ValidationCode, HeadData},
|
||||
parachain::{Id as ParaId, ValidationCode, HeadData},
|
||||
};
|
||||
use frame_support::{
|
||||
decl_storage, decl_module, decl_error,
|
||||
@@ -35,7 +35,7 @@ use frame_support::{
|
||||
weights::Weight,
|
||||
};
|
||||
use codec::{Encode, Decode};
|
||||
use crate::configuration;
|
||||
use crate::{configuration, initializer::SessionChangeNotification};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Deserialize};
|
||||
@@ -159,11 +159,11 @@ impl<N: Ord + Copy> ParaPastCodeMeta<N> {
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
pub struct ParaGenesisArgs {
|
||||
/// The initial head data to use.
|
||||
genesis_head: HeadData,
|
||||
pub genesis_head: HeadData,
|
||||
/// The initial validation code to use.
|
||||
validation_code: ValidationCode,
|
||||
pub validation_code: ValidationCode,
|
||||
/// True if parachain, false if parathread.
|
||||
parachain: bool,
|
||||
pub parachain: bool,
|
||||
}
|
||||
|
||||
|
||||
@@ -171,6 +171,8 @@ decl_storage! {
|
||||
trait Store for Module<T: Trait> as Paras {
|
||||
/// All parachains. Ordered ascending by ParaId. Parathreads are not included.
|
||||
Parachains get(fn parachains): Vec<ParaId>;
|
||||
/// All parathreads.
|
||||
Parathreads: map hasher(twox_64_concat) ParaId => Option<()>;
|
||||
/// The head-data of every registered para.
|
||||
Heads get(fn parachain_head): map hasher(twox_64_concat) ParaId => Option<HeadData>;
|
||||
/// The validation code of every live para.
|
||||
@@ -253,7 +255,7 @@ impl<T: Trait> Module<T> {
|
||||
pub(crate) fn initializer_finalize() { }
|
||||
|
||||
/// Called by the initializer to note that a new session has started.
|
||||
pub(crate) fn initializer_on_new_session(_validators: &[ValidatorId], _queued: &[ValidatorId]) {
|
||||
pub(crate) fn initializer_on_new_session(_notification: &SessionChangeNotification<T::BlockNumber>) {
|
||||
let now = <system::Module<T>>::block_number();
|
||||
let mut parachains = Self::clean_up_outgoing(now);
|
||||
Self::apply_incoming(&mut parachains);
|
||||
@@ -268,6 +270,8 @@ impl<T: Trait> Module<T> {
|
||||
for outgoing_para in outgoing {
|
||||
if let Ok(i) = parachains.binary_search(&outgoing_para) {
|
||||
parachains.remove(i);
|
||||
} else {
|
||||
<Self as Store>::Parathreads::remove(&outgoing_para);
|
||||
}
|
||||
|
||||
<Self as Store>::Heads::remove(&outgoing_para);
|
||||
@@ -296,6 +300,8 @@ impl<T: Trait> Module<T> {
|
||||
if let Err(i) = parachains.binary_search(&upcoming_para) {
|
||||
parachains.insert(i, upcoming_para);
|
||||
}
|
||||
} else {
|
||||
<Self as Store>::Parathreads::insert(&upcoming_para, ());
|
||||
}
|
||||
|
||||
<Self as Store>::Heads::insert(&upcoming_para, genesis_data.genesis_head);
|
||||
@@ -515,6 +521,11 @@ impl<T: Trait> Module<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a para ID corresponds to any live parathread.
|
||||
pub(crate) fn is_parathread(id: ParaId) -> bool {
|
||||
Parathreads::get(&id).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -536,7 +547,7 @@ mod tests {
|
||||
System::set_block_number(b + 1);
|
||||
|
||||
if new_session.as_ref().map_or(false, |v| v.contains(&(b + 1))) {
|
||||
Paras::initializer_on_new_session(&[], &[]);
|
||||
Paras::initializer_on_new_session(&Default::default());
|
||||
}
|
||||
Paras::initializer_initialize(b + 1);
|
||||
}
|
||||
@@ -1040,18 +1051,24 @@ mod tests {
|
||||
);
|
||||
|
||||
assert_eq!(<Paras as Store>::UpcomingParas::get(), vec![c, b, a]);
|
||||
assert!(<Paras as Store>::Parathreads::get(&a).is_none());
|
||||
|
||||
|
||||
// run to block without session change.
|
||||
run_to_block(2, None);
|
||||
|
||||
assert_eq!(Paras::parachains(), Vec::new());
|
||||
assert_eq!(<Paras as Store>::UpcomingParas::get(), vec![c, b, a]);
|
||||
assert!(<Paras as Store>::Parathreads::get(&a).is_none());
|
||||
|
||||
|
||||
run_to_block(3, Some(vec![3]));
|
||||
|
||||
assert_eq!(Paras::parachains(), vec![c, b]);
|
||||
assert_eq!(<Paras as Store>::UpcomingParas::get(), Vec::new());
|
||||
|
||||
assert!(<Paras as Store>::Parathreads::get(&a).is_some());
|
||||
|
||||
assert_eq!(Paras::current_code(&a), Some(vec![2].into()));
|
||||
assert_eq!(Paras::current_code(&b), Some(vec![1].into()));
|
||||
assert_eq!(Paras::current_code(&c), Some(vec![3].into()));
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user