-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpallet.rs
More file actions
363 lines (331 loc) · 9.85 KB
/
Copy pathpallet.rs
File metadata and controls
363 lines (331 loc) · 9.85 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use crate::{reads, writes, ExtrinsicName, PalletName};
use fancy_regex::Regex;
use lazy_static::lazy_static;
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use syn::{
punctuated::Punctuated, Attribute, Expr, ExprCall, ExprMethodCall, ImplItem, ImplItemMethod,
Item, Lit, ReturnType, Stmt, Token, Type,
};
use crate::{
mul,
parse::{path_to_string, PathStripping},
term::Term,
};
pub type Result<T> = std::result::Result<T, String>;
pub type ComponentName = String;
/// Inclusive range of a component.
#[derive(Clone, Debug, PartialEq, Copy)]
pub struct ComponentRange {
pub min: u32,
pub max: u32,
}
pub type ComponentRanges = HashMap<ComponentName, ComponentRange>;
#[derive(Clone, Debug, PartialEq)]
pub struct Extrinsic {
pub name: ExtrinsicName,
pub pallet: PalletName,
pub term: Term,
/// Min and max value that each weight component can have.
pub comp_ranges: Option<ComponentRanges>,
}
pub fn parse_file_in_repo(repo: &Path, file: &Path) -> Result<Vec<Extrinsic>> {
let content = super::read_file(file)?;
let name = PathStripping::RepoRelative.strip(repo, file);
parse_content(name, content).map_err(|e| format!("{}: {}", file.display(), e))
}
pub fn parse_file(file: &Path) -> Result<Vec<Extrinsic>> {
let content = super::read_file(file)?;
let name = PathStripping::FileName.strip(Path::new("."), file);
parse_content(name, content).map_err(|e| format!("{}: {}", file.display(), e))
}
pub fn parse_files_in_repo(repo: &Path, paths: &[PathBuf]) -> Result<Vec<Extrinsic>> {
let mut res = Vec::new();
for path in paths {
res.extend(parse_file_in_repo(repo, path)?);
}
Ok(res)
}
pub fn parse_files(paths: &[PathBuf]) -> Result<Vec<Extrinsic>> {
let mut res = Vec::new();
for path in paths {
res.extend(parse_file(path)?);
}
Ok(res)
}
pub fn try_parse_files_in_repo(repo: &Path, paths: &[PathBuf]) -> Vec<Extrinsic> {
let mut res = Vec::new();
for path in paths {
if let Ok(parsed) = parse_file_in_repo(repo, path) {
res.extend(parsed);
}
}
res
}
pub fn try_parse_files(paths: &[PathBuf]) -> Vec<Extrinsic> {
let mut res = Vec::new();
for path in paths {
if let Ok(parsed) = parse_file(path) {
res.extend(parsed);
}
}
res
}
pub fn parse_content(pallet: PalletName, content: String) -> Result<Vec<Extrinsic>> {
let ast = syn::parse_file(&content).map_err(|e| format!("syn refused to parse: {}", e))?;
for item in ast.items {
if let Ok(weights) = handle_item(pallet.clone(), &item) {
return Ok(weights)
}
}
log::warn!("Could not find a weight implementation in {}", &pallet);
Err("Could not find a weight implementation in the passed file".into())
}
pub(crate) fn handle_item(pallet: PalletName, item: &Item) -> Result<Vec<Extrinsic>> {
match item {
Item::Impl(imp) => {
match imp.self_ty.as_ref() {
// TODO handle both () and non () since ComposableFI uses ().
Type::Tuple(t) => {
if !t.elems.is_empty() {
// The substrate template contains the weight info twice.
// By skipping the not `impl ()` we ensure to parse it only once.
return Err("Skipped ()".into())
}
},
Type::Path(p) => {
if p.path.leading_colon.is_some() {
return Err("Skipped fn: impl leading color".into())
}
if p.path.segments.len() != 1 {
return Err("Skipped fn: impl path segment len".into())
}
if let Some(last) = p.path.segments.last() {
let name = last.ident.to_string();
if name != "WeightInfo" && name != "SubstrateWeight" {
return Err("Skipped fn: impl name last".into())
}
} else {
return Err("Skipped fn: impl name segments".into())
}
},
_ => return Err("Skipped fn: impl type".into()),
}
// TODO validate the trait type.
let mut weights = Vec::new();
for f in &imp.items {
if let ImplItem::Method(m) = f {
let (ext_name, term, comp_ranges) = handle_method(m)?;
weights.push(Extrinsic {
name: ext_name,
pallet: pallet.clone(),
term,
comp_ranges,
});
}
}
if weights.is_empty() {
Err("No weight functions found in trait impl".into())
} else {
Ok(weights)
}
},
_ => Err("No weight trait impl found".into()),
}
}
/// Parses range component attributes.
///
/// Returns `Ok(None)` if the attribute is was not detected.
/// Returns `Err(e)` if the attribute was detected but is invalid.
///
/// This doc comment:
/// The range of component `c` is `[1_337, 2000]`.
/// would be parsed into:
/// ("c", (1_337, =2000))
fn parse_component_attr(attr: &Attribute) -> Result<Option<(ComponentName, ComponentRange)>> {
lazy_static! {
// TODO syn seems to put a ="…" around the comment.
static ref REGEX: Regex = Regex::new(
r#"[\w\s]*`(?P<component>\w+)`[\w\s]*`\[(?P<min>[\d_]+),\s*(?P<max>[\d_]+)\]`.*"#
)
.unwrap();
}
let input = attr.tokens.to_string();
let caps = REGEX.captures(&input).expect("Regex is known good");
if caps.is_none() {
return Ok(None)
}
let caps = caps.unwrap();
let component = caps.name("component").ok_or("Missing component name")?.as_str();
let min: u32 = caps
.name("min")
.ok_or("Min value not found")?
.as_str()
.replace('_', "")
.parse()
.map_err(|e| format!("Could not parse min value: {:?}", e))?;
let max: u32 = caps
.name("max")
.ok_or("Max value not found")?
.as_str()
.replace('_', "")
.parse()
.map_err(|e| format!("Could not parse max value: {:?}", e))?;
// Sanity check
if min > max {
return Err("Min value is greater than max value".into())
}
Ok(Some((component.into(), ComponentRange { min, max })))
}
fn parse_component_attrs(attrs: &Vec<Attribute>) -> Result<Option<ComponentRanges>> {
let mut res = HashMap::new();
for attr in attrs {
match parse_component_attr(attr) {
Ok(Some((name, range))) => {
res.insert(name.replace('_', ""), range);
},
Ok(None) => {
// Some kind of other attribute that we ignore.
},
Err(e) => return Err(e),
}
}
if res.is_empty() {
Ok(None)
} else {
Ok(Some(res))
}
}
fn handle_method(m: &ImplItemMethod) -> Result<(ExtrinsicName, Term, Option<ComponentRanges>)> {
let name = m.sig.ident.to_string();
// Check the return type to end with `Weight`.
if let ReturnType::Type(_, i) = &m.sig.output {
if let Type::Path(p) = i.as_ref() {
let n = p.path.segments.last().map(|s| s.ident.to_string()).unwrap_or_default();
if !n.ends_with("Weight") {
return Err(format!("Skipped fn: {} not a weight", name))
}
} else {
return Err(format!("Skipped fn: {} not a weight", name))
}
} else {
return Err("Skipped fn: method return type".into())
}
if m.block.stmts.len() != 1 {
return Err("There must be only one statement per weight function".into())
}
let stmt = m.block.stmts.first().unwrap();
let weight = match stmt {
Stmt::Expr(expr) => parse_expression(expr)?,
_ => unreachable!("Expected expression"),
};
// We later on check that the number of weight components matches
// the number of components in the term. This cannot be done here
// as global constants could mess up the counting.
let comp_ranges = parse_component_attrs(&m.attrs)?;
Ok((name, weight, comp_ranges))
}
pub(crate) fn parse_expression(expr: &Expr) -> Result<Term> {
match expr {
Expr::Paren(expr) => parse_expression(&expr.expr),
// TODO check cast
Expr::Cast(cast) => parse_expression(&cast.expr),
Expr::MethodCall(call) => parse_method_call(call),
Expr::Lit(lit) => Ok(Term::Value(lit_to_value(&lit.lit))),
Expr::Path(p) => {
let ident = path_to_string(&p.path, Some("::"));
Ok(Term::Var(ident.into()))
},
Expr::Call(call) => parse_call(call),
_ => Err("Unexpected expression".into()),
}
}
// Example: T::DbWeight::get()
fn validate_db_call(call: &Expr) -> Result<()> {
match call {
Expr::Call(call) => {
validate_db_func(&call.func)?;
if !call.args.is_empty() {
Err("Unexpected arguments".into())
} else {
Ok(())
}
},
_ => Err("Unexpected DB call expression".into()),
}
}
// example: T::DbWeight::get
fn validate_db_func(func: &Expr) -> Result<()> {
match &func {
Expr::Path(p) => {
let path = p
.path
.segments
.iter()
.map(|s| s.ident.to_string())
.collect::<Vec<_>>()
.join("::");
if path != "T::DbWeight::get" &&
!path.ends_with("RocksDbWeight::get") &&
!path.ends_with("ParityDbWeight::get")
{
Err(format!("Unexpected DB path: {}", path))
} else {
Ok(())
}
},
_ => Err("Unexpected DB func".into()),
}
}
// V1.5 feature
fn parse_call(call: &ExprCall) -> Result<Term> {
let name = function_name(call)?;
if name.ends_with("from_ref_time") {
parse_args(&call.args)
} else {
Err(format!("Unexpected call: {}", name).into())
}
}
// Example: receiver.saturating_mul(5 as Weight)
fn parse_method_call(call: &ExprMethodCall) -> Result<Term> {
let name: &str = &call.method.to_string();
match name {
"reads" => {
// Can only be called on T::DbWeight::get()
validate_db_call(&call.receiver)?;
let reads = parse_args(&call.args)?;
Ok(reads!(reads))
},
"writes" => {
// Can only be called on T::DbWeight::get()
validate_db_call(&call.receiver)?;
let writes = parse_args(&call.args)?;
Ok(writes!(writes))
},
"saturating_add" =>
Ok(Term::Add(parse_expression(&call.receiver)?.into(), parse_args(&call.args)?.into())),
"saturating_mul" => Ok(mul!(parse_expression(&call.receiver)?, parse_args(&call.args)?)),
_ => Err(format!("Unknown function: {}", name)),
}
}
fn parse_args(args: &Punctuated<Expr, Token![,]>) -> Result<Term> {
if args.len() != 1 {
return Err(format!("Expected one argument, got {}", args.len()))
}
let args = args.first().ok_or("Empty args")?;
parse_expression(args)
}
pub(crate) fn lit_to_value(lit: &Lit) -> u128 {
match lit {
Lit::Int(i) => i.base10_digits().parse().expect("Lit must be a valid int; qed"),
_ => unreachable!(),
}
}
fn function_name(call: &ExprCall) -> Result<String> {
match call.func.as_ref() {
Expr::Path(p) => Ok(path_to_string(&p.path, Some("::"))),
_ => Err("Unexpected function".into()),
}
}