referrerpolicy=no-referrer-when-downgrade

substrate_test_runtime/
inherent_upgrade_pallet.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! A pallet for testing that inherents are checked by the runtime that built the block, after
19//! its migrations were applied.
20//!
21//! The pallet is not part of the `without_inherent_upgrade_pallet` runtime, so that runtime is
22//! not able to decode the inherent of this pallet.
23
24pub use pallet::*;
25
26#[frame_support::pallet(dev_mode)]
27pub mod pallet {
28	use frame_support::{
29		inherent::{InherentData, InherentIdentifier, ProvideInherent},
30		pallet_prelude::*,
31	};
32	use frame_system::pallet_prelude::*;
33
34	/// The inherent identifier.
35	///
36	/// The inherent data is a `bool`. When `true`, the inherent check requires [`Migrated`] to be
37	/// set.
38	pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"upgrade0";
39
40	#[pallet::pallet]
41	pub struct Pallet<T>(_);
42
43	#[pallet::config]
44	pub trait Config: frame_system::Config {}
45
46	/// Set by `on_runtime_upgrade`.
47	#[pallet::storage]
48	pub type Migrated<T: Config> = StorageValue<_, bool, ValueQuery>;
49
50	#[pallet::hooks]
51	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
52		fn on_runtime_upgrade() -> Weight {
53			Migrated::<T>::put(true);
54			T::DbWeight::get().writes(1)
55		}
56	}
57
58	#[pallet::call]
59	impl<T: Config> Pallet<T> {
60		#[pallet::weight((0, DispatchClass::Mandatory))]
61		pub fn inherent(origin: OriginFor<T>) -> DispatchResult {
62			ensure_none(origin)?;
63			Ok(())
64		}
65	}
66
67	#[pallet::inherent]
68	impl<T: Config> ProvideInherent for Pallet<T> {
69		type Call = Call<T>;
70		type Error = sp_inherents::MakeFatalError<()>;
71		const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
72
73		fn create_inherent(data: &InherentData) -> Option<Self::Call> {
74			data.get_data::<bool>(&INHERENT_IDENTIFIER)
75				.ok()
76				.flatten()
77				.map(|_| Call::inherent {})
78		}
79
80		fn check_inherent(_: &Self::Call, data: &InherentData) -> Result<(), Self::Error> {
81			let requires_migration =
82				data.get_data::<bool>(&INHERENT_IDENTIFIER).ok().flatten().unwrap_or(false);
83
84			if requires_migration && !Migrated::<T>::get() {
85				Err(().into())
86			} else {
87				Ok(())
88			}
89		}
90
91		fn is_inherent(call: &Self::Call) -> bool {
92			matches!(call, Call::inherent {})
93		}
94	}
95}