referrerpolicy=no-referrer-when-downgrade

asset_hub_westend_runtime/governance/
impls.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15#[cfg(feature = "runtime-benchmarks")]
16pub mod benchmarks {
17	use crate::ParachainSystem;
18	use core::marker::PhantomData;
19	use cumulus_primitives_core::{ChannelStatus, GetChannelInfo};
20	use frame_support::traits::{
21		tokens::{Pay, PaymentStatus},
22		Get,
23	};
24
25	/// Trait for setting up any prerequisites for successful execution of benchmarks.
26	pub trait EnsureSuccessful {
27		fn ensure_successful();
28	}
29
30	/// Implementation of the [`EnsureSuccessful`] trait which opens an HRMP channel between
31	/// the Collectives and a parachain with a given ID.
32	pub struct OpenHrmpChannel<I>(PhantomData<I>);
33	impl<I: Get<u32>> EnsureSuccessful for OpenHrmpChannel<I> {
34		fn ensure_successful() {
35			if let ChannelStatus::Closed = ParachainSystem::get_channel_status(I::get().into()) {
36				ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(I::get().into())
37			}
38		}
39	}
40
41	/// Type that wraps a type implementing the [`Pay`] trait to decorate its
42	/// [`Pay::ensure_successful`] function with a provided implementation of the
43	/// [`EnsureSuccessful`] trait.
44	pub struct PayWithEnsure<O, E>(PhantomData<(O, E)>);
45	impl<O, E> Pay for PayWithEnsure<O, E>
46	where
47		O: Pay,
48		E: EnsureSuccessful,
49	{
50		type AssetKind = O::AssetKind;
51		type Balance = O::Balance;
52		type Beneficiary = O::Beneficiary;
53		type Error = O::Error;
54		type Id = O::Id;
55
56		fn pay(
57			who: &Self::Beneficiary,
58			asset_kind: Self::AssetKind,
59			amount: Self::Balance,
60		) -> Result<Self::Id, Self::Error> {
61			O::pay(who, asset_kind, amount)
62		}
63		fn check_payment(id: Self::Id) -> PaymentStatus {
64			O::check_payment(id)
65		}
66		fn ensure_successful(
67			who: &Self::Beneficiary,
68			asset_kind: Self::AssetKind,
69			amount: Self::Balance,
70		) {
71			E::ensure_successful();
72			O::ensure_successful(who, asset_kind, amount)
73		}
74		fn ensure_concluded(id: Self::Id) {
75			O::ensure_concluded(id)
76		}
77	}
78}