pub mod migration;
use alloc::{vec, vec::Vec};
use core::result;
use frame_support::{
dispatch::DispatchResult,
ensure,
pallet_prelude::Weight,
traits::{Currency, Get, ReservableCurrency},
};
use frame_system::{self, ensure_root, ensure_signed};
use polkadot_primitives::{
HeadData, Id as ParaId, ValidationCode, LOWEST_PUBLIC_ID, MIN_CODE_SIZE,
};
use polkadot_runtime_parachains::{
configuration, ensure_parachain,
paras::{self, ParaGenesisArgs, UpgradeStrategy},
Origin, ParaLifecycle,
};
use crate::traits::{OnSwap, Registrar};
use codec::{Decode, Encode};
pub use pallet::*;
use polkadot_runtime_parachains::paras::{OnNewHead, ParaKind};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{CheckedSub, Saturating},
RuntimeDebug,
};
#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug, TypeInfo)]
pub struct ParaInfo<Account, Balance> {
pub(crate) manager: Account,
deposit: Balance,
locked: Option<bool>,
}
impl<Account, Balance> ParaInfo<Account, Balance> {
pub fn is_locked(&self) -> bool {
self.locked.unwrap_or(false)
}
}
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub trait WeightInfo {
fn reserve() -> Weight;
fn register() -> Weight;
fn force_register() -> Weight;
fn deregister() -> Weight;
fn swap() -> Weight;
fn schedule_code_upgrade(b: u32) -> Weight;
fn set_current_head(b: u32) -> Weight;
}
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn reserve() -> Weight {
Weight::zero()
}
fn register() -> Weight {
Weight::zero()
}
fn force_register() -> Weight {
Weight::zero()
}
fn deregister() -> Weight {
Weight::zero()
}
fn swap() -> Weight {
Weight::zero()
}
fn schedule_code_upgrade(_b: u32) -> Weight {
Weight::zero()
}
fn set_current_head(_b: u32) -> Weight {
Weight::zero()
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: configuration::Config + paras::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type RuntimeOrigin: From<<Self as frame_system::Config>::RuntimeOrigin>
+ Into<result::Result<Origin, <Self as Config>::RuntimeOrigin>>;
type Currency: ReservableCurrency<Self::AccountId>;
type OnSwap: crate::traits::OnSwap;
#[pallet::constant]
type ParaDeposit: Get<BalanceOf<Self>>;
#[pallet::constant]
type DataDepositPerByte: Get<BalanceOf<Self>>;
type WeightInfo: WeightInfo;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Registered { para_id: ParaId, manager: T::AccountId },
Deregistered { para_id: ParaId },
Reserved { para_id: ParaId, who: T::AccountId },
Swapped { para_id: ParaId, other_id: ParaId },
}
#[pallet::error]
pub enum Error<T> {
NotRegistered,
AlreadyRegistered,
NotOwner,
CodeTooLarge,
HeadDataTooLarge,
NotParachain,
NotParathread,
CannotDeregister,
CannotDowngrade,
CannotUpgrade,
ParaLocked,
NotReserved,
InvalidCode,
CannotSwap,
}
#[pallet::storage]
pub(super) type PendingSwap<T> = StorageMap<_, Twox64Concat, ParaId, ParaId>;
#[pallet::storage]
pub type Paras<T: Config> =
StorageMap<_, Twox64Concat, ParaId, ParaInfo<T::AccountId, BalanceOf<T>>>;
#[pallet::storage]
pub type NextFreeParaId<T> = StorageValue<_, ParaId, ValueQuery>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
#[serde(skip)]
pub _config: core::marker::PhantomData<T>,
pub next_free_para_id: ParaId,
}
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig { next_free_para_id: LOWEST_PUBLIC_ID, _config: Default::default() }
}
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
NextFreeParaId::<T>::put(self.next_free_para_id);
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::register())]
pub fn register(
origin: OriginFor<T>,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_register(who, None, id, genesis_head, validation_code, true)?;
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::force_register())]
pub fn force_register(
origin: OriginFor<T>,
who: T::AccountId,
deposit: BalanceOf<T>,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
) -> DispatchResult {
ensure_root(origin)?;
Self::do_register(who, Some(deposit), id, genesis_head, validation_code, false)
}
#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::deregister())]
pub fn deregister(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, id)?;
Self::do_deregister(id)
}
#[pallet::call_index(3)]
#[pallet::weight(<T as Config>::WeightInfo::swap())]
pub fn swap(origin: OriginFor<T>, id: ParaId, other: ParaId) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, id)?;
if id == other {
PendingSwap::<T>::remove(id);
return Ok(())
}
let id_lifecycle =
paras::Pallet::<T>::lifecycle(id).ok_or(Error::<T>::NotRegistered)?;
if PendingSwap::<T>::get(other) == Some(id) {
let other_lifecycle =
paras::Pallet::<T>::lifecycle(other).ok_or(Error::<T>::NotRegistered)?;
if id_lifecycle == ParaLifecycle::Parachain &&
other_lifecycle == ParaLifecycle::Parathread
{
Self::do_thread_and_chain_swap(id, other);
} else if id_lifecycle == ParaLifecycle::Parathread &&
other_lifecycle == ParaLifecycle::Parachain
{
Self::do_thread_and_chain_swap(other, id);
} else if id_lifecycle == ParaLifecycle::Parachain &&
other_lifecycle == ParaLifecycle::Parachain
{
T::OnSwap::on_swap(id, other);
} else {
return Err(Error::<T>::CannotSwap.into())
}
Self::deposit_event(Event::<T>::Swapped { para_id: id, other_id: other });
PendingSwap::<T>::remove(other);
} else {
PendingSwap::<T>::insert(id, other);
}
Ok(())
}
#[pallet::call_index(4)]
#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
pub fn remove_lock(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
Self::ensure_root_or_para(origin, para)?;
<Self as Registrar>::remove_lock(para);
Ok(())
}
#[pallet::call_index(5)]
#[pallet::weight(<T as Config>::WeightInfo::reserve())]
pub fn reserve(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let id = NextFreeParaId::<T>::get().max(LOWEST_PUBLIC_ID);
Self::do_reserve(who, None, id)?;
NextFreeParaId::<T>::set(id + 1);
Ok(())
}
#[pallet::call_index(6)]
#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
pub fn add_lock(origin: OriginFor<T>, para: ParaId) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, para)?;
<Self as Registrar>::apply_lock(para);
Ok(())
}
#[pallet::call_index(7)]
#[pallet::weight(<T as Config>::WeightInfo::schedule_code_upgrade(new_code.0.len() as u32))]
pub fn schedule_code_upgrade(
origin: OriginFor<T>,
para: ParaId,
new_code: ValidationCode,
) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, para)?;
polkadot_runtime_parachains::schedule_code_upgrade::<T>(
para,
new_code,
UpgradeStrategy::ApplyAtExpectedBlock,
)?;
Ok(())
}
#[pallet::call_index(8)]
#[pallet::weight(<T as Config>::WeightInfo::set_current_head(new_head.0.len() as u32))]
pub fn set_current_head(
origin: OriginFor<T>,
para: ParaId,
new_head: HeadData,
) -> DispatchResult {
Self::ensure_root_para_or_owner(origin, para)?;
polkadot_runtime_parachains::set_current_head::<T>(para, new_head);
Ok(())
}
}
}
impl<T: Config> Registrar for Pallet<T> {
type AccountId = T::AccountId;
fn manager_of(id: ParaId) -> Option<T::AccountId> {
Some(Paras::<T>::get(id)?.manager)
}
fn parachains() -> Vec<ParaId> {
paras::Parachains::<T>::get()
}
fn is_parathread(id: ParaId) -> bool {
paras::Pallet::<T>::is_parathread(id)
}
fn is_parachain(id: ParaId) -> bool {
paras::Pallet::<T>::is_parachain(id)
}
fn apply_lock(id: ParaId) {
Paras::<T>::mutate(id, |x| x.as_mut().map(|info| info.locked = Some(true)));
}
fn remove_lock(id: ParaId) {
Paras::<T>::mutate(id, |x| x.as_mut().map(|info| info.locked = Some(false)));
}
fn register(
manager: T::AccountId,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
) -> DispatchResult {
Self::do_register(manager, None, id, genesis_head, validation_code, false)
}
fn deregister(id: ParaId) -> DispatchResult {
Self::do_deregister(id)
}
fn make_parachain(id: ParaId) -> DispatchResult {
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parathread),
Error::<T>::NotParathread
);
polkadot_runtime_parachains::schedule_parathread_upgrade::<T>(id)
.map_err(|_| Error::<T>::CannotUpgrade)?;
Ok(())
}
fn make_parathread(id: ParaId) -> DispatchResult {
ensure!(
paras::Pallet::<T>::lifecycle(id) == Some(ParaLifecycle::Parachain),
Error::<T>::NotParachain
);
polkadot_runtime_parachains::schedule_parachain_downgrade::<T>(id)
.map_err(|_| Error::<T>::CannotDowngrade)?;
Ok(())
}
#[cfg(any(feature = "runtime-benchmarks", test))]
fn worst_head_data() -> HeadData {
let max_head_size = configuration::ActiveConfig::<T>::get().max_head_data_size;
assert!(max_head_size > 0, "max_head_data can't be zero for generating worst head data.");
vec![0u8; max_head_size as usize].into()
}
#[cfg(any(feature = "runtime-benchmarks", test))]
fn worst_validation_code() -> ValidationCode {
let max_code_size = configuration::ActiveConfig::<T>::get().max_code_size;
assert!(max_code_size > 0, "max_code_size can't be zero for generating worst code data.");
let validation_code = vec![0u8; max_code_size as usize];
validation_code.into()
}
#[cfg(any(feature = "runtime-benchmarks", test))]
fn execute_pending_transitions() {
use polkadot_runtime_parachains::shared;
shared::Pallet::<T>::set_session_index(shared::Pallet::<T>::scheduled_session());
paras::Pallet::<T>::test_on_new_session();
}
}
impl<T: Config> Pallet<T> {
fn ensure_root_para_or_owner(
origin: <T as frame_system::Config>::RuntimeOrigin,
id: ParaId,
) -> DispatchResult {
if let Ok(who) = ensure_signed(origin.clone()) {
let para_info = Paras::<T>::get(id).ok_or(Error::<T>::NotRegistered)?;
if para_info.manager == who {
ensure!(!para_info.is_locked(), Error::<T>::ParaLocked);
return Ok(())
}
}
Self::ensure_root_or_para(origin, id)
}
fn ensure_root_or_para(
origin: <T as frame_system::Config>::RuntimeOrigin,
id: ParaId,
) -> DispatchResult {
if ensure_root(origin.clone()).is_ok() {
return Ok(())
}
let caller_id = ensure_parachain(<T as Config>::RuntimeOrigin::from(origin))?;
ensure!(caller_id == id, Error::<T>::NotOwner);
Ok(())
}
fn do_reserve(
who: T::AccountId,
deposit_override: Option<BalanceOf<T>>,
id: ParaId,
) -> DispatchResult {
ensure!(!Paras::<T>::contains_key(id), Error::<T>::AlreadyRegistered);
ensure!(paras::Pallet::<T>::lifecycle(id).is_none(), Error::<T>::AlreadyRegistered);
let deposit = deposit_override.unwrap_or_else(T::ParaDeposit::get);
<T as Config>::Currency::reserve(&who, deposit)?;
let info = ParaInfo { manager: who.clone(), deposit, locked: None };
Paras::<T>::insert(id, info);
Self::deposit_event(Event::<T>::Reserved { para_id: id, who });
Ok(())
}
fn do_register(
who: T::AccountId,
deposit_override: Option<BalanceOf<T>>,
id: ParaId,
genesis_head: HeadData,
validation_code: ValidationCode,
ensure_reserved: bool,
) -> DispatchResult {
let deposited = if let Some(para_data) = Paras::<T>::get(id) {
ensure!(para_data.manager == who, Error::<T>::NotOwner);
ensure!(!para_data.is_locked(), Error::<T>::ParaLocked);
para_data.deposit
} else {
ensure!(!ensure_reserved, Error::<T>::NotReserved);
Default::default()
};
ensure!(paras::Pallet::<T>::lifecycle(id).is_none(), Error::<T>::AlreadyRegistered);
let (genesis, deposit) =
Self::validate_onboarding_data(genesis_head, validation_code, ParaKind::Parathread)?;
let deposit = deposit_override.unwrap_or(deposit);
if let Some(additional) = deposit.checked_sub(&deposited) {
<T as Config>::Currency::reserve(&who, additional)?;
} else if let Some(rebate) = deposited.checked_sub(&deposit) {
<T as Config>::Currency::unreserve(&who, rebate);
};
let info = ParaInfo { manager: who.clone(), deposit, locked: None };
Paras::<T>::insert(id, info);
let res = polkadot_runtime_parachains::schedule_para_initialize::<T>(id, genesis);
debug_assert!(res.is_ok());
Self::deposit_event(Event::<T>::Registered { para_id: id, manager: who });
Ok(())
}
fn do_deregister(id: ParaId) -> DispatchResult {
match paras::Pallet::<T>::lifecycle(id) {
Some(ParaLifecycle::Parathread) | None => {},
_ => return Err(Error::<T>::NotParathread.into()),
}
polkadot_runtime_parachains::schedule_para_cleanup::<T>(id)
.map_err(|_| Error::<T>::CannotDeregister)?;
if let Some(info) = Paras::<T>::take(&id) {
<T as Config>::Currency::unreserve(&info.manager, info.deposit);
}
PendingSwap::<T>::remove(id);
Self::deposit_event(Event::<T>::Deregistered { para_id: id });
Ok(())
}
fn validate_onboarding_data(
genesis_head: HeadData,
validation_code: ValidationCode,
para_kind: ParaKind,
) -> Result<(ParaGenesisArgs, BalanceOf<T>), sp_runtime::DispatchError> {
let config = configuration::ActiveConfig::<T>::get();
ensure!(validation_code.0.len() >= MIN_CODE_SIZE as usize, Error::<T>::InvalidCode);
ensure!(validation_code.0.len() <= config.max_code_size as usize, Error::<T>::CodeTooLarge);
ensure!(
genesis_head.0.len() <= config.max_head_data_size as usize,
Error::<T>::HeadDataTooLarge
);
let per_byte_fee = T::DataDepositPerByte::get();
let deposit = T::ParaDeposit::get()
.saturating_add(per_byte_fee.saturating_mul((genesis_head.0.len() as u32).into()))
.saturating_add(per_byte_fee.saturating_mul(config.max_code_size.into()));
Ok((ParaGenesisArgs { genesis_head, validation_code, para_kind }, deposit))
}
fn do_thread_and_chain_swap(to_downgrade: ParaId, to_upgrade: ParaId) {
let res1 = polkadot_runtime_parachains::schedule_parachain_downgrade::<T>(to_downgrade);
debug_assert!(res1.is_ok());
let res2 = polkadot_runtime_parachains::schedule_parathread_upgrade::<T>(to_upgrade);
debug_assert!(res2.is_ok());
T::OnSwap::on_swap(to_upgrade, to_downgrade);
}
}
impl<T: Config> OnNewHead for Pallet<T> {
fn on_new_head(id: ParaId, _head: &HeadData) -> Weight {
let mut writes = 0;
if let Some(mut info) = Paras::<T>::get(id) {
if info.locked.is_none() {
info.locked = Some(true);
Paras::<T>::insert(id, info);
writes += 1;
}
}
T::DbWeight::get().reads_writes(1, writes)
}
}
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;