Skip to content

Commit

Permalink
Fix expect_pallet benchmarks not relaying on hard-coded `frame_syst…
Browse files Browse the repository at this point in the history
…em` 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: #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: #2284
  • Loading branch information
bkontur authored Nov 14, 2023
1 parent b371c35 commit ae1bdcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 8 additions & 7 deletions polkadot/xcm/pallet-xcm-benchmarks/src/generic/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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]);
}: {
Expand Down
12 changes: 12 additions & 0 deletions polkadot/xcm/pallet-xcm-benchmarks/src/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ae1bdcf

Please sign in to comment.