referrerpolicy=no-referrer-when-downgrade

pallet_dap/
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//! Benchmarks for pallet-dap.
19
20use super::*;
21use frame_benchmarking::v2::*;
22use frame_system::RawOrigin;
23use sp_staking::budget::BudgetRecipientList;
24
25#[benchmarks(where T: pallet_timestamp::Config<Moment = u64>)]
26mod benchmarks {
27	use super::*;
28
29	/// Build a valid allocation from registered recipients, distributing evenly and giving
30	/// the remainder to the last recipient to ensure the sum is exactly 100%.
31	fn build_even_allocation<T: Config>() -> BudgetAllocationMap {
32		let recipients = T::BudgetRecipients::recipients();
33		let count = recipients.len() as u32;
34		let mut allocations = BudgetAllocationMap::new();
35
36		for (i, (key, _)) in recipients.into_iter().enumerate() {
37			let perbill = if i as u32 == count - 1 {
38				let used: u32 = allocations.values().map(|p| p.deconstruct()).sum();
39				Perbill::from_parts(Perbill::one().deconstruct().saturating_sub(used))
40			} else {
41				Perbill::from_rational(1u32, count)
42			};
43			allocations.try_insert(key, perbill).expect("bounded by MAX_BUDGET_RECIPIENTS");
44		}
45
46		allocations
47	}
48
49	#[benchmark]
50	fn set_budget_allocation() {
51		let allocations = build_even_allocation::<T>();
52
53		#[extrinsic_call]
54		_(RawOrigin::Root, allocations.clone());
55
56		assert_eq!(BudgetAllocation::<T>::get(), allocations);
57	}
58
59	#[benchmark]
60	fn drip_issuance() {
61		let allocations = build_even_allocation::<T>();
62		BudgetAllocation::<T>::put(allocations);
63
64		// Set a timestamp so the drip fires.
65		let now: u64 = 1_000_000;
66		pallet_timestamp::Now::<T>::put(now);
67		let past = now.saturating_sub(T::IssuanceCadence::get() + 1);
68		LastIssuanceTimestamp::<T>::put(past);
69
70		#[block]
71		{
72			Pallet::<T>::drip_issuance();
73		}
74
75		assert!(LastIssuanceTimestamp::<T>::get() > past);
76	}
77}