frame_system/extensions/
check_genesis.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::{pallet_prelude::BlockNumberFor, Config, Pallet};
19use codec::{Decode, Encode};
20use scale_info::TypeInfo;
21use sp_runtime::{
22	traits::{DispatchInfoOf, SignedExtension, Zero},
23	transaction_validity::TransactionValidityError,
24};
25
26/// Genesis hash check to provide replay protection between different networks.
27///
28/// # Transaction Validity
29///
30/// Note that while a transaction with invalid `genesis_hash` will fail to be decoded,
31/// the extension does not affect any other fields of `TransactionValidity` directly.
32#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
33#[scale_info(skip_type_params(T))]
34pub struct CheckGenesis<T: Config + Send + Sync>(core::marker::PhantomData<T>);
35
36impl<T: Config + Send + Sync> core::fmt::Debug for CheckGenesis<T> {
37	#[cfg(feature = "std")]
38	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
39		write!(f, "CheckGenesis")
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> CheckGenesis<T> {
49	/// Creates new `SignedExtension` to check genesis hash.
50	pub fn new() -> Self {
51		Self(core::marker::PhantomData)
52	}
53}
54
55impl<T: Config + Send + Sync> SignedExtension for CheckGenesis<T> {
56	type AccountId = T::AccountId;
57	type Call = <T as Config>::RuntimeCall;
58	type AdditionalSigned = T::Hash;
59	type Pre = ();
60	const IDENTIFIER: &'static str = "CheckGenesis";
61
62	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
63		Ok(<Pallet<T>>::block_hash(BlockNumberFor::<T>::zero()))
64	}
65
66	fn pre_dispatch(
67		self,
68		who: &Self::AccountId,
69		call: &Self::Call,
70		info: &DispatchInfoOf<Self::Call>,
71		len: usize,
72	) -> Result<Self::Pre, TransactionValidityError> {
73		self.validate(who, call, info, len).map(|_| ())
74	}
75}