referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_solo_to_para/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#![cfg_attr(not(feature = "std"), no_std)]
18
19extern crate alloc;
20
21use alloc::vec::Vec;
22use cumulus_pallet_parachain_system as parachain_system;
23use frame_support::pallet_prelude::*;
24use frame_system::pallet_prelude::*;
25pub use pallet::*;
26use polkadot_primitives::PersistedValidationData;
27
28#[frame_support::pallet]
29pub mod pallet {
30	use super::*;
31
32	#[pallet::config]
33	pub trait Config:
34		frame_system::Config + parachain_system::Config + pallet_sudo::Config
35	{
36		#[allow(deprecated)]
37		type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
38	}
39
40	#[pallet::pallet]
41	#[pallet::without_storage_info]
42	pub struct Pallet<T>(_);
43
44	/// In case of a scheduled migration, this storage field contains the custom head data to be
45	/// applied.
46	#[pallet::storage]
47	pub(super) type PendingCustomValidationHeadData<T: Config> =
48		StorageValue<_, Vec<u8>, OptionQuery>;
49
50	#[pallet::event]
51	#[pallet::generate_deposit(pub(super) fn deposit_event)]
52	pub enum Event {
53		/// The custom validation head data has been scheduled to apply.
54		CustomValidationHeadDataStored,
55		/// The custom validation head data was applied as of the contained relay chain block
56		/// number.
57		CustomValidationHeadDataApplied,
58	}
59
60	#[pallet::error]
61	pub enum Error<T> {
62		/// CustomHeadData is not stored in storage.
63		NoCustomHeadData,
64	}
65
66	#[pallet::call]
67	impl<T: Config> Pallet<T> {
68		#[pallet::call_index(0)]
69		#[pallet::weight({0})]
70		pub fn schedule_migration(
71			origin: OriginFor<T>,
72			code: Vec<u8>,
73			head_data: Vec<u8>,
74		) -> DispatchResult {
75			ensure_root(origin)?;
76
77			parachain_system::Pallet::<T>::schedule_code_upgrade(code)?;
78			Self::store_pending_custom_validation_head_data(head_data);
79			Ok(())
80		}
81	}
82
83	impl<T: Config> Pallet<T> {
84		/// Set a custom head data that should only be applied when upgradeGoAheadSignal from
85		/// the Relay Chain is GoAhead
86		fn store_pending_custom_validation_head_data(head_data: Vec<u8>) {
87			PendingCustomValidationHeadData::<T>::put(head_data);
88			Self::deposit_event(Event::CustomValidationHeadDataStored);
89		}
90
91		/// Set pending custom head data as head data that will be returned by `validate_block`. on
92		/// the relay chain.
93		fn set_pending_custom_validation_head_data() {
94			if let Some(head_data) = <PendingCustomValidationHeadData<T>>::take() {
95				parachain_system::Pallet::<T>::set_custom_validation_head_data(head_data);
96				Self::deposit_event(Event::CustomValidationHeadDataApplied);
97			}
98		}
99	}
100
101	impl<T: Config> parachain_system::OnSystemEvent for Pallet<T> {
102		fn on_validation_data(_data: &PersistedValidationData) {}
103		fn on_validation_code_applied() {
104			crate::Pallet::<T>::set_pending_custom_validation_head_data();
105		}
106	}
107}