1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::construct_runtime::Pallet;
use proc_macro2::TokenStream;
use quote::quote;
use std::str::FromStr;
use syn::Ident;
pub fn expand_outer_dispatch(
runtime: &Ident,
system_pallet: &Pallet,
pallet_decls: &[Pallet],
scrate: &TokenStream,
) -> TokenStream {
let mut variant_defs = TokenStream::new();
let mut variant_patterns = Vec::new();
let mut query_call_part_macros = Vec::new();
let mut pallet_names = Vec::new();
let mut pallet_attrs = Vec::new();
let system_path = &system_pallet.path;
let pallets_with_call = pallet_decls.iter().filter(|decl| decl.exists_part("Call"));
for pallet_declaration in pallets_with_call {
let name = &pallet_declaration.name;
let path = &pallet_declaration.path;
let index = pallet_declaration.index;
let attr =
pallet_declaration.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
.expect("was successfully parsed before; qed");
quote! {
#acc
#attr
}
});
variant_defs.extend(quote! {
#attr
#[codec(index = #index)]
#name( #scrate::dispatch::CallableCallFor<#name, #runtime> ),
});
variant_patterns.push(quote!(Call::#name(call)));
pallet_names.push(name);
pallet_attrs.push(attr);
query_call_part_macros.push(quote! {
#path::__substrate_call_check::is_call_part_defined!(#name);
});
}
quote! {
#( #query_call_part_macros )*
#[derive(
Clone, PartialEq, Eq,
#scrate::codec::Encode,
#scrate::codec::Decode,
#scrate::scale_info::TypeInfo,
#scrate::RuntimeDebug,
)]
pub enum Call {
#variant_defs
}
#[cfg(test)]
impl Call {
pub const fn sizes() -> &'static [( &'static str, usize )] {
use #scrate::dispatch::Callable;
use core::mem::size_of;
&[#(
#pallet_attrs
(
stringify!(#pallet_names),
size_of::< <#pallet_names as Callable<#runtime>>::Call >(),
),
)*]
}
pub fn assert_size_under(limit: usize) {
let size = core::mem::size_of::<Self>();
let call_oversize = size > limit;
if call_oversize {
println!("Size of `Call` is {} bytes (provided limit is {} bytes)", size, limit);
let mut sizes = Self::sizes().to_vec();
sizes.sort_by_key(|x| -(x.1 as isize));
for (i, &(name, size)) in sizes.iter().enumerate().take(5) {
println!("Offender #{}: {} at {} bytes", i + 1, name, size);
}
if let Some((_, next_size)) = sizes.get(5) {
println!("{} others of size {} bytes or less", sizes.len() - 5, next_size);
}
panic!(
"Size of `Call` is more than limit; use `Box` on complex parameter types to reduce the
size of `Call`.
If the limit is too strong, maybe consider providing a higher limit."
);
}
}
}
impl #scrate::dispatch::GetDispatchInfo for Call {
fn get_dispatch_info(&self) -> #scrate::dispatch::DispatchInfo {
match self {
#(
#pallet_attrs
#variant_patterns => call.get_dispatch_info(),
)*
}
}
}
impl #scrate::dispatch::GetCallMetadata for Call {
fn get_call_metadata(&self) -> #scrate::dispatch::CallMetadata {
use #scrate::dispatch::GetCallName;
match self {
#(
#pallet_attrs
#variant_patterns => {
let function_name = call.get_call_name();
let pallet_name = stringify!(#pallet_names);
#scrate::dispatch::CallMetadata { function_name, pallet_name }
}
)*
}
}
fn get_module_names() -> &'static [&'static str] {
&[#(
#pallet_attrs
stringify!(#pallet_names),
)*]
}
fn get_call_names(module: &str) -> &'static [&'static str] {
use #scrate::dispatch::{Callable, GetCallName};
match module {
#(
#pallet_attrs
stringify!(#pallet_names) =>
<<#pallet_names as Callable<#runtime>>::Call
as GetCallName>::get_call_names(),
)*
_ => unreachable!(),
}
}
}
impl #scrate::dispatch::Dispatchable for Call {
type Origin = Origin;
type Config = Call;
type Info = #scrate::weights::DispatchInfo;
type PostInfo = #scrate::weights::PostDispatchInfo;
fn dispatch(self, origin: Origin) -> #scrate::dispatch::DispatchResultWithPostInfo {
if !<Self::Origin as #scrate::traits::OriginTrait>::filter_call(&origin, &self) {
return #scrate::sp_std::result::Result::Err(
#system_path::Error::<#runtime>::CallFiltered.into()
);
}
#scrate::traits::UnfilteredDispatchable::dispatch_bypass_filter(self, origin)
}
}
impl #scrate::traits::UnfilteredDispatchable for Call {
type Origin = Origin;
fn dispatch_bypass_filter(self, origin: Origin) -> #scrate::dispatch::DispatchResultWithPostInfo {
match self {
#(
#pallet_attrs
#variant_patterns =>
#scrate::traits::UnfilteredDispatchable::dispatch_bypass_filter(call, origin),
)*
}
}
}
#(
#pallet_attrs
impl #scrate::traits::IsSubType<#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> for Call {
#[allow(unreachable_patterns)]
fn is_sub_type(&self) -> Option<&#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> {
match self {
#variant_patterns => Some(call),
_ => None,
}
}
}
#pallet_attrs
impl From<#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> for Call {
fn from(call: #scrate::dispatch::CallableCallFor<#pallet_names, #runtime>) -> Self {
#variant_patterns
}
}
)*
}
}