polkadot_runtime_common/paras_registrar/
mod.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//! Pallet to handle parachain registration and related fund management.
18//! In essence this is a simple wrapper around `paras`.
19
20pub mod migration;
21
22use alloc::{vec, vec::Vec};
23use core::result;
24use frame_support::{
25	dispatch::DispatchResult,
26	ensure,
27	pallet_prelude::Weight,
28	traits::{Currency, Get, ReservableCurrency},
29};
30use frame_system::{self, ensure_root, ensure_signed};
31use polkadot_primitives::{
32	HeadData, Id as ParaId, ValidationCode, LOWEST_PUBLIC_ID, MIN_CODE_SIZE,
33};
34use polkadot_runtime_parachains::{
35	configuration, ensure_parachain,
36	paras::{self, ParaGenesisArgs, UpgradeStrategy},
37	Origin, ParaLifecycle,
38};
39
40use crate::traits::{OnSwap, Registrar};
41use codec::{Decode, Encode};
42pub use pallet::*;
43use polkadot_runtime_parachains::paras::{OnNewHead, ParaKind};
44use scale_info::TypeInfo;
45use sp_runtime::{
46	traits::{CheckedSub, Saturating},
47	RuntimeDebug,
48};
49
50#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug, TypeInfo)]
51pub struct ParaInfo<Account, Balance> {
52	/// The account that has placed a deposit for registering this para.
53	pub(crate) manager: Account,
54	/// The amount reserved by the `manager` account for the registration.
55	deposit: Balance,
56	/// Whether the para registration should be locked from being controlled by the manager.
57	/// None means the lock had not been explicitly set, and should be treated as false.
58	locked: Option<bool>,
59}
60
61impl<Account, Balance> ParaInfo<Account, Balance> {
62	/// Returns if the para is locked.
63	pub fn is_locked(&self) -> bool {
64		self.locked.unwrap_or(false)
65	}
66}
67
68type BalanceOf<T> =
69	<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
70
71pub trait WeightInfo {
72	fn reserve() -> Weight;
73	fn register() -> Weight;
74	fn force_register() -> Weight;
75	fn deregister() -> Weight;
76	fn swap() -> Weight;
77	fn schedule_code_upgrade(b: u32) -> Weight;
78	fn set_current_head(b: u32) -> Weight;
79}
80
81pub struct TestWeightInfo;
82impl WeightInfo for TestWeightInfo {
83	fn reserve() -> Weight {
84		Weight::zero()
85	}
86	fn register() -> Weight {
87		Weight::zero()
88	}
89	fn force_register() -> Weight {
90		Weight::zero()
91	}
92	fn deregister() -> Weight {
93		Weight::zero()
94	}
95	fn swap() -> Weight {
96		Weight::zero()
97	}
98	fn schedule_code_upgrade(_b: u32) -> Weight {
99		Weight::zero()
100	}
101	fn set_current_head(_b: u32) -> Weight {
102		Weight::zero()
103	}
104}
105
106#[frame_support::pallet]
107pub mod pallet {
108	use super::*;
109	use frame_support::pallet_prelude::*;
110	use frame_system::pallet_prelude::*;
111
112	/// The in-code storage version.
113	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
114
115	#[pallet::pallet]
116	#[pallet::without_storage_info]
117	#[pallet::storage_version(STORAGE_VERSION)]
118	pub struct Pallet<T>(_);
119
120	#[pallet::config]
121	#[pallet::disable_frame_system_supertrait_check]
122	pub trait Config: configuration::Config + paras::Config {
123		/// The overarching event type.
124		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
125
126		/// The aggregated origin type must support the `parachains` origin. We require that we can
127		/// infallibly convert between this origin and the system origin, but in reality, they're
128		/// the same type, we just can't express that to the Rust type system without writing a
129		/// `where` clause everywhere.
130		type RuntimeOrigin: From<<Self as frame_system::Config>::RuntimeOrigin>
131			+ Into<result::Result<Origin, <Self as Config>::RuntimeOrigin>>;
132
133		/// The system's currency for on-demand parachain payment.
134		type Currency: ReservableCurrency<Self::AccountId>;
135
136		/// Runtime hook for when a lease holding parachain and on-demand parachain swap.
137		type OnSwap: crate::traits::OnSwap;
138
139		/// The deposit to be paid to run a on-demand parachain.
140		/// This should include the cost for storing the genesis head and validation code.
141		#[pallet::constant]
142		type ParaDeposit: Get<BalanceOf<Self>>;
143
144		/// The deposit to be paid per byte stored on chain.
145		#[pallet::constant]
146		type DataDepositPerByte: Get<BalanceOf<Self>>;
147
148		/// Weight Information for the Extrinsics in the Pallet
149		type WeightInfo: WeightInfo;
150	}
151
152	#[pallet::event]
153	#[pallet::generate_deposit(pub(super) fn deposit_event)]
154	pub enum Event<T: Config> {
155		Registered { para_id: ParaId, manager: T::AccountId },
156		Deregistered { para_id: ParaId },
157		Reserved { para_id: ParaId, who: T::AccountId },
158		Swapped { para_id: ParaId, other_id: ParaId },
159	}
160
161	#[pallet::error]
162	pub enum Error<T> {
163		/// The ID is not registered.
164		NotRegistered,
165		/// The ID is already registered.
166		AlreadyRegistered,
167		/// The caller is not the owner of this Id.
168		NotOwner,
169		/// Invalid para code size.
170		CodeTooLarge,
171		/// Invalid para head data size.
172		HeadDataTooLarge,
173		/// Para is not a Parachain.
174		NotParachain,
175		/// Para is not a Parathread (on-demand parachain).
176		NotParathread,
177		/// Cannot deregister para
178		CannotDeregister,
179		/// Cannot schedule downgrade of lease holding parachain to on-demand parachain
180		CannotDowngrade,
181		/// Cannot schedule upgrade of on-demand parachain to lease holding parachain
182		CannotUpgrade,
183		/// Para is locked from manipulation by the manager. Must use parachain or relay chain
184		/// governance.
185		ParaLocked,
186		/// The ID given for registration has not been reserved.
187		NotReserved,
188		/// The validation code is invalid.
189		InvalidCode,
190		/// Cannot perform a parachain slot / lifecycle swap. Check that the state of both paras
191		/// are correct for the swap to work.
192		CannotSwap,
193	}
194
195	/// Pending swap operations.
196	#[pallet::storage]
197	pub(super) type PendingSwap<T> = StorageMap<_, Twox64Concat, ParaId, ParaId>;
198
199	/// Amount held on deposit for each para and the original depositor.
200	///
201	/// The given account ID is responsible for registering the code and initial head data, but may
202	/// only do so if it isn't yet registered. (After that, it's up to governance to do so.)
203	#[pallet::storage]
204	pub type Paras<T: Config> =
205		StorageMap<_, Twox64Concat, ParaId, ParaInfo<T::AccountId, BalanceOf<T>>>;
206
207	/// The next free `ParaId`.
208	#[pallet::storage]
209	pub type NextFreeParaId<T> = StorageValue<_, ParaId, ValueQuery>;
210
211	#[pallet::genesis_config]
212	pub struct GenesisConfig<T: Config> {
213		#[serde(skip)]
214		pub _config: core::marker::PhantomData<T>,
215		pub next_free_para_id: ParaId,
216	}
217
218	impl<T: Config> Default for GenesisConfig<T> {
219		fn default() -> Self {
220			GenesisConfig { next_free_para_id: LOWEST_PUBLIC_ID, _config: Default::default() }
221		}
222	}
223
224	#[pallet::genesis_build]
225	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
226		fn build(&self) {
227			NextFreeParaId::<T>::put(self.next_free_para_id);
228		}
229	}
230
231	#[pallet::hooks]
232	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
233
234	#[pallet::call]
235	impl<T: Config> Pallet<T> {
236		/// Register head data and validation code for a reserved Para Id.
237		///
238		/// ## Arguments
239		/// - `origin`: Must be called by a `Signed` origin.
240		/// - `id`: The para ID. Must be owned/managed by the `origin` signing account.
241		/// - `genesis_head`: The genesis head data of the parachain/thread.
242		/// - `validation_code`: The initial validation code of the parachain/thread.
243		///
244		/// ## Deposits/Fees
245		/// The account with the originating signature must reserve a deposit.
246		///
247		/// The deposit is required to cover the costs associated with storing the genesis head
248		/// data and the validation code.
249		/// This accounts for the potential to store validation code of a size up to the
250		/// `max_code_size`, as defined in the configuration pallet
251		///
252		/// Anything already reserved previously for this para ID is accounted for.
253		///
254		/// ## Events
255		/// The `Registered` event is emitted in case of success.
256		#[pallet::call_index(0)]
257		#[pallet::weight(<T as Config>::WeightInfo::register())]
258		pub fn register(
259			origin: OriginFor<T>,
260			id: ParaId,
261			genesis_head: HeadData,
262			validation_code: ValidationCode,
263		) -> DispatchResult {
264			let who = ensure_signed(origin)?;
265			Self::do_register(who, None, id, genesis_head, validation_code, true)?;
266			Ok(())
267		}
268
269		/// Force the registration of a Para Id on the relay chain.
270		///
271		/// This function must be called by a Root origin.
272		///
273		/// The deposit taken can be specified for this registration. Any `ParaId`
274		/// can be registered, including sub-1000 IDs which are System Parachains.
275		#[pallet::call_index(1)]
276		#[pallet::weight(<T as Config>::WeightInfo::force_register())]
277		pub fn force_register(
278			origin: OriginFor<T>,
279			who: T::AccountId,
280			deposit: BalanceOf<T>,
281			id: ParaId,
282			genesis_head: HeadData,
283			validation_code: ValidationCode,
284		) -> DispatchResult {
285			ensure_root(origin)?;
286			Self::do_register(who, Some(deposit), id, genesis_head, validation_code, false)
287		}
288
289		/// Deregister a Para Id, freeing all data and returning any deposit.
290		///
291		/// The caller must be Root, the `para` owner, or the `para` itself. The para must be an
292		/// on-demand parachain.
293		#[pallet::call_index(2)]
294		#[pallet::weight(<T as Config>::WeightInfo::deregister())]
295		pub fn deregister(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
296			Self::ensure_root_para_or_owner(origin, id)?;
297			Self::do_deregister(id)
298		}
299
300		/// Swap a lease holding parachain with another parachain, either on-demand or lease
301		/// holding.
302		///
303		/// The origin must be Root, the `para` owner, or the `para` itself.
304		///
305		/// The swap will happen only if there is already an opposite swap pending. If there is not,
306		/// the swap will be stored in the pending swaps map, ready for a later confirmatory swap.
307		///
308		/// The `ParaId`s remain mapped to the same head data and code so external code can rely on
309		/// `ParaId` to be a long-term identifier of a notional "parachain". However, their
310		/// scheduling info (i.e. whether they're an on-demand parachain or lease holding
311		/// parachain), auction information and the auction deposit are switched.
312		#[pallet::call_index(3)]
313		#[pallet::weight(<T as Config>::WeightInfo::swap())]
314		pub fn swap(origin: OriginFor<T>, id: ParaId, other: ParaId) -> DispatchResult {
315			Self::ensure_root_para_or_owner(origin, id)?;
316
317			// If `id` and `other` is the same id, we treat this as a "clear" function, and exit
318			// early, since swapping the same id would otherwise be a noop.
319			if id == other {
320				PendingSwap::<T>::remove(id);
321				return Ok(());
322			}
323
324			// Sanity check that `id` is even a para.
325			let id_lifecycle =
326				paras::Pallet::<T>::lifecycle(id).ok_or(Error::<T>::NotRegistered)?;
327
328			if PendingSwap::<T>::get(other) == Some(id) {
329				let other_lifecycle =
330					paras::Pallet::<T>::lifecycle(other).ok_or(Error::<T>::NotRegistered)?;
331				// identify which is a lease holding parachain and which is a parathread (on-demand
332				// parachain)
333				if id_lifecycle == ParaLifecycle::Parachain &&
334					other_lifecycle == ParaLifecycle::Parathread
335				{
336					Self::do_thread_and_chain_swap(id, other);
337				} else if id_lifecycle == ParaLifecycle::Parathread &&
338					other_lifecycle == ParaLifecycle::Parachain
339				{
340					Self::do_thread_and_chain_swap(other, id);
341				} else if id_lifecycle == ParaLifecycle::Parachain &&
342					other_lifecycle == ParaLifecycle::Parachain
343				{
344					// If both chains are currently parachains, there is nothing funny we
345					// need to do for their lifecycle management, just swap the underlying
346					// data.
347					T::OnSwap::on_swap(id, other);
348				} else {
349					return Err(Error::<T>::CannotSwap.into());
350				}
351				Self::deposit_event(Event::<T>::Swapped { para_id: id, other_id: other });
352				PendingSwap::<T>::remove(other);
353			} else {
354				PendingSwap::<T>::insert(id, other);
355			}
356
357			Ok(())
358		}
359
360		/// Remove a manager lock from a para. This will allow the manager of a
361		/// previously locked para to deregister or swap a para without using governance.
362		///
363		/// Can only be called by the Root origin or the parachain.
364		#[pallet::call_index(4)]
365		#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
366		pub fn remove_lock(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
367			Self::ensure_root_or_para(origin, para)?;
368			<Self as Registrar>::remove_lock(para);
369			Ok(())
370		}
371
372		/// Reserve a Para Id on the relay chain.
373		///
374		/// This function will reserve a new Para Id to be owned/managed by the origin account.
375		/// The origin account is able to register head data and validation code using `register` to
376		/// create an on-demand parachain. Using the Slots pallet, an on-demand parachain can then
377		/// be upgraded to a lease holding parachain.
378		///
379		/// ## Arguments
380		/// - `origin`: Must be called by a `Signed` origin. Becomes the manager/owner of the new
381		///   para ID.
382		///
383		/// ## Deposits/Fees
384		/// The origin must reserve a deposit of `ParaDeposit` for the registration.
385		///
386		/// ## Events
387		/// The `Reserved` event is emitted in case of success, which provides the ID reserved for
388		/// use.
389		#[pallet::call_index(5)]
390		#[pallet::weight(<T as Config>::WeightInfo::reserve())]
391		pub fn reserve(origin: OriginFor<T>) -> DispatchResult {
392			let who = ensure_signed(origin)?;
393			let id = NextFreeParaId::<T>::get().max(LOWEST_PUBLIC_ID);
394			Self::do_reserve(who, None, id)?;
395			NextFreeParaId::<T>::set(id + 1);
396			Ok(())
397		}
398
399		/// Add a manager lock from a para. This will prevent the manager of a
400		/// para to deregister or swap a para.
401		///
402		/// Can be called by Root, the parachain, or the parachain manager if the parachain is
403		/// unlocked.
404		#[pallet::call_index(6)]
405		#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
406		pub fn add_lock(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
407			Self::ensure_root_para_or_owner(origin, para)?;
408			<Self as Registrar>::apply_lock(para);
409			Ok(())
410		}
411
412		/// Schedule a parachain upgrade.
413		///
414		/// This will kick off a check of `new_code` by all validators. After the majority of the
415		/// validators have reported on the validity of the code, the code will either be enacted
416		/// or the upgrade will be rejected. If the code will be enacted, the current code of the
417		/// parachain will be overwritten directly. This means that any PoV will be checked by this
418		/// new code. The parachain itself will not be informed explicitly that the validation code
419		/// has changed.
420		///
421		/// Can be called by Root, the parachain, or the parachain manager if the parachain is
422		/// unlocked.
423		#[pallet::call_index(7)]
424		#[pallet::weight(<T as Config>::WeightInfo::schedule_code_upgrade(new_code.0.len() as u32))]
425		pub fn schedule_code_upgrade(
426			origin: OriginFor<T>,
427			para: ParaId,
428			new_code: ValidationCode,
429		) -> DispatchResult {
430			Self::ensure_root_para_or_owner(origin, para)?;
431			polkadot_runtime_parachains::schedule_code_upgrade::<T>(
432				para,
433				new_code,
434				UpgradeStrategy::ApplyAtExpectedBlock,
435			)?;
436			Ok(())
437		}
438
439		/// Set the parachain's current head.
440		///
441		/// Can be called by Root, the parachain, or the parachain manager if the parachain is
442		/// unlocked.
443		#[pallet::call_index(8)]
444		#[pallet::weight(<T as Config>::WeightInfo::set_current_head(new_head.0.len() as u32))]
445		pub fn set_current_head(
446			origin: OriginFor<T>,
447			para: ParaId,
448			new_head: HeadData,
449		) -> DispatchResult {
450			Self::ensure_root_para_or_owner(origin, para)?;
451			polkadot_runtime_parachains::set_current_head::<T>(para, new_head);
452			Ok(())
453		}
454	}
455}
456
457impl<T: Config> Registrar for Pallet<T> {
458	type AccountId = T::AccountId;
459
460	/// Return the manager `AccountId` of a para if one exists.
461	fn manager_of(id: ParaId) -> Option<T::AccountId> {
462		Some(Paras::<T>::get(id)?.manager)
463	}
464
465	// All lease holding parachains. Ordered ascending by ParaId. On-demand parachains are not
466	// included.
467	fn parachains() -> Vec<ParaId> {
468		paras::Parachains::<T>::get()
469	}
470
471	// Return if a para is a parathread (on-demand parachain)
472	fn is_parathread(id: ParaId) -> bool {
473		paras::Pallet::<T>::is_parathread(id)
474	}
475
476	// Return if a para is a lease holding parachain
477	fn is_parachain(id: ParaId) -> bool {
478		paras::Pallet::<T>::is_parachain(id)
479	}
480
481	// Apply a lock to the parachain.
482	fn apply_lock(id: ParaId) {
483		Paras::<T>::mutate(id, |x| x.as_mut().map(|info| info.locked = Some(true)));
484	}
485
486	// Remove a lock from the parachain.
487	fn remove_lock(id: ParaId) {
488		Paras::<T>::mutate(id, |x| x.as_mut().map(|info| info.locked = Some(false)));
489	}
490
491	// Register a Para ID under control of `manager`.
492	//
493	// Note this is a backend registration API, so verification of ParaId
494	// is not done here to prevent.
495	fn register(
496		manager: T::AccountId,
497		id: ParaId,
498		genesis_head: HeadData,
499		validation_code: ValidationCode,
500	) -> DispatchResult {
501		Self::do_register(manager, None, id, genesis_head, validation_code, false)
502	}
503
504	// Deregister a Para ID, free any data, and return any deposits.
505	fn deregister(id: ParaId) -> DispatchResult {
506		Self::do_deregister(id)
507	}
508
509	// Upgrade a registered on-demand parachain into a lease holding parachain.
510	fn make_parachain(id: ParaId) -> DispatchResult {
511		// Para backend should think this is an on-demand parachain...
512		ensure!(
513			paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parathread),
514			Error::<T>::NotParathread
515		);
516		polkadot_runtime_parachains::schedule_parathread_upgrade::<T>(id)
517			.map_err(|_| Error::<T>::CannotUpgrade)?;
518
519		Ok(())
520	}
521
522	// Downgrade a registered para into a parathread (on-demand parachain).
523	fn make_parathread(id: ParaId) -> DispatchResult {
524		// Para backend should think this is a parachain...
525		ensure!(
526			paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parachain),
527			Error::<T>::NotParachain
528		);
529		polkadot_runtime_parachains::schedule_parachain_downgrade::<T>(id)
530			.map_err(|_| Error::<T>::CannotDowngrade)?;
531		Ok(())
532	}
533
534	#[cfg(any(feature = "runtime-benchmarks", test))]
535	fn worst_head_data() -> HeadData {
536		let max_head_size = configuration::ActiveConfig::<T>::get().max_head_data_size;
537		assert!(max_head_size > 0, "max_head_data can't be zero for generating worst head data.");
538		vec![0u8; max_head_size as usize].into()
539	}
540
541	#[cfg(any(feature = "runtime-benchmarks", test))]
542	fn worst_validation_code() -> ValidationCode {
543		let max_code_size = configuration::ActiveConfig::<T>::get().max_code_size;
544		assert!(max_code_size > 0, "max_code_size can't be zero for generating worst code data.");
545		let validation_code = vec![0u8; max_code_size as usize];
546		validation_code.into()
547	}
548
549	#[cfg(any(feature = "runtime-benchmarks", test))]
550	fn execute_pending_transitions() {
551		use polkadot_runtime_parachains::shared;
552		shared::Pallet::<T>::set_session_index(shared::Pallet::<T>::scheduled_session());
553		paras::Pallet::<T>::test_on_new_session();
554	}
555}
556
557impl<T: Config> Pallet<T> {
558	/// Ensure the origin is one of Root, the `para` owner, or the `para` itself.
559	/// If the origin is the `para` owner, the `para` must be unlocked.
560	fn ensure_root_para_or_owner(
561		origin: <T as frame_system::Config>::RuntimeOrigin,
562		id: ParaId,
563	) -> DispatchResult {
564		if let Ok(who) = ensure_signed(origin.clone()) {
565			let para_info = Paras::<T>::get(id).ok_or(Error::<T>::NotRegistered)?;
566
567			if para_info.manager == who {
568				ensure!(!para_info.is_locked(), Error::<T>::ParaLocked);
569				return Ok(())
570			}
571		}
572
573		Self::ensure_root_or_para(origin, id)
574	}
575
576	/// Ensure the origin is one of Root or the `para` itself.
577	fn ensure_root_or_para(
578		origin: <T as frame_system::Config>::RuntimeOrigin,
579		id: ParaId,
580	) -> DispatchResult {
581		if ensure_root(origin.clone()).is_ok() {
582			return Ok(())
583		}
584
585		let caller_id = ensure_parachain(<T as Config>::RuntimeOrigin::from(origin))?;
586		// Check if matching para id...
587		ensure!(caller_id == id, Error::<T>::NotOwner);
588
589		Ok(())
590	}
591
592	fn do_reserve(
593		who: T::AccountId,
594		deposit_override: Option<BalanceOf<T>>,
595		id: ParaId,
596	) -> DispatchResult {
597		ensure!(!Paras::<T>::contains_key(id), Error::<T>::AlreadyRegistered);
598		ensure!(paras::Pallet::<T>::lifecycle(id).is_none(), Error::<T>::AlreadyRegistered);
599
600		let deposit = deposit_override.unwrap_or_else(T::ParaDeposit::get);
601		<T as Config>::Currency::reserve(&who, deposit)?;
602		let info = ParaInfo { manager: who.clone(), deposit, locked: None };
603
604		Paras::<T>::insert(id, info);
605		Self::deposit_event(Event::<T>::Reserved { para_id: id, who });
606		Ok(())
607	}
608
609	/// Attempt to register a new Para Id under management of `who` in the
610	/// system with the given information.
611	fn do_register(
612		who: T::AccountId,
613		deposit_override: Option<BalanceOf<T>>,
614		id: ParaId,
615		genesis_head: HeadData,
616		validation_code: ValidationCode,
617		ensure_reserved: bool,
618	) -> DispatchResult {
619		let deposited = if let Some(para_data) = Paras::<T>::get(id) {
620			ensure!(para_data.manager == who, Error::<T>::NotOwner);
621			ensure!(!para_data.is_locked(), Error::<T>::ParaLocked);
622			para_data.deposit
623		} else {
624			ensure!(!ensure_reserved, Error::<T>::NotReserved);
625			Default::default()
626		};
627		ensure!(paras::Pallet::<T>::lifecycle(id).is_none(), Error::<T>::AlreadyRegistered);
628		let (genesis, deposit) =
629			Self::validate_onboarding_data(genesis_head, validation_code, ParaKind::Parathread)?;
630		let deposit = deposit_override.unwrap_or(deposit);
631
632		if let Some(additional) = deposit.checked_sub(&deposited) {
633			<T as Config>::Currency::reserve(&who, additional)?;
634		} else if let Some(rebate) = deposited.checked_sub(&deposit) {
635			<T as Config>::Currency::unreserve(&who, rebate);
636		};
637		let info = ParaInfo { manager: who.clone(), deposit, locked: None };
638
639		Paras::<T>::insert(id, info);
640		// We check above that para has no lifecycle, so this should not fail.
641		let res = polkadot_runtime_parachains::schedule_para_initialize::<T>(id, genesis);
642		debug_assert!(res.is_ok());
643		Self::deposit_event(Event::<T>::Registered { para_id: id, manager: who });
644		Ok(())
645	}
646
647	/// Deregister a Para Id, freeing all data returning any deposit.
648	fn do_deregister(id: ParaId) -> DispatchResult {
649		match paras::Pallet::<T>::lifecycle(id) {
650			// Para must be a parathread (on-demand parachain), or not exist at all.
651			Some(ParaLifecycle::Parathread) | None => {},
652			_ => return Err(Error::<T>::NotParathread.into()),
653		}
654		polkadot_runtime_parachains::schedule_para_cleanup::<T>(id)
655			.map_err(|_| Error::<T>::CannotDeregister)?;
656
657		if let Some(info) = Paras::<T>::take(&id) {
658			<T as Config>::Currency::unreserve(&info.manager, info.deposit);
659		}
660
661		PendingSwap::<T>::remove(id);
662		Self::deposit_event(Event::<T>::Deregistered { para_id: id });
663		Ok(())
664	}
665
666	/// Verifies the onboarding data is valid for a para.
667	///
668	/// Returns `ParaGenesisArgs` and the deposit needed for the data.
669	fn validate_onboarding_data(
670		genesis_head: HeadData,
671		validation_code: ValidationCode,
672		para_kind: ParaKind,
673	) -> Result<(ParaGenesisArgs, BalanceOf<T>), sp_runtime::DispatchError> {
674		let config = configuration::ActiveConfig::<T>::get();
675		ensure!(validation_code.0.len() >= MIN_CODE_SIZE as usize, Error::<T>::InvalidCode);
676		ensure!(validation_code.0.len() <= config.max_code_size as usize, Error::<T>::CodeTooLarge);
677		ensure!(
678			genesis_head.0.len() <= config.max_head_data_size as usize,
679			Error::<T>::HeadDataTooLarge
680		);
681
682		let per_byte_fee = T::DataDepositPerByte::get();
683		let deposit = T::ParaDeposit::get()
684			.saturating_add(per_byte_fee.saturating_mul((genesis_head.0.len() as u32).into()))
685			.saturating_add(per_byte_fee.saturating_mul(config.max_code_size.into()));
686
687		Ok((ParaGenesisArgs { genesis_head, validation_code, para_kind }, deposit))
688	}
689
690	/// Swap a lease holding parachain and parathread (on-demand parachain), which involves
691	/// scheduling an appropriate lifecycle update.
692	fn do_thread_and_chain_swap(to_downgrade: ParaId, to_upgrade: ParaId) {
693		let res1 = polkadot_runtime_parachains::schedule_parachain_downgrade::<T>(to_downgrade);
694		debug_assert!(res1.is_ok());
695		let res2 = polkadot_runtime_parachains::schedule_parathread_upgrade::<T>(to_upgrade);
696		debug_assert!(res2.is_ok());
697		T::OnSwap::on_swap(to_upgrade, to_downgrade);
698	}
699}
700
701impl<T: Config> OnNewHead for Pallet<T> {
702	fn on_new_head(id: ParaId, _head: &HeadData) -> Weight {
703		// mark the parachain locked if the locked value is not already set
704		let mut writes = 0;
705		if let Some(mut info) = Paras::<T>::get(id) {
706			if info.locked.is_none() {
707				info.locked = Some(true);
708				Paras::<T>::insert(id, info);
709				writes += 1;
710			}
711		}
712		T::DbWeight::get().reads_writes(1, writes)
713	}
714}
715
716#[cfg(test)]
717mod mock;
718
719#[cfg(test)]
720mod tests;
721
722#[cfg(feature = "runtime-benchmarks")]
723mod benchmarking;