referrerpolicy=no-referrer-when-downgrade

frame_system/extensions/
check_tx_version.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
18use crate::{Config, Pallet};
19use codec::{Decode, DecodeWithMemTracking, Encode};
20use scale_info::TypeInfo;
21use sp_runtime::{
22	impl_tx_ext_default, traits::TransactionExtension,
23	transaction_validity::TransactionValidityError,
24};
25
26/// Ensure the transaction version registered in the transaction is the same as at present.
27///
28/// # Transaction Validity
29///
30/// The transaction with incorrect `transaction_version` are considered invalid. The validity
31/// is not affected in any other way.
32#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)]
33#[scale_info(skip_type_params(T))]
34pub struct CheckTxVersion<T: Config + Send + Sync>(core::marker::PhantomData<T>);
35
36impl<T: Config + Send + Sync> core::fmt::Debug for CheckTxVersion<T> {
37	#[cfg(feature = "std")]
38	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
39		write!(f, "CheckTxVersion")
40	}
41
42	#[cfg(not(feature = "std"))]
43	fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
44		Ok(())
45	}
46}
47
48impl<T: Config + Send + Sync> CheckTxVersion<T> {
49	/// Create new `TransactionExtension` to check transaction version.
50	pub fn new() -> Self {
51		Self(core::marker::PhantomData)
52	}
53}
54
55impl<T: Config + Send + Sync> TransactionExtension<<T as Config>::RuntimeCall>
56	for CheckTxVersion<T>
57{
58	const IDENTIFIER: &'static str = "CheckTxVersion";
59	type Implicit = u32;
60	fn implicit(&self) -> Result<Self::Implicit, TransactionValidityError> {
61		Ok(<Pallet<T>>::runtime_version().transaction_version)
62	}
63	type Val = ();
64	type Pre = ();
65	fn weight(&self, _: &<T as Config>::RuntimeCall) -> sp_weights::Weight {
66		<T::ExtensionsWeightInfo as super::WeightInfo>::check_tx_version()
67	}
68	impl_tx_ext_default!(<T as Config>::RuntimeCall; validate prepare);
69}