Parachains para_inherent.rs to FrameV2 (#3531)

* last parachains migration!

* migrate runtimes

* disable frame supertrait

* add pallet::inherent

* mock

* cargo +nightly fmt
This commit is contained in:
ferrell-code
2021-08-03 07:02:34 -04:00
committed by GitHub
parent f179e8f5e1
commit 24c0d5a51e
7 changed files with 110 additions and 103 deletions
+1 -1
View File
@@ -1488,7 +1488,7 @@ construct_runtime! {
Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>} = 51, Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>} = 51,
ParasShared: parachains_shared::{Pallet, Call, Storage} = 52, ParasShared: parachains_shared::{Pallet, Call, Storage} = 52,
ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>} = 53, ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>} = 53,
ParasInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent} = 54, ParaInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent} = 54,
ParaScheduler: parachains_scheduler::{Pallet, Storage} = 55, ParaScheduler: parachains_scheduler::{Pallet, Storage} = 55,
Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 56, Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 56,
Initializer: parachains_initializer::{Pallet, Call, Storage} = 57, Initializer: parachains_initializer::{Pallet, Call, Storage} = 57,
@@ -1150,6 +1150,9 @@ mod tests {
while System::block_number() < to { while System::block_number() < to {
let b = System::block_number(); let b = System::block_number();
if b != 0 { if b != 0 {
// circumvent requirement to have bitfields and headers in block for testing purposes
crate::paras_inherent::Included::<Test>::set(Some(()));
AllPallets::on_finalize(b); AllPallets::on_finalize(b);
System::finalize(); System::finalize();
} }
+3 -2
View File
@@ -17,8 +17,8 @@
//! Mocks for all the traits. //! Mocks for all the traits.
use crate::{ use crate::{
configuration, disputes, dmp, hrmp, inclusion, initializer, paras, scheduler, session_info, configuration, disputes, dmp, hrmp, inclusion, initializer, paras, paras_inherent, scheduler,
shared, ump, session_info, shared, ump,
}; };
use frame_support::{parameter_types, traits::GenesisBuild}; use frame_support::{parameter_types, traits::GenesisBuild};
use frame_support_test::TestRandomness; use frame_support_test::TestRandomness;
@@ -45,6 +45,7 @@ frame_support::construct_runtime!(
Configuration: configuration::{Pallet, Call, Storage, Config<T>}, Configuration: configuration::{Pallet, Call, Storage, Config<T>},
ParasShared: shared::{Pallet, Call, Storage}, ParasShared: shared::{Pallet, Call, Storage},
ParaInclusion: inclusion::{Pallet, Call, Storage, Event<T>}, ParaInclusion: inclusion::{Pallet, Call, Storage, Event<T>},
ParaInherent: paras_inherent::{Pallet, Call, Storage},
Scheduler: scheduler::{Pallet, Storage}, Scheduler: scheduler::{Pallet, Storage},
Initializer: initializer::{Pallet, Call, Storage}, Initializer: initializer::{Pallet, Call, Storage},
Dmp: dmp::{Pallet, Call, Storage}, Dmp: dmp::{Pallet, Call, Storage},
+100 -97
View File
@@ -28,20 +28,18 @@ use crate::{
shared, ump, shared, ump,
}; };
use frame_support::{ use frame_support::{
decl_error, decl_module, decl_storage,
dispatch::DispatchResultWithPostInfo,
ensure,
inherent::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent}, inherent::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent},
traits::Get, pallet_prelude::*,
weights::{DispatchClass, Weight},
}; };
use frame_system::ensure_none; use frame_system::pallet_prelude::*;
use primitives::v1::{ use primitives::v1::{
BackedCandidate, InherentData as ParachainsInherentData, PARACHAINS_INHERENT_IDENTIFIER, BackedCandidate, InherentData as ParachainsInherentData, PARACHAINS_INHERENT_IDENTIFIER,
}; };
use sp_runtime::traits::Header as HeaderT; use sp_runtime::traits::Header as HeaderT;
use sp_std::prelude::*; use sp_std::prelude::*;
pub use pallet::*;
const LOG_TARGET: &str = "runtime::inclusion-inherent"; const LOG_TARGET: &str = "runtime::inclusion-inherent";
// In the future, we should benchmark these consts; these are all untested assumptions for now. // In the future, we should benchmark these consts; these are all untested assumptions for now.
const BACKED_CANDIDATE_WEIGHT: Weight = 100_000; const BACKED_CANDIDATE_WEIGHT: Weight = 100_000;
@@ -49,22 +47,20 @@ const INCLUSION_INHERENT_CLAIMED_WEIGHT: Weight = 1_000_000_000;
// we assume that 75% of an paras inherent's weight is used processing backed candidates // we assume that 75% of an paras inherent's weight is used processing backed candidates
const MINIMAL_INCLUSION_INHERENT_WEIGHT: Weight = INCLUSION_INHERENT_CLAIMED_WEIGHT / 4; const MINIMAL_INCLUSION_INHERENT_WEIGHT: Weight = INCLUSION_INHERENT_CLAIMED_WEIGHT / 4;
pub trait Config: inclusion::Config + scheduler::Config {} #[frame_support::pallet]
pub mod pallet {
use super::*;
decl_storage! { #[pallet::pallet]
trait Store for Module<T: Config> as ParaInherent { #[pallet::generate_store(pub(super) trait Store)]
/// Whether the paras inherent was included within this block. pub struct Pallet<T>(_);
///
/// The `Option<()>` is effectively a `bool`, but it never hits storage in the `None` variant
/// due to the guarantees of FRAME's storage APIs.
///
/// If this is `None` at the end of the block, we panic and render the block invalid.
Included: Option<()>;
}
}
decl_error! { #[pallet::config]
pub enum Error for Module<T: Config> { #[pallet::disable_frame_system_supertrait_check]
pub trait Config: inclusion::Config + scheduler::Config {}
#[pallet::error]
pub enum Error<T> {
/// Inclusion inherent called more than once per block. /// Inclusion inherent called more than once per block.
TooManyInclusionInherents, TooManyInclusionInherents,
/// The hash of the submitted parent header doesn't correspond to the saved block hash of /// The hash of the submitted parent header doesn't correspond to the saved block hash of
@@ -73,30 +69,89 @@ decl_error! {
/// Potentially invalid candidate. /// Potentially invalid candidate.
CandidateCouldBeInvalid, CandidateCouldBeInvalid,
} }
}
decl_module! { /// Whether the paras inherent was included within this block.
/// The paras inherent module. ///
pub struct Module<T: Config> for enum Call where origin: <T as frame_system::Config>::Origin { /// The `Option<()>` is effectively a `bool`, but it never hits storage in the `None` variant
type Error = Error<T>; /// due to the guarantees of FRAME's storage APIs.
///
/// If this is `None` at the end of the block, we panic and render the block invalid.
#[pallet::storage]
pub(crate) type Included<T> = StorageValue<_, ()>;
fn on_initialize() -> Weight { #[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: T::BlockNumber) -> Weight {
T::DbWeight::get().reads_writes(1, 1) // in on_finalize. T::DbWeight::get().reads_writes(1, 1) // in on_finalize.
} }
fn on_finalize() { fn on_finalize(_: T::BlockNumber) {
if Included::take().is_none() { if Included::<T>::take().is_none() {
panic!("Bitfields and heads must be included every block"); panic!("Bitfields and heads must be included every block");
} }
} }
}
#[pallet::inherent]
impl<T: Config> ProvideInherent for Pallet<T> {
type Call = Call<T>;
type Error = MakeFatalError<()>;
const INHERENT_IDENTIFIER: InherentIdentifier = PARACHAINS_INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let mut inherent_data: ParachainsInherentData<T::Header> =
match data.get_data(&Self::INHERENT_IDENTIFIER) {
Ok(Some(d)) => d,
Ok(None) => return None,
Err(_) => {
log::warn!(target: LOG_TARGET, "ParachainsInherentData failed to decode",);
return None
},
};
// filter out any unneeded dispute statements
T::DisputesHandler::filter_multi_dispute_data(&mut inherent_data.disputes);
// Sanity check: session changes can invalidate an inherent, and we _really_ don't want that to happen.
// See github.com/paritytech/polkadot/issues/1327
let inherent_data =
match Self::enter(frame_system::RawOrigin::None.into(), inherent_data.clone()) {
Ok(_) => inherent_data,
Err(err) => {
log::warn!(
target: LOG_TARGET,
"dropping signed_bitfields and backed_candidates because they produced \
an invalid paras inherent: {:?}",
err,
);
ParachainsInherentData {
bitfields: Vec::new(),
backed_candidates: Vec::new(),
disputes: Vec::new(),
parent_header: inherent_data.parent_header,
}
},
};
Some(Call::enter(inherent_data))
}
fn is_inherent(call: &Self::Call) -> bool {
matches!(call, Call::enter(..))
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Enter the paras inherent. This will process bitfields and backed candidates. /// Enter the paras inherent. This will process bitfields and backed candidates.
#[weight = ( #[pallet::weight((
MINIMAL_INCLUSION_INHERENT_WEIGHT + data.backed_candidates.len() as Weight * BACKED_CANDIDATE_WEIGHT, MINIMAL_INCLUSION_INHERENT_WEIGHT + data.backed_candidates.len() as Weight * BACKED_CANDIDATE_WEIGHT,
DispatchClass::Mandatory, DispatchClass::Mandatory,
)] ))]
pub fn enter( pub fn enter(
origin, origin: OriginFor<T>,
data: ParachainsInherentData<T::Header>, data: ParachainsInherentData<T::Header>,
) -> DispatchResultWithPostInfo { ) -> DispatchResultWithPostInfo {
let ParachainsInherentData { let ParachainsInherentData {
@@ -107,7 +162,7 @@ decl_module! {
} = data; } = data;
ensure_none(origin)?; ensure_none(origin)?;
ensure!(!<Included>::exists(), Error::<T>::TooManyInclusionInherents); ensure!(!Included::<T>::exists(), Error::<T>::TooManyInclusionInherents);
// Check that the submitted parent header indeed corresponds to the previous block hash. // Check that the submitted parent header indeed corresponds to the previous block hash.
let parent_hash = <frame_system::Pallet<T>>::parent_hash(); let parent_hash = <frame_system::Pallet<T>>::parent_hash();
@@ -122,17 +177,16 @@ decl_module! {
let fresh_disputes = T::DisputesHandler::provide_multi_dispute_data(disputes)?; let fresh_disputes = T::DisputesHandler::provide_multi_dispute_data(disputes)?;
if T::DisputesHandler::is_frozen() { if T::DisputesHandler::is_frozen() {
// The relay chain we are currently on is invalid. Proceed no further on parachains. // The relay chain we are currently on is invalid. Proceed no further on parachains.
Included::set(Some(())); Included::<T>::set(Some(()));
return Ok(Some( return Ok(Some(MINIMAL_INCLUSION_INHERENT_WEIGHT).into())
MINIMAL_INCLUSION_INHERENT_WEIGHT
).into());
} }
let any_current_session_disputes = fresh_disputes.iter() let any_current_session_disputes =
.any(|(s, _)| s == &current_session); fresh_disputes.iter().any(|(s, _)| s == &current_session);
if any_current_session_disputes { if any_current_session_disputes {
let current_session_disputes: Vec<_> = fresh_disputes.iter() let current_session_disputes: Vec<_> = fresh_disputes
.iter()
.filter(|(s, _)| s == &current_session) .filter(|(s, _)| s == &current_session)
.map(|(_, c)| *c) .map(|(_, c)| *c)
.collect(); .collect();
@@ -170,7 +224,8 @@ decl_module! {
}; };
// Schedule paras again, given freed cores, and reasons for freeing. // Schedule paras again, given freed cores, and reasons for freeing.
let mut freed = freed_disputed.into_iter() let mut freed = freed_disputed
.into_iter()
.chain(freed_concluded.into_iter().map(|(c, _hash)| (c, FreedReason::Concluded))) .chain(freed_concluded.into_iter().map(|(c, _hash)| (c, FreedReason::Concluded)))
.chain(freed_timeout.into_iter().map(|c| (c, FreedReason::TimedOut))) .chain(freed_timeout.into_iter().map(|c| (c, FreedReason::TimedOut)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -178,10 +233,7 @@ decl_module! {
freed.sort_unstable_by_key(|pair| pair.0); // sort by core index freed.sort_unstable_by_key(|pair| pair.0); // sort by core index
<scheduler::Pallet<T>>::clear(); <scheduler::Pallet<T>>::clear();
<scheduler::Pallet<T>>::schedule( <scheduler::Pallet<T>>::schedule(freed, <frame_system::Pallet<T>>::block_number());
freed,
<frame_system::Pallet<T>>::block_number(),
);
let backed_candidates = limit_backed_candidates::<T>(backed_candidates); let backed_candidates = limit_backed_candidates::<T>(backed_candidates);
let backed_candidates_len = backed_candidates.len() as Weight; let backed_candidates_len = backed_candidates.len() as Weight;
@@ -213,12 +265,13 @@ decl_module! {
<ump::Pallet<T>>::process_pending_upward_messages(); <ump::Pallet<T>>::process_pending_upward_messages();
// And track that we've finished processing the inherent for this block. // And track that we've finished processing the inherent for this block.
Included::set(Some(())); Included::<T>::set(Some(()));
Ok(Some( Ok(Some(
MINIMAL_INCLUSION_INHERENT_WEIGHT + MINIMAL_INCLUSION_INHERENT_WEIGHT +
(backed_candidates_len * BACKED_CANDIDATE_WEIGHT) (backed_candidates_len * BACKED_CANDIDATE_WEIGHT),
).into()) )
.into())
} }
} }
} }
@@ -267,56 +320,6 @@ fn limit_backed_candidates<T: Config>(
} }
} }
impl<T: Config> ProvideInherent for Module<T> {
type Call = Call<T>;
type Error = MakeFatalError<()>;
const INHERENT_IDENTIFIER: InherentIdentifier = PARACHAINS_INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let mut inherent_data: ParachainsInherentData<T::Header> =
match data.get_data(&Self::INHERENT_IDENTIFIER) {
Ok(Some(d)) => d,
Ok(None) => return None,
Err(_) => {
log::warn!(target: LOG_TARGET, "ParachainsInherentData failed to decode",);
return None
},
};
// filter out any unneeded dispute statements
T::DisputesHandler::filter_multi_dispute_data(&mut inherent_data.disputes);
// Sanity check: session changes can invalidate an inherent, and we _really_ don't want that to happen.
// See github.com/paritytech/polkadot/issues/1327
let inherent_data =
match Self::enter(frame_system::RawOrigin::None.into(), inherent_data.clone()) {
Ok(_) => inherent_data,
Err(err) => {
log::warn!(
target: LOG_TARGET,
"dropping signed_bitfields and backed_candidates because they produced \
an invalid paras inherent: {:?}",
err,
);
ParachainsInherentData {
bitfields: Vec::new(),
backed_candidates: Vec::new(),
disputes: Vec::new(),
parent_header: inherent_data.parent_header,
}
},
};
Some(Call::enter(inherent_data))
}
fn is_inherent(call: &Self::Call) -> bool {
matches!(call, Call::enter(..))
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+1 -1
View File
@@ -213,7 +213,7 @@ construct_runtime! {
Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>}, Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>},
ParasShared: parachains_shared::{Pallet, Call, Storage}, ParasShared: parachains_shared::{Pallet, Call, Storage},
ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>}, ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>},
ParasInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent}, ParaInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent},
ParaScheduler: parachains_scheduler::{Pallet, Storage}, ParaScheduler: parachains_scheduler::{Pallet, Storage},
Paras: parachains_paras::{Pallet, Call, Storage, Event, Config}, Paras: parachains_paras::{Pallet, Call, Storage, Event, Config},
Initializer: parachains_initializer::{Pallet, Call, Storage}, Initializer: parachains_initializer::{Pallet, Call, Storage},
+1 -1
View File
@@ -531,7 +531,7 @@ construct_runtime! {
// Parachains runtime modules // Parachains runtime modules
Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>}, Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>},
ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>}, ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>},
ParasInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent}, ParaInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent},
Initializer: parachains_initializer::{Pallet, Call, Storage}, Initializer: parachains_initializer::{Pallet, Call, Storage},
Paras: parachains_paras::{Pallet, Call, Storage, Origin, Event}, Paras: parachains_paras::{Pallet, Call, Storage, Origin, Event},
ParasShared: parachains_shared::{Pallet, Call, Storage}, ParasShared: parachains_shared::{Pallet, Call, Storage},
+1 -1
View File
@@ -1071,7 +1071,7 @@ construct_runtime! {
Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>} = 42, Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>} = 42,
ParasShared: parachains_shared::{Pallet, Call, Storage} = 43, ParasShared: parachains_shared::{Pallet, Call, Storage} = 43,
ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>} = 44, ParaInclusion: parachains_inclusion::{Pallet, Call, Storage, Event<T>} = 44,
ParasInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent} = 45, ParaInherent: parachains_paras_inherent::{Pallet, Call, Storage, Inherent} = 45,
ParaScheduler: parachains_scheduler::{Pallet, Storage} = 46, ParaScheduler: parachains_scheduler::{Pallet, Storage} = 46,
Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 47, Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 47,
Initializer: parachains_initializer::{Pallet, Call, Storage} = 48, Initializer: parachains_initializer::{Pallet, Call, Storage} = 48,