referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_common/
paras_sudo_wrapper.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! A simple wrapper allowing `Sudo` to call into `paras` routines.
18
19use alloc::boxed::Box;
20use codec::Encode;
21use frame_support::pallet_prelude::*;
22use frame_system::pallet_prelude::*;
23pub use pallet::*;
24use polkadot_primitives::Id as ParaId;
25use polkadot_runtime_parachains::{
26	configuration, dmp, hrmp,
27	paras::{self, AssignCoretime, ParaGenesisArgs, ParaKind},
28	ParaLifecycle,
29};
30
31#[frame_support::pallet]
32pub mod pallet {
33	use super::*;
34
35	#[pallet::pallet]
36	pub struct Pallet<T>(_);
37
38	#[pallet::config]
39	#[pallet::disable_frame_system_supertrait_check]
40	pub trait Config: configuration::Config + paras::Config + dmp::Config + hrmp::Config {}
41
42	#[pallet::error]
43	pub enum Error<T> {
44		/// The specified parachain is not registered.
45		ParaDoesntExist,
46		/// The specified parachain is already registered.
47		ParaAlreadyExists,
48		/// A DMP message couldn't be sent because it exceeds the maximum size allowed for a
49		/// downward message.
50		ExceedsMaxMessageSize,
51		/// A DMP message couldn't be sent because the destination is unreachable.
52		Unroutable,
53		/// Could not schedule para cleanup.
54		CouldntCleanup,
55		/// Not a parathread (on-demand parachain).
56		NotParathread,
57		/// Not a lease holding parachain.
58		NotParachain,
59		/// Cannot upgrade on-demand parachain to lease holding parachain.
60		CannotUpgrade,
61		/// Cannot downgrade lease holding parachain to on-demand.
62		CannotDowngrade,
63		/// There are more cores than supported by the runtime.
64		TooManyCores,
65	}
66
67	#[pallet::hooks]
68	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
69
70	#[pallet::call]
71	impl<T: Config> Pallet<T> {
72		/// Schedule a para to be initialized at the start of the next session.
73		///
74		/// This should only be used for TESTING and not on PRODUCTION chains. It automatically
75		/// assigns Coretime to the chain and increases the number of cores. Thus, there is no
76		/// running coretime chain required.
77		#[pallet::call_index(0)]
78		#[pallet::weight((1_000, DispatchClass::Operational))]
79		pub fn sudo_schedule_para_initialize(
80			origin: OriginFor<T>,
81			id: ParaId,
82			genesis: ParaGenesisArgs,
83		) -> DispatchResult {
84			ensure_root(origin)?;
85
86			let assign_coretime = genesis.para_kind == ParaKind::Parachain;
87
88			polkadot_runtime_parachains::schedule_para_initialize::<T>(id, genesis)
89				.map_err(|_| Error::<T>::ParaAlreadyExists)?;
90
91			if assign_coretime {
92				T::AssignCoretime::assign_coretime(id)?;
93			}
94
95			Ok(())
96		}
97
98		/// Schedule a para to be cleaned up at the start of the next session.
99		#[pallet::call_index(1)]
100		#[pallet::weight((1_000, DispatchClass::Operational))]
101		pub fn sudo_schedule_para_cleanup(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
102			ensure_root(origin)?;
103			polkadot_runtime_parachains::schedule_para_cleanup::<T>(id)
104				.map_err(|_| Error::<T>::CouldntCleanup)?;
105			Ok(())
106		}
107
108		/// Upgrade a parathread (on-demand parachain) to a lease holding parachain
109		#[pallet::call_index(2)]
110		#[pallet::weight((1_000, DispatchClass::Operational))]
111		pub fn sudo_schedule_parathread_upgrade(
112			origin: OriginFor<T>,
113			id: ParaId,
114		) -> DispatchResult {
115			ensure_root(origin)?;
116			// Para backend should think this is a parathread (on-demand parachain)...
117			ensure!(
118				paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parathread),
119				Error::<T>::NotParathread,
120			);
121			polkadot_runtime_parachains::schedule_parathread_upgrade::<T>(id)
122				.map_err(|_| Error::<T>::CannotUpgrade)?;
123			Ok(())
124		}
125
126		/// Downgrade a lease holding parachain to an on-demand parachain
127		#[pallet::call_index(3)]
128		#[pallet::weight((1_000, DispatchClass::Operational))]
129		pub fn sudo_schedule_parachain_downgrade(
130			origin: OriginFor<T>,
131			id: ParaId,
132		) -> DispatchResult {
133			ensure_root(origin)?;
134			// Para backend should think this is a parachain...
135			ensure!(
136				paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parachain),
137				Error::<T>::NotParachain,
138			);
139			polkadot_runtime_parachains::schedule_parachain_downgrade::<T>(id)
140				.map_err(|_| Error::<T>::CannotDowngrade)?;
141			Ok(())
142		}
143
144		/// Send a downward XCM to the given para.
145		///
146		/// The given parachain should exist and the payload should not exceed the preconfigured
147		/// size `config.max_downward_message_size`.
148		#[pallet::call_index(4)]
149		#[pallet::weight((1_000, DispatchClass::Operational))]
150		pub fn sudo_queue_downward_xcm(
151			origin: OriginFor<T>,
152			id: ParaId,
153			xcm: Box<xcm::opaque::VersionedXcm>,
154		) -> DispatchResult {
155			ensure_root(origin)?;
156			ensure!(paras::Pallet::<T>::is_valid_para(id), Error::<T>::ParaDoesntExist);
157			let config = configuration::ActiveConfig::<T>::get();
158			dmp::Pallet::<T>::queue_downward_message(&config, id, xcm.encode()).map_err(|e| match e
159			{
160				dmp::QueueDownwardMessageError::ExceedsMaxMessageSize =>
161					Error::<T>::ExceedsMaxMessageSize.into(),
162				dmp::QueueDownwardMessageError::Unroutable => Error::<T>::Unroutable.into(),
163			})
164		}
165
166		/// Forcefully establish a channel from the sender to the recipient.
167		///
168		/// This is equivalent to sending an `Hrmp::hrmp_init_open_channel` extrinsic followed by
169		/// `Hrmp::hrmp_accept_open_channel`.
170		#[pallet::call_index(5)]
171		#[pallet::weight((1_000, DispatchClass::Operational))]
172		pub fn sudo_establish_hrmp_channel(
173			origin: OriginFor<T>,
174			sender: ParaId,
175			recipient: ParaId,
176			max_capacity: u32,
177			max_message_size: u32,
178		) -> DispatchResult {
179			ensure_root(origin)?;
180
181			hrmp::Pallet::<T>::init_open_channel(
182				sender,
183				recipient,
184				max_capacity,
185				max_message_size,
186			)?;
187			hrmp::Pallet::<T>::accept_open_channel(recipient, sender)?;
188			Ok(())
189		}
190	}
191}