Skip to main content

topsoil_core/system/extensions/
check_spec_version.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use crate::system::{Config, Pallet};
8use codec::{Decode, DecodeWithMemTracking, Encode};
9use scale_info::TypeInfo;
10use subsoil::runtime::{
11	traits::TransactionExtension, transaction_validity::TransactionValidityError,
12};
13
14/// Ensure the runtime version registered in the transaction is the same as at present.
15///
16/// # Transaction Validity
17///
18/// The transaction with incorrect `spec_version` are considered invalid. The validity
19/// is not affected in any other way.
20#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)]
21#[scale_info(skip_type_params(T))]
22pub struct CheckSpecVersion<T: Config + Send + Sync>(core::marker::PhantomData<T>);
23
24impl<T: Config + Send + Sync> core::fmt::Debug for CheckSpecVersion<T> {
25	#[cfg(feature = "std")]
26	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
27		write!(f, "CheckSpecVersion")
28	}
29
30	#[cfg(not(feature = "std"))]
31	fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
32		Ok(())
33	}
34}
35
36impl<T: Config + Send + Sync> CheckSpecVersion<T> {
37	/// Create new `TransactionExtension` to check runtime version.
38	pub fn new() -> Self {
39		Self(core::marker::PhantomData)
40	}
41}
42
43impl<T: Config + Send + Sync> TransactionExtension<<T as Config>::RuntimeCall>
44	for CheckSpecVersion<T>
45{
46	const IDENTIFIER: &'static str = "CheckSpecVersion";
47	type Implicit = u32;
48	fn implicit(&self) -> Result<Self::Implicit, TransactionValidityError> {
49		Ok(<Pallet<T>>::runtime_version().spec_version)
50	}
51	type Val = ();
52	type Pre = ();
53	fn weight(&self, _: &<T as Config>::RuntimeCall) -> subsoil::weights::Weight {
54		<T::ExtensionsWeightInfo as super::WeightInfo>::check_spec_version()
55	}
56	subsoil::impl_tx_ext_default!(<T as Config>::RuntimeCall; validate prepare);
57}