referrerpolicy=no-referrer-when-downgrade

pallet_ah_ops/
benchmarking.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use crate::*;
18use frame_benchmarking::{account, v2::*};
19use frame_support::{dispatch::RawOrigin, traits::Currency};
20
21#[benchmarks]
22pub mod benchmarks {
23	use super::*;
24
25	#[benchmark]
26	fn unreserve_lease_deposit() {
27		let sender = account("sender", 0, 0);
28		let ed = <T::Currency as Currency<_>>::minimum_balance();
29		let _ = T::Currency::deposit_creating(&sender, ed + ed);
30		let _ = T::Currency::reserve(&sender, ed);
31		let block = T::RcBlockNumberProvider::current_block_number();
32		let para_id = ParaId::from(1u16);
33		RcLeaseReserve::<T>::insert((block, para_id, &sender), ed);
34
35		assert_eq!(T::Currency::reserved_balance(&sender), ed);
36
37		#[extrinsic_call]
38		_(RawOrigin::Signed(sender.clone()), block, None, para_id);
39
40		assert_eq!(T::Currency::reserved_balance(&sender), 0);
41		assert_eq!(RcLeaseReserve::<T>::get((block, para_id, &sender)), None);
42	}
43
44	#[benchmark]
45	fn withdraw_crowdloan_contribution() {
46		let pot = account("pot", 0, 0);
47		let ed = <T::Currency as Currency<_>>::minimum_balance();
48		let _ = T::Currency::deposit_creating(&pot, ed + ed);
49		let _ = T::Currency::reserve(&pot, ed);
50		let block = T::RcBlockNumberProvider::current_block_number();
51		let para_id = ParaId::from(1u16);
52		RcLeaseReserve::<T>::insert((block, para_id, &pot), ed);
53
54		let sender = account("sender", 0, 0);
55		RcCrowdloanContribution::<T>::insert((block, para_id, &sender), (pot.clone(), ed));
56
57		assert_eq!(T::Currency::free_balance(&sender), 0);
58
59		#[extrinsic_call]
60		_(RawOrigin::Signed(sender.clone()), block, None, para_id);
61
62		assert_eq!(RcCrowdloanContribution::<T>::get((block, para_id, &sender)), None);
63		assert_eq!(RcLeaseReserve::<T>::get((block, para_id, &pot)), None);
64		assert_eq!(T::Currency::free_balance(&pot), ed);
65	}
66
67	#[benchmark]
68	fn unreserve_crowdloan_reserve() {
69		let sender = account("sender", 0, 0);
70		let ed = <T::Currency as Currency<_>>::minimum_balance();
71		let _ = T::Currency::deposit_creating(&sender, ed + ed);
72		let _ = T::Currency::reserve(&sender, ed);
73		let block = T::RcBlockNumberProvider::current_block_number();
74		let para_id = ParaId::from(1u16);
75		RcCrowdloanReserve::<T>::insert((block, para_id, &sender), ed);
76
77		assert_eq!(T::Currency::reserved_balance(&sender), ed);
78
79		#[extrinsic_call]
80		_(RawOrigin::Signed(sender.clone()), block, None, para_id);
81
82		assert_eq!(T::Currency::reserved_balance(&sender), 0);
83		assert_eq!(RcCrowdloanReserve::<T>::get((block, para_id, &sender)), None);
84	}
85
86	#[cfg(feature = "std")]
87	pub fn test_unreserve_lease_deposit<T: Config>() {
88		_unreserve_lease_deposit::<T>(true)
89	}
90
91	#[cfg(feature = "std")]
92	pub fn test_withdraw_crowdloan_contribution<T: Config>() {
93		_withdraw_crowdloan_contribution::<T>(true)
94	}
95
96	#[cfg(feature = "std")]
97	pub fn test_unreserve_crowdloan_reserve<T: Config>() {
98		_unreserve_crowdloan_reserve::<T>(true)
99	}
100}