referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/construct_runtime/expand/
unsigned.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::construct_runtime::Pallet;
19use proc_macro2::TokenStream;
20use quote::quote;
21use syn::Ident;
22
23pub fn expand_outer_validate_unsigned(
24	runtime: &Ident,
25	pallet_decls: &[Pallet],
26	scrate: &TokenStream,
27) -> TokenStream {
28	let mut pallet_names = Vec::new();
29	let mut pallet_attrs = Vec::new();
30	let mut query_validate_unsigned_part_macros = Vec::new();
31
32	for pallet_decl in pallet_decls {
33		if pallet_decl.exists_part("ValidateUnsigned") {
34			let name = &pallet_decl.name;
35			let path = &pallet_decl.path;
36			let attr = pallet_decl.get_attributes();
37
38			pallet_names.push(name);
39			pallet_attrs.push(attr);
40			query_validate_unsigned_part_macros.push(quote! {
41				#path::__substrate_validate_unsigned_check::is_validate_unsigned_part_defined!(#name);
42			});
43		}
44	}
45
46	quote! {
47		#( #query_validate_unsigned_part_macros )*
48
49		impl #scrate::unsigned::ValidateUnsigned for #runtime {
50			type Call = RuntimeCall;
51
52			fn pre_dispatch(call: &Self::Call) -> Result<(), #scrate::unsigned::TransactionValidityError> {
53				#[allow(unreachable_patterns)]
54				match call {
55					#(
56						#pallet_attrs
57						RuntimeCall::#pallet_names(inner_call) => #pallet_names::pre_dispatch(inner_call),
58					)*
59					// pre-dispatch should not stop inherent extrinsics, validation should prevent
60					// including arbitrary (non-inherent) extrinsics to blocks.
61					_ => Ok(()),
62				}
63			}
64
65			fn validate_unsigned(
66				#[allow(unused_variables)]
67				source: #scrate::unsigned::TransactionSource,
68				call: &Self::Call,
69			) -> #scrate::unsigned::TransactionValidity {
70				#[allow(unreachable_patterns)]
71				match call {
72					#(
73						#pallet_attrs
74						RuntimeCall::#pallet_names(inner_call) => #pallet_names::validate_unsigned(source, inner_call),
75					)*
76					_ => #scrate::unsigned::UnknownTransaction::NoUnsignedValidator.into(),
77				}
78			}
79		}
80	}
81}