referrerpolicy=no-referrer-when-downgrade

pallet_xcm_bridge_hub_router/
benchmarking.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! XCM bridge hub router pallet benchmarks.
18
19#![cfg(feature = "runtime-benchmarks")]
20
21use crate::{Bridge, BridgeState, Call};
22use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError};
23use frame_support::traits::{EnsureOrigin, Get, Hooks, UnfilteredDispatchable};
24use polkadot_runtime_parachains::FeeTracker;
25use sp_runtime::{traits::Zero, Saturating};
26use xcm::prelude::*;
27
28/// Pallet we're benchmarking here.
29pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);
30
31/// Trait that must be implemented by runtime to be able to benchmark pallet properly.
32pub trait Config<I: 'static>: crate::Config<I> {
33	/// Fill up queue so it becomes congested.
34	fn make_congested();
35
36	/// Returns destination which is valid for this router instance.
37	/// (Needs to pass `T::Bridges`)
38	/// Make sure that `SendXcm` will pass.
39	fn ensure_bridged_target_destination() -> Result<Location, BenchmarkError> {
40		Ok(Location::new(
41			Self::UniversalLocation::get().len() as u8,
42			[GlobalConsensus(Self::BridgedNetworkId::get().unwrap())],
43		))
44	}
45}
46
47benchmarks_instance_pallet! {
48	on_initialize_when_non_congested {
49		Bridge::<T, I>::put(BridgeState {
50			is_congested: false,
51			delivery_fee_factor: crate::Pallet::<T, I>::MIN_FEE_FACTOR.saturating_mul(2.into()),
52		});
53	}: {
54		crate::Pallet::<T, I>::on_initialize(Zero::zero())
55	}
56
57	on_initialize_when_congested {
58		Bridge::<T, I>::put(BridgeState {
59			is_congested: false,
60			delivery_fee_factor: crate::Pallet::<T, I>::MIN_FEE_FACTOR.saturating_mul(2.into()),
61		});
62		let _ = T::ensure_bridged_target_destination()?;
63		T::make_congested();
64	}: {
65		crate::Pallet::<T, I>::on_initialize(Zero::zero())
66	}
67
68	report_bridge_status {
69		Bridge::<T, I>::put(BridgeState::default());
70
71		let origin: T::RuntimeOrigin = T::BridgeHubOrigin::try_successful_origin().expect("expected valid BridgeHubOrigin");
72		let bridge_id = Default::default();
73		let is_congested = true;
74
75		let call = Call::<T, I>::report_bridge_status { bridge_id, is_congested };
76	}: { call.dispatch_bypass_filter(origin)? }
77	verify {
78		assert!(Bridge::<T, I>::get().is_congested);
79	}
80}