From ae1bdcfb91a26c5f65c5ca534aa8a04523ca2277 Mon Sep 17 00:00:00 2001
From: Branislav Kontur <bkontur@gmail.com>
Date: Tue, 14 Nov 2023 10:43:40 +0100
Subject: [PATCH] Fix `expect_pallet` benchmarks not relaying on hard-coded
 `frame_system` dependency version (#2288)

## Problem/Motivation
The benchmark for the `ExpectPallet` XCM instruction uses a hard-coded
version `4.0.0` for the `frame_system` pallet. Unfortunately, this
doesn't work for the `polkadot-fellows/runtimes` repository, where we
use dependencies from `crates.io`, e.g.,
[frame-system::23.0.0.0](https://github.com/polkadot-fellows/runtimes/blob/dd7f86f0d50064481ed0b7c0218494a5cfad997e/relay/kusama/Cargo.toml#L83).

Closes: https://github.com/paritytech/polkadot-sdk/issues/2284

## Solution
This PR fixes the benchmarks that require pallet information and enables
the runtime to provide the correct/custom pallet information. The
default implementation provides `frame_system::Pallet` with index `0`,
where the version is not hard-coded but read from the runtime.


## Local testing

Added log for `T::valid_pallet` to the benchmarks like:
```
let valid_pallet = T::valid_pallet();
log::info!(
	target: "frame::benchmark::pallet",
	"valid_pallet: {}::{}::{}::{}::{}",
	valid_pallet.index,
	valid_pallet.module_name,
	valid_pallet.crate_version.major,
	valid_pallet.crate_version.minor,
	valid_pallet.crate_version.patch,
);
```

Run benchmarks for `westend`:
```
cargo run --bin=polkadot --features=runtime-benchmarks -- benchmark pallet --steps=2 --repeat=1 --extrinsic=* --heap-pages=4096 --json-file=./bench.json --chain=westend-dev --template=./polkadot/xcm/pallet-xcm-benchmarks/template.hbs --pallet=pallet_xcm_benchmarks::generic --output=./polkadot/runtime/westend/src/weights/xcm
```

---

For actual `frame_system` version:
```
[package]
name = "frame-system"
version = "4.0.0-dev"
```

Log dump:
```
2023-11-13 12:56:45 Starting benchmark: pallet_xcm_benchmarks::generic::query_pallet
2023-11-13 12:56:45 valid_pallet: 0::frame_system::4::0::0
2023-11-13 12:56:45 valid_pallet: 0::frame_system::4::0::0
2023-11-13 12:56:45 valid_pallet: 0::frame_system::4::0::0
2023-11-13 12:56:45 Starting benchmark: pallet_xcm_benchmarks::generic::expect_pallet
2023-11-13 12:56:45 valid_pallet: 0::frame_system::4::0::0
2023-11-13 12:56:45 valid_pallet: 0::frame_system::4::0::0
2023-11-13 12:56:45 valid_pallet: 0::frame_system::4::0::0
```


For changed `frame_system` version:
```
[package]
name = "frame-system"
version = "5.1.3-dev"
```

Log dump:
```
2023-11-13 12:51:51 Starting benchmark: pallet_xcm_benchmarks::generic::query_pallet
2023-11-13 12:51:51 valid_pallet: 0::frame_system::5::1::3
2023-11-13 12:51:51 valid_pallet: 0::frame_system::5::1::3
2023-11-13 12:51:51 valid_pallet: 0::frame_system::5::1::3
2023-11-13 12:51:51 Starting benchmark: pallet_xcm_benchmarks::generic::expect_pallet
2023-11-13 12:51:51 valid_pallet: 0::frame_system::5::1::3
2023-11-13 12:51:51 valid_pallet: 0::frame_system::5::1::3
2023-11-13 12:51:51 valid_pallet: 0::frame_system::5::1::3
```

## References

Closes: https://github.com/paritytech/polkadot-sdk/issues/2284
---
 .../src/generic/benchmarking.rs                   | 15 ++++++++-------
 .../xcm/pallet-xcm-benchmarks/src/generic/mod.rs  | 12 ++++++++++++
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs
index 4a997666027..f1c48ba9b83 100644
--- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs
+++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs
@@ -413,8 +413,9 @@ benchmarks! {
 			executor.set_holding(expected_assets_in_holding.into());
 		}
 
+		let valid_pallet = T::valid_pallet();
 		let instruction = Instruction::QueryPallet {
-			module_name: b"frame_system".to_vec(),
+			module_name: valid_pallet.module_name.as_bytes().to_vec(),
 			response_info: QueryResponseInfo { destination, query_id, max_weight },
 		};
 		let xcm = Xcm(vec![instruction]);
@@ -428,13 +429,13 @@ benchmarks! {
 
 	expect_pallet {
 		let mut executor = new_executor::<T>(Default::default());
-
+		let valid_pallet = T::valid_pallet();
 		let instruction = Instruction::ExpectPallet {
-			index: 0,
-			name: b"System".to_vec(),
-			module_name: b"frame_system".to_vec(),
-			crate_major: 4,
-			min_crate_minor: 0,
+			index: valid_pallet.index as u32,
+			name: valid_pallet.name.as_bytes().to_vec(),
+			module_name: valid_pallet.module_name.as_bytes().to_vec(),
+			crate_major: valid_pallet.crate_version.major.into(),
+			min_crate_minor: valid_pallet.crate_version.minor.into(),
 		};
 		let xcm = Xcm(vec![instruction]);
 	}: {
diff --git a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs
index cbdfa8d0112..11f7bba19a9 100644
--- a/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs
+++ b/polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs
@@ -91,6 +91,18 @@ pub mod pallet {
 		///
 		/// If set to `Err`, benchmarks which rely on a universal alias will be skipped.
 		fn alias_origin() -> Result<(MultiLocation, MultiLocation), BenchmarkError>;
+
+		/// Returns a valid pallet info for `ExpectPallet` or `QueryPallet` benchmark.
+		///
+		/// By default returns `frame_system::Pallet` info with expected pallet index `0`.
+		fn valid_pallet() -> frame_support::traits::PalletInfoData {
+			frame_support::traits::PalletInfoData {
+				index: <frame_system::Pallet<Self> as frame_support::traits::PalletInfoAccess>::index(),
+				name: <frame_system::Pallet<Self> as frame_support::traits::PalletInfoAccess>::name(),
+				module_name: <frame_system::Pallet<Self> as frame_support::traits::PalletInfoAccess>::module_name(),
+				crate_version: <frame_system::Pallet<Self> as frame_support::traits::PalletInfoAccess>::crate_version(),
+			}
+		}
 	}
 
 	#[pallet::pallet]
-- 
GitLab