Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for weights V1.5 #22

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
},
SubCommand::Parse(ParseCmd::Files(ParseFilesCmd { files })) => {
println!("Trying to parse {} files...", files.len());
let parsed = try_parse_files(&files);
let parsed = parse_files(&files)?;
println!("Parsed {} files successfully", parsed.len());
},
}
Expand Down
22 changes: 20 additions & 2 deletions core/src/parse/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{
path::{Path, PathBuf},
};
use syn::{
punctuated::Punctuated, Attribute, Expr, ExprMethodCall, ImplItem, ImplItemMethod, Item, Lit,
ReturnType, Stmt, Token, Type,
punctuated::Punctuated, Attribute, Expr, ExprCall, ExprMethodCall, ImplItem, ImplItemMethod,
Item, Lit, ReturnType, Stmt, Token, Type,
};

use crate::{
Expand Down Expand Up @@ -263,6 +263,7 @@ pub(crate) fn parse_expression(expr: &Expr) -> Result<Term> {
let ident = path_to_string(&p.path, Some("::"));
Ok(Term::Var(ident.into()))
},
Expr::Call(call) => parse_call(call),
_ => Err("Unexpected expression".into()),
}
}
Expand Down Expand Up @@ -306,6 +307,16 @@ fn validate_db_func(func: &Expr) -> Result<()> {
}
}

// 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();
Expand Down Expand Up @@ -343,3 +354,10 @@ pub(crate) fn lit_to_value(lit: &Lit) -> u128 {
_ => 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()),
}
}
21 changes: 21 additions & 0 deletions core/src/test/parse/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,24 @@ fn parse_expression_works(#[case] input: &str, #[case] want: Term) {
// Eval does not panic
let _ = got.eval(&Scope::empty());
}

// V1.5 syntax
#[rstest]
// Basic arithmetic.
#[case("Weight::from_ref_time(123 as u64)", val!(123))]
// All together.
#[case("Weight::from_ref_time(123 as u64)
// Standard Error: 1_000
.saturating_add(Weight::from_ref_time(7 as u64).saturating_mul(s as u64))
.saturating_add(T::DbWeight::get().reads(12 as u64))
.saturating_add(T::DbWeight::get().writes(12 as u64))
.saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(s as u64)))",
add!(add!(add!(add!(val!(123), mul!(val!(7), var!("s"))), reads!(val!(12))), writes!(val!(12))), writes!(mul!(val!(1), var!("s")))))]
fn v1_5_parse_expression_works(#[case] input: &str, #[case] want: Term) {
let expr: Expr = syn::parse_str(input).unwrap();
let got = parse_expression(&expr).unwrap();
assert_eq!(want, got);

// Eval does not panic
let _ = got.eval(&Scope::empty());
}