diff --git a/resources/weight_analyzer/src/lib.rs b/resources/weight_analyzer/src/lib.rs index b8c6bffd0cd0c041e0e4b43eff8c396e194a8b89..8b212ce1296c6eaf2231f897ed26d547900006f1 100644 --- a/resources/weight_analyzer/src/lib.rs +++ b/resources/weight_analyzer/src/lib.rs @@ -52,10 +52,10 @@ pub struct WeightInfo { pub fn analyze_weight( folder_path: &Path, max_block_weight: &MaxBlockWeight, -) -> HashMap<String, HashMap<String, WeightInfo>> { - let pallet_weights = read_pallet_weight(folder_path); - let db_weight = read_db_weight(folder_path); - let overhead_weights = read_overhead_weight(folder_path); +) -> Result<HashMap<String, HashMap<String, WeightInfo>>, String> { + let pallet_weights = read_pallet_weight(folder_path)?; + let db_weight = read_db_weight(folder_path)?; + let overhead_weights = read_overhead_weight(folder_path)?; // Initialize scope with db weights let mut scope = Scope::from_substrate(); @@ -64,7 +64,7 @@ pub fn analyze_weight( process(pallet_weights, scope, max_block_weight, &overhead_weights) } -fn read_pallet_weight(folder_path: &Path) -> Vec<Vec<ChromaticExtrinsic>> { +fn read_pallet_weight(folder_path: &Path) -> Result<Vec<Vec<ChromaticExtrinsic>>, String> { let mut parsed_files = Vec::new(); for path in glob(folder_path.join("*").to_str().expect("Invalid pallet path")) .expect("Invalid pallet pattern") @@ -76,19 +76,17 @@ fn read_pallet_weight(folder_path: &Path) -> Vec<Vec<ChromaticExtrinsic>> { } } if parsed_files.is_empty() { - panic!("No pallet found"); + return Err("No pallet found".into()); } - parsed_files + Ok(parsed_files) } -fn read_db_weight(folder_path: &Path) -> Weights { +fn read_db_weight(folder_path: &Path) -> Result<Weights, String> { subweight_core::parse::storage::parse_file(folder_path.join("paritydb_weights.rs").as_path()) - .expect("No DB weights") } -fn read_overhead_weight(folder_path: &Path) -> Weight { +fn read_overhead_weight(folder_path: &Path) -> Result<Weight, String> { subweight_core::parse::overhead::parse_file(folder_path.join("extrinsic_weights.rs").as_path()) - .expect("No overhead weight") } fn evaluate_weight( @@ -111,16 +109,14 @@ fn evaluate_weight( .term .simplify(subweight_core::Dimension::Time) .expect("Can't evaluate") - .eval(scope) - .unwrap(); + .eval(scope)?; // Add base extrinsic overhead if let Weight::ExtrinsicBase(i) = overhead { weight += i .simplify(subweight_core::Dimension::Time) .expect("Can't evaluate") - .eval(scope) - .unwrap(); + .eval(scope)?; } let relative_weight = (weight as f64) / max_block_weight * 100.; @@ -146,12 +142,12 @@ fn process( mut scope: Scope<Term<u128>>, max_block_weight: &MaxBlockWeight, overhead: &Weight, -) -> HashMap<String, HashMap<String, WeightInfo>> { +) -> Result<HashMap<String, HashMap<String, WeightInfo>>, String> { let mut weight_by_pallet: HashMap<String, HashMap<String, WeightInfo>> = HashMap::new(); for i in pallet_weights { for j in i { let (pallet, extrinsic, weight) = - evaluate_weight(j, &mut scope, max_block_weight, overhead).unwrap(); + evaluate_weight(j, &mut scope, max_block_weight, overhead)?; if let Some(i) = weight_by_pallet.get_mut(&pallet) { i.insert(extrinsic, weight); } else { @@ -159,7 +155,7 @@ fn process( } } } - weight_by_pallet + Ok(weight_by_pallet) } #[cfg(test)] @@ -173,12 +169,20 @@ mod tests { Path::new("../../runtime/common/src/weights/"), &MaxBlockWeight::default(), ); - assert!(weight_by_pallet.get("Balances").unwrap().len() == 7); // 7 extrinsics in pallet + assert!( + weight_by_pallet + .clone() + .unwrap() + .get("Balances") + .unwrap() + .len() + == 7 + ); // 7 extrinsics in pallet println!("{:?}", weight_by_pallet); // cargo test -- --nocapture } #[test] #[should_panic] fn should_not_works() { - let _ = analyze_weight(Path::new(""), &MaxBlockWeight::default()); + let _ = analyze_weight(Path::new(""), &MaxBlockWeight::default()).unwrap(); } } diff --git a/xtask/src/gen_doc.rs b/xtask/src/gen_doc.rs index ca8af3995011787d59beeea0c88f39636c17fad0..ba26fafba546ec7c74d76243c03e18eb5a5624d2 100644 --- a/xtask/src/gen_doc.rs +++ b/xtask/src/gen_doc.rs @@ -395,6 +395,7 @@ fn get_from_metadata_v14( fn get_weights() -> HashMap<String, HashMap<String, WeightInfo>> { analyze_weight(Path::new(WEIGHT_FILEPATH), &MaxBlockWeight::default()) + .expect("Weights unavailable") } /// use template to render markdown file with runtime calls documentation