referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_parachain_system_proc_macro/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use proc_macro2::Span;
18use proc_macro_crate::{crate_name, FoundCrate};
19use syn::{
20	parse::{Parse, ParseStream},
21	spanned::Spanned,
22	token, Error, Ident, Path,
23};
24
25mod keywords {
26	syn::custom_keyword!(Runtime);
27	syn::custom_keyword!(BlockExecutor);
28	syn::custom_keyword!(CheckInherents);
29}
30
31struct Input {
32	runtime: Path,
33	block_executor: Path,
34}
35
36impl Parse for Input {
37	fn parse(input: ParseStream) -> Result<Self, Error> {
38		let mut runtime = None;
39		let mut block_executor = None;
40
41		fn parse_inner<KW: Parse + Spanned>(
42			input: ParseStream,
43			result: &mut Option<Path>,
44		) -> Result<(), Error> {
45			let kw = input.parse::<KW>()?;
46
47			if result.is_none() {
48				input.parse::<token::Eq>()?;
49				*result = Some(input.parse::<Path>()?);
50				if input.peek(token::Comma) {
51					input.parse::<token::Comma>()?;
52				}
53
54				Ok(())
55			} else {
56				Err(Error::new(kw.span(), "Is only allowed to be passed once"))
57			}
58		}
59
60		while !input.is_empty() || runtime.is_none() || block_executor.is_none() {
61			let lookahead = input.lookahead1();
62
63			if lookahead.peek(keywords::Runtime) {
64				parse_inner::<keywords::Runtime>(input, &mut runtime)?;
65			} else if lookahead.peek(keywords::BlockExecutor) {
66				parse_inner::<keywords::BlockExecutor>(input, &mut block_executor)?;
67			} else if lookahead.peek(keywords::CheckInherents) {
68				return Err(Error::new(input.span(), "`CheckInherents` is not supported anymore!"));
69			} else {
70				return Err(lookahead.error())
71			}
72		}
73
74		Ok(Self {
75			runtime: runtime.expect("Everything is parsed before; qed"),
76			block_executor: block_executor.expect("Everything is parsed before; qed"),
77		})
78	}
79}
80
81fn crate_() -> Result<Ident, Error> {
82	match crate_name("cumulus-pallet-parachain-system") {
83		Ok(FoundCrate::Itself) =>
84			Ok(syn::Ident::new("cumulus_pallet_parachain_system", Span::call_site())),
85		Ok(FoundCrate::Name(name)) => Ok(Ident::new(&name, Span::call_site())),
86		Err(e) => Err(Error::new(Span::call_site(), e)),
87	}
88}
89
90#[proc_macro]
91pub fn register_validate_block(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
92	let Input { runtime, block_executor } = match syn::parse(input) {
93		Ok(t) => t,
94		Err(e) => return e.into_compile_error().into(),
95	};
96
97	let crate_ = match crate_() {
98		Ok(c) => c,
99		Err(e) => return e.into_compile_error().into(),
100	};
101
102	if cfg!(not(feature = "std")) {
103		quote::quote! {
104			#[doc(hidden)]
105			mod parachain_validate_block {
106				use super::*;
107
108				#[no_mangle]
109				unsafe fn validate_block(arguments: *mut u8, arguments_len: usize) -> u64 {
110					// We convert the `arguments` into a boxed slice and then into `Bytes`.
111					let args = #crate_::validate_block::Box::from_raw(
112						#crate_::validate_block::slice::from_raw_parts_mut(
113							arguments,
114							arguments_len,
115						)
116					);
117					let args = #crate_::validate_block::bytes::Bytes::from(args);
118
119					// Then we decode from these bytes the `MemoryOptimizedValidationParams`.
120					let params = #crate_::validate_block::decode_from_bytes::<
121						#crate_::validate_block::MemoryOptimizedValidationParams
122					>(args).expect("Invalid arguments to `validate_block`.");
123
124					let res = #crate_::validate_block::implementation::validate_block::<
125						<#runtime as #crate_::validate_block::GetRuntimeBlockType>::RuntimeBlock,
126						#block_executor,
127						#runtime,
128					>(params);
129
130					#crate_::validate_block::polkadot_parachain_primitives::write_result(&res)
131				}
132			}
133		}
134	} else {
135		quote::quote!()
136	}
137	.into()
138}