referrerpolicy=no-referrer-when-downgrade

pallet_staking_async_rc_client/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Benchmarking setup for pallet-staking-async-rc-client.
19
20use alloc::vec::Vec;
21use frame_benchmarking::v2::*;
22use frame_system::RawOrigin;
23use xcm::latest::Location;
24use xcm_builder::EnsureDelivery;
25
26use crate::*;
27
28/// Wrapper pallet for benchmarking.
29pub struct Pallet<T: Config>(crate::Pallet<T>);
30
31/// Configuration trait for benchmarking `pallet-staking-async-rc-client`.
32///
33/// The runtime must implement this trait to provide session keys generation
34/// and XCM delivery setup for benchmarking purposes.
35pub trait Config: crate::Config {
36	/// Helper that ensures successful XCM delivery for benchmarks.
37	type DeliveryHelper: EnsureDelivery;
38
39	/// The relay chain location for XCM delivery.
40	fn relay_chain_location() -> Location {
41		Location::parent()
42	}
43
44	/// Convert an AccountId to an XCM Location for fee charging.
45	fn account_to_location(account: Self::AccountId) -> Location;
46
47	/// Generate relay chain session keys and ownership proof for benchmarking.
48	///
49	/// Returns the SCALE-encoded session keys and SCALE-encoded ownership proof.
50	fn generate_session_keys_and_proof(owner: Self::AccountId) -> (Vec<u8>, Vec<u8>);
51
52	/// Setup a validator account for benchmarking.
53	///
54	/// Should return an account that:
55	/// - Is registered as a validator in the staking pallet
56	/// - Has sufficient balance for XCM delivery fees
57	fn setup_validator() -> Self::AccountId;
58}
59
60#[benchmarks]
61mod benchmarks {
62	use super::*;
63	use xcm_executor::traits::FeeReason;
64
65	#[benchmark]
66	fn set_keys() -> Result<(), BenchmarkError> {
67		let stash = T::setup_validator();
68		let (keys, proof) = T::generate_session_keys_and_proof(stash.clone());
69
70		// Ensure XCM delivery will succeed by setting up required fees/accounts.
71		let stash_location = T::account_to_location(stash.clone());
72		let dest = T::relay_chain_location();
73		T::DeliveryHelper::ensure_successful_delivery(
74			&stash_location,
75			&dest,
76			FeeReason::ChargeFees,
77		);
78
79		#[extrinsic_call]
80		crate::Pallet::<T>::set_keys(RawOrigin::Signed(stash), keys, proof, None);
81
82		Ok(())
83	}
84
85	#[benchmark]
86	fn purge_keys() -> Result<(), BenchmarkError> {
87		let caller = T::setup_validator();
88		let (keys, proof) = T::generate_session_keys_and_proof(caller.clone());
89
90		// Set keys first so purge_keys hits the worst-case path (deposit release).
91		let caller_location = T::account_to_location(caller.clone());
92		let dest = T::relay_chain_location();
93		T::DeliveryHelper::ensure_successful_delivery(
94			&caller_location,
95			&dest,
96			FeeReason::ChargeFees,
97		);
98		crate::Pallet::<T>::set_keys(RawOrigin::Signed(caller.clone()).into(), keys, proof, None)?;
99
100		// Ensure XCM delivery will succeed for purge_keys too.
101		T::DeliveryHelper::ensure_successful_delivery(
102			&caller_location,
103			&dest,
104			FeeReason::ChargeFees,
105		);
106
107		#[extrinsic_call]
108		crate::Pallet::<T>::purge_keys(RawOrigin::Signed(caller), None);
109
110		Ok(())
111	}
112}