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

Commit

Permalink
Update para sudo wrapper pallet to FRAME v2 (#3074)
Browse files Browse the repository at this point in the history
* Migrate para sudo wrapper pallet to pallet attribute macro.

* Apply review suggestions.
  • Loading branch information
shaunxw authored Jun 1, 2021
1 parent 4a1ca73 commit 7f3745f
Showing 1 changed file with 57 additions and 41 deletions.
98 changes: 57 additions & 41 deletions runtime/common/src/paras_sudo_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,33 @@

//! A simple wrapper allowing `Sudo` to call into `paras` routines.
use sp_std::prelude::*;
use frame_support::{
decl_error, decl_module, ensure,
dispatch::DispatchResult,
weights::DispatchClass,
};
use frame_system::ensure_root;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use runtime_parachains::{
configuration, dmp, ump, hrmp,
ParaLifecycle,
paras::{self, ParaGenesisArgs},
};
use primitives::v1::Id as ParaId;
use parity_scale_codec::Encode;
pub use pallet::*;

/// The module's configuration trait.
pub trait Config:
configuration::Config + paras::Config + dmp::Config + ump::Config + hrmp::Config
{
}
#[frame_support::pallet]
pub mod pallet {
use super::*;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config:
configuration::Config + paras::Config + dmp::Config + ump::Config + hrmp::Config {}

decl_error! {
pub enum Error for Module<T: Config> {

#[pallet::error]
pub enum Error<T> {
/// The specified parachain or parathread is not registered.
ParaDoesntExist,
/// The specified parachain or parathread is already registered.
Expand All @@ -57,63 +61,75 @@ decl_error! {
/// Cannot downgrade parachain.
CannotDowngrade,
}
}

decl_module! {
/// A sudo wrapper to call into v1 paras module.
pub struct Module<T: Config> for enum Call where origin: <T as frame_system::Config>::Origin {
type Error = Error<T>;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Schedule a para to be initialized at the start of the next session.
#[weight = (1_000, DispatchClass::Operational)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_para_initialize(
origin,
origin: OriginFor<T>,
id: ParaId,
genesis: ParaGenesisArgs,
) -> DispatchResult {
ensure_root(origin)?;
runtime_parachains::schedule_para_initialize::<T>(id, genesis).map_err(|_| Error::<T>::ParaAlreadyExists)?;
runtime_parachains::schedule_para_initialize::<T>(id, genesis)
.map_err(|_| Error::<T>::ParaAlreadyExists)?;
Ok(())
}

/// Schedule a para to be cleaned up at the start of the next session.
#[weight = (1_000, DispatchClass::Operational)]
pub fn sudo_schedule_para_cleanup(origin, id: ParaId) -> DispatchResult {
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_para_cleanup(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
ensure_root(origin)?;
runtime_parachains::schedule_para_cleanup::<T>(id).map_err(|_| Error::<T>::CouldntCleanup)?;
Ok(())
}

/// Upgrade a parathread to a parachain
#[weight = (1_000, DispatchClass::Operational)]
pub fn sudo_schedule_parathread_upgrade(origin, id: ParaId) -> DispatchResult {
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_parathread_upgrade(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
ensure_root(origin)?;
// Para backend should think this is a parathread...
ensure!(paras::Module::<T>::lifecycle(id) == Some(ParaLifecycle::Parathread), Error::<T>::NotParathread);
runtime_parachains::schedule_parathread_upgrade::<T>(id).map_err(|_| Error::<T>::CannotUpgrade)?;
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parathread),
Error::<T>::NotParathread,
);
runtime_parachains::schedule_parathread_upgrade::<T>(id)
.map_err(|_| Error::<T>::CannotUpgrade)?;
Ok(())
}

/// Downgrade a parachain to a parathread
#[weight = (1_000, DispatchClass::Operational)]
pub fn sudo_schedule_parachain_downgrade(origin, id: ParaId) -> DispatchResult {
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_schedule_parachain_downgrade(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
ensure_root(origin)?;
// Para backend should think this is a parachain...
ensure!(paras::Module::<T>::lifecycle(id) == Some(ParaLifecycle::Parachain), Error::<T>::NotParachain);
runtime_parachains::schedule_parachain_downgrade::<T>(id).map_err(|_| Error::<T>::CannotDowngrade)?;
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parachain),
Error::<T>::NotParachain,
);
runtime_parachains::schedule_parachain_downgrade::<T>(id)
.map_err(|_| Error::<T>::CannotDowngrade)?;
Ok(())
}

/// Send a downward XCM to the given para.
///
/// The given parachain should exist and the payload should not exceed the preconfigured size
/// `config.max_downward_message_size`.
#[weight = (1_000, DispatchClass::Operational)]
pub fn sudo_queue_downward_xcm(origin, id: ParaId, xcm: xcm::opaque::VersionedXcm) -> DispatchResult {
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_queue_downward_xcm(
origin: OriginFor<T>,
id: ParaId,
xcm: xcm::opaque::VersionedXcm,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(<paras::Module<T>>::is_valid_para(id), Error::<T>::ParaDoesntExist);
let config = <configuration::Module<T>>::config();
<dmp::Module<T>>::queue_downward_message(&config, id, xcm.encode())
ensure!(<paras::Pallet<T>>::is_valid_para(id), Error::<T>::ParaDoesntExist);
let config = <configuration::Pallet<T>>::config();
<dmp::Pallet<T>>::queue_downward_message(&config, id, xcm.encode())
.map_err(|e| match e {
dmp::QueueDownwardMessageError::ExceedsMaxMessageSize =>
Error::<T>::ExceedsMaxMessageSize.into(),
Expand All @@ -124,23 +140,23 @@ decl_module! {
///
/// This is equivalent to sending an `Hrmp::hrmp_init_open_channel` extrinsic followed by
/// `Hrmp::hrmp_accept_open_channel`.
#[weight = (1_000, DispatchClass::Operational)]
#[pallet::weight((1_000, DispatchClass::Operational))]
pub fn sudo_establish_hrmp_channel(
origin,
origin: OriginFor<T>,
sender: ParaId,
recipient: ParaId,
max_capacity: u32,
max_message_size: u32,
) -> DispatchResult {
ensure_root(origin)?;

<hrmp::Module<T>>::init_open_channel(
<hrmp::Pallet<T>>::init_open_channel(
sender,
recipient,
max_capacity,
max_message_size,
)?;
<hrmp::Module<T>>::accept_open_channel(recipient, sender)?;
<hrmp::Pallet<T>>::accept_open_channel(recipient, sender)?;
Ok(())
}
}
Expand Down

0 comments on commit 7f3745f

Please sign in to comment.